Search in sources :

Example 1 with Rect2i

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

the class StringTextIconRenderer method draw.

@Override
public void draw(T value, Canvas canvas) {
    // Drawing the icon
    TextureRegion texture = getTexture(value);
    if (texture != null) {
        if (marginTop + texture.getHeight() + marginBottom > canvas.size().y) {
            // Icon does not fit within the canvas - vertically shrinking it
            int iconHeight = canvas.size().y - marginTop - marginBottom;
            canvas.drawTexture(texture, Rect2i.createFromMinAndSize(marginLeft, marginTop, texture.getWidth(), iconHeight));
        } else {
            // Icon fits within the canvas - vertically centering it
            int iconVerticalPosition = (canvas.size().y - texture.getHeight()) / 2;
            canvas.drawTexture(texture, Rect2i.createFromMinAndSize(marginLeft, iconVerticalPosition, texture.getWidth(), texture.getHeight()));
        }
    }
    // Drawing the text, adjusting for icon width
    String text = getString(value);
    int iconWidth;
    if (texture != null) {
        iconWidth = marginLeft + texture.getWidth() + marginRight;
    } else {
        iconWidth = 0;
    }
    Rect2i textRegion = Rect2i.createFromMinAndSize(iconWidth, 0, canvas.getRegion().width() - iconWidth, canvas.getRegion().height());
    canvas.drawText(text, textRegion);
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) TextureRegion(org.terasology.rendering.assets.texture.TextureRegion)

Example 2 with Rect2i

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

the class FacetLayerPreview method render.

@Override
public ByteBuffer render(TextureData texData, int scale, ProgressListener progressListener) throws InterruptedException {
    int width = texData.getWidth();
    int height = texData.getWidth();
    final int offX = -width * scale / 2;
    final int offY = -height * scale / 2;
    // trigger building the World now
    worldGenerator.getWorld();
    Rect2i worldArea = Rect2i.createFromMinAndSize(offX, offY, width * scale, height * scale);
    Rect2i tileArea = worldToTileArea(worldArea);
    AtomicInteger tilesComplete = new AtomicInteger(0);
    int tileCount = tileArea.area();
    int[] masks = colorModel.getMasks();
    DataBufferInt imageBuffer = new DataBufferInt(width * height);
    WritableRaster raster = Raster.createPackedRaster(imageBuffer, width, height, width, masks, null);
    BufferedImage view = new BufferedImage(colorModel, raster, false, null);
    Graphics2D g = view.createGraphics();
    g.scale(1f / scale, 1f / scale);
    g.translate(-offX, -offY);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    Map<ImmutableVector2i, Future<BufferedImage>> imageFutures = new HashMap<>(tileCount);
    for (int z = tileArea.minY(); z < tileArea.maxY(); z++) {
        for (int x = tileArea.minX(); x < tileArea.maxX(); x++) {
            ImmutableVector2i pos = new ImmutableVector2i(x, z);
            imageFutures.put(pos, threadPool.submit(() -> {
                Region createRegion = createRegion(pos);
                BufferedImage image = rasterize(createRegion);
                if (progressListener != null) {
                    progressListener.onProgress(tilesComplete.incrementAndGet() / (float) tileCount);
                }
                return image;
            }));
        }
    }
    for (int z = tileArea.minY(); z < tileArea.maxY(); z++) {
        for (int x = tileArea.minX(); x < tileArea.maxX(); x++) {
            ImmutableVector2i pos = new ImmutableVector2i(x, z);
            try {
                BufferedImage tileImage = imageFutures.get(pos).get();
                g.drawImage(tileImage, x * TILE_SIZE_X, z * TILE_SIZE_Y, null);
            } catch (ExecutionException e) {
                logger.warn("Could not rasterize tile {}", pos, e);
            }
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException();
            }
        }
    }
    // draw coordinate lines through 0 / 0
    g.setColor(Color.GRAY);
    g.drawLine(worldArea.minX(), 0, worldArea.maxX(), 0);
    g.drawLine(0, worldArea.minY(), 0, worldArea.maxY());
    g.dispose();
    int[] data = imageBuffer.getData();
    ByteBuffer byteBuffer = texData.getBuffers()[0];
    byteBuffer.asIntBuffer().put(data);
    return byteBuffer;
}
Also used : HashMap(java.util.HashMap) DataBufferInt(java.awt.image.DataBufferInt) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Rect2i(org.terasology.math.geom.Rect2i) ImmutableVector2i(org.terasology.math.geom.ImmutableVector2i) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WritableRaster(java.awt.image.WritableRaster) Future(java.util.concurrent.Future) Region(org.terasology.world.generation.Region) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with Rect2i

use of org.terasology.math.geom.Rect2i 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 4 with Rect2i

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

the class RowLayout method onDraw.

/**
 * Draws the widgets contained in this layout's widget list,
 * according to the widths calculated in {@link #calcWidths(Canvas)}.
 * This is called every frame.
 *
 * @param canvas The {@link Canvas} on which this {@code RowLayout} is drawn
 */
@Override
public void onDraw(Canvas canvas) {
    TIntList widths = calcWidths(canvas);
    if (!contents.isEmpty()) {
        int xOffset = 0;
        for (int i = 0; i < contents.size(); ++i) {
            int itemWidth = widths.get(i);
            Rect2i region = Rect2i.createFromMinAndSize(xOffset, 0, itemWidth, canvas.size().y);
            canvas.drawWidget(contents.get(i), region);
            xOffset += itemWidth;
            xOffset += horizontalSpacing;
        }
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) TIntList(gnu.trove.list.TIntList)

Example 5 with Rect2i

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

the class ScrollableArea method layoutWithJustHorizontal.

private void layoutWithJustHorizontal(Canvas canvas, Vector2i contentSize, int availableWidth, int fullHeight, int scrollbarHeight) {
    int availableHeight = fullHeight - scrollbarHeight;
    Rect2i contentRegion = Rect2i.createFromMinAndSize(0, 0, availableWidth, availableHeight);
    horizontalBar.setRange(contentSize.x - contentRegion.width());
    canvas.addInteractionRegion(scrollListener);
    canvas.drawWidget(horizontalBar, Rect2i.createFromMinAndSize(0, availableHeight, availableWidth, scrollbarHeight));
    try (SubRegion ignored = canvas.subRegion(contentRegion, true)) {
        canvas.drawWidget(content, Rect2i.createFromMinAndSize(-horizontalBar.getValue(), 0, contentSize.x, availableHeight));
    }
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) SubRegion(org.terasology.rendering.nui.SubRegion) LayoutHint(org.terasology.rendering.nui.LayoutHint)

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