From 56c4d3e7f1f60e3cb8cd83c997c5d721f34888f1 Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Tue, 18 Mar 2025 03:02:42 +0200 Subject: [PATCH] More GuiNpcTextField tweaks --- .../npcs/client/gui/util/GuiNpcTextField.java | 30 +++++++++-- .../util/animation/panel/PanelTextBox.java | 54 +++++++++++++++++-- src/main/java/noppes/npcs/util/ValueUtil.java | 8 +++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextField.java b/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextField.java index 7c96de033..5adba65af 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextField.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiNpcTextField.java @@ -4,6 +4,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiTextField; +import noppes.npcs.util.ValueUtil; public class GuiNpcTextField extends GuiTextField { public boolean enabled = true; @@ -19,6 +20,8 @@ public class GuiNpcTextField extends GuiTextField { protected static GuiNpcTextField activeTextfield = null; public boolean canEdit = true; + private String floatingPrecision = "%.4f"; //4 decimal places + private final int[] allowedSpecialChars = {0, 1, 3, 14, 8, 22, 24, 199, 211, 203, 205, 207, 211}; public GuiNpcTextField(int id, GuiScreen parent, FontRenderer fontRenderer, int i, int j, int k, int l, String s) { @@ -47,7 +50,7 @@ public class GuiNpcTextField extends GuiTextField { String s = getText(); boolean isNumbersOnly = integersOnly || floatsOnly || doublesOnly; boolean isDecimal = !integersOnly && c == '.' && !s.contains("."); - boolean isNegative = isNumbersOnly && c == '-' && !s.contains("-") && getCursorPosition() == 0; + boolean isNegative = isNumbersOnly && c == '-' && (getSelectionEnd() == 0 || !s.contains("-") && getCursorPosition() == 0); if (!isNumbersOnly || isNegative || isDecimal || Character.isDigit(c)) return true; @@ -120,7 +123,7 @@ public class GuiNpcTextField extends GuiTextField { activeTextfield = this; } - public void unFocused() { + public void toDefaults() { if (integersOnly && !doublesOnly && !floatsOnly) { if (isEmpty() || !isInteger()) setText(def + ""); @@ -129,6 +132,9 @@ public class GuiNpcTextField extends GuiTextField { else if (getInteger() > max) setText(max + ""); } else if (doublesOnly && !floatsOnly) { + if (floatingPrecision != null) + setText(ValueUtil.format(floatingPrecision, getDouble())); + if (isEmpty() || !isDouble()) setText(defDouble + ""); else if (getDouble() < minDouble) @@ -136,6 +142,9 @@ public class GuiNpcTextField extends GuiTextField { else if (getDouble() > maxDouble) setText(maxDouble + ""); } else if (floatsOnly) { + if (floatingPrecision != null) + setText(ValueUtil.format(floatingPrecision, getFloat())); + if (isEmpty() || !isFloat()) setText(defFloat + ""); else if (getFloat() < minFloat) @@ -143,6 +152,10 @@ public class GuiNpcTextField extends GuiTextField { else if (getFloat() > maxFloat) setText(maxFloat + ""); } + } + + public void unFocused() { + toDefaults(); setCursorPositionZero(); if (listener != null) listener.unFocused(this); @@ -157,22 +170,25 @@ public class GuiNpcTextField extends GuiTextField { super.drawTextBox(); } - public void setMinMaxDefault(int min, int max, int def) { + public GuiNpcTextField setMinMaxDefault(int min, int max, int def) { this.min = min; this.max = max; this.def = def; + return this; } - public void setMinMaxDefaultDouble(double min, double max, double def) { + public GuiNpcTextField setMinMaxDefaultDouble(double min, double max, double def) { minDouble = min; maxDouble = max; defDouble = def; + return this; } - public void setMinMaxDefaultFloat(float min, float max, float def) { + public GuiNpcTextField setMinMaxDefaultFloat(float min, float max, float def) { minFloat = min; maxFloat = max; defFloat = def; + return this; } public static void unfocus() { @@ -202,5 +218,9 @@ public class GuiNpcTextField extends GuiTextField { return this; } + public void setFloatingPrecision(int decimalPlaces) { + floatingPrecision = "%." + decimalPlaces + "f"; + } + } diff --git a/src/main/java/noppes/npcs/client/gui/util/animation/panel/PanelTextBox.java b/src/main/java/noppes/npcs/client/gui/util/animation/panel/PanelTextBox.java index b2a54178f..f2612738d 100644 --- a/src/main/java/noppes/npcs/client/gui/util/animation/panel/PanelTextBox.java +++ b/src/main/java/noppes/npcs/client/gui/util/animation/panel/PanelTextBox.java @@ -6,6 +6,7 @@ import noppes.npcs.client.gui.util.GuiNpcTextField; import noppes.npcs.client.gui.util.GuiUtil; import noppes.npcs.client.gui.util.animation.OverlayKeyPresetViewer; import noppes.npcs.client.utils.Color; +import org.lwjgl.input.Mouse; import org.lwjgl.opengl.GL11; public class PanelTextBox { @@ -15,6 +16,8 @@ public class PanelTextBox { public GuiNpcTextField text; public Color boxColor; + private boolean isEditing; + private FontRenderer font; public PanelTextBox() { @@ -22,9 +25,20 @@ public class PanelTextBox { text.setEnableBackgroundDrawing(false); } - public void draw(FontRenderer font) { + public PanelTextBox setDoublesOnly(double def, double min, double max) { + text.setDoublesOnly(); + text.setMinMaxDefaultDouble(min, max, def); + text.setFloatingPrecision(2); + return this; + } + + public void draw(int mouseX, FontRenderer font) { if (this.font == null) this.font = font; + + if (Mouse.isButtonDown(0) && isEditing && text.isFocused()) + setCursorPositionToMouse(mouseX); + //////////////////////////////////////////// //////////////////////////////////////////// //Box texture @@ -35,7 +49,7 @@ public class PanelTextBox { //Box grey background new Color(0x3d3d3d, 1).glColor(); - GuiUtil.drawTexturedModalRect(screenX / boxScaleX, screenY / boxScaleY, 32, 20, 0, 492); +// GuiUtil.drawTexturedModalRect(screenX / boxScaleX, screenY / boxScaleY, 32, 20, 0, 492); //Box color if (boxColor != null) @@ -51,7 +65,7 @@ public class PanelTextBox { //////////////////////////////////////////// // Box text float textWidth = font.getStringWidth(text.getText()); - float textX = screenX + 1; + float textX = screenX + 2; float centeredTextX = (screenX) + boxWidth / 2 - textWidth / 2 * textScale; textScreenX = text.isFocused() || textWidth * textScale >= boxWidth ? textX : centeredTextX; @@ -74,4 +88,38 @@ public class PanelTextBox { text.setCursorPosition(font.trimStringToWidth(text.getText(), (int) (relativeMX / textScale)).length() + text.lineScrollOffset + pos); } + + public boolean isEditing() { + return isEditing; + } + + public static byte ENTER_KEY = 0, CLICK_OUT = 1; + + public void cancelEdit(byte operation) { + text.toDefaults(); + text.setFocused(false); + isEditing = false; + } + + public void type(char c, int i) { + if (!isEditing()) + return; + + if (i == 28) { + cancelEdit(ENTER_KEY); + } else + text.textboxKeyTyped(c, i); + } + + public void click(int mouseX, int mouseY, int button) { + if (!isMouseAbove(mouseX, mouseY)) { + if (isEditing()) + cancelEdit(CLICK_OUT); + } else { + text.setFocused(true); + text.setCursorPositionEnd(); + isEditing = true; + } + } + } diff --git a/src/main/java/noppes/npcs/util/ValueUtil.java b/src/main/java/noppes/npcs/util/ValueUtil.java index 55766e174..f6fb8cb48 100644 --- a/src/main/java/noppes/npcs/util/ValueUtil.java +++ b/src/main/java/noppes/npcs/util/ValueUtil.java @@ -60,4 +60,12 @@ public class ValueUtil { public static String format(double value) { return (value % 1 == 0) ? String.format("%.0f", value) : String.format("%.2f", value); } + + public static String format(String precision, float value) { + return (value % 1 == 0) ? String.format("%.0f", value) : String.format(precision, value); + } + + public static String format(String precision, double value) { + return (value % 1 == 0) ? String.format("%.0f", value) : String.format(precision, value); + } }