From db22e6fa02957d89993c87ab06e93f1ce5ba2430 Mon Sep 17 00:00:00 2001 From: kurrycat Date: Mon, 17 Oct 2022 15:52:38 +0200 Subject: [PATCH] added input fields to lb gui --- .../mpkmod/gui/components/InputField.java | 58 +++++++++++----- .../mpkmod/gui/components/ScrollableList.java | 46 +++++++------ .../gui/components/ScrollableListItem.java | 6 +- .../gui/screens/LandingBlockGuiScreen.java | 69 +++++++++---------- .../kurrycat/mpkmod/util/ArrayListUtil.java | 31 +++++++-- 5 files changed, 127 insertions(+), 83 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 0175bb9f..9d4a8ba6 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,10 +12,11 @@ public class InputField extends Component implements KeyInputListener, MouseInputListener { public boolean numbersOnly; public String content; + 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, 150); + public Color highlightColor = new Color(255, 255, 255, 175); private boolean isFocused = false; private int cursorPos = 0; @@ -37,36 +38,54 @@ public InputField(String content, Vector2D pos, double width, boolean numbersOnl this.numbersOnly = numbersOnly; } + public InputField setName(String name) { + this.name = name; + return this; + } + @Override public void render(Vector2D mouse) { - Renderer2D.drawRectWithEdge(getDisplayPos(), getSize(), 1, normalColor, normalColor); + Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name); + Vector2D rectPos = getDisplayPos().add(nameSize.getX(), 0); + Vector2D rectSize = getSize().sub(nameSize.getX(), 0); + + if (name != null) { + FontRenderer.drawCenteredString( + name, + getDisplayPos().add(nameSize.getX() / 2D, getSize().getY() / 2D + 1), + Color.WHITE, + false + ); + } + + Renderer2D.drawRectWithEdge(rectPos.round(), rectSize.round(), 1, normalColor, normalColor); FontRenderer.drawString( content.substring(0, highlightStart), - getDisplayPos().add(2, 2), + rectPos.add(2, 2), Color.WHITE, false ); if (highlightStart != highlightEnd) Renderer2D.drawRect( - getDisplayPos().add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2), - new Vector2D(FontRenderer.getStringSize(content.substring(highlightStart, highlightEnd)).getX(), getSize().getY() - 4), + rectPos.add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2), + new Vector2D(FontRenderer.getStringSize(content.substring(highlightStart, highlightEnd)).getX(), rectSize.getY() - 4), highlightColor ); FontRenderer.drawString( content.substring(highlightStart, highlightEnd), - getDisplayPos().add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2), + rectPos.add(2 + FontRenderer.getStringSize(content.substring(0, highlightStart)).getX(), 2), Color.BLACK, false ); FontRenderer.drawString( content.substring(highlightEnd), - getDisplayPos().add(2 + FontRenderer.getStringSize(content.substring(0, highlightEnd)).getX(), 2), + rectPos.add(2 + FontRenderer.getStringSize(content.substring(0, highlightEnd)).getX(), 2), Color.WHITE, false ); if (isFocused && highlightStart == highlightEnd) Renderer2D.drawRect( - new Vector2D(getDisplayPos().getX() + getCursorX(), getDisplayPos().getY() + 1), - new Vector2D(1, getSize().getY() - 2), + new Vector2D(rectPos.getX() + getCursorX(), rectPos.getY() + 1), + new Vector2D(1, rectSize.getY() - 2), cursorColor ); } @@ -78,10 +97,10 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) { if (pressed) { String character = null; String testKey = key; - if(testKey.startsWith("NUMPAD")) testKey = testKey.substring(6); + if (testKey.startsWith("NUMPAD")) testKey = testKey.substring(6); - if(testKey.length() == 1) character = testKey.toLowerCase(); - if(Arrays.asList("COMMA", "PERIOD", "DECIMAL").contains(testKey)) character = "."; + if (testKey.length() == 1) character = testKey.toLowerCase(); + if (Arrays.asList("COMMA", "PERIOD", "DECIMAL").contains(testKey)) character = "."; switch (key) { /*case "LSHIFT": @@ -94,10 +113,10 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) { else cursorPos = highlightStart; break; case "DELETE": - if(highlightStart == highlightEnd) + if (highlightStart == highlightEnd) cursorPos++; deleteSelection(); - if(highlightStart == highlightEnd) + if (highlightStart == highlightEnd) cursorPos--; break; case "LEFT": @@ -119,7 +138,7 @@ public boolean handleKeyInput(int keyCode, String key, boolean pressed) { }*/ break; } - if(Arrays.asList("BACK", "DELETE", "LEFT", "RIGHT").contains(key) || character != null) { + if (Arrays.asList("BACK", "DELETE", "LEFT", "RIGHT").contains(key) || character != null) { cursorPos = MathUtil.constrain(cursorPos, 0, content.length()); highlightStart = cursorPos; highlightEnd = cursorPos; @@ -145,7 +164,10 @@ private double getCursorX() { } private int getCursorPosFromMousePos(Vector2D mouse) { - double x = mouse.getX() - getDisplayPos().getX() - 2; + Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name); + Vector2D rectPos = getDisplayPos().add(nameSize.getX(), 0); + + double x = mouse.getX() - rectPos.getX() - 2; if (x < 0) return 0; else if (x > FontRenderer.getStringSize(content).getX()) @@ -163,6 +185,10 @@ else if (x < charWidth) return content.length(); } + public void setWidth(double width) { + this.size.setX(width); + } + @Override public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) { if (button == Mouse.Button.LEFT) { diff --git a/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableList.java b/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableList.java index fa9ab6f3..d30effaa 100644 --- a/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableList.java +++ b/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableList.java @@ -6,7 +6,7 @@ import java.awt.*; import java.util.ArrayList; -public class ScrollableList> extends Component implements MouseInputListener, MouseScrollListener { +public class ScrollableList> extends Component implements MouseInputListener, MouseScrollListener, KeyInputListener { public Color backgroundColor = Color.DARK_GRAY; public ScrollBar scrollBar; public ArrayList items = new ArrayList<>(); @@ -22,6 +22,10 @@ public void addItem(I item) { } public ArrayList getItems() { + ArrayList items = new ArrayList<>(); + for (int i = 0; i < getItemCount(); i++) { + items.add(getItem(i)); + } return items; } @@ -30,7 +34,7 @@ public int getItemCount() { } public I getItem(int index) { - return getItems().get(index); + return items.get(index); } public void render(Vector2D mouse) { @@ -75,7 +79,6 @@ public Pair getItemAndRelMousePosUnderMouse(Vector2D mouse) { double currY = mouse.getY() - 1 - getDisplayPos().getY() + scrollBar.scrollAmount; for (int i = 0; i < getItemCount(); i++) { I item = getItem(i); - if (item == null) item = getItems().get(i); if (currY >= 0 && currY <= item.getHeight()) { return new Pair<>(item, new Vector2D(mouse.getX() - getDisplayPos().getX() - 1, currY)); } @@ -97,40 +100,39 @@ private boolean shouldRenderScrollbar() { } public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) { - if (shouldRenderScrollbar()) - scrollBar.handleMouseInput(state, mousePos, button); + if (shouldRenderScrollbar() && scrollBar.handleMouseInput(state, mousePos, button)) + return true; - Pair p = getItemAndRelMousePosUnderMouse(mousePos); - if (p != null) { - I item = p.first; - Vector2D relMousePos = p.second; - return item.handleMouseInput(state, mousePos, button); - } - - return contains(mousePos); + return ArrayListUtil.orMapAll( + getItems(), + e -> e.handleMouseInput(state, mousePos, button) + ) || contains(mousePos); } public boolean handleMouseScroll(Vector2D mousePos, int delta) { - Pair p = getItemAndRelMousePosUnderMouse(mousePos); - if (p != null) { - I item = p.first; - Vector2D relMousePos = p.second; - return item.handleMouseScroll(mousePos, delta); - } + if(ArrayListUtil.orMapAll( + getItems(), + e -> e.handleMouseScroll(mousePos, delta) + )) return true; if (shouldRenderScrollbar()) scrollBar.scrollBy(-delta); return contains(mousePos); } + public boolean handleKeyInput(int keyCode, String key, boolean pressed) { + return ArrayListUtil.orMapAll( + getItems(), + e -> e.handleKeyInput(keyCode, key, pressed) + ); + } + public int totalHeight() { if (getItemCount() == 0) return 0; int sum = 3; - ArrayList items = getItems(); for (int i = 0; i < getItemCount(); i++) { - if (getItem(i) != null) sum += getItem(i).getHeight() + 1; - else sum += items.get(i).getHeight() + 1; + sum += getItem(i).getHeight() + 1; } return sum; } diff --git a/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableListItem.java b/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableListItem.java index 95a15184..af82dbe4 100644 --- a/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableListItem.java +++ b/src/main/java/io/github/kurrycat/mpkmod/gui/components/ScrollableListItem.java @@ -3,7 +3,7 @@ import io.github.kurrycat.mpkmod.util.Mouse; import io.github.kurrycat.mpkmod.util.Vector2D; -public abstract class ScrollableListItem> implements MouseInputListener, MouseScrollListener { +public abstract class ScrollableListItem> implements MouseInputListener, KeyInputListener, MouseScrollListener { protected int height; private ScrollableList parent; @@ -26,4 +26,8 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt public boolean handleMouseScroll(Vector2D mousePos, int delta) { return false; } + + public boolean handleKeyInput(int keyCode, String key, boolean pressed) { + return false; + } } 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 0f940a08..7664586d 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 @@ -123,6 +123,9 @@ public static class LBListItem extends ScrollableListItem { public CheckButton shouldRender; public LandingBlock landingBlock; + public InputField minX, minY, minZ, maxX, maxY, maxZ; + public InputField[] fields; + public LBListItem(ScrollableList parent, LandingBlock landingBlock) { super(parent); this.landingBlock = landingBlock; @@ -130,55 +133,45 @@ 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: "); + + fields = new InputField[]{minX, minY, minZ, maxX, maxY, maxZ}; } public void render(int index, Vector2D pos, Vector2D size, Vector2D mouse) { - shouldRender.pos = pos.add(size.getX() / 5 - 11, size.getY() / 2 - 5.5); + shouldRender.pos = pos.add(size.getX() / 16 - 4, size.getY() / 2 - 5.5); Renderer2D.drawRectWithEdge(pos, size, 1, lbListColorBg, lbListColorItemEdge); - FontRenderer.drawCenteredString( - "minX: " + landingBlock.boundingBox.getMin().getX(), - pos.add(size.getX() / 5 * 2, - size.getY() / 4), - Color.WHITE, false - ); - FontRenderer.drawCenteredString( - "minY: " + landingBlock.boundingBox.getMin().getY(), - pos.add(size.getX() / 5 * 2, - size.getY() / 2), - Color.WHITE, false - ); - FontRenderer.drawCenteredString( - "minZ: " + landingBlock.boundingBox.getMin().getZ(), - pos.add(size.getX() / 5 * 2, - size.getY() / 4 * 3), - Color.WHITE, false - ); - FontRenderer.drawCenteredString( - "maxX: " + landingBlock.boundingBox.getMax().getX(), - pos.add(size.getX() / 5 * 4, - size.getY() / 4), - Color.WHITE, false - ); - FontRenderer.drawCenteredString( - "maxY: " + landingBlock.boundingBox.getMax().getY(), - pos.add(size.getX() / 5 * 4, - size.getY() / 2), - Color.WHITE, false - ); - FontRenderer.drawCenteredString( - "maxZ: " + landingBlock.boundingBox.getMax().getZ(), - pos.add(size.getX() / 5 * 4, - size.getY() / 4 * 3), - Color.WHITE, false - ); + for (int i = 0; i < fields.length; i++) { + fields[i].pos = pos.add( + size.getX() / 7 * (1 + ((int) (i / 3) * 3)), + size.getY() / 4 * (1 + (i % 3)) - fields[i].getSize().getY() / 2 + ); + fields[i].setWidth(size.getX() / 3); + fields[i].render(mouse); + } shouldRender.render(mouse); } public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) { - return shouldRender.handleMouseInput(state, mousePos, button); + return ArrayListUtil.orMapAll( + ArrayListUtil.getAllOfType(MouseInputListener.class, minX, minY, minZ, maxX, maxY, maxZ, shouldRender), + ele -> ele.handleMouseInput(state, mousePos, button) + ); + } + + public boolean handleKeyInput(int keyCode, String key, boolean pressed) { + return ArrayListUtil.orMapAll( + ArrayListUtil.getAllOfType(KeyInputListener.class, minX, minY, minZ, maxX, maxY, maxZ), + ele -> ele.handleKeyInput(keyCode, key, pressed) + ); } } } diff --git a/src/main/java/io/github/kurrycat/mpkmod/util/ArrayListUtil.java b/src/main/java/io/github/kurrycat/mpkmod/util/ArrayListUtil.java index 61f4474f..0d8bb5de 100644 --- a/src/main/java/io/github/kurrycat/mpkmod/util/ArrayListUtil.java +++ b/src/main/java/io/github/kurrycat/mpkmod/util/ArrayListUtil.java @@ -1,6 +1,9 @@ package io.github.kurrycat.mpkmod.util; +import scala.actors.threadpool.Arrays; + import java.util.ArrayList; +import java.util.Objects; import java.util.stream.Collectors; @SuppressWarnings("unused") @@ -8,9 +11,10 @@ public class ArrayListUtil { /** * Runs elementSupplier for every element of list, until it returns true.
* Info: Returns after the first true return of elementSupplier - * @param list list of elements of type T + * + * @param list list of elements of type T * @param elementSupplier supplier that takes an element as argument and returns a boolean - * @param any type + * @param any type * @return true if elementSupplier returned true for any element or else false */ public static boolean orMap(ArrayList list, ListElementSupplier elementSupplier) { @@ -19,10 +23,17 @@ public static boolean orMap(ArrayList list, ListElementSupplier elemen return false; } + public static boolean orMapAll(ArrayList list, ListElementSupplier elementSupplier) { + boolean b = false; + for (T e : list) + if (elementSupplier.apply(e)) b = true; + return b; + } + /** * @param tClass any class - * @param list any list - * @param the type to filter for + * @param list any list + * @param the type to filter for * @return a list containing any element of list for that element instanceof tClass is true */ @SuppressWarnings("unchecked") @@ -32,10 +43,18 @@ public static ArrayList getAllOfType(Class tClass, ArrayList list) .map(c -> (T) c).collect(Collectors.toCollection(ArrayList::new)); } + @SuppressWarnings("unchecked") + public static ArrayList getAllOfType(Class tClass, Object... list) { + return (ArrayList) Arrays.asList(list).stream() + .filter(Objects::nonNull) + .filter(tClass::isInstance) + .map(c -> (T) c).collect(Collectors.toCollection(ArrayList::new)); + } + /** * @param tClass any class - * @param lists any list of lists - * @param any type + * @param lists any list of lists + * @param any type * @return the result of {@link ArrayListUtil#getAllOfType getAllOfType(tClass, list)} for every list in lists combined */ public static ArrayList getAllOfType(Class tClass, ArrayList... lists) {