Search in sources :

Example 6 with JsonTreeValue

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

the class NUIEditorNodeUtils method createNewScreen.

/**
 * @return The {@link JsonTree} to be used as an initial screen template within {@link NUIEditorScreen}.
 */
public static JsonTree createNewScreen() {
    JsonTree tree = new JsonTree(new JsonTreeValue(null, null, JsonTreeValue.Type.OBJECT));
    tree.addChild(new JsonTreeValue("type", "PlaceholderScreen", JsonTreeValue.Type.KEY_VALUE_PAIR));
    tree.addChild(new JsonTreeValue("skin", "engine:default", JsonTreeValue.Type.KEY_VALUE_PAIR));
    JsonTree layout = new JsonTree(new JsonTreeValue("contents", null, JsonTreeValue.Type.OBJECT));
    layout.addChild(new JsonTreeValue("type", "RelativeLayout", JsonTreeValue.Type.KEY_VALUE_PAIR));
    JsonTree contents = new JsonTree(new JsonTreeValue("contents", null, JsonTreeValue.Type.ARRAY));
    JsonTree label = createNewWidget("UILabel", "sampleLabel", true);
    label.addChild(new JsonTreeValue("text", SAMPLE_LABEL_TEXT, JsonTreeValue.Type.KEY_VALUE_PAIR));
    contents.addChild(label);
    layout.addChild(contents);
    tree.addChild(layout);
    return tree;
}
Also used : JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree)

Example 7 with JsonTreeValue

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

the class NUIEditorTextEntryBuilder method createValueEditor.

/**
 * @return A {@link UITextEntry} to be used to edit a JSON value node.
 */
public static UITextEntry<JsonTree> createValueEditor() {
    UITextEntry.Formatter<JsonTree> formatter = value -> value.getValue().toString();
    UITextEntry.Parser<JsonTree> parser = value -> {
        try {
            Double valueDouble = Double.parseDouble(value);
            return new JsonTree(new JsonTreeValue(null, valueDouble, JsonTreeValue.Type.VALUE));
        } catch (NumberFormatException e) {
            if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
                return new JsonTree(new JsonTreeValue(null, Boolean.parseBoolean(value), JsonTreeValue.Type.VALUE));
            } else {
                return new JsonTree(new JsonTreeValue(null, value, JsonTreeValue.Type.VALUE));
            }
        }
    };
    return createEditorEntry(formatter, parser);
}
Also used : JsonElement(com.google.gson.JsonElement) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonObject(com.google.gson.JsonObject) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson) Map(java.util.Map) JsonPrimitive(com.google.gson.JsonPrimitive) JsonParser(com.google.gson.JsonParser) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry)

Example 8 with JsonTreeValue

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

the class NUIEditorTextEntryBuilder method createArrayEditor.

/**
 * @return A {@link UITextEntry} to be used to edit a JSON array node.
 */
public static UITextEntry<JsonTree> createArrayEditor() {
    UITextEntry.Formatter<JsonTree> formatter = value -> value.getValue().getKey();
    UITextEntry.Parser<JsonTree> parser = value -> new JsonTree(new JsonTreeValue(value, null, JsonTreeValue.Type.ARRAY));
    return createEditorEntry(formatter, parser);
}
Also used : JsonElement(com.google.gson.JsonElement) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonObject(com.google.gson.JsonObject) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson) Map(java.util.Map) JsonPrimitive(com.google.gson.JsonPrimitive) JsonParser(com.google.gson.JsonParser) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry)

Example 9 with JsonTreeValue

use of org.terasology.rendering.nui.widgets.treeView.JsonTreeValue 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 10 with JsonTreeValue

use of org.terasology.rendering.nui.widgets.treeView.JsonTreeValue 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)

Aggregations

JsonTree (org.terasology.rendering.nui.widgets.treeView.JsonTree)14 JsonTreeValue (org.terasology.rendering.nui.widgets.treeView.JsonTreeValue)14 Map (java.util.Map)5 Gson (com.google.gson.Gson)4 JsonElement (com.google.gson.JsonElement)4 JsonObject (com.google.gson.JsonObject)4 JsonParser (com.google.gson.JsonParser)4 JsonPrimitive (com.google.gson.JsonPrimitive)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 UITextEntry (org.terasology.rendering.nui.widgets.UITextEntry)4 UIWidget (org.terasology.rendering.nui.UIWidget)3 Field (java.lang.reflect.Field)2 LayoutConfig (org.terasology.rendering.nui.LayoutConfig)2 UILayout (org.terasology.rendering.nui.UILayout)2 MenuTree (org.terasology.rendering.nui.contextMenu.MenuTree)2 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 SerializedName (com.google.gson.annotations.SerializedName)1 Modifier (java.lang.reflect.Modifier)1 ParameterizedType (java.lang.reflect.ParameterizedType)1