diff options
Diffstat (limited to 'src/main/java/io')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java | 39 |
1 files changed, 13 insertions, 26 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java index a0b05f2..70c0613 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTextField.java @@ -25,7 +25,6 @@ import io.github.cottonmc.cotton.gui.widget.data.InputResult; import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; -import java.util.Objects; import java.util.function.Consumer; import java.util.function.Predicate; @@ -70,29 +69,14 @@ public class WTextField extends WWidget { public WTextField(Text suggestion) { this.suggestion = suggestion; } - - /** - * Sets the text in this text field. - * - * @param s the new text - * @throws NullPointerException if the text is null - */ + public void setText(String s) { - Objects.requireNonNull(s, "text"); - if (this.textPredicate==null || this.textPredicate.test(s)) { - String original = this.text; this.text = (s.length()>maxLength) ? s.substring(0,maxLength) : s; - if (cursor > text.length()) cursor = text.length(); - if (!original.equals(s) && onChanged!=null) onChanged.accept(this.text); + if (onChanged!=null) onChanged.accept(this.text); } } - /** - * Gets the text in this text field. - * - * @return the text in this text field - */ public String getText() { return this.text; } @@ -125,7 +109,6 @@ public class WTextField extends WWidget { if (select==cursor) return null; //Tidy some things - // TODO: This tidying doesn't belong here if (select>text.length()) select = text.length(); if (cursor<0) cursor = 0; if (cursor>text.length()) cursor = text.length(); @@ -332,9 +315,10 @@ public class WTextField extends WWidget { if (cursor>this.text.length()) cursor = this.text.length(); String before = this.text.substring(0, cursor); - String after = this.text.substring(cursor); + String after = this.text.substring(cursor, this.text.length()); + this.text = before+ch+after; cursor++; - setText(before+ch+after); + if (onChanged != null) onChanged.accept(text); } } @@ -363,7 +347,7 @@ public class WTextField extends WWidget { String after = this.text.substring(b); String clip = MinecraftClient.getInstance().keyboard.getClipboard(); - setText(before+clip+after); + text = before+clip+after; select = -1; cursor = (before+clip).length(); } else { @@ -371,8 +355,12 @@ public class WTextField extends WWidget { String after = this.text.substring(cursor, this.text.length()); String clip = MinecraftClient.getInstance().keyboard.getClipboard(); - setText(before + clip + after); + text = before + clip + after; cursor += clip.length(); + if (text.length()>this.maxLength) { + text = text.substring(0, maxLength); + if (cursor>text.length()) cursor = text.length(); + } } if (onChanged != null) onChanged.accept(text); @@ -386,7 +374,6 @@ public class WTextField extends WWidget { //System.out.println("Ch: "+ch+", Key: "+key+", Mod: "+modifiers); if (modifiers==0) { - // TODO: Make Del work backwards as it should if (ch==GLFW.GLFW_KEY_DELETE || ch==GLFW.GLFW_KEY_BACKSPACE) { if (text.length()>0 && cursor>0) { if (select>=0 && select!=cursor) { @@ -399,7 +386,7 @@ public class WTextField extends WWidget { } String before = this.text.substring(0, a); String after = this.text.substring(b); - setText(before+after); + text = before+after; if (cursor==b) cursor = a; select = -1; } else { @@ -407,7 +394,7 @@ public class WTextField extends WWidget { String after = this.text.substring(cursor, this.text.length()); before = before.substring(0,before.length()-1); - setText(before+after); + text = before+after; cursor--; } |