From de0aa28a24fb003c58ba320ec98ad19f7b0dd813 Mon Sep 17 00:00:00 2001 From: kurrycat Date: Mon, 17 Oct 2022 21:34:51 +0200 Subject: [PATCH] changed lb gui input fields to number only and added bounding box updating --- .../mpkmod/gui/components/InputField.java | 50 ++++++++++++++++--- .../gui/screens/LandingBlockGuiScreen.java | 28 ++++++++--- .../kurrycat/mpkmod/util/BoundingBox3D.java | 30 +++++++++++ 3 files changed, 94 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java b/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java index 9d4a8ba6..62bbffa4 100644 --- a/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java +++ b/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java @@ -12,12 +12,11 @@ public class InputField extends Component implements KeyInputListener, MouseInputListener { public boolean numbersOnly; public String content; + public ContentProvider onContentChange = null; public String name = null; - public Color normalColor = new Color(31, 31, 31, 150); public Color cursorColor = new Color(255, 255, 255, 150); public Color highlightColor = new Color(255, 255, 255, 175); - private boolean isFocused = false; private int cursorPos = 0; private int highlightStart = 0; @@ -43,6 +42,11 @@ public InputField setName(String name) { return this; } + public InputField setOnContentChange(ContentProvider onContentChange) { + this.onContentChange = onContentChange; + return this; + } + @Override public void render(Vector2D mouse) { Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name); @@ -99,8 +103,11 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) { String testKey = key; if (testKey.startsWith("NUMPAD")) testKey = testKey.substring(6); - if (testKey.length() == 1) character = testKey.toLowerCase(); + if (testKey.length() == 1) + if (!numbersOnly || "1234567890".contains(testKey)) + character = testKey.toLowerCase(); if (Arrays.asList("COMMA", "PERIOD", "DECIMAL").contains(testKey)) character = "."; + if (testKey.equals("MINUS")) character = "-"; switch (key) { /*case "LSHIFT": @@ -150,13 +157,19 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) { private void deleteSelection() { if (highlightStart == highlightEnd) - content = content.substring(0, Math.max(cursorPos - 1, 0)) + (cursorPos >= content.length() ? "" : content.substring(cursorPos)); + updateContent(content.substring(0, Math.max(cursorPos - 1, 0)) + (cursorPos >= content.length() ? "" : content.substring(cursorPos))); else - content = content.substring(0, highlightStart) + content.substring(highlightEnd); + updateContent(content.substring(0, highlightStart) + content.substring(highlightEnd)); } private void replaceSelectionWithChar(String c) { - content = content.substring(0, highlightStart) + c + content.substring(highlightEnd); + updateContent(content.substring(0, highlightStart) + c + content.substring(highlightEnd)); + } + + private void updateContent(String content) { + this.content = content; + if (onContentChange != null) + onContentChange.apply(new Content(content)); } private double getCursorX() { @@ -223,4 +236,29 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt } return false; } + + @FunctionalInterface + public interface ContentProvider { + void apply(Content content); + } + + public static class Content { + public String content; + + public Content(String content) { + this.content = content; + } + + public String getContent() { + return content; + } + + public Double getNumber() { + try { + return Double.parseDouble(content); + } catch (NumberFormatException e) { + return null; + } + } + } } diff --git a/src/main/java/io/github/kurrycat/mpkmod/gui/screens/LandingBlockGuiScreen.java b/src/main/java/io/github/kurrycat/mpkmod/gui/screens/LandingBlockGuiScreen.java index 7664586d..b7857a03 100644 --- a/src/main/java/io/github/kurrycat/mpkmod/gui/screens/LandingBlockGuiScreen.java +++ b/src/main/java/io/github/kurrycat/mpkmod/gui/screens/LandingBlockGuiScreen.java @@ -38,8 +38,8 @@ public void onGuiInit() { Vector2D windowSize = Renderer2D.getScaledSize(); lbList = new LBList( - new Vector2D(windowSize.getX() / 4D, 20), - new Vector2D(windowSize.getX() / 2D, windowSize.getY() - 40) + new Vector2D(windowSize.getX() / 5D, 20).round(), + new Vector2D(windowSize.getX() / 5D * 3D, windowSize.getY() - 40).round() ); components.add( @@ -133,12 +133,24 @@ public LBListItem(ScrollableList parent, LandingBlock landingBlock) landingBlock.shouldRender = checked; }); shouldRender.setChecked(landingBlock.shouldRender); - minX = new InputField("" + landingBlock.boundingBox.getMin().getX(), Vector2D.OFFSCREEN, 25).setName("minX: "); - minY = new InputField("" + landingBlock.boundingBox.getMin().getY(), Vector2D.OFFSCREEN, 25).setName("minY: "); - minZ = new InputField("" + landingBlock.boundingBox.getMin().getZ(), Vector2D.OFFSCREEN, 25).setName("minZ: "); - maxX = new InputField("" + landingBlock.boundingBox.getMax().getX(), Vector2D.OFFSCREEN, 25).setName("maxX: "); - maxY = new InputField("" + landingBlock.boundingBox.getMax().getY(), Vector2D.OFFSCREEN, 25).setName("maxY: "); - maxZ = new InputField("" + landingBlock.boundingBox.getMax().getZ(), Vector2D.OFFSCREEN, 25).setName("maxZ: "); + minX = new InputField("" + landingBlock.boundingBox.getMin().getX(), Vector2D.OFFSCREEN, 25, true) + .setName("minX: ") + .setOnContentChange(c -> landingBlock.boundingBox.setMinX(c.getNumber())); + minY = new InputField("" + landingBlock.boundingBox.getMin().getY(), Vector2D.OFFSCREEN, 25, true) + .setName("minY: ") + .setOnContentChange(c -> landingBlock.boundingBox.setMinY(c.getNumber()));; + minZ = new InputField("" + landingBlock.boundingBox.getMin().getZ(), Vector2D.OFFSCREEN, 25, true) + .setName("minZ: ") + .setOnContentChange(c -> landingBlock.boundingBox.setMinZ(c.getNumber()));; + maxX = new InputField("" + landingBlock.boundingBox.getMax().getX(), Vector2D.OFFSCREEN, 25, true) + .setName("maxX: ") + .setOnContentChange(c -> landingBlock.boundingBox.setMaxX(c.getNumber()));; + maxY = new InputField("" + landingBlock.boundingBox.getMax().getY(), Vector2D.OFFSCREEN, 25, true) + .setName("maxY: ") + .setOnContentChange(c -> landingBlock.boundingBox.setMaxY(c.getNumber()));; + maxZ = new InputField("" + landingBlock.boundingBox.getMax().getZ(), Vector2D.OFFSCREEN, 25, true) + .setName("maxZ: ") + .setOnContentChange(c -> landingBlock.boundingBox.setMaxZ(c.getNumber()));; fields = new InputField[]{minX, minY, minZ, maxX, maxY, maxZ}; } diff --git a/src/main/java/io/github/kurrycat/mpkmod/util/BoundingBox3D.java b/src/main/java/io/github/kurrycat/mpkmod/util/BoundingBox3D.java index 609588de..51d73af8 100644 --- a/src/main/java/io/github/kurrycat/mpkmod/util/BoundingBox3D.java +++ b/src/main/java/io/github/kurrycat/mpkmod/util/BoundingBox3D.java @@ -60,6 +60,36 @@ public double midZ() { return (minZ() + maxZ()) / 2D; } + public BoundingBox3D setMinX(double minX) { + this.min.setX(minX); + return this; + } + + public BoundingBox3D setMinY(double minY) { + this.min.setY(minY); + return this; + } + + public BoundingBox3D setMinZ(double minZ) { + this.min.setY(minZ); + return this; + } + + public BoundingBox3D setMaxX(double maxX) { + this.max.setX(maxX); + return this; + } + + public BoundingBox3D setMaxY(double maxY) { + this.max.setY(maxY); + return this; + } + + public BoundingBox3D setMaxZ(double maxZ) { + this.max.setZ(maxZ); + return this; + } + public boolean intersectsOrTouchesXZ(BoundingBox3D other) { return other.maxX() >= this.minX() && other.minX() <= this.maxX() &&