Search in sources :

Example 11 with JsonTreeValue

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

Example 12 with JsonTreeValue

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

the class NUIEditorNodeUtils method createNewWidget.

/**
 * @param type          The type of the widget.
 * @param id            The id of the widget.
 * @param addLayoutInfo Whether a few layout settings from {@link RelativeLayout} should be added.
 * @return The {@link JsonTree} with the given type/id to be used as an empty widget template within {@link NUIEditorScreen}.
 */
public static JsonTree createNewWidget(String type, String id, boolean addLayoutInfo) {
    JsonTree widget = new JsonTree(new JsonTreeValue(null, null, JsonTreeValue.Type.OBJECT));
    widget.addChild(new JsonTreeValue("type", type, JsonTreeValue.Type.KEY_VALUE_PAIR));
    widget.addChild(new JsonTreeValue("id", id, JsonTreeValue.Type.KEY_VALUE_PAIR));
    JsonTree layoutInfo = new JsonTree(new JsonTreeValue("layoutInfo", null, JsonTreeValue.Type.OBJECT));
    if (addLayoutInfo) {
        layoutInfo.addChild(new JsonTreeValue("width", 500, JsonTreeValue.Type.KEY_VALUE_PAIR));
        JsonTree hPosition = new JsonTree(new JsonTreeValue("position-horizontal-center", null, JsonTreeValue.Type.OBJECT));
        JsonTree vPosition = new JsonTree(new JsonTreeValue("position-vertical-center", null, JsonTreeValue.Type.OBJECT));
        layoutInfo.addChild(hPosition);
        layoutInfo.addChild(vPosition);
    }
    widget.addChild(layoutInfo);
    return widget;
}
Also used : JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree)

Example 13 with JsonTreeValue

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

the class NUIEditorTextEntryBuilder method createObjectEditor.

/**
 * @return A {@link UITextEntry} to be used to edit a JSON object node.
 */
public static UITextEntry<JsonTree> createObjectEditor() {
    UITextEntry.Formatter<JsonTree> formatter = value -> value.getValue().getKey();
    UITextEntry.Parser<JsonTree> parser = value -> new JsonTree(new JsonTreeValue(value, null, JsonTreeValue.Type.OBJECT));
    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 14 with JsonTreeValue

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

the class NUIEditorTextEntryBuilder method createKeyValueEditor.

/**
 * @return A {@link UITextEntry} to be used to edit a JSON key/value node.
 */
public static UITextEntry<JsonTree> createKeyValueEditor() {
    UITextEntry.Formatter<JsonTree> formatter = value -> {
        JsonObject jsonObject = new JsonObject();
        String jsonKey = value.getValue().getKey();
        Object jsonValue = value.getValue().getValue();
        if (jsonValue instanceof Boolean) {
            jsonObject.addProperty(jsonKey, (Boolean) jsonValue);
        } else if (jsonValue instanceof Number) {
            jsonObject.addProperty(jsonKey, (Number) jsonValue);
        } else if (jsonValue instanceof String) {
            jsonObject.addProperty(jsonKey, (String) jsonValue);
        } else {
            jsonObject.addProperty(jsonKey, (Character) jsonValue);
        }
        String jsonString = new Gson().toJson(jsonObject);
        return jsonString.substring(1, jsonString.length() - 1);
    };
    UITextEntry.Parser<JsonTree> parser = value -> {
        String jsonString = String.format("{%s}", value);
        try {
            JsonElement jsonElement = new JsonParser().parse(jsonString);
            Map.Entry keyValuePair = jsonElement.getAsJsonObject().entrySet().iterator().next();
            String jsonKey = (String) keyValuePair.getKey();
            JsonTreeValue parsedNode;
            if (keyValuePair.getValue() == null) {
                parsedNode = new JsonTreeValue(jsonKey, null, JsonTreeValue.Type.KEY_VALUE_PAIR);
            } else {
                JsonPrimitive jsonValue = (JsonPrimitive) keyValuePair.getValue();
                if (jsonValue.isBoolean()) {
                    parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsBoolean(), JsonTreeValue.Type.KEY_VALUE_PAIR);
                } else if (jsonValue.isNumber()) {
                    parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsNumber(), JsonTreeValue.Type.KEY_VALUE_PAIR);
                } else {
                    parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsString(), JsonTreeValue.Type.KEY_VALUE_PAIR);
                }
            }
            return new JsonTree(parsedNode);
        } catch (JsonSyntaxException e) {
            return null;
        }
    };
    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) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) UITextEntry(org.terasology.rendering.nui.widgets.UITextEntry) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

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