Skip to content

Commit

Permalink
Merge pull request #1 from EmmaTheMartian/master
Browse files Browse the repository at this point in the history
Add ctrl+arrows, ctrl+backspace, and tab keybinds
  • Loading branch information
InsertSoda authored Apr 30, 2024
2 parents 4ca0bb1 + 2321f9f commit 4585710
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
Empty file modified gradlew
100644 → 100755
Empty file.
68 changes: 68 additions & 0 deletions src/main/java/com/insertsoda/craterchat/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,13 @@
public interface UITextInputAccessor {
@Accessor
int getDesiredCharIdx();

@Accessor
void setDesiredCharIdx(int val);

@Accessor
boolean getIsDefaultText();

@Accessor
void setIsDefaultText(boolean val);
}

0 comments on commit 4585710

Please sign in to comment.