Search in sources :

Example 1 with UpdateListener

use of org.terasology.nui.widgets.UpdateListener in project Terasology by MovingBlocks.

the class WidgetSelectionScreen method initialise.

@Override
public void initialise() {
    availableWidgets = find("availableWidgets", UIDropdownScrollable.class);
    // Populate the widget list.
    ClassLibrary<UIWidget> metadataLibrary = getManager().getWidgetMetadataLibrary();
    for (ClassMetadata metadata : metadataLibrary) {
        if (!CoreScreenLayer.class.isAssignableFrom(metadata.getType())) {
            widgets.put(metadata.toString(), metadata);
        }
    }
    List<String> options = Lists.newArrayList(widgets.keySet());
    Collections.sort(options);
    availableWidgets.setOptions(options);
    // Add the widget as a child of the node.
    WidgetUtil.trySubscribe(this, "ok", button -> {
        String selection = availableWidgets.getSelection();
        JsonTree childNode;
        if (node.getValue().getType() == JsonTreeValue.Type.ARRAY) {
            ClassMetadata metadata = widgets.get(selection);
            // Get the widget tree from a utility method.
            childNode = NUIEditorNodeUtils.createNewWidget(selection, "newWidget", false);
            // If the widget is an UILayout override, also add a "contents" array node to the tree.
            if (UILayout.class.isAssignableFrom(metadata.getType())) {
                childNode.addChild(new JsonTreeValue("contents", null, JsonTreeValue.Type.ARRAY));
            }
        } else {
            childNode = new JsonTree(new JsonTreeValue(selection, null, JsonTreeValue.Type.OBJECT));
            childNode.setExpanded(true);
        }
        node.addChild(childNode);
        closeListeners.forEach(UpdateListener::onAction);
        getManager().closeScreen(ASSET_URI);
    });
    find("ok", UIButton.class).bindEnabled(new ReadOnlyBinding<Boolean>() {

        @Override
        public Boolean get() {
            return availableWidgets.getSelection() != null;
        }
    });
    WidgetUtil.trySubscribe(this, "cancel", button -> getManager().closeScreen(ASSET_URI));
}
Also used : ClassMetadata(org.terasology.reflection.metadata.ClassMetadata) JsonTree(org.terasology.nui.widgets.treeView.JsonTree) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) CoreScreenLayer(org.terasology.engine.rendering.nui.CoreScreenLayer) UIWidget(org.terasology.nui.UIWidget) JsonTreeValue(org.terasology.nui.widgets.treeView.JsonTreeValue) UIButton(org.terasology.nui.widgets.UIButton) UpdateListener(org.terasology.nui.widgets.UpdateListener)

Aggregations

CoreScreenLayer (org.terasology.engine.rendering.nui.CoreScreenLayer)1 UIWidget (org.terasology.nui.UIWidget)1 UIButton (org.terasology.nui.widgets.UIButton)1 UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)1 UpdateListener (org.terasology.nui.widgets.UpdateListener)1 JsonTree (org.terasology.nui.widgets.treeView.JsonTree)1 JsonTreeValue (org.terasology.nui.widgets.treeView.JsonTreeValue)1 ClassMetadata (org.terasology.reflection.metadata.ClassMetadata)1