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))));
}
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))));
}
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;
}
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;
}
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;
}
}
Aggregations