diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java index dd069183699..0eb4fd524c7 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WDoubleEdit.java @@ -91,21 +91,27 @@ public void init() { } } + private String insertAtCursor(String text, char c) { + int cursor = Math.min(textBox.selectionStart, textBox.selectionEnd); + cursor = Math.clamp(cursor, 0, text.length()); + return text.substring(0, cursor) + c + text.substring(cursor); + } + private boolean filter(String text, char c) { boolean good; boolean validate = true; - if (c == '-' && !text.contains("-") && textBox.cursor == 0) { + if (c == '-' && !text.contains("-") && Math.min(textBox.selectionStart, textBox.selectionEnd) == 0) { good = true; validate = false; } else if (c == '.' && !text.contains(".")) { good = true; - if (text.isEmpty()) validate = false; + if (text.isEmpty() || text.equals("-")) validate = false; } else good = Character.isDigit(c); if (good && validate) { try { - Double.parseDouble(text + c); + Double.parseDouble(insertAtCursor(text, c)); } catch (NumberFormatException _) { good = false; } @@ -143,4 +149,4 @@ public void set(double value) { private String valueString() { return String.format(Locale.US, "%." + decimalPlaces + "f", value); } -} +} \ No newline at end of file diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java index c6231b16193..e6732e0299e 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WIntEdit.java @@ -78,11 +78,17 @@ public void init() { } } + private String insertAtCursor(String text, char c) { + int cursor = Math.min(textBox.selectionStart, textBox.selectionEnd); + cursor = Math.clamp(cursor, 0, text.length()); + return text.substring(0, cursor) + c + text.substring(cursor); + } + private boolean filter(String text, char c) { boolean good; boolean validate = true; - if (c == '-' && !text.contains("-") && textBox.cursor == 0) { + if (c == '-' && !text.contains("-") && Math.min(textBox.selectionStart, textBox.selectionEnd) == 0) { good = true; validate = false; } @@ -90,7 +96,7 @@ private boolean filter(String text, char c) { if (good && validate) { try { - Integer.parseInt(text + c); + Integer.parseInt(insertAtCursor(text, c)); } catch (NumberFormatException _) { good = false; } diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java index b1f0b283101..51d3aa61fe6 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java @@ -50,11 +50,17 @@ public void init() { }; } + private String insertAtCursor(String text, char c) { + int cursor = Math.min(textBox.selectionStart, textBox.selectionEnd); + cursor = Math.clamp(cursor, 0, text.length()); + return text.substring(0, cursor) + c + text.substring(cursor); + } + private boolean filter(String text, char c) { boolean good; boolean validate = true; - if (c == '-' && !text.contains("-") && textBox.cursor == 0) { + if (c == '-' && !text.contains("-") && Math.min(textBox.selectionStart, textBox.selectionEnd) == 0) { good = true; validate = false; } else { @@ -63,7 +69,7 @@ private boolean filter(String text, char c) { if (good && validate) { try { - Long.parseLong(text + c); + Long.parseLong(insertAtCursor(text, c)); } catch (NumberFormatException _) { good = false; } diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java index 318c45f8643..cc3b4ef245b 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WTextBox.java @@ -559,19 +559,28 @@ private int getSelectedCompletion() { public boolean onCharTyped(CharacterEvent input) { if (!focused) return false; - if (filter.filter(text, input.codepoint())) { - clearSelection(); + int start = Math.min(selectionStart, selectionEnd); + int end = Math.max(selectionStart, selectionEnd); - text = text.substring(0, cursor) + input.codepointAsString() + text.substring(cursor); + String newText = text; + if (start != end) { + newText = text.substring(0, start) + text.substring(end); + } - cursor++; - resetSelection(); + if (!filter.filter(newText, input.codepoint())) return false; - runAction(); - return true; - } + String inserted = input.codepointAsString(); - return false; + text = newText.substring(0, start) + + inserted + + newText.substring(start); + + cursor = start + inserted.length(); + + resetSelection(); + runAction(); + + return true; } @Override