Search in sources :

Example 6 with Vector2i

use of org.terasology.math.geom.Vector2i 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)

Example 7 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class FlowLayout method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
    Vector2i result = new Vector2i();
    int filledWidth = 0;
    int filledHeight = 0;
    for (UIWidget widget : contents) {
        Vector2i size = canvas.calculatePreferredSize(widget);
        if (filledWidth != 0 && filledWidth + size.x > sizeHint.x) {
            result.x = Math.max(result.x, filledWidth);
            result.y += filledHeight;
            filledWidth = size.x;
            filledHeight = size.y;
        } else {
            filledWidth += size.x;
            filledHeight = Math.max(filledHeight, size.y);
        }
    }
    result.x = Math.max(result.x, filledWidth);
    result.y += filledHeight;
    return result;
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 8 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class FlowLayout method onDraw.

@Override
public void onDraw(Canvas canvas) {
    int filledWidth = 0;
    int filledHeight = 0;
    int heightOffset = 0;
    for (UIWidget widget : contents) {
        Vector2i size = canvas.calculatePreferredSize(widget);
        if (filledWidth != 0 && filledWidth + size.x > canvas.size().x) {
            heightOffset += filledHeight;
            filledWidth = 0;
            filledHeight = 0;
        }
        canvas.drawWidget(widget, Rect2i.createFromMinAndSize(filledWidth, heightOffset, size.x, size.y));
        filledWidth += size.x;
        filledHeight = Math.max(filledHeight, size.y);
    }
}
Also used : Vector2i(org.terasology.math.geom.Vector2i) LayoutHint(org.terasology.rendering.nui.LayoutHint) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 9 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class MultiRowLayout method getMaxContentSize.

@Override
public Vector2i getMaxContentSize(Canvas canvas) {
    Iterator<List<UIWidget>> columns = getColumnIterator();
    Vector2i size = new Vector2i();
    int[] rowSizes = new int[rows];
    while (columns.hasNext()) {
        List<UIWidget> column = columns.next();
        int columnWidth = 0;
        for (int i = 0; i < column.size(); ++i) {
            Vector2i maxSize = canvas.calculateMaximumSize(column.get(i));
            rowSizes[i] = Math.max(rowSizes[i], maxSize.y);
            columnWidth = Math.max(columnWidth, maxSize.x);
        }
        size.x = TeraMath.addClampAtMax(size.x, columnWidth);
        if (columns.hasNext()) {
            size.x = TeraMath.addClampAtMax(size.x, horizontalSpacing);
        }
    }
    long height = 0;
    for (int rowSize : rowSizes) {
        height += rowSize;
    }
    if (!autoSizeRows) {
        for (int i = 0; i < rows; ++i) {
            height = Math.min(height, TeraMath.floorToInt(rowSizes[i] / rowHeights[i]));
        }
    }
    height += verticalSpacing * (rows - 1);
    size.y = (int) Math.min(Integer.MAX_VALUE, height);
    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 10 with Vector2i

use of org.terasology.math.geom.Vector2i in project Terasology by MovingBlocks.

the class MultiRowLayout method getPreferredContentSize.

@Override
public Vector2i getPreferredContentSize(Canvas canvas, Vector2i areaHint) {
    Vector2i availableSize = new Vector2i(areaHint);
    int numColumns = TeraMath.ceilToInt((float) widgetList.size() / rows);
    if (numColumns > 0) {
        availableSize.x -= horizontalSpacing * (numColumns - 1);
    }
    if (rows > 0) {
        availableSize.y -= verticalSpacing * (rows - 1);
    }
    Iterator<List<UIWidget>> columns = getColumnIterator();
    Vector2i size = new Vector2i();
    int[] rowSizes = new int[rows];
    while (columns.hasNext()) {
        List<UIWidget> column = columns.next();
        ColumnInfo columnInfo = calculateColumnSize(column, canvas, availableSize);
        size.x += columnInfo.width;
        if (columns.hasNext()) {
            size.x += horizontalSpacing;
        }
        for (int i = 0; i < columnInfo.widgetSizes.size(); ++i) {
            rowSizes[i] = Math.max(rowSizes[i], columnInfo.widgetSizes.get(i).getY());
        }
    }
    for (int rowSize : rowSizes) {
        size.y += rowSize;
    }
    if (!autoSizeRows) {
        for (int i = 0; i < rows; ++i) {
            size.y = Math.max(size.y, TeraMath.floorToInt(rowSizes[i] / rowHeights[i]));
        }
    }
    size.y += verticalSpacing * (rows - 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)

Aggregations

Vector2i (org.terasology.math.geom.Vector2i)76 UIWidget (org.terasology.rendering.nui.UIWidget)17 Rect2i (org.terasology.math.geom.Rect2i)14 BaseVector2i (org.terasology.math.geom.BaseVector2i)13 LayoutHint (org.terasology.rendering.nui.LayoutHint)13 List (java.util.List)9 Test (org.junit.Test)9 Vector2f (org.terasology.math.geom.Vector2f)6 Canvas (org.terasology.rendering.nui.Canvas)5 SubRegion (org.terasology.rendering.nui.SubRegion)5 UIStyle (org.terasology.rendering.nui.skin.UIStyle)5 UILabel (org.terasology.rendering.nui.widgets.UILabel)5 Before (org.junit.Before)4 Font (org.terasology.rendering.assets.font.Font)4 Color (org.terasology.rendering.nui.Color)4 Border (org.terasology.math.Border)3 Vector3f (org.terasology.math.geom.Vector3f)3 Mesh (org.terasology.rendering.assets.mesh.Mesh)3 HorizontalAlign (org.terasology.rendering.nui.HorizontalAlign)3 Sets (com.google.common.collect.Sets)2