diff options
author | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-05-06 17:59:08 +0100 |
---|---|---|
committer | nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> | 2022-05-06 17:59:08 +0100 |
commit | 493ba953d0b5755c064c63b259b9368adf3a7a4f (patch) | |
tree | 083829432338ffd9d34955191476dfbc55bedef3 /src/main/java/cc/polyfrost/oneconfig/gui/elements/text | |
parent | 3c7d6a1a4d49ef40969bef2cb67825862c41407c (diff) | |
download | OneConfig-493ba953d0b5755c064c63b259b9368adf3a7a4f.tar.gz OneConfig-493ba953d0b5755c064c63b259b9368adf3a7a4f.tar.bz2 OneConfig-493ba953d0b5755c064c63b259b9368adf3a7a4f.zip |
number input field
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/gui/elements/text')
3 files changed, 558 insertions, 0 deletions
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); + } + } +} |