Search in sources :

Example 36 with Rect2i

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

the class HUDScreenLayer method onDraw.

@Override
public void onDraw(Canvas canvas) {
    for (HUDElement element : elementsLookup.values()) {
        int minX = TeraMath.floorToInt(element.region.minX() * canvas.size().x);
        int minY = TeraMath.floorToInt(element.region.minY() * canvas.size().y);
        int sizeX = TeraMath.floorToInt(element.region.width() * canvas.size().x);
        int sizeY = TeraMath.floorToInt(element.region.height() * canvas.size().y);
        Rect2i region = Rect2i.createFromMinAndSize(minX, minY, sizeX, sizeY);
        canvas.drawWidget(element.widget, region);
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i)

Example 37 with Rect2i

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

the class MultiRowLayout method onDraw.

@Override
public void onDraw(Canvas canvas) {
    if (!widgetList.isEmpty()) {
        Vector2i availableSize = canvas.size();
        int numColumns = TeraMath.ceilToInt((float) widgetList.size() / rows);
        if (numColumns > 0) {
            availableSize.x -= horizontalSpacing * (numColumns - 1);
        }
        if (rows > 0) {
            availableSize.y -= verticalSpacing * (rows - 1);
        }
        List<List<UIWidget>> columns = Lists.newArrayList(getColumnIterator());
        List<ColumnInfo> columnInfos = Lists.newArrayList();
        columnInfos.addAll(columns.stream().map(column -> calculateColumnSize(column, canvas, availableSize)).collect(Collectors.toList()));
        int[] minHeights = new int[rows];
        int minColumnHeight = 0;
        int columnOffsetY = 0;
        if (autoSizeRows) {
            for (ColumnInfo column : columnInfos) {
                for (int row = 0; row < column.widgetSizes.size(); row++) {
                    minHeights[row] = Math.max(minHeights[row], column.widgetSizes.get(row).getY());
                }
            }
            for (int height : minHeights) {
                minColumnHeight += height;
            }
            minColumnHeight += (rows - 1) * verticalSpacing;
            columnOffsetY = (canvas.size().y - minColumnHeight) / 2;
        } else {
            minColumnHeight = canvas.size().y;
            for (int i = 0; i < rows; ++i) {
                minHeights[i] = TeraMath.floorToInt((minColumnHeight - (rows - 1) * verticalSpacing) * rowHeights[i]);
            }
        }
        int columnOffsetX = 0;
        int usedWidth = 0;
        for (ColumnInfo column : columnInfos) {
            usedWidth += column.width;
        }
        usedWidth += (columnInfos.size() - 1) * horizontalSpacing;
        columnOffsetX = (canvas.size().x - usedWidth) / 2;
        for (int columnIndex = 0; columnIndex < columns.size(); ++columnIndex) {
            List<UIWidget> column = columns.get(columnIndex);
            ColumnInfo columnInfo = columnInfos.get(columnIndex);
            int cellOffsetY = columnOffsetY;
            for (int i = 0; i < column.size(); ++i) {
                UIWidget widget = column.get(i);
                int columnWidth = columnInfo.width;
                if (widget != null) {
                    Rect2i drawRegion = Rect2i.createFromMinAndSize(columnOffsetX, cellOffsetY, columnWidth, minHeights[i]);
                    canvas.drawWidget(widget, drawRegion);
                }
                cellOffsetY += minHeights[i] + verticalSpacing;
            }
            columnOffsetX += columnInfo.width + horizontalSpacing;
        }
    }
}
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 38 with Rect2i

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

the class ScrollableArea method layoutWithJustVertical.

private void layoutWithJustVertical(Canvas canvas, Vector2i contentSize, int fullWidth, int availableHeight, int scrollbarWidth) {
    int availableWidth = fullWidth - scrollbarWidth;
    boolean atBottom = verticalBar.getRange() == verticalBar.getValue();
    Rect2i contentRegion = Rect2i.createFromMinAndSize(0, 0, availableWidth, availableHeight);
    verticalBar.setRange(contentSize.y - contentRegion.height());
    if ((stickToBottom && atBottom) || moveToBottomPending) {
        verticalBar.setValue(verticalBar.getRange());
        moveToBottomPending = false;
    }
    if (moveToTopPending) {
        verticalBar.setValue(0);
        moveToTopPending = false;
    }
    canvas.addInteractionRegion(scrollListener);
    canvas.drawWidget(verticalBar, Rect2i.createFromMinAndSize(availableWidth, 0, scrollbarWidth, availableHeight));
    try (SubRegion ignored = canvas.subRegion(contentRegion, true)) {
        canvas.drawWidget(content, Rect2i.createFromMinAndSize(0, -verticalBar.getValue(), availableWidth, contentSize.y));
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) SubRegion(org.terasology.rendering.nui.SubRegion) LayoutHint(org.terasology.rendering.nui.LayoutHint)

Example 39 with Rect2i

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

the class TextureUtil method convertToImage.

public static BufferedImage convertToImage(TextureRegion textureRegion) {
    final int width = textureRegion.getWidth();
    final int height = textureRegion.getHeight();
    final Rect2i pixelRegion = textureRegion.getPixelRegion();
    final Texture texture = textureRegion.getTexture();
    ByteBuffer textureBytes = texture.getData().getBuffers()[0];
    int stride = texture.getWidth() * 4;
    int posX = pixelRegion.minX();
    int posY = pixelRegion.minY();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int r = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4));
            int g = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 1));
            int b = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 2));
            int a = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 3));
            int argb = (a << 24) + (r << 16) + (g << 8) + b;
            image.setRGB(x, y, argb);
        }
    }
    return image;
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage)

Example 40 with Rect2i

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

the class ResettableUIText method onDraw.

@Override
public void onDraw(Canvas canvas) {
    Rect2i clearButtonRegion = Rect2i.createFromMinAndSize(0, 0, 30, canvas.size().y);
    lastWidth = canvas.size().x - clearButtonRegion.size().x;
    if (isEnabled()) {
        canvas.addInteractionRegion(interactionListener, Rect2i.createFromMinAndMax(0, 0, canvas.size().x, canvas.size().y));
        canvas.addInteractionRegion(clearInteractionListener, Rect2i.createFromMinAndMax(canvas.size().x, 0, canvas.size().x + clearButtonRegion.size().x, canvas.size().y));
    }
    drawAll(canvas, canvas.size().x - clearButtonRegion.size().x);
}
Also used : Rect2i(org.terasology.math.geom.Rect2i)

Aggregations

Rect2i (org.terasology.math.geom.Rect2i)59 Vector2i (org.terasology.math.geom.Vector2i)13 SubRegion (org.terasology.rendering.nui.SubRegion)8 BaseVector2i (org.terasology.math.geom.BaseVector2i)7 Font (org.terasology.rendering.assets.font.Font)5 LayoutHint (org.terasology.rendering.nui.LayoutHint)5 SurfaceHeightFacet (org.terasology.world.generation.facets.SurfaceHeightFacet)4 ByteBuffer (java.nio.ByteBuffer)3 List (java.util.List)3 Color (org.terasology.rendering.nui.Color)3 BufferedImage (java.awt.image.BufferedImage)2 Test (org.junit.Test)2 UIWidget (org.terasology.rendering.nui.UIWidget)2 FallbackParagraphRenderStyle (org.terasology.rendering.nui.widgets.browser.ui.style.FallbackParagraphRenderStyle)2 ParagraphRenderStyle (org.terasology.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)2 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 TIntList (gnu.trove.list.TIntList)1 Graphics2D (java.awt.Graphics2D)1 DataBufferInt (java.awt.image.DataBufferInt)1