From cc3c434d35369b9a48eda7e9da1ca4adcd44284d Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Thu, 29 Aug 2024 15:03:08 -0400 Subject: [PATCH] v2.5.0: improved text input performance regarding copy/paste --- .../java/com/watabou/noosa/TextInput.java | 41 ++++++++++++++++++- .../windows/WndTextInput.java | 27 ++++++++---- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/SPD-classes/src/main/java/com/watabou/noosa/TextInput.java b/SPD-classes/src/main/java/com/watabou/noosa/TextInput.java index 4654a9490..aa5bdc8ee 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/TextInput.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/TextInput.java @@ -76,7 +76,35 @@ public class TextInput extends Component { TextField.TextFieldStyle style = skin.get(TextField.TextFieldStyle.class); style.font = Game.platform.getFont(size, "", false, false); style.background = null; - textField = multiline ? new TextArea("", style) : new TextField("", style); + if (multiline){ + textField = new TextArea("", style){ + @Override + public void cut() { + super.cut(); + onClipBoardUpdate(); + } + + @Override + public void copy() { + super.copy(); + onClipBoardUpdate(); + } + }; + } else { + textField = new TextField("", style){ + @Override + public void cut() { + super.cut(); + onClipBoardUpdate(); + } + + @Override + public void copy() { + super.copy(); + onClipBoardUpdate(); + } + }; + } textField.setProgrammaticChangeEvents(true); if (!multiline) textField.setAlignment(Align.center); @@ -90,6 +118,7 @@ public class TextInput extends Component { style.font = f; textField.setStyle(style); } + onChanged(); } }); @@ -117,9 +146,17 @@ public class TextInput extends Component { } public void enterPressed(){ - //do nothing by default + //fires any time enter is pressed, do nothing by default }; + public void onChanged(){ + //fires any time the text box is changed, do nothing by default + } + + public void onClipBoardUpdate(){ + //fires any time the clipboard is updated via cut or copy, do nothing by default + } + public void setText(String text){ textField.setText(text); textField.setCursorPosition(textField.getText().length()); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndTextInput.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndTextInput.java index 16e3352f1..adef26f3c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndTextInput.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndTextInput.java @@ -90,6 +90,18 @@ public class WndTextInput extends Window { onSelect(true, getText()); hide(); } + + @Override + public void onChanged() { + super.onChanged(); + if (btnCopy != null) btnCopy.enable(!getText().isEmpty()); + } + + @Override + public void onClipBoardUpdate() { + super.onClipBoardUpdate(); + btnPaste.enable(Gdx.app.getClipboard().hasContents()); + } }; if (initialValue != null) textBox.setText(initialValue); textBox.setMaxLength(maxLength); @@ -127,6 +139,7 @@ public class WndTextInput extends Window { } }; btnCopy.icon(Icons.COPY.get()); + btnCopy.enable(!textBox.getText().isEmpty()); add(btnCopy); btnPaste = new RedButton(""){ @@ -145,11 +158,16 @@ public class WndTextInput extends Window { @Override protected void onClick() { super.onClick(); - textBox.pasteFromClipboard(); + if (Gdx.app.getClipboard().hasContents()) { + textBox.pasteFromClipboard(); + } else { + enable(false); + } } }; btnPaste.icon(Icons.PASTE.get()); + btnPaste.enable(Gdx.app.getClipboard().hasContents()); add(btnPaste); btnCopy.setRect(textBoxWidth + 2*MARGIN, pos, BUTTON_HEIGHT, BUTTON_HEIGHT); @@ -200,13 +218,6 @@ public class WndTextInput extends Window { } - @Override - public synchronized void update() { - super.update(); - btnCopy.enable(!textBox.getText().isEmpty()); - btnPaste.enable(Gdx.app.getClipboard().hasContents()); - } - @Override public void offset(int xOffset, int yOffset) { super.offset(xOffset, yOffset);