Search in sources :

Example 6 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class ZoomableLayoutTest method testZoomOut.

@Test
public void testZoomOut() throws Exception {
    zoomableLayout.onDraw(canvas);
    // zoom out 2x from top left corner
    zoomableLayout.zoom(2, 2, new Vector2i(0, 0));
    zoomableLayout.onDraw(canvas);
    // world size doubled
    assertEquals(zoomableLayout.getWindowSize(), new Vector2f(WORLD_WIDTH * 2 * 2, WORLD_HEIGHT * 2));
    assertEquals(zoomableLayout.getScreenSize(), new Vector2i(CANVAS_WIDTH, CANVAS_HEIGHT));
    assertEquals(zoomableLayout.getPixelSize(), new Vector2f(CANVAS_WIDTH / (WORLD_WIDTH * 2 * 2), CANVAS_HEIGHT / (WORLD_HEIGHT * 2)));
    verify(canvas).drawWidget(item1, new Rectanglei(new Vector2i(ceilToInt(pos1.x / 4), ceilToInt(pos1.y / 4)), new Vector2i(ceilToInt((pos1.x + size1.x) / 4), ceilToInt((pos1.y + size1.y) / 4))));
    verify(canvas).drawWidget(item2, new Rectanglei(new Vector2i(ceilToInt(pos2.x / 4), ceilToInt(pos2.y / 4)), new Vector2i(ceilToInt((pos2.x + size2.x) / 4), ceilToInt((pos2.y + size2.y) / 4))));
    verify(canvas).drawWidget(item3, new Rectanglei(new Vector2i(ceilToInt(pos3.x / 4), ceilToInt(pos3.y / 4)), new Vector2i(ceilToInt((pos3.x + size3.x) / 4), ceilToInt((pos3.y + size3.y) / 4))));
}
Also used : Vector2f(org.joml.Vector2f) Vector2i(org.joml.Vector2i) Rectanglei(org.terasology.joml.geom.Rectanglei) Test(org.junit.jupiter.api.Test)

Example 7 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class ZoomableLayoutTest method testScaling.

@Test
public void testScaling() throws Exception {
    zoomableLayout.onDraw(canvas);
    // world size scaled to fit ratio of screen size - world size now 200 x 100
    assertEquals(zoomableLayout.getWindowSize(), new Vector2f(WORLD_WIDTH * 2, WORLD_HEIGHT));
    assertEquals(zoomableLayout.getScreenSize(), new Vector2i(CANVAS_WIDTH, CANVAS_HEIGHT));
    assertEquals(zoomableLayout.getPixelSize(), new Vector2f(CANVAS_WIDTH / (WORLD_WIDTH * 2), CANVAS_HEIGHT / WORLD_HEIGHT));
    // coordinates on widgets scaled down by half
    verify(canvas).drawWidget(item1, new Rectanglei(new Vector2i(ceilToInt(pos1.x / 2), ceilToInt(pos1.y / 2)), new Vector2i(ceilToInt((pos1.x + size1.x) / 2), ceilToInt((pos1.y + size1.y) / 2))));
    verify(canvas).drawWidget(item2, new Rectanglei(new Vector2i(ceilToInt(pos2.x / 2), ceilToInt(pos2.y / 2)), new Vector2i(ceilToInt((pos2.x + size2.x) / 2), ceilToInt((pos2.y + size2.y) / 2))));
    verify(canvas).drawWidget(item3, new Rectanglei(new Vector2i(ceilToInt(pos3.x / 2), ceilToInt(pos3.y / 2)), new Vector2i(ceilToInt((pos3.x + size3.x) / 2), ceilToInt((pos3.y + size3.y) / 2))));
}
Also used : Vector2f(org.joml.Vector2f) Vector2i(org.joml.Vector2i) Rectanglei(org.terasology.joml.geom.Rectanglei) Test(org.junit.jupiter.api.Test)

Example 8 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class IconMeshFactory method generateIconMeshData.

