Search in sources :

Example 11 with UIWidget

use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.

the class NUIEditorScreen method resetPreviewWidget.

/**
 * {@inheritDoc}
 */
@Override
public void resetPreviewWidget() {
    try {
        // Serialize the editor's contents and update the widget.
        JsonElement element = JsonTreeConverter.deserialize(getEditor().getRoot());
        UIWidget widget = new UIFormat().load(element, alternativeLocale).getRootWidget();
        selectedScreenBox.setContent(widget);
    } catch (Throwable t) {
        String truncatedStackTrace = Joiner.on(System.lineSeparator()).join(Arrays.copyOfRange(ExceptionUtils.getStackFrames(t), 0, 10));
        selectedScreenBox.setContent(new UILabel(truncatedStackTrace));
    }
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) UIFormat(org.terasology.rendering.nui.asset.UIFormat) JsonElement(com.google.gson.JsonElement) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 12 with UIWidget

use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.

the class NUISkinEditorScreen method resetPreviewWidget.

/**
 * {@inheritDoc}
 */
@Override
public void resetPreviewWidget() {
    if (selectedScreen != null) {
        try {
            // Construct a UISkinData instance.
            JsonElement skinElement = JsonTreeConverter.deserialize(getEditor().getRoot());
            UISkinData data = new UISkinFormat().load(skinElement);
            // Get the selected screen asset.
            Optional<UIElement> sourceAsset = assetManager.getAsset(selectedScreen, UIElement.class);
            if (!sourceAsset.isPresent()) {
                throw new FileNotFoundException(String.format("Asset %s not found", selectedScreen));
            }
            AssetDataFile source = sourceAsset.get().getSource();
            String content;
            try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
                reader.setLenient(true);
                content = new JsonParser().parse(reader).toString();
            }
            if (content != null) {
                JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
                JsonElement screenElement = JsonTreeConverter.deserialize(node);
                UIWidget widget = new UIFormat().load(screenElement, alternativeLocale).getRootWidget();
                // Set the screen's skin using the previously generated UISkinData.
                widget.setSkin(Assets.generateAsset(data, UISkin.class));
                selectedScreenBox.setContent(widget);
            }
        } catch (Throwable t) {
            String truncatedStackTrace = Joiner.on(System.lineSeparator()).join(Arrays.copyOfRange(ExceptionUtils.getStackFrames(t), 0, 10));
            selectedScreenBox.setContent(new UILabel(truncatedStackTrace));
        }
    }
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) UISkinFormat(org.terasology.rendering.nui.skin.UISkinFormat) UIElement(org.terasology.rendering.nui.asset.UIElement) InputStreamReader(java.io.InputStreamReader) UIFormat(org.terasology.rendering.nui.asset.UIFormat) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) UISkinData(org.terasology.rendering.nui.skin.UISkinData) UISkin(org.terasology.rendering.nui.skin.UISkin) FileNotFoundException(java.io.FileNotFoundException) UIWidget(org.terasology.rendering.nui.UIWidget) JsonElement(com.google.gson.JsonElement) AssetDataFile(org.terasology.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) JsonParser(com.google.gson.JsonParser)

Example 13 with UIWidget

use of org.terasology.rendering.nui.UIWidget 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.rendering.nui.widgets.treeView.JsonTree) UIDropdownScrollable(org.terasology.rendering.nui.widgets.UIDropdownScrollable) CoreScreenLayer(org.terasology.rendering.nui.CoreScreenLayer) UIWidget(org.terasology.rendering.nui.UIWidget) JsonTreeValue(org.terasology.rendering.nui.widgets.treeView.JsonTreeValue) UIButton(org.terasology.rendering.nui.widgets.UIButton) UpdateListener(org.terasology.rendering.nui.widgets.UpdateListener)

Example 14 with UIWidget

use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.

the class MigLayout method onDraw.

@Override
public void onDraw(Canvas canvas) {
    int[] bounds = { 0, 0, canvas.size().x, canvas.size().y };
    layoutContainer(canvas, bounds);
    for (ComponentWrapper wrapper : wrappers.values()) {
        UIWidget component = (UIWidget) wrapper.getComponent();
        Rect2i region = Rect2i.createFromMinAndSize(wrapper.getX(), wrapper.getY(), wrapper.getWidth(), wrapper.getHeight());
        canvas.drawWidget(component, region);
    }
    if (debug) {
        grid.paintDebug();
    }
    for (Rect2i region : debugRects) {
        canvas.drawLine(region.minX(), region.minY(), region.maxX(), region.minY(), Color.WHITE);
        canvas.drawLine(region.maxX(), region.minY(), region.maxX(), region.maxY(), Color.WHITE);
        canvas.drawLine(region.maxX(), region.maxY(), region.minX(), region.maxY(), Color.WHITE);
        canvas.drawLine(region.minX(), region.maxY(), region.minX(), region.minY(), Color.WHITE);
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) ComponentWrapper(net.miginfocom.layout.ComponentWrapper) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 15 with UIWidget

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

Aggregations

UIWidget (org.terasology.rendering.nui.UIWidget)23 Vector2i (org.terasology.math.geom.Vector2i)13 LayoutHint (org.terasology.rendering.nui.LayoutHint)12 List (java.util.List)6 UILabel (org.terasology.rendering.nui.widgets.UILabel)4 Rect2i (org.terasology.math.geom.Rect2i)3 UIElement (org.terasology.rendering.nui.asset.UIElement)3 JsonTree (org.terasology.rendering.nui.widgets.treeView.JsonTree)3 JsonElement (com.google.gson.JsonElement)2 UIFormat (org.terasology.rendering.nui.asset.UIFormat)2 JsonParser (com.google.gson.JsonParser)1 JsonReader (com.google.gson.stream.JsonReader)1 TIntList (gnu.trove.list.TIntList)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ComponentWrapper (net.miginfocom.layout.ComponentWrapper)1 AssetDataFile (org.terasology.assets.format.AssetDataFile)1