From 2321f9f11f0f14e4b2e1de06d093fb0246e37d3e Mon Sep 17 00:00:00 2001 From: EmmaTheMartian Date: Tue, 30 Apr 2024 13:20:53 -0400 Subject: [PATCH] Add ctrl+arrows, ctrl+backspace, and tab keybinds --- gradlew | 0 .../java/com/insertsoda/craterchat/Chat.java | 68 +++++++++++++++++++ .../mixins/UITextInputAccessor.java | 9 +++ 3 files changed, 77 insertions(+) mode change 100644 => 100755 gradlew diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/com/insertsoda/craterchat/Chat.java b/src/main/java/com/insertsoda/craterchat/Chat.java index 8691aff..f533672 100644 --- a/src/main/java/com/insertsoda/craterchat/Chat.java +++ b/src/main/java/com/insertsoda/craterchat/Chat.java @@ -2,6 +2,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; +import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; @@ -91,6 +92,46 @@ public void onCreate() { super.onCreate(); } + @Override + public boolean keyDown(int keycode) { + switch (keycode) { + case Keys.LEFT: + if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) { + var input = ((UITextInputAccessor)this); + + int pos = input.getDesiredCharIdx() - 1; + if (pos <= 0) return true; + + // Move left until a non-alphanumeric character or the start of the input + do { --pos; } + while (pos > 0 && Character.isLetterOrDigit(textInput.inputText.charAt(pos))); + + input.setDesiredCharIdx(pos); + input.setIsDefaultText(false); + return true; + } + break; + case Keys.RIGHT: + if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) { + var input = ((UITextInputAccessor)this); + + int pos = input.getDesiredCharIdx(); + if (pos >= textInput.inputText.length()) return true; + + // Move right until a non-alphanumeric character or the end of the input + do { ++pos; } + while (pos <= textInput.inputText.length() - 1 && Character.isLetterOrDigit(textInput.inputText.charAt(pos))); + + input.setDesiredCharIdx(pos); + input.setIsDefaultText(false); + return true; + } + break; + } + + return super.keyDown(keycode); + } + @Override public boolean keyTyped(char character) { // Types in the character into the text field and stores its returning boolean for later @@ -134,6 +175,33 @@ public boolean keyTyped(char character) { return false; } + if (character == '\t' && commandSuggestions != null) { + try { + inputText = "/" + commandSuggestions.get().getList().get(0).getText(); + var input = ((UITextInputAccessor)this); + input.setDesiredCharIdx(inputText.length()); + input.setIsDefaultText(false); + return true; + } catch (Exception e) {} + } + + if (character == '\b' && Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) { + var input = ((UITextInputAccessor)this); + + int pos = input.getDesiredCharIdx() - 1; + if (pos <= 0) return true; + + // Move left until a non-alphanumeric character or the start of the input + do { --pos; } + while (pos > 0 && Character.isLetterOrDigit(textInput.inputText.charAt(pos))); + + input.setDesiredCharIdx(pos); + input.setIsDefaultText(false); + // The caret has already been moved, so now we can just take a substring from 0-caret to remove a whole word + inputText = inputText.substring(0, input.getDesiredCharIdx()); + return true; + } + // Resume other operations I guess return keyTypedBoolean; } diff --git a/src/main/java/com/insertsoda/craterchat/mixins/UITextInputAccessor.java b/src/main/java/com/insertsoda/craterchat/mixins/UITextInputAccessor.java index 986827b..4f7f4b0 100644 --- a/src/main/java/com/insertsoda/craterchat/mixins/UITextInputAccessor.java +++ b/src/main/java/com/insertsoda/craterchat/mixins/UITextInputAccessor.java @@ -8,4 +8,13 @@ public interface UITextInputAccessor { @Accessor int getDesiredCharIdx(); + + @Accessor + void setDesiredCharIdx(int val); + + @Accessor + boolean getIsDefaultText(); + + @Accessor + void setIsDefaultText(boolean val); }