mirror of
https://github.com/GTNewHorizons/lwjgl3ify.git
synced 2026-11-21 04:32:30 +00:00
[WIP] IME support (#43)
* Reapply "Rewrite Keyboard to behave like lwjgl3 (#42)"
This reverts commit 0713491dd5.
* Everthing works now
* Add char mappings for Enter, Escape, Tab
Closes #44
---------
Co-authored-by: geeky_kappa <[email protected]>
This commit is contained in:
1 parent
0713491dd5
commit
7b925da85c
3 files changed
+74
-132
No files matched your search
@@ -2,13 +2,10 @@ package org.lwjglx.input;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
import me.eigenraven.lwjgl3ify.core.Config;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
@@ -163,10 +160,7 @@ public class Keyboard {
|
||||
|
||||
public static final int keyCount;
|
||||
|
||||
private static EventQueue queue = new EventQueue(128);
|
||||
private static BlockingQueue<Character> imeCharQueue = new ArrayBlockingQueue<>(128);
|
||||
|
||||
private enum KeyState {
|
||||
public enum KeyState {
|
||||
|
||||
PRESS(true),
|
||||
RELEASE(false),
|
||||
@@ -181,19 +175,8 @@ public class Keyboard {
|
||||
|
||||
private static boolean doRepeatEvents = true;
|
||||
|
||||
private static int[] keyEvents = new int[queue.getMaxEvents()];
|
||||
private static char[] keySpecificChars = new char[queue.getMaxEvents()];
|
||||
private static KeyState[] keyEventStates = new KeyState[queue.getMaxEvents()];
|
||||
|
||||
static {
|
||||
Arrays.fill(keyEventStates, KeyState.RELEASE);
|
||||
}
|
||||
|
||||
private static long[] nanoTimeEvents = new long[queue.getMaxEvents()];
|
||||
private static char[] keyEventChars = new char[Short.MAX_VALUE];
|
||||
|
||||
public static final int KEYBOARD_SIZE = Short.MAX_VALUE;
|
||||
|
||||
public static Queue<KeyEvent> eventQueue = new ArrayBlockingQueue<>(256);
|
||||
private static final String[] keyName = new String[Short.MAX_VALUE];
|
||||
private static final Map<String, Integer> keyMap = new HashMap<>(Short.MAX_VALUE);
|
||||
|
||||
@@ -223,13 +206,10 @@ public class Keyboard {
|
||||
}
|
||||
keyMap.put(keyName[i], i);
|
||||
}
|
||||
for (int key = 32; key < 128; key++) {
|
||||
keyEventChars[KeyCodes.glfwToLwjgl(key)] = (char) key;
|
||||
}
|
||||
keyEventChars[KEY_NONE] = '\0';
|
||||
eventQueue.add(new KeyEvent(0, '\0', KeyState.RELEASE, Sys.getNanoTime()));
|
||||
}
|
||||
|
||||
public static void addGlfwKeyEvent(long window, int key, int scancode, int action, int mods) {
|
||||
public static void addGlfwKeyEvent(long window, int key, int scancode, int action, int mods, char c) {
|
||||
final KeyState state;
|
||||
switch (action) {
|
||||
case GLFW.GLFW_PRESS -> state = KeyState.PRESS;
|
||||
@@ -242,52 +222,21 @@ public class Keyboard {
|
||||
}
|
||||
default -> state = KeyState.RELEASE;
|
||||
}
|
||||
final int nextPos = queue.getNextPos();
|
||||
keyEvents[nextPos] = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventStates[nextPos] = state;
|
||||
nanoTimeEvents[nextPos] = Sys.getNanoTime();
|
||||
keySpecificChars[nextPos] = '\0';
|
||||
|
||||
queue.add();
|
||||
try {
|
||||
eventQueue.add(new KeyEvent(KeyCodes.glfwToLwjgl(key), c, state, Sys.getNanoTime()));
|
||||
} catch (IllegalStateException ignored) {}
|
||||
}
|
||||
|
||||
public static void addKeyEvent(int key, boolean pressed) {
|
||||
final int nextPos = queue.getNextPos();
|
||||
keyEvents[nextPos] = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventStates[nextPos] = pressed ? KeyState.PRESS : KeyState.RELEASE;
|
||||
nanoTimeEvents[nextPos] = Sys.getNanoTime();
|
||||
keySpecificChars[nextPos] = '\0';
|
||||
|
||||
queue.add();
|
||||
}
|
||||
|
||||
private static void duplicateKeyEvent() {
|
||||
final int nextPos = queue.getNextPos();
|
||||
final int prevPos = queue.getLastWrittenPos();
|
||||
keyEvents[nextPos] = keyEvents[prevPos];
|
||||
keyEventStates[nextPos] = keyEventStates[prevPos];
|
||||
nanoTimeEvents[nextPos] = nanoTimeEvents[prevPos];
|
||||
keySpecificChars[nextPos] = keySpecificChars[prevPos];
|
||||
queue.add();
|
||||
try {
|
||||
eventQueue.add(new KeyEvent(key, '\0', pressed ? KeyState.PRESS : KeyState.RELEASE, Sys.getNanoTime()));
|
||||
} catch (IllegalStateException ignored) {}
|
||||
}
|
||||
|
||||
public static void addCharEvent(int key, char c) {
|
||||
final int nextPos = queue.getNextPos();
|
||||
final int prevPos = queue.getLastWrittenPos();
|
||||
int index = KeyCodes.glfwToLwjgl(key);
|
||||
keyEventChars[index] = c;
|
||||
if (Config.MBE_ENABLED) {
|
||||
if (keySpecificChars[prevPos] == '\0') {
|
||||
keySpecificChars[prevPos] = c;
|
||||
} else {
|
||||
duplicateKeyEvent();
|
||||
keySpecificChars[nextPos] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void addIMECharEvent(char c) {
|
||||
imeCharQueue.offer(c);
|
||||
try {
|
||||
eventQueue.add(new KeyEvent(KEY_NONE, c, KeyState.PRESS, Sys.getNanoTime()));
|
||||
} catch (IllegalStateException ignored) {}
|
||||
}
|
||||
|
||||
public static void create() throws LWJGLException {}
|
||||
@@ -313,41 +262,36 @@ public class Keyboard {
|
||||
}
|
||||
|
||||
public static int getNumKeyboardEvents() {
|
||||
return queue.getEventCount();
|
||||
return eventQueue.size();
|
||||
}
|
||||
|
||||
public static boolean isRepeatEvent() {
|
||||
return keyEventStates[queue.getCurrentPos()] == KeyState.REPEAT;
|
||||
return eventQueue.peek().state == KeyState.REPEAT;
|
||||
}
|
||||
|
||||
public static boolean next() {
|
||||
return queue.next();
|
||||
boolean next = eventQueue.size() > 1;
|
||||
if (next) {
|
||||
eventQueue.remove();
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
public static int getEventKey() {
|
||||
return keyEvents[queue.getCurrentPos()];
|
||||
return eventQueue.peek().key;
|
||||
}
|
||||
|
||||
public static char getEventCharacter() {
|
||||
if (!imeCharQueue.isEmpty() && Display.imeOn) {
|
||||
return imeCharQueue.remove();
|
||||
}
|
||||
final int eventKey = getEventKey();
|
||||
final char eventSpecificChar = keySpecificChars[queue.getCurrentPos()];
|
||||
// On some systems it seems esc and backspace can generate broken chars sometimes, make sure they always work
|
||||
return switch (eventKey) {
|
||||
case KEY_ESCAPE -> '\0';
|
||||
case KEY_BACK -> '\b';
|
||||
default -> (eventSpecificChar != '\0') ? eventSpecificChar : keyEventChars[eventKey];
|
||||
};
|
||||
return eventQueue.peek().aChar;
|
||||
|
||||
}
|
||||
|
||||
public static boolean getEventKeyState() {
|
||||
return keyEventStates[queue.getCurrentPos()].isPressed || (!imeCharQueue.isEmpty() && Display.imeOn);
|
||||
return eventQueue.peek().state.isPressed;
|
||||
}
|
||||
|
||||
public static long getEventNanoseconds() {
|
||||
return nanoTimeEvents[queue.getCurrentPos()];
|
||||
return eventQueue.peek().nano;
|
||||
}
|
||||
|
||||
public static String getKeyName(int key) {
|
||||
@@ -373,4 +317,19 @@ public class Keyboard {
|
||||
}
|
||||
|
||||
public static void destroy() {}
|
||||
|
||||
public static class KeyEvent {
|
||||
|
||||
public int key;
|
||||
public char aChar;
|
||||
public KeyState state;
|
||||
public long nano;
|
||||
|
||||
public KeyEvent(int key, char aChar, KeyState state, long nano) {
|
||||
this.key = key;
|
||||
this.aChar = aChar;
|
||||
this.state = state;
|
||||
this.nano = nano;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user