Search in sources :

Example 21 with JsonTree

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

the class NUIEditorNodeUtils method getPathToRoot.

private static Deque<JsonTree> getPathToRoot(JsonTree node) {
    Deque<JsonTree> pathToRoot = Queues.newArrayDeque();
    // Create a stack with the root node at the top and the argument at the bottom.
    JsonTree currentNode = node;
    while (!currentNode.isRoot()) {
        pathToRoot.push(currentNode);
        currentNode = (JsonTree) currentNode.getParent();
    }
    pathToRoot.push(currentNode);
    return pathToRoot;
}
Also used : JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree)

Example 22 with JsonTree

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

the class NUIEditorNodeUtils method getSkinNodeInfo.

/**
 * @param node A node in an asset tree.
 * @return The info about this node's type.
 */
public static NodeInfo getSkinNodeInfo(JsonTree node) {
    Deque<JsonTree> pathToRoot = getPathToRoot(node);
    // Start iterating from top to bottom.
    Class nodeClass = null;
    for (JsonTree n : pathToRoot) {
        if (n.isRoot()) {
            nodeClass = UIStyleFragment.class;
        } else {
            if ("elements".equals(n.getValue().getKey()) || "families".equals(n.getValue().getKey())) {
                nodeClass = null;
            } else if (n.getParent().getValue().getKey() != null && ("elements".equals(n.getParent().getValue().getKey()) || "families".equals(n.getParent().getValue().getKey()))) {
                nodeClass = UIStyleFragment.class;
            } else {
                String value = n.getValue().toString();
                Set<Field> fields = ReflectionUtils.getAllFields(nodeClass);
                Optional<Field> newField = fields.stream().filter(f -> f.getName().equalsIgnoreCase(value)).findFirst();
                if (newField.isPresent()) {
                    nodeClass = newField.get().getType();
                } else {
                    Optional<Field> serializedNameField = fields.stream().filter(f -> f.isAnnotationPresent(SerializedName.class) && f.getAnnotation(SerializedName.class).value().equals(value)).findFirst();
                    if (serializedNameField.isPresent()) {
                        nodeClass = serializedNameField.get().getType();
                    } else {
                        return null;
                    }
                }
            }
        }
    }
    return new NodeInfo(nodeClass, null);
}
Also used : Set(java.util.Set) Optional(java.util.Optional) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UIStyleFragment(org.terasology.rendering.nui.skin.UIStyleFragment)

Example 23 with JsonTree

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

the class NUIEditorNodeUtils method getNodeInfo.

/**
 * @param node       A node in an asset tree.
 * @param nuiManager The {@link NUIManager} to be used for widget type resolution.
 * @return The info about this node's type.
 */
public static NodeInfo getNodeInfo(JsonTree node, NUIManager nuiManager) {
    Deque<JsonTree> pathToRoot = getPathToRoot(node);
    // Start iterating from top to bottom.
    Class currentClass = null;
    Class activeLayoutClass = null;
    for (JsonTree n : pathToRoot) {
        if (n.isRoot()) {
            // currentClass is not set - set it to the screen type.
            String type = (String) n.getChildWithKey("type").getValue().getValue();
            currentClass = nuiManager.getWidgetMetadataLibrary().resolve(type, ModuleContext.getContext()).getType();
        } else {
            if (List.class.isAssignableFrom(currentClass) && n.getValue().getKey() == null && "contents".equals(n.getParent().getValue().getKey())) {
                // Transition from a "contents" list to a UIWidget.
                currentClass = UIWidget.class;
            } else {
                // Retrieve the type of an unspecified UIWidget.
                if (currentClass == UIWidget.class && n.hasSiblingWithKey("type")) {
                    String type = (String) n.getSiblingWithKey("type").getValue().getValue();
                    currentClass = nuiManager.getWidgetMetadataLibrary().resolve(type, ModuleContext.getContext()).getType();
                }
                // If the current class is a layout, remember its' value (but do not set until later on!)
                Class layoutClass = null;
                if (UILayout.class.isAssignableFrom(currentClass)) {
                    layoutClass = currentClass;
                }
                if (UILayout.class.isAssignableFrom(currentClass) && "contents".equals(n.getValue().getKey())) {
                    // "contents" fields of a layout are always (widget) lists.
                    currentClass = List.class;
                } else if (UIWidget.class.isAssignableFrom(currentClass) && "layoutInfo".equals(n.getValue().getKey())) {
                    // Set currentClass to the layout hint type for the active layout.
                    currentClass = (Class) ReflectionUtil.getTypeParameter(activeLayoutClass.getGenericSuperclass(), 0);
                } else {
                    String value = n.getValue().toString();
                    Set<Field> fields = ReflectionUtils.getAllFields(currentClass);
                    Optional<Field> newField = fields.stream().filter(f -> f.getName().equalsIgnoreCase(value)).findFirst();
                    if (newField.isPresent()) {
                        currentClass = newField.get().getType();
                    } else {
                        Optional<Field> serializedNameField = fields.stream().filter(f -> f.isAnnotationPresent(SerializedName.class) && f.getAnnotation(SerializedName.class).value().equals(value)).findFirst();
                        if (serializedNameField.isPresent()) {
                            currentClass = serializedNameField.get().getType();
                        } else {
                            return null;
                        }
                    }
                }
                // Set the layout class value.
                if (layoutClass != null) {
                    activeLayoutClass = layoutClass;
                }
            }
        }
    }
    // If the final result is a generic UIWidget, attempt to retrieve its' type.
    if (currentClass == UIWidget.class && node.hasChildWithKey("type")) {
        String type = (String) node.getChildWithKey("type").getValue().getValue();
        currentClass = nuiManager.getWidgetMetadataLibrary().resolve(type, ModuleContext.getContext()).getType();
    }
    return new NodeInfo(currentClass, activeLayoutClass);
}
Also used : Set(java.util.Set) UILayout(org.terasology.rendering.nui.UILayout) Optional(java.util.Optional) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 24 with JsonTree

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

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

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