Search in sources :

Example 1 with UIWidget

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

the class NUIManagerInternal method createScreen.

@Override
public <T extends CoreScreenLayer> T createScreen(ResourceUrn screenUri, Class<T> expectedType) {
    boolean existsAlready = !screenUri.isInstance() && assetManager.isLoaded(screenUri, UIElement.class);
    Optional<UIElement> opt = Assets.get(screenUri, UIElement.class);
    if (!opt.isPresent()) {
        logger.error("Can't find screen '{}'", screenUri);
    } else {
        UIElement element = opt.get();
        UIWidget root = element.getRootWidget();
        if (expectedType.isInstance(root)) {
            T screen = expectedType.cast(root);
            if (!existsAlready) {
                initialiseScreen(screen, screenUri);
            }
            return screen;
        } else {
            logger.error("Screen '{}' is a '{}' and not a '{}'", screenUri, root.getClass(), expectedType);
        }
    }
    return null;
}
Also used : UIElement(org.terasology.rendering.nui.asset.UIElement) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 2 with UIWidget

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

the class CardLayout method getPreferredContentSize.

/**
 * Retrieves the preferred content size of this {@code CardLayout}.
 * This is the minimum size this layout will take, given no space restrictions.
 *
 * @param canvas The {@code Canvas} on which this {@code CardLayout} is drawn
 * @param sizeHint A {@link Vector2i} representing the space available for widgets to be drawn in this layout
 * @return A {@code Vector2i} representing the preferred content size of this {@code CardLayout}
 */
@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
    int maxX = 0;
    int maxY = 0;
    for (UIWidget uiWidget : widgets.values()) {
        Vector2i preferredContentSize = uiWidget.getPreferredContentSize(canvas, sizeHint);
        maxX = Math.max(maxX, preferredContentSize.x);
        maxY = Math.max(maxY, preferredContentSize.y);
    }
    return new Vector2i(maxX, maxY);
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 3 with UIWidget

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

the class CardLayout method getMaxContentSize.

/**
 * Retrieves the maximum content size of this {@code CardLayout}.
 *
 * @param canvas The {@code Canvas} on which this {@code CardLayout} is drawn
 * @return A {@code Vector2i} representing the maximum content size of this {@code CardLayout}
 */
@Override
public Vector2i getMaxContentSize(Canvas canvas) {
    int maxX = 0;
    int maxY = 0;
    for (UIWidget uiWidget : widgets.values()) {
        Vector2i maxContentSize = uiWidget.getMaxContentSize(canvas);
        maxX = Math.max(maxX, maxContentSize.x);
        maxY = Math.max(maxY, maxContentSize.y);
    }
    return new Vector2i(maxX, maxY);
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 4 with UIWidget

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

the class ColumnLayout method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
    Vector2i availableSize = new Vector2i(areaHint);
    int numRows = TeraMath.ceilToInt((float) widgetList.size() / columns);
    if (numRows > 0) {
        availableSize.y -= verticalSpacing * (numRows - 1);
    }
    if (columns > 0) {
        availableSize.x -= horizontalSpacing * (columns - 1);
    }
    Iterator<List<UIWidget>> rows = getRowIterator();
    Vector2i size = new Vector2i();
    int[] columnSizes = new int[columns];
    while (rows.hasNext()) {
        List<UIWidget> row = rows.next();
        RowInfo rowInfo = calculateRowSize(row, canvas, availableSize);
        size.y += rowInfo.height;
        if (rows.hasNext()) {
            size.y += verticalSpacing;
        }
        for (int i = 0; i < rowInfo.widgetSizes.size(); ++i) {
            columnSizes[i] = Math.max(columnSizes[i], rowInfo.widgetSizes.get(i).getX());
        }
    }
    for (int columnSize : columnSizes) {
        size.x += columnSize;
    }
    if (!autoSizeColumns) {
        for (int i = 0; i < columns; ++i) {
            size.x = Math.max(size.x, TeraMath.floorToInt(columnSizes[i] / columnWidths[i]));
        }
    }
    size.x += horizontalSpacing * (columns - 1);
    return size;
}
Also used : List(java.util.List) Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 5 with UIWidget

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

the class ColumnLayout method onDraw.

@Override
public void onDraw(Canvas canvas) {
    if (!widgetList.isEmpty()) {
        Vector2i availableSize = canvas.size();
        int numRows = TeraMath.ceilToInt((float) widgetList.size() / columns);
        if (numRows > 0) {
            availableSize.y -= verticalSpacing * (numRows - 1);
        }
        if (columns > 0) {
            availableSize.x -= horizontalSpacing * (columns - 1);
        }
        List<List<UIWidget>> rows = Lists.newArrayList(getRowIterator());
        List<RowInfo> rowInfos = Lists.newArrayList();
        rowInfos.addAll(rows.stream().map(row -> calculateRowSize(row, canvas, availableSize)).collect(Collectors.toList()));
        int[] minWidths = new int[columns];
        int minRowWidth = 0;
        int rowOffsetX = 0;
        if (autoSizeColumns) {
            for (RowInfo row : rowInfos) {
                for (int column = 0; column < row.widgetSizes.size(); column++) {
                    minWidths[column] = Math.max(minWidths[column], row.widgetSizes.get(column).getX());
                }
            }
            for (int width : minWidths) {
                minRowWidth += width;
            }
            minRowWidth += (columns - 1) * horizontalSpacing;
            rowOffsetX = (canvas.size().x - minRowWidth) / 2;
        } else {
            minRowWidth = canvas.size().x;
            for (int i = 0; i < columns; ++i) {
                minWidths[i] = TeraMath.floorToInt((minRowWidth - (columns - 1) * horizontalSpacing) * columnWidths[i]);
            }
        }
        int rowOffsetY = 0;
        int usedHeight = 0;
        for (RowInfo row : rowInfos) {
            usedHeight += row.height;
        }
        usedHeight += (rowInfos.size() - 1) * verticalSpacing;
        int excessHeight = canvas.size().y - usedHeight;
        if (fillVerticalSpace) {
            if (extendLast && numRows > 0) {
                // give all the extra space to the last entry
                rowInfos.get(numRows - 1).height += excessHeight;
            } else {
                // distribute extra height equally
                int extraSpacePerRow = excessHeight / rowInfos.size();
                for (RowInfo row : rowInfos) {
                    row.height += extraSpacePerRow;
                }
            }
        } else {
            rowOffsetY = excessHeight / 2;
        }
        for (int rowIndex = 0; rowIndex < rows.size(); ++rowIndex) {
            List<UIWidget> row = rows.get(rowIndex);
            RowInfo rowInfo = rowInfos.get(rowIndex);
            int cellOffsetX = rowOffsetX;
            for (int i = 0; i < row.size(); ++i) {
                UIWidget widget = row.get(i);
                int rowHeight = rowInfo.height;
                if (widget != null) {
                    Rect2i drawRegion = Rect2i.createFromMinAndSize(cellOffsetX, rowOffsetY, minWidths[i], rowHeight);
                    canvas.drawWidget(widget, drawRegion);
                }
                cellOffsetX += minWidths[i] + horizontalSpacing;
            }
            rowOffsetY += rowInfo.height + verticalSpacing;
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) List(java.util.List) Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint) 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