public static MeshData generateIconMeshData(TextureRegion tex, int alphaLimit, boolean withContour, Vector4f colorContour) {
    ByteBuffer buffer = tex.getTexture().getData().getBuffers()[0];
    Rectanglei pixelRegion = tex.getPixelRegion();
    int posX = pixelRegion.minX;
    int posY = pixelRegion.minY;
    int stride = tex.getTexture().getWidth() * 4;
    float textureSize = Math.max(tex.getWidth(), tex.getHeight());
    StandardMeshData mesh = new StandardMeshData();
    Vector2f pos = new Vector2f();
    Color color = new Color();
    for (int y = 0; y < tex.getHeight(); y++) {
        for (int x = 0; x < tex.getWidth(); x++) {
            int r = buffer.get((posY + y) * stride + (posX + x) * 4) & 255;
            int g = buffer.get((posY + y) * stride + (posX + x) * 4 + 1) & 255;
            int b = buffer.get((posY + y) * stride + (posX + x) * 4 + 2) & 255;
            int a = buffer.get((posY + y) * stride + (posX + x) * 4 + 3) & 255;
            if (a > alphaLimit) {
                color.setRed(r).setGreen(g).setBlue(b).setAlpha(a);
                pos.set(2f / textureSize * x - 1f, 2f / textureSize * (tex.getHeight() - y - 1) - 1f);
                addPixel(mesh, pos, 2f / textureSize, color);
                if (withContour) {
                    int newX = 0;
                    int newY = 0;
                    int newA = 0;
                    for (int i = 0; i < 4; i++) {
                        newA = alphaLimit + 1;
                        switch(i) {
                            case 0:
                                // check left
                                if (x > 0) {
                                    newX = x - 1;
                                    newY = y;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 1:
                                // check top
                                if (y > 0) {
                                    newX = x;
                                    newY = y - 1;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 2:
                                // check right
                                if (x < 16) {
                                    newX = x + 1;
                                    newY = y;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            case 3:
                                // check bottom
                                if (y < 16) {
                                    newX = x;
                                    newY = y + 1;
                                    newA = buffer.get((posY + newY) * stride + (posX + newX) * 4 + 3) & 255;
                                }
                                break;
                            default:
                                break;
                        }
                        if (newA < alphaLimit) {
                            color.setRed(colorContour.x).setGreen(colorContour.y).setBlue(colorContour.z).setAlpha(colorContour.w);
                            addPixel(mesh, pos.set(2f * 0.0625f * newX - 0.5f, 0.125f * (15 - newY) - 1f), 0.125f, color);
                        }
                    }
                }
            }
        }
    }
    return mesh;
}
Also used : StandardMeshData(org.terasology.engine.rendering.assets.mesh.StandardMeshData) Vector2f(org.joml.Vector2f) Color(org.terasology.nui.Color) Rectanglei(org.terasology.joml.geom.Rectanglei) ByteBuffer(java.nio.ByteBuffer)

Example 9 with Rectanglei

use of org.terasology.joml.geom.Rectanglei 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 Rectanglei 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 : Rectanglei(org.terasology.joml.geom.Rectanglei) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage)

Example 10 with Rectanglei

use of org.terasology.joml.geom.Rectanglei in project Terasology by MovingBlocks.

the class FlowParagraphRenderable method renderContents.

@Override
public void renderContents(Canvas canvas, Vector2i startPos, ContainerRenderSpace containerRenderSpace, int leftIndent, int rightIndent, ParagraphRenderStyle defaultStyle, HorizontalAlign horizontalAlign, HyperlinkRegister hyperlinkRegister) {
    int y = startPos.y;
    for (LaidFlowLine<FlowRenderable> line : updateCacheIfNeeded(defaultStyle, startPos.y, containerRenderSpace)) {
        int x = 0;
        int insetXAdvance = containerRenderSpace.getAdvanceForVerticalPosition(y);
        int availableWidth = containerRenderSpace.getWidthForVerticalPosition(y);
        if (horizontalAlign == HorizontalAlign.LEFT || horizontalAlign == HorizontalAlign.CENTER) {
            availableWidth -= leftIndent;
        }
        if (horizontalAlign == HorizontalAlign.RIGHT || horizontalAlign == HorizontalAlign.CENTER) {
            availableWidth -= rightIndent;
        }
        int lineHeight = line.getHeight();
        int lineWidth = line.getWidth();
        int alignOffset = horizontalAlign.getOffset(lineWidth, availableWidth);
        for (FlowRenderable flowRenderable : line.getFlowRenderables()) {
            int elementWidth = flowRenderable.getWidth(defaultStyle);
            Rectanglei renderableRegion = RectUtility.createFromMinAndSize(insetXAdvance + leftIndent + alignOffset + startPos.x + x, y, elementWidth, lineHeight);
            String hyperlink = flowRenderable.getAction();
            if (hyperlink != null) {
                hyperlinkRegister.registerHyperlink(renderableRegion, hyperlink);
            }
            flowRenderable.render(canvas, renderableRegion, defaultStyle);
            x += elementWidth;
        }
        y += lineHeight;
    }
}
Also used : FlowRenderable(org.terasology.engine.rendering.nui.widgets.browser.data.basic.flow.FlowRenderable) Rectanglei(org.terasology.joml.geom.Rectanglei)

Aggregations

Rectanglei (org.terasology.joml.geom.Rectanglei)24 Vector2i (org.joml.Vector2i)9 Vector2f (org.joml.Vector2f)6 ByteBuffer (java.nio.ByteBuffer)3 Test (org.junit.jupiter.api.Test)3 FontMeshBuilder (org.terasology.engine.rendering.assets.font.FontMeshBuilder)3 Mesh (org.terasology.engine.rendering.assets.mesh.Mesh)3 MeshBuilder (org.terasology.engine.rendering.assets.mesh.MeshBuilder)3 TextureRegion (org.terasology.engine.rendering.assets.texture.TextureRegion)3 Rectanglef (org.terasology.joml.geom.Rectanglef)3 UITextureRegion (org.terasology.nui.UITextureRegion)3 BufferedImage (java.awt.image.BufferedImage)2 Matrix4f (org.joml.Matrix4f)2 Vector2ic (org.joml.Vector2ic)2 Color (org.terasology.nui.Color)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