@Before public void setup() { cropRegion = Rect2i.createFromMinAndMax(10, 20, 30, 30); }
/** * Same as {@code {@link #expandTo2D(Region3i)}} but with a Vector3i instead of a Region3i. * @param size The size used. * @return The 2D representation with the additional space added to it with the additional space added to it in the 3 dimensions. */ public Rect2i expandTo2D(Vector3i size) { return Rect2i.createFromMinAndMax(-getSides(), -getSides(), size.x + getSides() - 1, size.z + getSides() - 1); }
@Override public void onDraw(Canvas canvas) { if (!contents.isEmpty()) { Rect2i region = canvas.getRegion(); Rect2i buttonRegion = Rect2i.createFromMinAndSize(region.minX(), region.minY(), region.width(), tabBarHeight); Rect2i boxRegion = Rect2i.createFromMinAndMax(region.minX(), region.minY() + tabBarHeight, region.width(), region.height()); canvas.drawWidget(contents.get(currentTab), boxRegion); canvas.drawWidget(buttonLayout, buttonRegion); } }
public static LineCoordinates getLineCoordinates(int startX, int startY, int endX, int endY, Rect2i baseRegion, Rect2i cropRegion) { Rect2i region = Rect2i.createFromMinAndMax(Math.min(startX, endX), Math.min(startY, endY), Math.max(startX, endX), Math.max(startY, endY)); Rect2i absoluteRegion = relativeToAbsolute(region, baseRegion); Rect2i finalRegion = cropRegion.intersect(absoluteRegion); if (!finalRegion.isEmpty()) { int sx = startX > endX ? finalRegion.maxX() : finalRegion.minX(); int sy = startY > endY ? finalRegion.maxY() : finalRegion.minY(); int ex = startX > endX ? finalRegion.minX() : finalRegion.maxX(); int ey = startY > endY ? finalRegion.minY() : finalRegion.maxY(); return new LineCoordinates(new Vector2i(sx, sy), new Vector2i(ex, ey)); } else { return null; } }
/** * Returns a 2D representation of the world (top view) with the given borders added to the size of the original region. * @param region The original region to be used. * @return The 2D representation with the additional space added to it. */ public Rect2i expandTo2D(Region3i region) { return Rect2i.createFromMinAndMax(region.minX() - getSides(), region.minZ() - getSides(), region.maxX() + getSides(), region.maxZ() + getSides()); }
@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); } }
private static Rect2i worldToTileArea(Rect2i area) { int chunkMinX = IntMath.divide(area.minX(), TILE_SIZE_X, RoundingMode.FLOOR); int chunkMinZ = IntMath.divide(area.minY(), TILE_SIZE_Y, RoundingMode.FLOOR); int chunkMaxX = IntMath.divide(area.maxX(), TILE_SIZE_X, RoundingMode.CEILING); int chunkMaxZ = IntMath.divide(area.maxY(), TILE_SIZE_Y, RoundingMode.CEILING); return Rect2i.createFromMinAndMax(chunkMinX, chunkMinZ, chunkMaxX, chunkMaxZ); }
@Override public void apply(float v) { consumer.accept(Rect2i.createFromMinAndMax( // rounds towards zero (int) (v * (to.minX() - from.minX()) + from.minX()), (int) (v * (to.minY() - from.minY()) + from.minY()), (int) (v * (to.maxX() - from.maxX()) + from.maxX()), (int) (v * (to.maxY() - from.maxY()) + from.maxY()) )); } }
protected void drawWidget(Canvas canvas, PositionalWidget widget) { if (!widget.isVisible()) { return; } Vector2i screenStart = worldToScreen(widget.getPosition()); Vector2f worldEnd = new Vector2f(widget.getPosition()); worldEnd.add(widget.getSize()); Vector2i screenEnd = worldToScreen(worldEnd); canvas.drawWidget(widget, Rect2i.createFromMinAndMax(screenStart, screenEnd)); }
private Rect2i determineRequiredRegion(Rect2i region) { int newMinX = region.minX() - IntMath.mod(region.minX(), sampleRate); int newMinY = region.minY() - IntMath.mod(region.minY(), sampleRate); int newMaxX = region.maxX() + 4 - IntMath.mod(region.maxX(), sampleRate) - 1; int newMaxY = region.maxY() + 4 - IntMath.mod(region.maxY(), sampleRate) - 1; return Rect2i.createFromMinAndMax(newMinX, newMinY, newMaxX, newMaxY); }
private Rect2i determineRequiredRegion(Rect2i region) { int newMinX = region.minX() - IntMath.mod(region.minX(), sampleRate); int newMinY = region.minY() - IntMath.mod(region.minY(), sampleRate); int newMaxX = region.maxX() + 4 - IntMath.mod(region.maxX(), sampleRate) - 1; int newMaxY = region.maxY() + 4 - IntMath.mod(region.maxY(), sampleRate) - 1; return Rect2i.createFromMinAndMax(newMinX, newMinY, newMaxX, newMaxY); } }
public void onDraw(Canvas canvas) { Rect2i region = canvas.getRegion(); inputPort.updateRect(); canvas.drawWidget(inputPort, Rect2i.createFromMinAndMax( (int) (inputPort.rect.minX() / 10.f * region.width()), (int) (inputPort.rect.minY() / 5.f * region.height()), (int) (inputPort.rect.maxX() / 10.f * region.width()), (int) (inputPort.rect.maxY() / 5.f * region.height()))); for (Port port : ports) { port.updateRect(); canvas.drawWidget(port, Rect2i.createFromMinAndMax( (int) (port.rect.minX() / 10.f * region.width()), (int) (port.rect.minY() / 5.f * region.height()), (int) (port.rect.maxX() / 10.f * region.width()), (int) (port.rect.maxY() / 5.f * region.height()))); } }
@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, Rect2i.createFromMinAndMax(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, Rect2i.createFromMinAndMax(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, Rect2i.createFromMinAndMax(new Vector2i(ceilToInt(pos3.x / 2), ceilToInt(pos3.y / 2)), new Vector2i(ceilToInt((pos3.x + size3.x) / 2), ceilToInt((pos3.y + size3.y) / 2)))); }
@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, Rect2i.createFromMinAndMax(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, Rect2i.createFromMinAndMax(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, Rect2i.createFromMinAndMax(new Vector2i(ceilToInt(pos3.x / 4), ceilToInt(pos3.y / 4)), new Vector2i(ceilToInt((pos3.x + size3.x) / 4), ceilToInt((pos3.y + size3.y) / 4)))); }
@Test public void testZoomInAndDrag() throws Exception { zoomableLayout.onDraw(canvas); //zoom in 2x towards left top corner zoomableLayout.zoom(0.5f, 0.5f, new Vector2i(0, 0)); zoomableLayout.onDraw(canvas); //world size halved assertEquals(zoomableLayout.getWindowSize(), new Vector2f(WORLD_WIDTH, WORLD_HEIGHT / 2)); assertEquals(zoomableLayout.getScreenSize(), new Vector2i(CANVAS_WIDTH, CANVAS_HEIGHT)); assertEquals(zoomableLayout.getPixelSize(), new Vector2f(CANVAS_WIDTH / WORLD_WIDTH, CANVAS_HEIGHT / (WORLD_HEIGHT / 2))); verify(canvas).drawWidget(item1, Rect2i.createFromMinAndMax(new Vector2i(ceilToInt(pos1.x), ceilToInt(pos1.y)), new Vector2i(ceilToInt(pos1.x + size1.x), ceilToInt(pos1.y + size1.y)))); verify(canvas).drawWidget(item2, Rect2i.createFromMinAndMax(new Vector2i(ceilToInt(pos2.x), ceilToInt(pos2.y)), new Vector2i(ceilToInt(pos2.x + size2.x), ceilToInt(pos2.y + size2.y)))); verify(canvas).drawWidget(item3, Rect2i.createFromMinAndMax(new Vector2i(ceilToInt(pos3.x), ceilToInt(pos3.y)), new Vector2i(ceilToInt(pos3.x + size3.x), ceilToInt(pos3.y + size3.y)))); //simulate drag to item2 zoomableLayout.setWindowPosition(pos2); zoomableLayout.onDraw(canvas); //item1 out of canvas verify(canvas).drawWidget(item1, Rect2i.createFromMinAndMax(new Vector2i(ceilToInt(pos1.x - pos2.x), ceilToInt(pos1.y - pos2.y)), new Vector2i(ceilToInt(pos1.x + size1.x - pos2.x), ceilToInt(pos1.y + size1.y - pos2.y)))); verify(canvas).drawWidget(item2, Rect2i.createFromMinAndMax(Vector2i.zero(), new Vector2i(ceilToInt(size2.x), ceilToInt(size2.y)))); verify(canvas).drawWidget(item3, Rect2i.createFromMinAndMax(new Vector2i(ceilToInt(pos3.x - pos2.x), ceilToInt(pos3.y - pos2.y)), new Vector2i(ceilToInt(pos3.x + size3.x - pos2.x), ceilToInt(pos3.y + size3.y - pos2.y)))); }
@Test public void testRelativeToAbsolute() { Rect2i relativeRegion = Rect2i.createFromMinAndMax(5, 10, 20, 15); assertEquals(Line.relativeToAbsolute(relativeRegion, cropRegion), Rect2i.createFromMinAndSize(cropRegion.minX() + relativeRegion.minX(), cropRegion.minY() + relativeRegion.minY(), relativeRegion.width(), relativeRegion.height())); }
@Override public void process(GeneratingRegion region) { Region3i reg = region.getRegion(); Rect2i rc = Rect2i.createFromMinAndMax(reg.minX(), reg.minZ(), reg.maxX(), reg.maxZ()); if (rc.distanceSquared(centerPos.x(), centerPos.y()) <= outerRadius * outerRadius) { SurfaceHeightFacet facet = region.getRegionFacet(SurfaceHeightFacet.class); // update the surface height for (BaseVector2i pos : facet.getWorldRegion().contents()) { float originalValue = facet.getWorld(pos); int distSq = pos.distanceSquared(centerPos); if (distSq <= innerRadius * innerRadius) { facet.setWorld(pos, targetHeight); } else if (distSq <= outerRadius * outerRadius) { double dist = pos.distance(centerPos) - innerRadius; float norm = (float) dist / (outerRadius - innerRadius); facet.setWorld(pos, TeraMath.lerp(targetHeight, originalValue, norm)); } } } } }
SubRegionImpl(Rect2i region, boolean crop) { previousState = state; int left = TeraMath.addClampAtMax(region.minX(), state.drawRegion.minX()); int right = TeraMath.addClampAtMax(region.maxX(), state.drawRegion.minX()); int top = TeraMath.addClampAtMax(region.minY(), state.drawRegion.minY()); int bottom = TeraMath.addClampAtMax(region.maxY(), state.drawRegion.minY()); Rect2i subRegion = Rect2i.createFromMinAndMax(left, top, right, bottom); if (crop) { Rect2i cropRegion = subRegion.intersect(state.cropRegion); if (cropRegion.isEmpty()) { state = new CanvasState(state, subRegion, cropRegion); } else if (!cropRegion.equals(state.cropRegion)) { state = new CanvasState(state, subRegion, cropRegion); renderer.crop(cropRegion); croppingRegion = true; } else { state = new CanvasState(state, subRegion); } } else { state = new CanvasState(state, subRegion); } }
@Override public void process(GeneratingRegion region) { SurfaceHeightFacet surfaceHeight = region.getRegionFacet(SurfaceHeightFacet.class); DensityFacet facet = new DensityFacet(region.getRegion(), region.getBorderForFacet(DensityFacet.class)); Region3i area = region.getRegion(); Rect2i rect = Rect2i.createFromMinAndMax(facet.getRelativeRegion().minX(), facet.getRelativeRegion().minZ(), facet.getRelativeRegion().maxX(), facet.getRelativeRegion().maxZ()); for (BaseVector2i pos : rect.contents()) { float height = surfaceHeight.get(pos); for (int y = facet.getRelativeRegion().minY(); y <= facet.getRelativeRegion().maxY(); ++y) { facet.set(pos.x(), y, pos.y(), height - area.minY() - y); } } region.setRegionFacet(DensityFacet.class, facet); } }
@Test public void testContains() { Rect2i a = Rect2i.createFromMinAndMax(0, 0, 0, 0); assertTrue(a.contains(0, 0)); assertFalse(a.contains(1, 0)); assertFalse(a.contains(0, 1)); assertFalse(a.contains(1, 1)); assertFalse(a.contains(-1, 0)); assertFalse(a.contains(0, -1)); assertFalse(a.contains(-1, 1)); assertFalse(a.contains(1, -1)); assertFalse(a.contains(-1, -1)); }