mirror of
https://github.com/GTNewHorizons/lwjgl3ify.git
synced 2026-11-21 04:32:30 +00:00
SDL3 Port for better keyboard&Wayland support - lwjgl3ify 3.0 (#202)
This commit is contained in:
1 parent
19bdc08297
commit
0fda85575f
59 files changed
+2729
-4100
No files matched your search
@@ -1,21 +1,26 @@
|
||||
package org.lwjglx.input;
|
||||
|
||||
import static org.lwjgl.glfw.GLFW.GLFW_KEY_UNKNOWN;
|
||||
import static org.lwjgl.sdl.SDLKeyboard.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Map;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Locale;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
import org.lwjglx.LWJGLException;
|
||||
import org.lwjgl.sdl.SDLKeycode;
|
||||
import org.lwjglx.Sys;
|
||||
import org.lwjglx.opengl.Display;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntArrayMap;
|
||||
import me.eigenraven.lwjgl3ify.Lwjgl3ify;
|
||||
import me.eigenraven.lwjgl3ify.api.InputEvents;
|
||||
import me.eigenraven.lwjgl3ify.client.MainThreadExec;
|
||||
import me.eigenraven.lwjgl3ify.core.Config;
|
||||
|
||||
public class Keyboard {
|
||||
@@ -165,7 +170,10 @@ public class Keyboard {
|
||||
|
||||
public static final int keyCount;
|
||||
|
||||
private static final Map<String, Integer> reverseKeyMap = new ConcurrentHashMap<>();
|
||||
public static ByteBuffer sdlKeyPressedArray;
|
||||
|
||||
private static final Int2ObjectArrayMap<String> keyMap = new Int2ObjectArrayMap();
|
||||
private static final Object2IntArrayMap<String> reverseKeyMap = new Object2IntArrayMap<>();
|
||||
|
||||
public enum KeyState {
|
||||
|
||||
@@ -236,7 +244,7 @@ public class Keyboard {
|
||||
event.key,
|
||||
event.state,
|
||||
event.state,
|
||||
java.awt.event.KeyEvent.getKeyText(KeyCodes.lwjglToAwt(KeyCodes.glfwToLwjgl(event.key))),
|
||||
java.awt.event.KeyEvent.getKeyText(KeyCodes.lwjglToAwt(event.key)),
|
||||
(event.key >= 32 && event.key < 127) ? (event.key) : '?');
|
||||
}
|
||||
try {
|
||||
@@ -244,14 +252,14 @@ public class Keyboard {
|
||||
} catch (IllegalStateException ignored) {}
|
||||
}
|
||||
|
||||
public static void addGlfwKeyEvent(long window, int key, int scancode, int action, int mods, int c) {
|
||||
public static void addSdlKeyEvent(int key, int scancode, InputEvents.KeyAction action, int mods, int c,
|
||||
long nanoTime) {
|
||||
final KeyState state = switch (action) {
|
||||
case GLFW.GLFW_PRESS -> KeyState.PRESS;
|
||||
case GLFW.GLFW_RELEASE -> KeyState.RELEASE;
|
||||
case GLFW.GLFW_REPEAT -> KeyState.REPEAT;
|
||||
default -> KeyState.RELEASE;
|
||||
case PRESSED -> KeyState.PRESS;
|
||||
case RELEASED -> KeyState.RELEASE;
|
||||
case REPEATED -> KeyState.REPEAT;
|
||||
};
|
||||
addRawKeyEvent(new KeyEvent(KeyCodes.glfwToLwjgl(key), c, state, Sys.getNanoTime()));
|
||||
addRawKeyEvent(new KeyEvent(KeyCodes.sdlScancodeToLwjgl(scancode), c, state, nanoTime));
|
||||
}
|
||||
|
||||
public static void addCharEvent(int key, int c) {
|
||||
@@ -263,14 +271,18 @@ public class Keyboard {
|
||||
} catch (IllegalStateException ignored) {}
|
||||
}
|
||||
|
||||
public static void create() throws LWJGLException {}
|
||||
public static void create() {}
|
||||
|
||||
public static boolean isKeyDown(int key) {
|
||||
if (key == KEY_NONE) {
|
||||
final ByteBuffer array = sdlKeyPressedArray;
|
||||
if (key == KEY_NONE || array == null) {
|
||||
return false;
|
||||
}
|
||||
final int keyCode = KeyCodes.lwjglToGlfw(key);
|
||||
return keyCode != GLFW_KEY_UNKNOWN && GLFW.glfwGetKey(Display.getWindow(), keyCode) == GLFW.GLFW_PRESS;
|
||||
final int sdlScancode = KeyCodes.lwjglToSdlScancode(key);
|
||||
if (sdlScancode <= 0 || sdlScancode >= array.limit()) {
|
||||
return false;
|
||||
}
|
||||
return array.get(sdlScancode) != 0;
|
||||
}
|
||||
|
||||
public static void poll() {
|
||||
@@ -325,32 +337,31 @@ public class Keyboard {
|
||||
return eventQueue.peek().nano;
|
||||
}
|
||||
|
||||
public static String getKeyName(int key) {
|
||||
public static synchronized String getKeyName(int key) {
|
||||
if (key == KEY_NONE) {
|
||||
return "NONE";
|
||||
}
|
||||
// GLFW caches this internally, and knows when keyboard layouts switch.
|
||||
final String glfwName = StringUtils.toRootUpperCase(GLFW.glfwGetKeyName(KeyCodes.lwjglToGlfw(key), 0));
|
||||
final String name;
|
||||
if (glfwName == null) {
|
||||
if (key >= 0 && key < unlocalizedKeyNameMiniLut.length) {
|
||||
name = unlocalizedKeyNameMiniLut[key];
|
||||
} else {
|
||||
name = "Key " + key;
|
||||
}
|
||||
} else {
|
||||
name = glfwName;
|
||||
final String cached = keyMap.get(key);
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
int sdlKey = KeyCodes.lwjglToSdlKeycode(key);
|
||||
if (sdlKey == -1 || sdlKey == SDLKeycode.SDLK_UNKNOWN) {
|
||||
return "Key " + key;
|
||||
}
|
||||
String name = Objects.firstNonNull(MainThreadExec.runOnMainThread(() -> SDL_GetKeyName(sdlKey)), "UNKNOWN")
|
||||
.toUpperCase(Locale.ROOT);
|
||||
keyMap.put(key, name);
|
||||
reverseKeyMap.put(name, key);
|
||||
return name;
|
||||
}
|
||||
|
||||
public static int getKeyIndex(String keyName) {
|
||||
public static synchronized int getKeyIndex(String keyName) {
|
||||
if (keyName.equals("NONE")) {
|
||||
return KEY_NONE;
|
||||
}
|
||||
Integer ret = reverseKeyMap.get(keyName);
|
||||
if (ret == null) {
|
||||
int ret = reverseKeyMap.getOrDefault(keyName, -1);
|
||||
if (ret == -1) {
|
||||
if (keyName.matches("Key -?[0-9]+]")) {
|
||||
return Integer.parseInt(StringUtils.removeStart(keyName, "Key "));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user