From 493ba953d0b5755c064c63b259b9368adf3a7a4f Mon Sep 17 00:00:00 2001 From: nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> Date: Fri, 6 May 2022 17:59:08 +0100 Subject: number input field --- .../cc/polyfrost/oneconfig/gui/OneConfigGui.java | 7 +- .../oneconfig/gui/elements/ModUpdateCard.java | 8 + .../oneconfig/gui/elements/SearchField.java | 14 - .../oneconfig/gui/elements/TextInputField.java | 418 -------------------- .../gui/elements/config/ConfigColorElement.java | 2 +- .../gui/elements/config/ConfigSlider.java | 39 +- .../gui/elements/config/ConfigTextBox.java | 2 +- .../gui/elements/text/NumberInputField.java | 119 ++++++ .../oneconfig/gui/elements/text/SearchField.java | 14 + .../gui/elements/text/TextInputField.java | 425 +++++++++++++++++++++ 10 files changed, 575 insertions(+), 473 deletions(-) create mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/ModUpdateCard.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/SearchField.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/text/SearchField.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java (limited to 'src/main/java/cc/polyfrost') diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java index 44e019e..380f89a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java @@ -3,7 +3,8 @@ package cc.polyfrost.oneconfig.gui; import cc.polyfrost.oneconfig.config.OneConfigConfig; import cc.polyfrost.oneconfig.gui.elements.BasicElement; import cc.polyfrost.oneconfig.gui.elements.ColorSelector; -import cc.polyfrost.oneconfig.gui.elements.TextInputField; +import cc.polyfrost.oneconfig.gui.elements.text.NumberInputField; +import cc.polyfrost.oneconfig.gui.elements.text.TextInputField; import cc.polyfrost.oneconfig.gui.pages.HomePage; import cc.polyfrost.oneconfig.gui.pages.Page; import cc.polyfrost.oneconfig.lwjgl.RenderManager; @@ -30,7 +31,6 @@ public class OneConfigGui extends GuiScreen { protected Page currentPage; protected Page prevPage; private float pageProgress = -224f; - private float scissorExclusionHeight = 0f; private final TextInputField textInputField = new TextInputField(248, 40, "Search all of OneConfig...", false, false); private final ArrayList previousPages = new ArrayList<>(); private final ArrayList nextPages = new ArrayList<>(); @@ -65,7 +65,6 @@ public class OneConfigGui extends GuiScreen { RenderManager.drawRect(vg, 544, 140, 20, 800, OneConfigConfig.GRAY_800); //RenderManager.drawDropShadow(vg, 544, 140, 1056, 800, 20f, 32f, OneConfigConfig.GRAY_800); } - RenderManager.drawLine(vg, 544, 212, 1600, 212, 1, OneConfigConfig.GRAY_700); RenderManager.drawLine(vg, 544, 140, 544, 940, 1, OneConfigConfig.GRAY_700); @@ -73,10 +72,10 @@ public class OneConfigGui extends GuiScreen { RenderManager.drawString(vg, "OneConfig", x + 69, y + 32, OneConfigConfig.WHITE, 18f, Fonts.INTER_BOLD); // added half line height to center text RenderManager.drawString(vg, "ALPHA - By Polyfrost", x + 69, y + 51, OneConfigConfig.WHITE, 12f, Fonts.INTER_REGULAR); + //RenderManager.drawRect(vg, x + 300, y + 500, 400, 12, OneConfigConfig.ERROR_700); //RenderManager.drawString(vg, "MoonTidez is Annoyinhg here is an f |||", x + 300, y + 500, OneConfigConfig.WHITE, 14f, 14,Fonts.INTER_REGULAR); - textInputField.draw(vg, x + 1020, y + 16); sideBar.draw(vg, x, y); backArrow.draw(vg, x + 240, y + 16); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/ModUpdateCard.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ModUpdateCard.java new file mode 100644 index 0000000..3baf001 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/ModUpdateCard.java @@ -0,0 +1,8 @@ +package cc.polyfrost.oneconfig.gui.elements; + +public class ModUpdateCard extends BasicElement{ + + public ModUpdateCard(int width, int height) { // TODO + super(width, height, true); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/SearchField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/SearchField.java deleted file mode 100644 index 6f460d6..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/SearchField.java +++ /dev/null @@ -1,14 +0,0 @@ -package cc.polyfrost.oneconfig.gui.elements; - -public class SearchField extends TextInputField { - - public SearchField(int width, int height, String defaultText, boolean multiLine, boolean password) { - super(width, height, defaultText, multiLine, password); - } - - @Override - public void draw(long vg, int x, int y) { - super.draw(vg, x, y); - // TODO - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java deleted file mode 100644 index 196c447..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/TextInputField.java +++ /dev/null @@ -1,418 +0,0 @@ -package cc.polyfrost.oneconfig.gui.elements; - -import cc.polyfrost.oneconfig.config.OneConfigConfig; -import cc.polyfrost.oneconfig.lwjgl.RenderManager; -import cc.polyfrost.oneconfig.lwjgl.scissor.Scissor; -import cc.polyfrost.oneconfig.lwjgl.scissor.ScissorManager; -import cc.polyfrost.oneconfig.lwjgl.font.Fonts; -import cc.polyfrost.oneconfig.utils.InputUtils; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.util.ChatAllowedCharacters; -import org.jetbrains.annotations.NotNull; -import org.lwjgl.input.Keyboard; -import org.lwjgl.input.Mouse; - -import java.awt.*; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; - -public class TextInputField extends BasicElement { - - protected final String defaultText; - protected String input, selectedText; - protected final boolean multiLine; - protected boolean password; - - protected int caretPos; - protected int x, y; - protected float start, end; - private long clickTimeD1; - protected long vg; - protected int prevCaret = 0; - protected boolean isDoubleClick = false; - protected boolean onlyNums = false; - protected boolean errored = false; - protected boolean centered = false; - - public TextInputField(int width, int height, String defaultText, boolean multiLine, boolean password) { - super(width, height, false); - this.multiLine = multiLine; - this.defaultText = defaultText; - this.password = password; - this.input = ""; - } - - public TextInputField(int width, int height, boolean centered, boolean onlyNums, String defaultText) { - this(width, height, defaultText, false, false); - this.centered = centered; - this.onlyNums = onlyNums; - } - - public void onlyAcceptNumbers(boolean state) { - onlyNums = state; - } - - public void setInput(String input) { - this.input = input; - } - - public String getInput() { - return input; - } - - public void setPassword(boolean password) { - this.password = password; - } - - public boolean getPassword() { - return password; - } - - public void setErrored(boolean errored) { - this.errored = errored; - } - - public void setCentered(boolean centered) { - this.centered = centered; - } - - public boolean isErrored() { - return errored; - } - - @Override - public void draw(long vg, int x, int y) { - this.x = x; - this.y = y; - this.vg = vg; - try { - Scissor scissor = ScissorManager.scissor(vg, x, y, width, height); - int colorOutline = errored ? OneConfigConfig.ERROR_700 : OneConfigConfig.GRAY_700; - RenderManager.drawHollowRoundRect(vg, x, y, width, height, colorOutline, 12f, 2f); - super.update(x, y); - if (Mouse.isButtonDown(0) && !InputUtils.isAreaHovered(x - 40, y - 20, width + 90, height + 20)) { - toggled = false; - } - int color = toggled ? OneConfigConfig.WHITE : OneConfigConfig.WHITE_60; - if (!toggled) caretPos = input.length(); - float width; - StringBuilder s = new StringBuilder(); - if (!password) { - width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f, Fonts.INTER_REGULAR); - } else { - for (int i = 0; i < input.length(); i++) { - s.append("*"); - } - width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f, Fonts.INTER_REGULAR); - } - if (hovered) { - while (Mouse.next()) { - if (Mouse.getEventButtonState()) { - if (Mouse.getEventButton() == 0) { - prevCaret = calculatePos(Mouse.getX()); - if (System.currentTimeMillis() - clickTimeD1 < 300) { - onDoubleClick(); - isDoubleClick = true; - } - clickTimeD1 = System.currentTimeMillis(); - } - } else { - if (Mouse.getEventButton() == 0) { - long clickTimeU = System.currentTimeMillis(); - if (clickTimeU - clickTimeD1 < 200) { - if (!isDoubleClick) { - start = 0; - end = 0; - } - prevCaret = caretPos; - isDoubleClick = false; - } - - } - } - } - } - float halfTextWidth = this.getTextWidth(vg, input) / 2f; - if (start != 0f && end != 0f && toggled) { - RenderManager.drawRect(vg, start, y + height / 2f - 10, end, 20, OneConfigConfig.GRAY_300); - } - if (hovered) { - if (Mouse.isButtonDown(0) && !isDoubleClick) { - caretPos = calculatePos(Mouse.getX()); - if (caretPos > prevCaret) { - if (!centered) start = x + 12 + this.getTextWidth(vg, input.substring(0, prevCaret)); - else - start = x + this.width / 2f - halfTextWidth + this.getTextWidth(vg, input.substring(0, prevCaret)); - end = this.getTextWidth(vg, input.substring(prevCaret, caretPos)); - selectedText = input.substring(prevCaret, caretPos); - } else { - if (!centered) start = x + 12 + this.getTextWidth(vg, input.substring(0, prevCaret)); - else - start = x + this.width / 2f - halfTextWidth + this.getTextWidth(vg, input.substring(0, prevCaret)); - end = -this.getTextWidth(vg, input.substring(caretPos, prevCaret)); - selectedText = input.substring(caretPos, prevCaret); - } - } - } - - - if (toggled) { - if (!centered) { - RenderManager.drawLine(vg, x + width + 12, (float) y + height / 2f - 10, x + width + 12, (float) y + height / 2f + 10, 1, OneConfigConfig.WHITE); - } else { - RenderManager.drawLine(vg, x + this.width / 2f - halfTextWidth + width, (float) y + height / 2f - 10, x + this.width / 2f - halfTextWidth + width, (float) y + height / 2f + 10, 1, OneConfigConfig.WHITE); - } - } - - - if (input.equals("")) { - if (!centered) { - RenderManager.drawString(vg, defaultText, x + 12, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); - } else { - RenderManager.drawString(vg, defaultText, x + this.width / 2f - halfTextWidth, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); - } - } - - if (!password) { - if (!centered) { - RenderManager.drawString(vg, input, x + 12, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); - } else { - RenderManager.drawString(vg, input, x + this.width / 2f - halfTextWidth, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); - } - } else { - RenderManager.drawString(vg, s.toString(), x + 12, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); - } - ScissorManager.resetScissor(vg, scissor); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public void keyTyped(char c, int key) { - try { - if (toggled) { - if (GuiScreen.isKeyComboCtrlC(key)) { - if (selectedText != null && start != 0f && end != 0f) { - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selectedText), null); - } - return; - } - if (GuiScreen.isKeyComboCtrlV(key) || key == Keyboard.KEY_INSERT) { - try { - String clip = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor).toString(); - input = input.substring(0, caretPos) + clip + input.substring(caretPos); - caretPos = caretPos + clip.length(); - return; - } catch (Exception e) { - e.printStackTrace(); - } - } - if (key == Keyboard.KEY_DELETE) { - input = ""; - } - - - if (GuiScreen.isCtrlKeyDown()) { - if (key == Keyboard.KEY_BACK && !GuiScreen.isKeyComboCtrlX(key)) { - try { - input = input.substring(0, input.lastIndexOf(" ")); - caretPos = input.length(); - } catch (Exception e) { - input = ""; - caretPos = 0; - } - return; - } - if (GuiScreen.isKeyComboCtrlA(key)) { - prevCaret = 0; - caretPos = input.length(); - start = !centered ? x + 12 : x + this.width / 2f - this.getTextWidth(vg, input) / 2f; - selectedText = input; - end = this.getTextWidth(vg, input); - return; - } - if (GuiScreen.isKeyComboCtrlX(key)) { - if (selectedText != null && start != 0f && end != 0f) { - Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selectedText), null); - key = Keyboard.KEY_BACK; - } else return; - } - if (key == Keyboard.KEY_LEFT) { - caretPos = input.substring(0, caretPos).lastIndexOf(' ') + 1; - } - if (key == Keyboard.KEY_RIGHT) { - caretPos = input.indexOf(' ', caretPos); - if (caretPos == -1) caretPos = input.length(); - } - - } - if (key == Keyboard.KEY_BACK) { - if (input.length() > 0) { - if (start != 0f && end != 0f) { - start = 0f; - end = 0f; - if (caretPos > prevCaret) { - input = input.substring(0, prevCaret) + input.substring(caretPos); - caretPos = prevCaret; - } - if (caretPos < prevCaret) { - input = input.substring(0, caretPos) + input.substring(prevCaret); - } - return; - } - if (caretPos == input.length()) { - input = input.substring(0, input.length() - 1); - } else { - input = input.substring(0, caretPos - 1) + input.substring(caretPos); - } - caretPos--; - } - return; - } - if (key == Keyboard.KEY_TAB) { - if (onlyNums) return; - input += " "; - caretPos += 4; - return; - } - - if (key == Keyboard.KEY_RIGHT) { - caretPos++; - if (caretPos > input.length()) { - caretPos = input.length(); - } - if (start != 0f && end != 0f) { - start = 0f; - end = 0f; - } - return; - } - if (key == Keyboard.KEY_LEFT) { - caretPos--; - if (caretPos < 0) { - caretPos = 0; - } - if (start != 0f && end != 0f) { - start = 0f; - end = 0f; - } - return; - } - if (key == Keyboard.KEY_UP || key == 201) { // 201 = page up - caretPos = 0; - if (start != 0f && end != 0f) { - start = 0f; - end = 0f; - } - return; - } - if (key == Keyboard.KEY_DOWN || key == 209) { // 209 = page down - caretPos = input.length(); - if (start != 0f && end != 0f) { - start = 0f; - end = 0f; - } - return; - } - - - if (key == Keyboard.KEY_RETURN) { - toggled = false; - if (start != 0f && end != 0f) { - start = 0f; - end = 0f; - } - } - if (key == Keyboard.KEY_END) { - toggled = false; - } - - - if (key == Keyboard.KEY_LCONTROL || key == Keyboard.KEY_RCONTROL || key == Keyboard.KEY_LMENU || key == Keyboard.KEY_RMENU || key == Keyboard.KEY_LMETA || key == Keyboard.KEY_RMETA || key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT || key == Keyboard.KEY_RETURN || key == Keyboard.KEY_CAPITAL || key == 221 || key == Keyboard.KEY_HOME) { - return; - } - if (onlyNums) { - if (!Character.isDigit(c) && key != 52) return; - } - if (!Character.isDefined(key)) return; - if (!Character.isDefined(c)) return; - if (GuiScreen.isCtrlKeyDown()) return; - if (ChatAllowedCharacters.isAllowedCharacter(c)) { - if (selectedText != null) { - if (caretPos > prevCaret) { - input = input.substring(0, prevCaret) + input.substring(prevCaret, caretPos); - caretPos = prevCaret; - } else { - input = input.substring(0, caretPos) + input.substring(caretPos, prevCaret); - } - if (selectedText.equals(input)) { - input = ""; - } - selectedText = null; - } - input = addCharAtPoint(caretPos, c); - caretPos++; - } - if (start != 0f && end != 0f) { - start = 0f; - end = 0f; - } - - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - private @NotNull String addCharAtPoint(int index, char c) { - return input.substring(0, index) + c + input.substring(index); - } - - @Override - public void onClick() { - toggled = true; - } - - private void onDoubleClick() { - prevCaret = input.substring(0, caretPos).lastIndexOf(' ') + 1; - caretPos = input.indexOf(' ', caretPos); - if (caretPos == -1) caretPos = input.length(); - selectedText = input.substring(prevCaret, caretPos); - if (!centered) start = x + 12 + this.getTextWidth(vg, input.substring(0, prevCaret)); - else - start = x + this.width / 2f - this.getTextWidth(vg, input) / 2f + this.getTextWidth(vg, input.substring(0, prevCaret)); - end = this.getTextWidth(vg, input.substring(prevCaret, caretPos)); - } - - private int calculatePos(int pos) { - if (centered) pos -= 12; - String s1 = ""; - int i; - for (char c : input.toCharArray()) { - if (pos - x - 12 < 0) { - return 0; - } - if (pos - x - 12 > this.getTextWidth(vg, input)) { - return input.length(); - } - s1 += c; - i = (int) this.getTextWidth(vg, s1); - if (i >= pos - x - 16) { - return s1.length(); - } - } - return 0; - } - - private float getTextWidth(long vg, String s) { - if (password) { - StringBuilder s1 = new StringBuilder(); - while (s1.length() < s.length()) { - s1.append('*'); - } - return RenderManager.getTextWidth(vg, s1.toString(), 14.0f, Fonts.INTER_REGULAR); - } else { - return RenderManager.getTextWidth(vg, s, 14.0f, Fonts.INTER_REGULAR); - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java index 2c89133..a5c82e2 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigColorElement.java @@ -5,7 +5,7 @@ import cc.polyfrost.oneconfig.config.interfaces.BasicOption; import cc.polyfrost.oneconfig.gui.OneConfigGui; import cc.polyfrost.oneconfig.gui.elements.BasicElement; import cc.polyfrost.oneconfig.gui.elements.ColorSelector; -import cc.polyfrost.oneconfig.gui.elements.TextInputField; +import cc.polyfrost.oneconfig.gui.elements.text.TextInputField; import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.Images; diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java index 69cb887..856fbf3 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java @@ -2,27 +2,21 @@ package cc.polyfrost.oneconfig.gui.elements.config; import cc.polyfrost.oneconfig.config.OneConfigConfig; import cc.polyfrost.oneconfig.config.interfaces.BasicOption; +import cc.polyfrost.oneconfig.gui.elements.BasicElement; +import cc.polyfrost.oneconfig.gui.elements.text.TextInputField; import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; -import cc.polyfrost.oneconfig.lwjgl.image.Images; -import cc.polyfrost.oneconfig.utils.ColorUtils; import cc.polyfrost.oneconfig.utils.InputUtils; import cc.polyfrost.oneconfig.utils.MathUtils; -import cc.polyfrost.oneconfig.gui.elements.BasicElement; -import cc.polyfrost.oneconfig.gui.elements.TextInputField; import org.lwjgl.input.Mouse; -import org.lwjgl.nanovg.NanoVG; import java.lang.reflect.Field; public class ConfigSlider extends BasicOption { private final BasicElement slideYBoi = new BasicElement(24, 24, false); private final TextInputField inputField = new TextInputField(84, 24, "", false, false); - private final BasicElement upArrow = new BasicElement(12, 14, false); - private final BasicElement downArrow = new BasicElement(12, 14, false); private final float min, max; private int steps = 0; - private int colorTop, colorBottom; private boolean isFloat = true; private Float prevAsNum = null; private final int step; @@ -112,33 +106,6 @@ public class ConfigSlider extends BasicOption { } currentAsNum = current * (max - min) + min; - RenderManager.drawRoundedRect(vg, x + 980, y, 12, 28, OneConfigConfig.GRAY_500, 6f); - upArrow.update(x + 980, y); - downArrow.update(x + 980, y + 14); - if (current == 1f) colorTop = OneConfigConfig.GRAY_500_80; - if (current == 0f) colorBottom = OneConfigConfig.GRAY_500_80; - colorTop = ColorUtils.getColor(colorTop, 2, upArrow.isHovered(), upArrow.isClicked()); - colorBottom = ColorUtils.getColor(colorBottom, 2, downArrow.isHovered(), downArrow.isClicked()); - if (upArrow.isClicked()) { - currentAsNum += step == 0 ? 1 : step; - current = MathUtils.clamp((currentAsNum - min) / (max - min)); - } - if (downArrow.isClicked()) { - currentAsNum -= step == 0 ? 1 : step; - current = MathUtils.clamp((currentAsNum - min) / (max - min)); - } - if (current == 1f) NanoVG.nvgGlobalAlpha(vg, 0.3f); - RenderManager.drawRoundedRectVaried(vg, x + 980, y, 12, 14, colorTop, 6f, 6f, 0f, 0f); - RenderManager.drawImage(vg, Images.UP_ARROW, x + 981, y + 2, 10, 10); - if (current == 1f) NanoVG.nvgGlobalAlpha(vg, 1f); - - if (current == 0f) NanoVG.nvgGlobalAlpha(vg, 0.3f); - RenderManager.drawRoundedRectVaried(vg, x + 980, y + 14, 12, 14, colorBottom, 0f, 0f, 6f, 6f); - NanoVG.nvgTranslate(vg, x + 991, y + 25); - NanoVG.nvgRotate(vg, (float) Math.toRadians(180)); - RenderManager.drawImage(vg, Images.UP_ARROW, 0, 0, 10, 10); - NanoVG.nvgResetTransform(vg); - NanoVG.nvgGlobalAlpha(vg, 1f); if (currentAsNum != prevAsNum) { try { @@ -172,6 +139,8 @@ public class ConfigSlider extends BasicOption { } } + + @Override public boolean hasHalfSize() { return false; diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigTextBox.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigTextBox.java index 94ed127..66dece5 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigTextBox.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigTextBox.java @@ -6,7 +6,7 @@ import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.lwjgl.image.Images; import cc.polyfrost.oneconfig.utils.InputUtils; -import cc.polyfrost.oneconfig.gui.elements.TextInputField; +import cc.polyfrost.oneconfig.gui.elements.text.TextInputField; import java.awt.*; import java.lang.reflect.Field; diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java new file mode 100644 index 0000000..0e28a90 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java @@ -0,0 +1,119 @@ +package cc.polyfrost.oneconfig.gui.elements.text; + +import cc.polyfrost.oneconfig.config.OneConfigConfig; +import cc.polyfrost.oneconfig.gui.elements.BasicElement; +import cc.polyfrost.oneconfig.lwjgl.RenderManager; +import cc.polyfrost.oneconfig.lwjgl.image.Images; +import cc.polyfrost.oneconfig.utils.ColorUtils; +import org.lwjgl.nanovg.NanoVG; + +public class NumberInputField extends TextInputField { + private final BasicElement upArrow = new BasicElement(12, 14, false); + private final BasicElement downArrow = new BasicElement(12, 14, false); + private float min; + private float max; + private float step; + private int colorTop, colorBottom; + private float current; + + public NumberInputField(int width, int height, float defaultValue, float min, float max, float step) { + super(width - 16, height, true, ""); + super.onlyNums = true; + this.min = min; + this.max = max; + this.step = step; + this.input = String.format("%.01f", defaultValue); + } + + @Override + public void draw(long vg, int x, int y) { + super.errored = false; + RenderManager.drawRoundedRect(vg, x + width + 4, y, 12, 28, OneConfigConfig.GRAY_500, 6f); + upArrow.update(x + width + 4, y); + downArrow.update(x + width + 4, y + 14); + try { + current = Float.parseFloat(input); + if (current < min || current > max) { + super.errored = true; + } else { + upArrow.disable(false); + downArrow.disable(false); + } + if (current == max) colorTop = OneConfigConfig.GRAY_500_80; + if (current == min) colorBottom = OneConfigConfig.GRAY_500_80; + + colorTop = ColorUtils.getColor(colorTop, 2, upArrow.isHovered(), upArrow.isClicked()); + colorBottom = ColorUtils.getColor(colorBottom, 2, downArrow.isHovered(), downArrow.isClicked()); + if (upArrow.isClicked()) { + current += step; + if (current > max) current = max; + setCurrentValue(current); + } + if (downArrow.isClicked()) { + current -= step; + if(current < min) current = min; + setCurrentValue(current); + } + if (current >= max) { + NanoVG.nvgGlobalAlpha(vg, 0.3f); + upArrow.disable(true); + } + RenderManager.drawRoundedRectVaried(vg, x + width + 4, y, 12, 14, colorTop, 6f, 6f, 0f, 0f); + RenderManager.drawImage(vg, Images.UP_ARROW, x + width + 5, y + 2, 10, 10); + if (current >= max) NanoVG.nvgGlobalAlpha(vg, 1f); + + if (current <= min) { + NanoVG.nvgGlobalAlpha(vg, 0.3f); + downArrow.disable(true); + } + RenderManager.drawRoundedRectVaried(vg, x + width + 4, y + 14, 12, 14, colorBottom, 0f, 0f, 6f, 6f); + NanoVG.nvgTranslate(vg, x + width + 15, y + 25); + NanoVG.nvgRotate(vg, (float) Math.toRadians(180)); + RenderManager.drawImage(vg, Images.UP_ARROW, 0, 0, 10, 10); + NanoVG.nvgResetTransform(vg); + NanoVG.nvgGlobalAlpha(vg, 1f); + + } catch (Exception e) { + super.errored = true; + } + try { + super.draw(vg, x, y - 2); + } catch (Exception e) { + setCurrentValue(current); + super.caretPos = 0; + super.prevCaret = 0; + } + } + + + public float getCurrentValue() { + return current; + } + + public void setCurrentValue(float value) { + input = String.format("%.01f", value); + } + + @Override + public void onClose() { + try { + if (current < min) current = min; + if (current > max) current = max; + setCurrentValue(current); + } catch (Exception ignored) { + + } + } + + public void setStep(float step) { + this.step = step; + } + + public void setMax(float max) { + this.max = max; + } + + public void setMin(float min) { + this.min = min; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/SearchField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/SearchField.java new file mode 100644 index 0000000..af7b6e1 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/SearchField.java @@ -0,0 +1,14 @@ +package cc.polyfrost.oneconfig.gui.elements.text; + +public class SearchField extends TextInputField { + + public SearchField(int width, int height, String defaultText, boolean multiLine, boolean password) { + super(width, height, defaultText, multiLine, password); + } + + @Override + public void draw(long vg, int x, int y) { + super.draw(vg, x, y); + // TODO + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java new file mode 100644 index 0000000..6983ae8 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java @@ -0,0 +1,425 @@ +package cc.polyfrost.oneconfig.gui.elements.text; + +import cc.polyfrost.oneconfig.config.OneConfigConfig; +import cc.polyfrost.oneconfig.gui.elements.BasicElement; +import cc.polyfrost.oneconfig.lwjgl.RenderManager; +import cc.polyfrost.oneconfig.lwjgl.scissor.Scissor; +import cc.polyfrost.oneconfig.lwjgl.scissor.ScissorManager; +import cc.polyfrost.oneconfig.lwjgl.font.Fonts; +import cc.polyfrost.oneconfig.utils.InputUtils; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.util.ChatAllowedCharacters; +import org.jetbrains.annotations.NotNull; +import org.lwjgl.input.Keyboard; +import org.lwjgl.input.Mouse; + +import java.awt.*; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; + +public class TextInputField extends BasicElement { + + protected final String defaultText; + protected String input, selectedText; + protected final boolean multiLine; + protected boolean password; + + protected int caretPos; + protected int x, y; + protected float start, end; + private long clickTimeD1; + protected long vg; + protected int prevCaret = 0; + protected boolean isDoubleClick = false; + protected boolean onlyNums = false; + protected boolean errored = false; + protected boolean centered = false; + + public TextInputField(int width, int height, String defaultText, boolean multiLine, boolean password) { + super(width, height, false); + this.multiLine = multiLine; + this.defaultText = defaultText; + this.password = password; + this.input = ""; + } + + public TextInputField(int width, int height, boolean centered, String defaultText) { + this(width, height, defaultText, false, false); + this.centered = centered; + } + + public void onlyAcceptNumbers(boolean state) { + onlyNums = state; + } + + public void setInput(String input) { + this.input = input; + } + + public String getInput() { + return input; + } + + public void setPassword(boolean password) { + this.password = password; + } + + public boolean getPassword() { + return password; + } + + public void setErrored(boolean errored) { + this.errored = errored; + } + + public void setCentered(boolean centered) { + this.centered = centered; + } + + public boolean isErrored() { + return errored; + } + + @Override + public void draw(long vg, int x, int y) { + this.x = x; + this.y = y; + this.vg = vg; + try { + Scissor scissor = ScissorManager.scissor(vg, x, y, width, height); + int colorOutline = errored ? OneConfigConfig.ERROR_700 : OneConfigConfig.GRAY_700; + RenderManager.drawHollowRoundRect(vg, x, y, width, height, colorOutline, 12f, 2f); + super.update(x, y); + if (Mouse.isButtonDown(0) && !InputUtils.isAreaHovered(x - 40, y - 20, width + 90, height + 20)) { + onClose(); + toggled = false; + } + int color = toggled ? OneConfigConfig.WHITE : OneConfigConfig.WHITE_60; + if (!toggled) caretPos = input.length(); + float width; + StringBuilder s = new StringBuilder(); + if (!password) { + width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f, Fonts.INTER_REGULAR); + } else { + for (int i = 0; i < input.length(); i++) { + s.append("*"); + } + width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f, Fonts.INTER_REGULAR); + } + if (hovered) { + while (Mouse.next()) { + if (Mouse.getEventButtonState()) { + if (Mouse.getEventButton() == 0) { + prevCaret = calculatePos(Mouse.getX()); + if (System.currentTimeMillis() - clickTimeD1 < 300) { + onDoubleClick(); + isDoubleClick = true; + } + clickTimeD1 = System.currentTimeMillis(); + } + } else { + if (Mouse.getEventButton() == 0) { + long clickTimeU = System.currentTimeMillis(); + if (clickTimeU - clickTimeD1 < 200) { + if (!isDoubleClick) { + start = 0; + end = 0; + } + prevCaret = caretPos; + isDoubleClick = false; + } + + } + } + } + } + float halfTextWidth = this.getTextWidth(vg, input) / 2f; + if (start != 0f && end != 0f && toggled) { + RenderManager.drawRect(vg, start, y + height / 2f - 10, end, 20, OneConfigConfig.GRAY_300); + } + if (hovered) { + if (Mouse.isButtonDown(0) && !isDoubleClick) { + caretPos = calculatePos(Mouse.getX()); + if (caretPos > prevCaret) { + if (!centered) start = x + 12 + this.getTextWidth(vg, input.substring(0, prevCaret)); + else + start = x + this.width / 2f - halfTextWidth + this.getTextWidth(vg, input.substring(0, prevCaret)); + end = this.getTextWidth(vg, input.substring(prevCaret, caretPos)); + selectedText = input.substring(prevCaret, caretPos); + } else { + if (!centered) start = x + 12 + this.getTextWidth(vg, input.substring(0, prevCaret)); + else + start = x + this.width / 2f - halfTextWidth + this.getTextWidth(vg, input.substring(0, prevCaret)); + end = -this.getTextWidth(vg, input.substring(caretPos, prevCaret)); + selectedText = input.substring(caretPos, prevCaret); + } + } + } + + + if (toggled) { + if (!centered) { + RenderManager.drawLine(vg, x + width + 12, (float) y + height / 2f - 10, x + width + 12, (float) y + height / 2f + 10, 1, OneConfigConfig.WHITE); + } else { + RenderManager.drawLine(vg, x + this.width / 2f - halfTextWidth + width, (float) y + height / 2f - 10, x + this.width / 2f - halfTextWidth + width, (float) y + height / 2f + 10, 1, OneConfigConfig.WHITE); + } + } + + + if (input.equals("")) { + if (!centered) { + RenderManager.drawString(vg, defaultText, x + 12, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); + } else { + RenderManager.drawString(vg, defaultText, x + this.width / 2f - halfTextWidth, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); + } + } + + if (!password) { + if (!centered) { + RenderManager.drawString(vg, input, x + 12, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); + } else { + RenderManager.drawString(vg, input, x + this.width / 2f - halfTextWidth, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); + } + } else { + RenderManager.drawString(vg, s.toString(), x + 12, y + height / 2f + 1, color, 14f, Fonts.INTER_REGULAR); + } + ScissorManager.resetScissor(vg, scissor); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void keyTyped(char c, int key) { + try { + if (toggled) { + if (GuiScreen.isKeyComboCtrlC(key)) { + if (selectedText != null && start != 0f && end != 0f) { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selectedText), null); + } + return; + } + if (GuiScreen.isKeyComboCtrlV(key) || key == Keyboard.KEY_INSERT) { + try { + String clip = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor).toString(); + input = input.substring(0, caretPos) + clip + input.substring(caretPos); + caretPos = caretPos + clip.length(); + return; + } catch (Exception e) { + e.printStackTrace(); + } + } + if (key == Keyboard.KEY_DELETE) { + input = ""; + } + + + if (GuiScreen.isCtrlKeyDown()) { + if (key == Keyboard.KEY_BACK && !GuiScreen.isKeyComboCtrlX(key)) { + try { + input = input.substring(0, input.lastIndexOf(" ")); + caretPos = input.length(); + } catch (Exception e) { + input = ""; + caretPos = 0; + } + return; + } + if (GuiScreen.isKeyComboCtrlA(key)) { + prevCaret = 0; + caretPos = input.length(); + start = !centered ? x + 12 : x + this.width / 2f - this.getTextWidth(vg, input) / 2f; + selectedText = input; + end = this.getTextWidth(vg, input); + return; + } + if (GuiScreen.isKeyComboCtrlX(key)) { + if (selectedText != null && start != 0f && end != 0f) { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(selectedText), null); + key = Keyboard.KEY_BACK; + } else return; + } + if (key == Keyboard.KEY_LEFT) { + caretPos = input.substring(0, caretPos).lastIndexOf(' ') + 1; + } + if (key == Keyboard.KEY_RIGHT) { + caretPos = input.indexOf(' ', caretPos); + if (caretPos == -1) caretPos = input.length(); + } + + } + if (key == Keyboard.KEY_BACK) { + if (input.length() > 0) { + if (start != 0f && end != 0f) { + start = 0f; + end = 0f; + if (caretPos > prevCaret) { + input = input.substring(0, prevCaret) + input.substring(caretPos); + caretPos = prevCaret; + } + if (caretPos < prevCaret) { + input = input.substring(0, caretPos) + input.substring(prevCaret); + } + return; + } + if (caretPos == input.length()) { + input = input.substring(0, input.length() - 1); + } else { + input = input.substring(0, caretPos - 1) + input.substring(caretPos); + } + caretPos--; + } + return; + } + if (key == Keyboard.KEY_TAB) { + if (onlyNums) return; + input += " "; + caretPos += 4; + return; + } + + if (key == Keyboard.KEY_RIGHT) { + caretPos++; + if (caretPos > input.length()) { + caretPos = input.length(); + } + if (start != 0f && end != 0f) { + start = 0f; + end = 0f; + } + return; + } + if (key == Keyboard.KEY_LEFT) { + caretPos--; + if (caretPos < 0) { + caretPos = 0; + } + if (start != 0f && end != 0f) { + start = 0f; + end = 0f; + } + return; + } + if (key == Keyboard.KEY_UP || key == 201) { // 201 = page up + caretPos = 0; + if (start != 0f && end != 0f) { + start = 0f; + end = 0f; + } + return; + } + if (key == Keyboard.KEY_DOWN || key == 209) { // 209 = page down + caretPos = input.length(); + if (start != 0f && end != 0f) { + start = 0f; + end = 0f; + } + return; + } + + + if (key == Keyboard.KEY_RETURN) { + onClose(); + toggled = false; + if (start != 0f && end != 0f) { + start = 0f; + end = 0f; + } + } + if (key == Keyboard.KEY_END) { + onClose(); + toggled = false; + } + + + if (key == Keyboard.KEY_LCONTROL || key == Keyboard.KEY_RCONTROL || key == Keyboard.KEY_LMENU || key == Keyboard.KEY_RMENU || key == Keyboard.KEY_LMETA || key == Keyboard.KEY_RMETA || key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT || key == Keyboard.KEY_RETURN || key == Keyboard.KEY_CAPITAL || key == 221 || key == Keyboard.KEY_HOME) { + return; + } + if (onlyNums) { + if (!Character.isDigit(c) && key != 52) return; + } + if (!Character.isDefined(key)) return; + if (!Character.isDefined(c)) return; + if (GuiScreen.isCtrlKeyDown()) return; + if (ChatAllowedCharacters.isAllowedCharacter(c)) { + if (selectedText != null) { + if (caretPos > prevCaret) { + input = input.substring(0, prevCaret) + input.substring(prevCaret, caretPos); + caretPos = prevCaret; + } else { + input = input.substring(0, caretPos) + input.substring(caretPos, prevCaret); + } + if (selectedText.equals(input)) { + input = ""; + } + selectedText = null; + } + input = addCharAtPoint(caretPos, c); + caretPos++; + } + if (start != 0f && end != 0f) { + start = 0f; + end = 0f; + } + + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private @NotNull String addCharAtPoint(int index, char c) { + return input.substring(0, index) + c + input.substring(index); + } + + @Override + public void onClick() { + toggled = true; + } + + private void onDoubleClick() { + prevCaret = input.substring(0, caretPos).lastIndexOf(' ') + 1; + caretPos = input.indexOf(' ', caretPos); + if (caretPos == -1) caretPos = input.length(); + selectedText = input.substring(prevCaret, caretPos); + if (!centered) start = x + 12 + this.getTextWidth(vg, input.substring(0, prevCaret)); + else + start = x + this.width / 2f - this.getTextWidth(vg, input) / 2f + this.getTextWidth(vg, input.substring(0, prevCaret)); + end = this.getTextWidth(vg, input.substring(prevCaret, caretPos)); + } + + private int calculatePos(int pos) { + if (centered) pos -= 12; + String s1 = ""; + int i; + for (char c : input.toCharArray()) { + if (pos - x - 12 < 0) { + return 0; + } + if (pos - x - 12 > this.getTextWidth(vg, input)) { + return input.length(); + } + s1 += c; + i = (int) this.getTextWidth(vg, s1); + if (i >= pos - x - 16) { + return s1.length(); + } + } + return 0; + } + + public void onClose() { + + } + + private float getTextWidth(long vg, String s) { + if (password) { + StringBuilder s1 = new StringBuilder(); + while (s1.length() < s.length()) { + s1.append('*'); + } + return RenderManager.getTextWidth(vg, s1.toString(), 14.0f, Fonts.INTER_REGULAR); + } else { + return RenderManager.getTextWidth(vg, s, 14.0f, Fonts.INTER_REGULAR); + } + } +} -- cgit