Search in sources :

Example 21 with UIWidget

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

the class MultiRowLayout method calculateColumnSize.

private ColumnInfo calculateColumnSize(List<UIWidget> column, Canvas canvas, Vector2i areaHint) {
    int availableHeight = areaHint.y - verticalSpacing * (rows - 1);
    ColumnInfo columnInfo = new ColumnInfo();
    for (int i = 0; i < rows && i < column.size(); ++i) {
        UIWidget widget = column.get(i);
        Vector2i cellSize = new Vector2i(areaHint.x, availableHeight);
        if (!autoSizeRows) {
            cellSize.y *= rowHeights[i];
        }
        if (widget != null) {
            Vector2i contentSize = canvas.calculateRestrictedSize(widget, cellSize);
            columnInfo.widgetSizes.add(contentSize);
            columnInfo.width = Math.max(columnInfo.width, contentSize.x);
        } else {
            columnInfo.widgetSizes.add(new Vector2i(0, 0));
        }
    }
    return columnInfo;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 22 with UIWidget

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

the class RowLayout method calcWidths.

/**
 * Calculates the widths of each of the widgets in this layout's widget list.
 * Widths are first calculated for widgets with a relative width specified,
 * followed by widgets which follow their content width.
 * The remaining width is then split equally among the remaining widgets.
 *
 * @param canvas The {@link Canvas} on which this {@code RowLayout} is drawn
 * @return A list of the widths of the widgets in this layout's widget list, in pixels
 */
private TIntList calcWidths(Canvas canvas) {
    TIntList results = new TIntArrayList(contents.size());
    if (contents.size() > 0) {
        int width = canvas.size().x - horizontalSpacing * (contents.size() - 1);
        int totalWidthUsed = 0;
        int unprocessedWidgets = 0;
        for (UIWidget widget : contents) {
            RowLayoutHint hint = hints.get(widget);
            if (hint != null) {
                if (!hint.isUseContentWidth() && hint.getRelativeWidth() != 0) {
                    int elementWidth = TeraMath.floorToInt(hint.getRelativeWidth() * width);
                    results.add(elementWidth);
                    totalWidthUsed += elementWidth;
                } else {
                    results.add(0);
                    unprocessedWidgets++;
                }
            } else {
                results.add(0);
                unprocessedWidgets++;
            }
        }
        if (unprocessedWidgets > 0) {
            int remainingWidthPerElement = (width - totalWidthUsed) / unprocessedWidgets;
            for (int i = 0; i < results.size(); ++i) {
                if (results.get(i) == 0) {
                    RowLayoutHint hint = hints.get(contents.get(i));
                    if (hint != null) {
                        if (hint.isUseContentWidth()) {
                            Vector2i contentSize = contents.get(i).getPreferredContentSize(canvas, new Vector2i(remainingWidthPerElement, canvas.size().y));
                            results.set(i, contentSize.x);
                            totalWidthUsed += contentSize.x;
                            unprocessedWidgets--;
                        }
                    }
                }
            }
        }
        if (unprocessedWidgets > 0) {
            int remainingWidthPerElement = (width - totalWidthUsed) / unprocessedWidgets;
            for (int i = 0; i < results.size(); ++i) {
                if (results.get(i) == 0) {
                    results.set(i, remainingWidthPerElement);
                }
            }
        }
    }
    return results;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) TIntList(gnu.trove.list.TIntList) TIntArrayList(gnu.trove.list.array.TIntArrayList) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 23 with UIWidget

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

the class BehaviorEditor method onDraw.

@Override
public void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    canvas.addInteractionRegion(mouseInteractionListener);
    try (SubRegion subRegion = canvas.subRegion(canvas.getRegion(), false)) {
        canvas.setDrawOnTop(true);
        for (UIWidget widget : getWidgets()) {
            if (!widget.isVisible()) {
                continue;
            }
            if (widget instanceof RenderableNode) {
                RenderableNode renderableNode = (RenderableNode) widget;
                for (Port port : renderableNode.getPorts()) {
                    Port targetPort = port.getTargetPort();
                    if (port.isInput() || targetPort == null || !targetPort.node.isVisible()) {
                        continue;
                    }
                    drawConnection(canvas, port, targetPort, port == activeConnectionStart ? Color.BLACK : Color.GREY);
                }
            }
        }
        if (activeConnectionStart != null) {
            drawConnection(canvas, activeConnectionStart, mouseWorldPosition, Color.WHITE);
        }
        if (selectedNode != null) {
            Vector2f size = selectedNode.getSize();
            Vector2f topLeft = selectedNode.getPosition();
            Vector2f topRight = new Vector2f(topLeft);
            topRight.add(new Vector2f(size.x + .1f, 0));
            Vector2f bottomLeft = new Vector2f(topLeft);
            bottomLeft.add(new Vector2f(0, size.y + .1f));
            Vector2f bottomRight = new Vector2f(topLeft);
            bottomRight.add(new Vector2f(size.x + 0.1f, size.y + 0.1f));
            drawConnection(canvas, topLeft, topRight, Color.GREEN);
            drawConnection(canvas, topRight, bottomRight, Color.GREEN);
            drawConnection(canvas, bottomRight, bottomLeft, Color.GREEN);
            drawConnection(canvas, bottomLeft, topLeft, Color.GREEN);
        }
        if (newNode != null) {
            newNode.visit(node -> drawWidget(canvas, node));
        }
        canvas.setDrawOnTop(false);
    }
}
Also used : Vector2f(org.terasology.math.geom.Vector2f) SubRegion(org.terasology.rendering.nui.SubRegion) 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