From 8c6e3e8c98ab845073b1397ed7059d8507eda825 Mon Sep 17 00:00:00 2001 From: kiip1 <25848425+kiip1@users.noreply.github.com> Date: Sat, 25 Feb 2023 19:27:51 +0100 Subject: [PATCH] More improvements --- .../minescreen/graphics/MapGraphics.java | 4 + .../minescreen/graphics/MapGraphicsImpl.java | 8 +- .../graphics/RelativeMapGraphicsImpl.java | 53 +++---- .../minescreen/map/SubMapImpl.java | 5 +- .../minescreen/widget/Widget.java | 21 ++- .../widget/widgets/ContainerWidget.java | 16 +-- .../minescreen/widget/widgets/GridWidget.java | 73 ++++++++++ .../widget/widgets/ImageWidget.java | 1 - .../widget/widgets/PlayerWidget.java | 3 +- .../widget/widgets/RenderWidget.java | 3 +- .../widget/widgets/ShapeWidget.java | 7 +- .../widget/widgets/SpacerWidget.java | 18 ++- .../widget/widgets/WrapperWidget.java | 17 ++- .../kiipdevelopment/minescreen/demo/Test.java | 2 + .../minescreen/demo/TestGui.java | 131 ++++++++++-------- 15 files changed, 233 insertions(+), 129 deletions(-) create mode 100644 src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/GridWidget.java diff --git a/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphics.java b/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphics.java index f68534f..d2eff43 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphics.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphics.java @@ -2,6 +2,7 @@ import net.minestom.server.map.MapColors; import nl.kiipdevelopment.minescreen.screen.ScreenGui; +import org.jetbrains.annotations.ApiStatus; public interface MapGraphics { static MapGraphics of(ScreenGui gui) { @@ -48,5 +49,8 @@ default void drawDot(MapColors color, int x, int y) { void drawDot(byte color, int x, int y); + @ApiStatus.Internal + void drawDirectDot(byte color, int x, int y); + MapGraphics relative(int x, int y, int width, int height); } diff --git a/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphicsImpl.java b/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphicsImpl.java index 543ccf6..48b5ab9 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphicsImpl.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/graphics/MapGraphicsImpl.java @@ -18,9 +18,8 @@ public MapGraphicsImpl(ScreenGui gui) { @Override public void fill(byte color) { - for (SubMap subView : gui.map().subs()) { + for (SubMap subView : gui.map().subs()) Arrays.fill(subView.colors(), color); - } } @Override @@ -38,8 +37,6 @@ public void drawRectangle(byte color, int startX, int startY, int endX, int endY public void drawString(byte color, String value, int x, int y) { ensureWithinBounds(x, y); - // TODO: Implement - throw new UnsupportedOperationException("MapGraphicsImpl#drawString isn't implemented yet."); } @@ -50,7 +47,8 @@ public void drawDot(byte color, int x, int y) { drawDirectDot(color, x, y); } - protected void drawDirectDot(byte color, int x, int y) { + @Override + public void drawDirectDot(byte color, int x, int y) { Map.SubMapAndIndex subMapAndIndex = map.sub(x, y); subMapAndIndex.map().colors()[subMapAndIndex.index()] = color; diff --git a/src/main/java/nl/kiipdevelopment/minescreen/graphics/RelativeMapGraphicsImpl.java b/src/main/java/nl/kiipdevelopment/minescreen/graphics/RelativeMapGraphicsImpl.java index 9717501..b6c7d3b 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/graphics/RelativeMapGraphicsImpl.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/graphics/RelativeMapGraphicsImpl.java @@ -3,8 +3,11 @@ import nl.kiipdevelopment.minescreen.screen.ScreenGui; final class RelativeMapGraphicsImpl extends MapGraphicsImpl { - private final int x, y; - private final int width, height; + private final int x; + private final int y; + + private final int width; + private final int height; public RelativeMapGraphicsImpl(ScreenGui gui, int x, int y, int width, int height) { super(gui); @@ -21,46 +24,26 @@ public void fill(byte color) { } @Override - public void drawRectangle(byte color, int startX, int startY, int endX, int endY) { - startX = fixX(startX); - startY = fixY(startY); - endX = fixX(endX); - endY = fixY(endY); - - super.drawRectangle(color, startX, startY, endX, endY); - } - - @Override - public void drawString(byte color, String value, int x, int y) { - x = fixX(x); - y = fixY(y); - - super.drawString(color, value, x, y); - } - - @Override - public void drawDot(byte color, int x, int y) { - x = fixX(x); - y = fixY(y); - - super.drawDot(color, x, y); + public void drawDirectDot(byte color, int x, int y) { + super.drawDirectDot(color, absX(x), absY(y)); } @Override public MapGraphics relative(int x, int y, int width, int height) { - x = fixX(x); - y = fixY(y); - width = fixX(width); - height = fixY(height); - - return super.relative(x, y, width, height); + return new RelativeMapGraphicsImpl( + gui, + absX(x) - (absX(x) + width > this.x + this.width ? width : 0), + absY(y) - (absY(y) + height > this.y + this.height ? height : 0), + Math.min(width, this.width), + Math.min(height, this.height) + ); } - private int fixX(int x) { - return x + this.x; + private int absX(int x) { + return this.x + Math.min(x, width); } - private int fixY(int y) { - return y + this.y; + private int absY(int y) { + return this.y + Math.min(y, height); } } diff --git a/src/main/java/nl/kiipdevelopment/minescreen/map/SubMapImpl.java b/src/main/java/nl/kiipdevelopment/minescreen/map/SubMapImpl.java index 240028e..c5a6f7c 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/map/SubMapImpl.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/map/SubMapImpl.java @@ -3,7 +3,6 @@ import net.minestom.server.entity.Player; import net.minestom.server.network.packet.server.SendablePacket; import net.minestom.server.network.packet.server.play.MapDataPacket; -import net.minestom.server.utils.PacketUtils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -74,9 +73,9 @@ public byte[] colors() { @Override public void sendPacket(Collection players) { - final SendablePacket packet = PacketUtils.allocateTrimmedPacket(new MapDataPacket( + final SendablePacket packet = new MapDataPacket( id(), (byte) 1, true, false, List.of(), - new MapDataPacket.ColorContent((byte) width, (byte) height, (byte) 0, (byte) 0, colors))); + new MapDataPacket.ColorContent((byte) width, (byte) height, (byte) 0, (byte) 0, colors)); for (Player player : players) player.sendPacket(packet); } diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/Widget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/Widget.java index b4a9346..81dcbb6 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/widget/Widget.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/Widget.java @@ -5,6 +5,7 @@ import nl.kiipdevelopment.minescreen.graphics.MapGraphics; import nl.kiipdevelopment.minescreen.widget.widgets.ButtonWidget; import nl.kiipdevelopment.minescreen.widget.widgets.ContainerWidget; +import nl.kiipdevelopment.minescreen.widget.widgets.GridWidget; import nl.kiipdevelopment.minescreen.widget.widgets.ImageWidget; import nl.kiipdevelopment.minescreen.widget.widgets.PlayerWidget; import nl.kiipdevelopment.minescreen.widget.widgets.RenderWidget; @@ -65,6 +66,12 @@ public interface Click { final class Containers { private Containers() {} + public static Widget grid(int spacing, int width, int height, @Nullable Widget... widgets) { + return new GridWidget(spacing, width, height, Arrays.stream(widgets) + .filter(Objects::nonNull) + .toList()); + } + public static Widget row(int width, int height, @Nullable Widget... widgets) { return new ContainerWidget(ContainerWidget.Type.ROW, width, height, Arrays.stream(widgets) .filter(Objects::nonNull) @@ -131,12 +138,12 @@ public static Widget render(int width, int height, @Nullable Consumer { for (Widget widget : widgets) { - widget.draw(renderer.relative(0, 0, widget.width(), widget.height())); + widget.draw(renderer); } } } @@ -84,11 +84,8 @@ public void onHover(Player player, int x, int y) { } case STACK -> { for (Widget widget : widgets) { - if (widget instanceof Interactable interactable) { - if (x <= widget.width() && y <= widget.height()) { - interactable.onHover(player, x, y); - } - } + if (widget instanceof Interactable interactable && x <= widget.width() && y <= widget.height()) + interactable.onHover(player, x, y); } } } @@ -129,11 +126,8 @@ public void onClick(Player player, int x, int y) { } case STACK -> { for (Widget widget : widgets) { - if (widget instanceof Interactable interactable) { - if (x <= widget.width() && y <= widget.height()) { - interactable.onClick(player, x, y); - } - } + if (widget instanceof Interactable interactable && x <= widget.width() && y <= widget.height()) + interactable.onClick(player, x, y); } } } diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/GridWidget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/GridWidget.java new file mode 100644 index 0000000..eb0f6a0 --- /dev/null +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/GridWidget.java @@ -0,0 +1,73 @@ +package nl.kiipdevelopment.minescreen.widget.widgets; + +import net.minestom.server.entity.Player; +import nl.kiipdevelopment.minescreen.graphics.MapGraphics; +import nl.kiipdevelopment.minescreen.util.MathUtils; +import nl.kiipdevelopment.minescreen.widget.AbstractWidget; +import nl.kiipdevelopment.minescreen.widget.Interactable; +import nl.kiipdevelopment.minescreen.widget.Widget; +import org.jetbrains.annotations.ApiStatus; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@ApiStatus.Internal +public final class GridWidget extends AbstractWidget implements Interactable { + + private final Map widgets = new HashMap<>(); + + public GridWidget(int spacing, int width, int height, List widgets) { + super(width, height); + + int xOffset = 0; + int yOffset = 0; + int largestY = 0; + for (Widget widget : widgets) { + if (widget.height() > largestY) + largestY = widget.height(); + if (xOffset + widget.width() > width()) { + yOffset += largestY + spacing; + largestY = 0; + xOffset = 0; + } + + this.widgets.put(new Area(xOffset, yOffset, widget.width(), widget.height()), widget); + xOffset += widget.width() + spacing; + } + } + + @Override + public void draw(MapGraphics renderer) { + for (Map.Entry entry : widgets.entrySet()) { + final Area area = entry.getKey(); + entry.getValue().draw(renderer.relative(area.x(), area.y(), area.width(), area.height())); + } + } + + @Override + public void onHover(Player player, int x, int y) { + for (Map.Entry entry : widgets.entrySet()) { + final Area area = entry.getKey(); + if (entry.getValue() instanceof Interactable interactable && area.intersect(x, y)) + interactable.onHover(player, x, y); + } + } + + @Override + public void onClick(Player player, int x, int y) { + for (Map.Entry entry : widgets.entrySet()) { + final Area area = entry.getKey(); + if (entry.getValue() instanceof Interactable interactable && area.intersect(x, y)) + interactable.onClick(player, x, y); + } + } + + private record Area(int x, int y, int width, int height) { + public boolean intersect(int x, int y) { + return MathUtils.isBetween(x, this.x, this.x + width) && + MathUtils.isBetween(y, this.y, this.y + height); + } + } + +} diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ImageWidget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ImageWidget.java index 82fb207..8669ec7 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ImageWidget.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ImageWidget.java @@ -17,7 +17,6 @@ public ImageWidget(int width, int height, BufferedImage image) { width = Math.min(image.getWidth(), width()); height = Math.min(image.getHeight(), height()); - colors = ByteBuffer.allocate(width * height); for (int x = 0; x < width; x++) diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/PlayerWidget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/PlayerWidget.java index fa65e86..8a572b4 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/PlayerWidget.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/PlayerWidget.java @@ -33,7 +33,8 @@ public PlayerWidget(Type type, int width, int height, UUID player) { @Override public void draw(MapGraphics renderer) { - image.thenAccept(widget -> widget.draw(renderer)); + Widget widget = this.image.getNow(null); + if (widget != null) widget.draw(renderer); } public enum Type { diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/RenderWidget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/RenderWidget.java index dc6a5fd..66f127c 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/RenderWidget.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/RenderWidget.java @@ -18,8 +18,7 @@ public RenderWidget(int width, int height, Consumer draw) { @Override public void draw(MapGraphics renderer) { - if (draw != null) { + if (draw != null) draw.accept(renderer); - } } } diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ShapeWidget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ShapeWidget.java index 9cfdf54..8c1ac22 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ShapeWidget.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/ShapeWidget.java @@ -7,15 +7,12 @@ @ApiStatus.Internal public final class ShapeWidget extends AbstractWidget { - private final int xOffset, yOffset; private final Type type; private final MapColors color; - public ShapeWidget(Type type, int width, int height, int xOffset, int yOffset, MapColors color) { + public ShapeWidget(Type type, int width, int height, MapColors color) { super(width, height); - this.xOffset = xOffset; - this.yOffset = yOffset; this.type = type; this.color = color; } @@ -23,7 +20,7 @@ public ShapeWidget(Type type, int width, int height, int xOffset, int yOffset, M @Override public void draw(MapGraphics renderer) { switch (type) { - case SQUARE, RECTANGLE -> renderer.drawRectangle(color, xOffset, yOffset, width(), type == Type.SQUARE ? width() : height()); + case SQUARE, RECTANGLE -> renderer.drawRectangle(color, 0, 0, width(), (type == Type.SQUARE ? width() : height())); } } diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/SpacerWidget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/SpacerWidget.java index 3ed6f00..17cad26 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/SpacerWidget.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/SpacerWidget.java @@ -2,14 +2,26 @@ import nl.kiipdevelopment.minescreen.widget.AbstractWidget; import nl.kiipdevelopment.minescreen.graphics.MapGraphics; +import nl.kiipdevelopment.minescreen.widget.Widget; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; @ApiStatus.Internal public final class SpacerWidget extends AbstractWidget { - public SpacerWidget(int width, int height) { - super(width, height); + private final int x; + private final int y; + private final Widget widget; + + public SpacerWidget(int width, int height, @Nullable Widget widget) { + super(width + (widget == null ? 0 : widget.width()), height + (widget == null ? 0 : widget.height())); + this.x = width; + this.y = height; + this.widget = widget; } @Override - public void draw(MapGraphics renderer) {} + public void draw(MapGraphics renderer) { + if (widget != null) + widget.draw(renderer.relative(x, y, widget.width(), widget.height())); + } } diff --git a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/WrapperWidget.java b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/WrapperWidget.java index 10ff9ec..e91ec88 100644 --- a/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/WrapperWidget.java +++ b/src/main/java/nl/kiipdevelopment/minescreen/widget/widgets/WrapperWidget.java @@ -1,13 +1,15 @@ package nl.kiipdevelopment.minescreen.widget.widgets; +import net.minestom.server.entity.Player; import nl.kiipdevelopment.minescreen.graphics.MapGraphics; +import nl.kiipdevelopment.minescreen.widget.Interactable; import nl.kiipdevelopment.minescreen.widget.Widget; import org.jetbrains.annotations.ApiStatus; import java.util.function.Supplier; @ApiStatus.Internal -public final class WrapperWidget implements Widget { +public final class WrapperWidget implements Widget, Interactable { private Supplier supplier; private Widget widget; @@ -36,4 +38,17 @@ public int width() { public int height() { return widget == null ? 0 : widget.height(); } + + @Override + public void onHover(Player player, int x, int y) { + if (widget instanceof Interactable interactable) + interactable.onHover(player, x, y); + } + + @Override + public void onClick(Player player, int x, int y) { + if (widget instanceof Interactable interactable) + interactable.onClick(player, x, y); + } + } diff --git a/src/test/java/nl/kiipdevelopment/minescreen/demo/Test.java b/src/test/java/nl/kiipdevelopment/minescreen/demo/Test.java index e194788..d208a13 100644 --- a/src/test/java/nl/kiipdevelopment/minescreen/demo/Test.java +++ b/src/test/java/nl/kiipdevelopment/minescreen/demo/Test.java @@ -1,6 +1,7 @@ package nl.kiipdevelopment.minescreen.demo; import net.minestom.server.MinecraftServer; +import net.minestom.server.attribute.Attribute; import net.minestom.server.coordinate.Pos; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; @@ -24,6 +25,7 @@ public static void main(String[] args) { Player player = event.getPlayer(); player.setPermissionLevel(4); player.setGameMode(GameMode.ADVENTURE); + player.getAttribute(Attribute.ATTACK_SPEED).setBaseValue(100f); gui.show(player); }); MojangAuth.init(); diff --git a/src/test/java/nl/kiipdevelopment/minescreen/demo/TestGui.java b/src/test/java/nl/kiipdevelopment/minescreen/demo/TestGui.java index 6fe231f..26a4382 100644 --- a/src/test/java/nl/kiipdevelopment/minescreen/demo/TestGui.java +++ b/src/test/java/nl/kiipdevelopment/minescreen/demo/TestGui.java @@ -1,20 +1,17 @@ package nl.kiipdevelopment.minescreen.demo; -import net.kyori.adventure.text.Component; import net.minestom.server.map.MapColors; import nl.kiipdevelopment.minescreen.screen.InteractableScreenGui; import nl.kiipdevelopment.minescreen.screen.ScreenGui; import nl.kiipdevelopment.minescreen.widget.Widget; +import org.jetbrains.skija.Bitmap; import org.jetbrains.skija.Canvas; +import org.jetbrains.skija.Image; import org.jetbrains.skija.Paint; import org.jetbrains.skija.PaintMode; import org.jetbrains.skija.Point; import org.jetbrains.skija.Surface; -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.IOException; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; @@ -24,11 +21,19 @@ final class TestGui { private TestGui() {} public static ScreenGui init() { - InteractableScreenGui gui = new InteractableScreenGui((short) 1, 512, 512, 20); + final InteractableScreenGui gui = new InteractableScreenGui((short) 1, 512, 512, 20); // Set background color gui.updateBackground(MapColors.COLOR_BLACK); + // Triangle help + final Random random = ThreadLocalRandom.current(); + final Point[] triangle = new Point[] { + new Point(random.nextInt(128), random.nextInt(128)), + new Point(random.nextInt(128), random.nextInt(128)), + new Point(random.nextInt(128), random.nextInt(128)) + }; + // Create container Widget containerWidget = Containers.column( 448, @@ -36,44 +41,40 @@ public static ScreenGui init() { Containers.row( 448, 214, - Buttons.click( - Containers.stack( - 128, - 128, - Shapes.square(128, 128, 0, 0, MapColors.COLOR_GREEN), - Shapes.square(127, 127, 1, 1, MapColors.COLOR_LIGHT_GREEN) - ), - (player1, x, y) -> { - player1.sendMessage(Component.text("GREEN")); - gui.updateBackground(MapColors.COLOR_GREEN); - } + Containers.grid( + 10, + 128, + 128, + colorButton(gui, MapColors.COLOR_RED), + colorButton(gui, MapColors.COLOR_ORANGE), + colorButton(gui, MapColors.COLOR_YELLOW), + colorButton(gui, MapColors.COLOR_GREEN), + colorButton(gui, MapColors.COLOR_BLUE), + colorButton(gui, MapColors.COLOR_PINK), + colorButton(gui, MapColors.SNOW), + colorButton(gui, MapColors.COLOR_BLACK) ), Spacers.spacer(10, 10), - Buttons.click( - Containers.stack( - 128, - 128, - Shapes.square(128, 128, 0, 0, MapColors.COLOR_BLUE), - Shapes.square(127, 127, 1, 1, MapColors.COLOR_LIGHT_BLUE) - ), - (player1, x, y) -> { - player1.sendMessage(Component.text("BLUE")); - gui.updateBackground(MapColors.COLOR_BLUE); + Wrappers.wrapper(() -> { + Widget[] widget = new Widget[10]; + for (int i = 0; i < 10; i++) { + widget[i] = Spacers.offset( + random.nextInt(64), + random.nextInt(64), + Shapes.rectangle( + random.nextInt(64), + random.nextInt(64), + MapColors.values()[random.nextInt(MapColors.values().length)] + ) + ); } - ), - Spacers.spacer(10, 10), - Buttons.click( - Containers.stack( - 128, - 128, - Shapes.square(128, 128, 0, 0, MapColors.COLOR_BLACK), - Shapes.square(127, 127, 1, 1, MapColors.COLOR_GRAY) - ), - (player1, x, y) -> { - player1.sendMessage(Component.text("BLACK")); - gui.updateBackground(MapColors.COLOR_BLACK); - } - ) + + return Containers.stack( + 128, + 128, + widget + ); + }) ), Containers.row( 448, @@ -92,29 +93,33 @@ public static ScreenGui init() { .setColor(0xFF1D7AA2) .setMode(PaintMode.STROKE) .setStrokeWidth(1f); - Random random = ThreadLocalRandom.current(); + for (int i = 0; i < triangle.length; i++) { + Point point = triangle[i]; + point = point.offset(random.nextInt(-1, 2), random.nextInt(-1, 2)); + if (point.getX() < 0) point = new Point(0, point.getY()); + if (point.getX() > 128) point = new Point(128, point.getY()); + if (point.getY() < 0) point = new Point(point.getX(), 0); + if (point.getY() > 128) point = new Point(point.getX(), 128); + + triangle[i] = point; + } canvas.drawTriangles( - new Point[] { - new Point(random.nextInt(128), random.nextInt(128)), - new Point(random.nextInt(128), random.nextInt(128)), - new Point(random.nextInt(128), random.nextInt(128)) - }, + triangle, new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF }, paint ); // Render - // TODO This can be better - try { - BufferedImage buffer = ImageIO.read(new ByteArrayInputStream(surface.makeImageSnapshot().encodeToData().getBytes())); - for (int x = 0; x < 128; x++) { - for (int y = 0; y < 128; y++) { - MapColors.PreciseMapColor color = MapColors.closestColor(buffer.getRGB(x, y)); - graphics.drawDot(color.getBaseColor(), x, y); - } + surface.flush(); + Image image = surface.makeImageSnapshot(); + Bitmap bitmap = new Bitmap(); + bitmap.allocN32Pixels(128, 128); + image.readPixels(bitmap); + + for (int x = 0; x < 128; x++) { + for (int y = 0; y < 128; y++) { + graphics.drawDirectDot(MapColors.closestColor(bitmap.getColor(x, y)).getIndex(), x, y); } - } catch (IOException e) { - throw new RuntimeException(e); } }) ) @@ -150,4 +155,16 @@ public static ScreenGui init() { return gui; } + + private static Widget colorButton(ScreenGui gui, MapColors color) { + return Buttons.click( + Containers.stack( + 36, + 36, + Shapes.square(36, MapColors.STONE), + Spacers.offset(1, 1, Shapes.square(34, color)) + ), + (player, x, y) -> gui.updateBackground(color) + ); + } }