From 4a9a414009f053db2caf518f5ef8b3796c56954c Mon Sep 17 00:00:00 2001 From: kurrycat Date: Sun, 9 Jul 2023 17:44:36 +0200 Subject: [PATCH] - added clickable Color List in main gui - added plus buttons to variables --- .../io/github/kurrycat/mpkmod/gui/Theme.java | 2 + .../mpkmod/gui/components/ColorLabel.java | 10 +- .../mpkmod/gui/components/InfoLabel.java | 92 ++++++++++++----- .../mpkmod/gui/components/InputField.java | 98 ++++++++++--------- .../mpkmod/gui/infovars/InfoString.java | 2 + .../mpkmod/gui/infovars/InfoTree.java | 17 ++++ .../kurrycat/mpkmod/gui/infovars/InfoVar.java | 19 ++-- .../compatibility/forge_1_8/MPKMod.java | 1 + 8 files changed, 159 insertions(+), 82 deletions(-) diff --git a/common/src/main/java/io/github/kurrycat/mpkmod/gui/Theme.java b/common/src/main/java/io/github/kurrycat/mpkmod/gui/Theme.java index 6002773..7401364 100644 --- a/common/src/main/java/io/github/kurrycat/mpkmod/gui/Theme.java +++ b/common/src/main/java/io/github/kurrycat/mpkmod/gui/Theme.java @@ -3,6 +3,8 @@ import java.awt.*; public class Theme { + public static Color NONE = new Color(0, 0, 0, 0); + public static Color lightEdge = new Color(255, 255, 255, 95); public static Color darkEdge = new Color(0, 0, 0, 255); public static Color darkBackground = new Color(31, 31, 31, 150); diff --git a/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/ColorLabel.java b/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/ColorLabel.java index c8f8529..2f7767f 100644 --- a/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/ColorLabel.java +++ b/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/ColorLabel.java @@ -6,16 +6,16 @@ public class ColorLabel extends Label { private final Colors color; - public ColorLabel(Colors color) { - super(color.getCode() + color.getName()); - this.color = color; - } - public ColorLabel(Colors color, Vector2D pos) { this(color); this.setPos(pos); } + public ColorLabel(Colors color) { + super(color.getCode() + color.getName()); + this.color = color; + } + @Override public void render(Vector2D mouse) { this.text = contains(mouse) ? color.getName() : color.getCode() + color.getName(); diff --git a/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InfoLabel.java b/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InfoLabel.java index c599a2a..d3c2da5 100644 --- a/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InfoLabel.java +++ b/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InfoLabel.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import io.github.kurrycat.mpkmod.Main; import io.github.kurrycat.mpkmod.compatibility.MCClasses.FontRenderer; +import io.github.kurrycat.mpkmod.gui.Theme; import io.github.kurrycat.mpkmod.gui.TickThread; import io.github.kurrycat.mpkmod.gui.infovars.InfoString; import io.github.kurrycat.mpkmod.gui.infovars.InfoVar; @@ -16,9 +17,8 @@ import io.github.kurrycat.mpkmod.util.Vector2D; import java.awt.*; -import java.util.ArrayList; import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; public class InfoLabel extends Label implements TickThread.Tickable { @@ -86,11 +86,13 @@ public PopupMenu getPopupMenu() { private static class InfoVarList extends ScrollableList { private final List allItems; + private final InputField inputField; - public InfoVarList(Vector2D pos, Vector2D size) { + public InfoVarList(InputField inputField, Vector2D pos, Vector2D size) { this.setPos(pos); this.setSize(size); this.setTitle("Variables"); + this.inputField = inputField; allItems = Main.infoTree.getEntries().stream() .map(entry -> new InfoVarListItem(this, entry.getValue()) @@ -113,26 +115,23 @@ public void render(Vector2D mouse) { private static class InfoVarListItem extends ScrollableListItem { public static final int HEIGHT = 18; - private final String varName; private final InfoVarComponent infoVarComponent; - public InfoVarListItem(ScrollableList parent, InfoVar infoVar) { + public InfoVarListItem(InfoVarList parent, InfoVar infoVar) { super(parent); this.setHeight(18); - this.varName = infoVar.getName(); - this.infoVarComponent = new InfoVarComponent(infoVar); + this.infoVarComponent = new InfoVarComponent(infoVar, parent.inputField); infoVarComponent.setSize(new Vector2D(1, -HEIGHT)); passPositionTo(infoVarComponent, PERCENT.SIZE_X); } @Override public int getHeight() { - return infoVarComponent.collapsed ? HEIGHT : infoVarComponent.getHeight(); + return infoVarComponent.getHeight(); } @Override public void render(int index, Vector2D pos, Vector2D size, Vector2D mouse) { - //renderDefaultBorder(pos, size); infoVarComponent.render(mouse); } @@ -149,17 +148,20 @@ public boolean containsSearchString(String searchString) { private static class InfoVarComponent extends Component implements MouseInputListener { private final InfoVar infoVar; + private final InputField inputField; private final ArrayList children = new ArrayList<>(); private final Button collapseButton; + private final Button addButton; private final boolean showCollapseButton; public boolean collapsed = true; private InfoVarComponent parent = null; - public InfoVarComponent(InfoVar infoVar) { + public InfoVarComponent(InfoVar infoVar, InputField inputField) { this.infoVar = infoVar; + this.inputField = inputField; double[] i = {InfoVarListItem.HEIGHT}; infoVar.getEntries().stream() - .map(entry -> new InfoVarComponent(entry.getValue())) + .map(entry -> new InfoVarComponent(entry.getValue(), inputField)) .forEach(c -> { c.parent = this; c.setSize(new Vector2D(-4, InfoVarListItem.HEIGHT)); @@ -186,13 +188,22 @@ public InfoVarComponent(InfoVar infoVar) { buttonHolder.setSize(new Vector2D(1, InfoVarListItem.HEIGHT)); passPositionTo(buttonHolder, PERCENT.SIZE_X); - collapseButton = new Button("v", new Vector2D(1, 0), new Vector2D(11, 11)); + collapseButton = new Button("v", new Vector2D(13, 0), new Vector2D(11, 11)); collapseButton.setButtonCallback(mouseButton -> { if (mouseButton != Mouse.Button.LEFT) return; setCollapsed(!collapsed); }); buttonHolder.passPositionTo(collapseButton, PERCENT.NONE, Anchor.CENTER_RIGHT); components.add(collapseButton); + + addButton = new Button("+", new Vector2D(1, 0), new Vector2D(11, 11)); + addButton.setButtonCallback(mouseButton -> { + if (mouseButton != Mouse.Button.LEFT) return; + inputField.typeContentAtCursor("{" + infoVar.getFullName() + "}"); + inputField.focus(); + }); + buttonHolder.passPositionTo(addButton, PERCENT.NONE, Anchor.CENTER_RIGHT); + components.add(addButton); } private void setCollapsed(boolean collapsed) { @@ -250,11 +261,49 @@ public int getHeight() { @Override public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Button button) { - return showCollapseButton && collapseButton.handleMouseInput(state, mousePos, button) || + return addButton.handleMouseInput(state, mousePos, button) || + showCollapseButton && collapseButton.handleMouseInput(state, mousePos, button) || !collapsed && ArrayListUtil.orMap(children, c -> c.handleMouseInput(state, mousePos, button)); } } + private static class ColorList extends ScrollableList { + public ColorList(InputField inputField) { + super(); + setTitle("Colors"); + setPos(new Vector2D(5, 5)); + double width = Arrays.stream(Colors.values()) + .map(c -> FontRenderer.getStringSize(c.getName()).getX()) + .max(Comparator.naturalOrder()).orElse(50D); + setSize(new Vector2D(width + 15, -10)); + for (Colors c : Colors.values()) { + addItem(new ColorItem(this, inputField, c)); + } + } + } + + private static class ColorItem extends ScrollableListItem { + public ColorItem(ScrollableList parent, InputField inputField, Colors color) { + super(parent); + setHeight(12); + Button button = new Button( + color.getCode() + color.getName(), + new Vector2D(0, 0), new Vector2D(1, 1), + mouseButton -> { + inputField.typeContentAtCursor("{" + color.getName() + "}"); + inputField.focus(); + } + ); + button.normalColor = Theme.NONE; + addChild(button, PERCENT.SIZE); + } + + @Override + public void render(int index, Vector2D pos, Vector2D size, Vector2D mouse) { + renderComponents(mouse); + } + } + public class EditPane extends Pane { private final TextRectangle label; @@ -274,16 +323,15 @@ public EditPane() { addChild(inputField, PERCENT.SIZE_X, Anchor.BOTTOM_CENTER); - Div colors = new Div(new Vector2D(5, 5), new Vector2D(0, 0)); - addChild(colors); - for (Colors c : Colors.values()) { - colors.addChildBelow(new ColorLabel(c)); - } - colors.backgroundColor = new Color(31, 31, 31, 150); - colors.setAbsolute(true); + ColorList cl = new ColorList(inputField); + cl.setAbsolute(true); + addChild(cl); - - InfoVarList vl = new InfoVarList(new Vector2D(5, 5), new Vector2D(2 / 9D, -10)); + InfoVarList vl = new InfoVarList( + inputField, + new Vector2D(5, 5), + new Vector2D(2 / 9D, -10) + ); vl.setAbsolute(true); addChild(vl, PERCENT.SIZE_X, Anchor.TOP_RIGHT); diff --git a/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java b/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java index 3237dfc..56beb09 100644 --- a/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java +++ b/common/src/main/java/io/github/kurrycat/mpkmod/gui/components/InputField.java @@ -36,10 +36,6 @@ public InputField(Vector2D pos, double width) { this("", pos, width, false); } - public InputField(String content, Vector2D pos, double width) { - this(content, pos, width, false); - } - public InputField(String content, Vector2D pos, double width, boolean numbersOnly) { this.setPos(pos); this.setSize(new Vector2D(width, HEIGHT)); @@ -47,16 +43,12 @@ public InputField(String content, Vector2D pos, double width, boolean numbersOnl this.numbersOnly = numbersOnly; } - private String getFilter() { - if (customFilter != null) { - return customFilter; - } - return numbersOnly ? FILTER_NUMBERS : FILTER_ALL; + public InputField(String content, Vector2D pos, double width) { + this(content, pos, width, false); } - public InputField setFilter(String filter) { - this.customFilter = filter; - return this; + public void focus() { + isFocused = true; } public InputField setName(String name) { @@ -116,14 +108,17 @@ public void render(Vector2D mouse) { ); } + private double getCursorX() { + return 2 + FontRenderer.getStringSize(content.substring(0, cursorPos)).getX(); + } + @Override public boolean handleKeyInput(int keyCode, int scanCode, int modifiers, boolean isCharTyped) { if (!isFocused) return false; boolean inputPerformed = true; if (isCharTyped && String.valueOf((char) keyCode).matches(getFilter())) { - replaceSelectionWithChar(Character.toString((char) keyCode)); - cursorPos = highlightStart + 1; + typeContentAtCursor(Character.toString((char) keyCode)); } else { switch (keyCode) { case InputConstants.KEY_BACKSPACE: @@ -164,6 +159,24 @@ public boolean handleKeyInput(int keyCode, int scanCode, int modifiers, boolean return true; } + private String getFilter() { + if (customFilter != null) { + return customFilter; + } + return numbersOnly ? FILTER_NUMBERS : FILTER_ALL; + } + + public InputField setFilter(String filter) { + this.customFilter = filter; + return this; + } + + public void typeContentAtCursor(String c) { + updateContent(content.substring(0, highlightStart) + c + content.substring(highlightEnd)); + cursorPos = highlightStart + c.length(); + highlightStart = highlightEnd = cursorPos; + } + private void deleteSelection() { if (highlightStart == highlightEnd) updateContent(content.substring(0, Math.max(cursorPos - 1, 0)) + (cursorPos >= content.length() ? "" : content.substring(cursorPos))); @@ -171,10 +184,6 @@ private void deleteSelection() { updateContent(content.substring(0, highlightStart) + content.substring(highlightEnd)); } - private void replaceSelectionWithChar(String c) { - updateContent(content.substring(0, highlightStart) + c + content.substring(highlightEnd)); - } - private void updateContent(String content) { this.content = content; if (onContentChange != null) @@ -189,32 +198,6 @@ public void clear() { content = ""; } - private double getCursorX() { - return 2 + FontRenderer.getStringSize(content.substring(0, cursorPos)).getX(); - } - - private int getCursorPosFromMousePos(Vector2D mouse) { - Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name); - Vector2D rectPos = getDisplayedPos().add(nameSize.getX(), 0); - - double x = mouse.getX() - rectPos.getX() - 2; - if (x < 0) - return 0; - else if (x > FontRenderer.getStringSize(content).getX()) - return content.length(); - - for (int i = 1; i <= content.length(); i++) { - int charWidth = FontRenderer.getStringSize(content.substring(i - 1, i)).getXI(); - if (x < charWidth / 2D) - return i - 1; - else if (x < charWidth) - return i; - - x -= charWidth; - } - return content.length(); - } - public void setWidth(double width) { this.size.setX(width); } @@ -232,8 +215,6 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt return true; } else { isFocused = false; - highlightStart = 0; - highlightEnd = 0; } case DRAG: case UP: @@ -246,14 +227,35 @@ public boolean handleMouseInput(Mouse.State state, Vector2D mousePos, Mouse.Butt highlightStart = cursorPos; highlightEnd = c; } - - return contains(mousePos); + return true; } } } return false; } + private int getCursorPosFromMousePos(Vector2D mouse) { + Vector2D nameSize = name == null ? Vector2D.ZERO : FontRenderer.getStringSize(name); + Vector2D rectPos = getDisplayedPos().add(nameSize.getX(), 0); + + double x = mouse.getX() - rectPos.getX() - 2; + if (x < 0) + return 0; + else if (x > FontRenderer.getStringSize(content).getX()) + return content.length(); + + for (int i = 1; i <= content.length(); i++) { + int charWidth = FontRenderer.getStringSize(content.substring(i - 1, i)).getXI(); + if (x < charWidth / 2D) + return i - 1; + else if (x < charWidth) + return i; + + x -= charWidth; + } + return content.length(); + } + @FunctionalInterface public interface ContentProvider { void apply(Content content); diff --git a/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoString.java b/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoString.java index d3dc575..c91859c 100644 --- a/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoString.java +++ b/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoString.java @@ -277,6 +277,8 @@ else if (o instanceof Float) return MathUtil.formatDecimals((Float) o, decimals, keepZeros); else if (o instanceof FormatDecimals) return ((FormatDecimals) o).formatDecimals(decimals, keepZeros); + if(o.toString().equals(o.getClass().getName() + "@" + Integer.toHexString(o.hashCode()))) + return "[" + o.getClass().getSimpleName() + " object]"; return o.toString(); } } diff --git a/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoTree.java b/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoTree.java index 2d73c25..c5c1a34 100644 --- a/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoTree.java +++ b/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoTree.java @@ -7,8 +7,17 @@ public class InfoTree { private final HashMap elements = new HashMap<>(); private InfoTree parentTree = null; + private InfoVar node = null; private int size = 0; + public InfoTree() { + + } + + public InfoTree(InfoVar node) { + this.node = node; + } + public void addElement(String name, InfoVar var) { if (name.contains(".")) { Debug.stacktrace("Expected String without \".\", got: \"" + name + "\""); @@ -44,7 +53,15 @@ public int getSize() { return size; } + public InfoTree getParent() { + return parentTree; + } + public void setParent(InfoTree infoTree) { this.parentTree = infoTree; } + + public InfoVar getNode() { + return node; + } } diff --git a/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoVar.java b/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoVar.java index 3de1fa7..251122a 100644 --- a/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoVar.java +++ b/common/src/main/java/io/github/kurrycat/mpkmod/gui/infovars/InfoVar.java @@ -5,37 +5,42 @@ import java.util.Set; public class InfoVar { - private final InfoTree node; + private final InfoTree childTree; private final List objects; private final String name; public InfoVar(String name, List objects) { this.name = name; this.objects = objects; - this.node = new InfoTree(); + this.childTree = new InfoTree(this); } public Set> getEntries() { - return node.getEntries(); + return childTree.getEntries(); } public InfoVar getElement(List name) { - return node.getElement(name); + return childTree.getElement(name); } public String getName() { return name; } + public String getFullName() { + if (this.childTree.getParent() == null || this.childTree.getParent().getNode() == null) return this.name; + return this.childTree.getParent().getNode().getFullName() + "." + this.name; + } + public InfoVar createChild(String name, List objects) { InfoVar child = new InfoVar(name, objects); - child.setParent(node); - node.addElement(name, child); + child.setParent(childTree); + childTree.addElement(name, child); return child; } public void setParent(InfoTree infoTree) { - this.node.setParent(infoTree); + this.childTree.setParent(infoTree); } public Object getObj() { diff --git a/forge-1.8.9/src/main/java/io/github/kurrycat/mpkmod/compatibility/forge_1_8/MPKMod.java b/forge-1.8.9/src/main/java/io/github/kurrycat/mpkmod/compatibility/forge_1_8/MPKMod.java index 7207ae5..cb9aeb9 100644 --- a/forge-1.8.9/src/main/java/io/github/kurrycat/mpkmod/compatibility/forge_1_8/MPKMod.java +++ b/forge-1.8.9/src/main/java/io/github/kurrycat/mpkmod/compatibility/forge_1_8/MPKMod.java @@ -4,6 +4,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.settings.GameSettings; import net.minecraft.client.settings.KeyBinding; +import net.minecraft.entity.EntityLivingBase; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.common.Mod;