Search in sources :

Example 16 with JsonTree

use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.

the class NUIEditorScreen method addWidget.

/**
 * {@inheritDoc}
 */
@Override
protected void addWidget(JsonTree node) {
    getManager().pushScreen(WidgetSelectionScreen.ASSET_URI, WidgetSelectionScreen.class);
    // Initialise and show the widget selection screen.
    WidgetSelectionScreen widgetSelectionScreen = (WidgetSelectionScreen) getManager().getScreen(WidgetSelectionScreen.ASSET_URI);
    widgetSelectionScreen.setNode(node);
    widgetSelectionScreen.subscribeClose(() -> {
        node.setExpanded(true);
        JsonTree widget = node.getChildAt(node.getChildren().size() - 1);
        widget.setExpanded(true);
        getEditor().fireUpdateListeners();
        // Automatically edit the id of a newly added widget.
        getEditor().getModel().resetNodes();
        getEditor().setSelectedIndex(getEditor().getModel().indexOf(widget.getChildWithKey("id")));
        editNode(widget.getChildWithKey("id"));
    });
}
Also used : JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree)

Example 17 with JsonTree

use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.

the class NUISkinEditorScreen method selectAsset.

/**
 * {@inheritDoc}
 */
@Override
public void selectAsset(ResourceUrn urn) {
    boolean isLoaded = assetManager.isLoaded(urn, UISkin.class);
    Optional<UISkin> asset = assetManager.getAsset(urn, UISkin.class);
    if (asset.isPresent()) {
        UISkin skin = asset.get();
        if (!isLoaded) {
            asset.get().dispose();
        }
        AssetDataFile source = skin.getSource();
        String content = null;
        try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
            reader.setLenient(true);
            content = new JsonParser().parse(reader).toString();
        } catch (IOException e) {
            logger.error(String.format("Could not load asset source file for %s", urn.toString()), e);
        }
        if (content != null) {
            JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
            selectedAssetPending = urn.toString();
            resetState(node);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UISkin(org.terasology.rendering.nui.skin.UISkin) AssetDataFile(org.terasology.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 18 with JsonTree

use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.

the class NUIEditorItemRenderer method getTexture.

@Override
public Texture getTexture(JsonTreeValue value) {
    String textureName = null;
    if (value.getType() == JsonTreeValue.Type.KEY_VALUE_PAIR) {
        textureName = ATTRIBUTE_TEXTURE_NAME;
    } else if (value.getType() == JsonTreeValue.Type.OBJECT) {
        JsonTree node = (JsonTree) editorTreeViewModel.getNodeByValue(value);
        // If the node has a "type": "..." child, retrieve the texture by the type name.
        if (node != null) {
            // If the node has no type and is a root node, do not draw an icon.
            if (node.isRoot()) {
                return null;
            } else if (!node.isRoot() && "elements".equals(node.getParent().getValue().getKey())) {
                textureName = node.getValue().getKey();
            } else {
                for (Tree<JsonTreeValue> child : node.getChildren()) {
                    JsonTreeValue childValue = child.getValue();
                    if (childValue.getType() == JsonTreeValue.Type.KEY_VALUE_PAIR && "type".equals(childValue.getKey())) {
                        textureName = (String) childValue.getValue();
                    }
                }
            }
        }
        // Otherwise use the default texture.
        if (textureName == null) {
            textureName = OBJECT_TEXTURE_NAME;
        }
    } else if (value.getType() == JsonTreeValue.Type.ARRAY) {
        textureName = ARRAY_TEXTURE_NAME;
    } else if (value.getKey() != null) {
        textureName = value.getKey();
    } else {
        return null;
    }
    Optional<Texture> texture = Assets.getTexture(textureName != null ? String.format("engine:editor_%s", textureName) : ICON_BLANK);
    return texture.isPresent() ? texture.get() : null;
}
Also used : JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) Texture(org.terasology.rendering.assets.texture.Texture)

Example 19 with JsonTree

use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.

the class NUIEditorMenuTreeBuilder method createWidgetChild.

private void createWidgetChild(String name, JsonTree node) {
    JsonTree widgetTree = new JsonTree(new JsonTreeValue(name, null, JsonTreeValue.Type.OBJECT));
    NUIEditorNodeUtils.createNewWidget("UILabel", "newWidget", false).getChildren().forEach(widgetTree::addChild);
    widgetTree.addChild(new JsonTreeValue("text", "", JsonTreeValue.Type.KEY_VALUE_PAIR));
    node.addChild(widgetTree);
}
Also used : JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree)

Example 20 with JsonTree

use of org.terasology.rendering.nui.widgets.treeView.JsonTree in project Terasology by MovingBlocks.

the class NUIEditorMenuTreeBuilder method populateContextMenu.

private void populateContextMenu(JsonTree node, MenuTree addTree, boolean isSkin) {
    NUIEditorNodeUtils.NodeInfo nodeInfo;
    if (isSkin) {
        nodeInfo = NUIEditorNodeUtils.getSkinNodeInfo(node);
    } else {
        nodeInfo = NUIEditorNodeUtils.getNodeInfo(node, nuiManager);
    }
    if (nodeInfo != null) {
        Class clazz = nodeInfo.getNodeClass();
        if (clazz != null) {
            for (Field field : ReflectionUtils.getAllFields(clazz)) {
                if ((!UIWidget.class.isAssignableFrom(clazz) || field.isAnnotationPresent(LayoutConfig.class)) && // Exclude static final fields, as they shouldn't be modified.
                !(Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()))) {
                    field.setAccessible(true);
                    String name = getNodeName(field);
                    if (!node.hasChildWithKey(name)) {
                        addTree.addOption(name, n -> {
                            try {
                                JsonTree child = createChild(name, node, field, clazz);
                                for (Consumer<JsonTree> listener : addContextMenuListeners) {
                                    listener.accept(child);
                                }
                            } catch (IllegalAccessException | InstantiationException e) {
                                logger.warn("Could not add child", e);
                            }
                        }, node);
                    }
                }
            }
            // If the node is part of a layout, add an option to add the layoutInfo node.
            if (!node.isRoot() && !node.getParent().isRoot()) {
                String layoutInfo = "layoutInfo";
                String contents = "contents";
                Class parentParentClass = NUIEditorNodeUtils.getNodeInfo((JsonTree) node.getParent().getParent(), nuiManager).getNodeClass();
                if (!node.hasChildWithKey(layoutInfo) && UIWidget.class.isAssignableFrom(clazz) && node.getParent().getValue() != null && node.getParent().getValue().getKey() != null && node.getParent().getValue().getKey().equals(contents) && UILayout.class.isAssignableFrom(parentParentClass)) {
                    addTree.addOption(layoutInfo, n -> {
                        JsonTree child = new JsonTree(new JsonTreeValue(layoutInfo, null, JsonTreeValue.Type.OBJECT));
                        child.setExpanded(true);
                        n.addChild(child);
                    }, node);
                }
            }
        }
    } else {
        logger.warn("Could not get class for node ", node.getValue().toString());
    }
}
Also used : LayoutConfig(org.terasology.rendering.nui.LayoutConfig) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UIWidget(org.terasology.rendering.nui.UIWidget) Field(java.lang.reflect.Field) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) UILayout(org.terasology.rendering.nui.UILayout)

Aggregations

JsonTree (org.terasology.rendering.nui.widgets.treeView.JsonTree)27 JsonTreeValue (org.terasology.rendering.nui.widgets.treeView.JsonTreeValue)14 JsonParser (com.google.gson.JsonParser)9 JsonElement (com.google.gson.JsonElement)6 JsonObject (com.google.gson.JsonObject)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 Map (java.util.Map)5 UIWidget (org.terasology.rendering.nui.UIWidget)5 Gson (com.google.gson.Gson)4 JsonPrimitive (com.google.gson.JsonPrimitive)4 JsonReader (com.google.gson.stream.JsonReader)4 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 UITextEntry (org.terasology.rendering.nui.widgets.UITextEntry)4 Optional (java.util.Optional)3 AssetDataFile (org.terasology.assets.format.AssetDataFile)3 UISkin (org.terasology.rendering.nui.skin.UISkin)3 Field (java.lang.reflect.Field)2 Set (java.util.Set)2 UILayout (org.terasology.rendering.nui.UILayout)2