From 0caf508535edeec7ae7bb59d08b1ebae0c84df36 Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 17 May 2024 12:50:12 +0200 Subject: [PATCH] Finished Keyboard Device, Started Joystick Support --- .../{divices => devices}/AbstractDevice.java | 7 +- .../{divices => devices}/InputDevice.java | 2 +- .../rendering/input/devices/Joystick.java | 37 ++++++++++ .../rendering/input/devices/Keyboard.java | 74 +++++++++++++++++++ .../input/{divices => devices}/Mouse.java | 6 +- .../rendering/input/divices/Keyboard.java | 51 ------------- .../rendering/input/events/KeyEvent.java | 43 +++++++++++ .../rendering/input/window/WindowManager.java | 2 +- 8 files changed, 165 insertions(+), 57 deletions(-) rename src/main/java/speiger/src/coreengine/rendering/input/{divices => devices}/AbstractDevice.java (91%) rename src/main/java/speiger/src/coreengine/rendering/input/{divices => devices}/InputDevice.java (79%) create mode 100644 src/main/java/speiger/src/coreengine/rendering/input/devices/Joystick.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/input/devices/Keyboard.java rename src/main/java/speiger/src/coreengine/rendering/input/{divices => devices}/Mouse.java (95%) delete mode 100644 src/main/java/speiger/src/coreengine/rendering/input/divices/Keyboard.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/input/events/KeyEvent.java diff --git a/src/main/java/speiger/src/coreengine/rendering/input/divices/AbstractDevice.java b/src/main/java/speiger/src/coreengine/rendering/input/devices/AbstractDevice.java similarity index 91% rename from src/main/java/speiger/src/coreengine/rendering/input/divices/AbstractDevice.java rename to src/main/java/speiger/src/coreengine/rendering/input/devices/AbstractDevice.java index 03505f1..ea188b7 100644 --- a/src/main/java/speiger/src/coreengine/rendering/input/divices/AbstractDevice.java +++ b/src/main/java/speiger/src/coreengine/rendering/input/devices/AbstractDevice.java @@ -1,9 +1,10 @@ -package speiger.src.coreengine.rendering.input.divices; +package speiger.src.coreengine.rendering.input.devices; import java.util.Deque; import java.util.Objects; import java.util.concurrent.ConcurrentLinkedDeque; +import speiger.src.collections.longs.collections.LongIterable; import speiger.src.collections.longs.maps.impl.concurrent.Long2ObjectConcurrentOpenHashMap; import speiger.src.collections.longs.maps.interfaces.Long2ObjectMap; import speiger.src.coreengine.rendering.input.window.IWindowListener.Reason; @@ -20,6 +21,10 @@ public abstract class AbstractDevice implements InputDevice { this.bus = bus; } + protected LongIterable knownWindows() { + return queues.keySet(); + } + public void register(Window window) { registerCallbacks(window); long id = window.id(); diff --git a/src/main/java/speiger/src/coreengine/rendering/input/divices/InputDevice.java b/src/main/java/speiger/src/coreengine/rendering/input/devices/InputDevice.java similarity index 79% rename from src/main/java/speiger/src/coreengine/rendering/input/divices/InputDevice.java rename to src/main/java/speiger/src/coreengine/rendering/input/devices/InputDevice.java index 1f490f7..e8b3562 100644 --- a/src/main/java/speiger/src/coreengine/rendering/input/divices/InputDevice.java +++ b/src/main/java/speiger/src/coreengine/rendering/input/devices/InputDevice.java @@ -1,4 +1,4 @@ -package speiger.src.coreengine.rendering.input.divices; +package speiger.src.coreengine.rendering.input.devices; import speiger.src.coreengine.rendering.input.window.Window; diff --git a/src/main/java/speiger/src/coreengine/rendering/input/devices/Joystick.java b/src/main/java/speiger/src/coreengine/rendering/input/devices/Joystick.java new file mode 100644 index 0000000..1891abf --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/input/devices/Joystick.java @@ -0,0 +1,37 @@ +package speiger.src.coreengine.rendering.input.devices; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWGamepadState; + +import speiger.src.coreengine.rendering.input.devices.Joystick.JoyStickData; +import speiger.src.coreengine.rendering.input.devices.Joystick.JoyStickTask; +import speiger.src.coreengine.rendering.input.window.Window; + +public class Joystick extends AbstractDevice { + + @Override + protected void registerCallbacks(Window window) { + super.registerCallbacks(window); + } + + @Override + public void reset(long windowId) { + + } + @Override + protected JoyStickData createData(long windowId) { + return new JoyStickData(); + } + @Override + protected void process(JoyStickTask task) { + + } + + public static class JoyStickData { + + } + + public static interface JoyStickTask { + + } +} diff --git a/src/main/java/speiger/src/coreengine/rendering/input/devices/Keyboard.java b/src/main/java/speiger/src/coreengine/rendering/input/devices/Keyboard.java new file mode 100644 index 0000000..c885a3d --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/input/devices/Keyboard.java @@ -0,0 +1,74 @@ +package speiger.src.coreengine.rendering.input.devices; + +import java.util.function.BiPredicate; + +import org.lwjgl.glfw.GLFW; + +import speiger.src.coreengine.rendering.input.devices.Keyboard.KeyData; +import speiger.src.coreengine.rendering.input.devices.Keyboard.KeyTask; +import speiger.src.coreengine.rendering.input.events.KeyEvent; +import speiger.src.coreengine.rendering.input.events.KeyEvent.Key; +import speiger.src.coreengine.rendering.input.window.Window; + +public class Keyboard extends AbstractDevice { + @Override + public void reset(long windowId) { get(windowId).reset(); } + @Override + protected KeyData createData(long windowId) { return new KeyData(); } + @Override + protected void process(KeyTask task) { task.process(this); } + + @Override + protected void registerCallbacks(Window window) { + super.registerCallbacks(window); + window.addCallback(this::keyPress, GLFW::glfwSetKeyCallback); + window.addCallback(this::charTyped, GLFW::glfwSetCharCallback); + } + + private void keyPress(long window, int key, int scancode, int action, int mods) { push(window, new KeyPressed(window, key, scancode, action, mods)); } + private void charTyped(long window, int codepoint) { push(window, new CharTyped(window, codepoint)); } + + public static class KeyData { + BiPredicate filter; + boolean[] pressedKeys = new boolean[350]; + boolean[] nonConsumedKeys = new boolean[350]; + + void reset() { + filter = null; + } + } + + public static interface KeyTask { + public void process(Keyboard board); + } + + public record KeyPressed(long window, int key, int scancode, int action, int mods) implements KeyTask { + @Override + public void process(Keyboard board) { + if(key < 0 || key >= 350) return; + KeyData data = board.get(window); + if(action >= 1) { + data.pressedKeys[key] = true; + onKeyPressed(board, data); + return; + } + data.pressedKeys[key] = false; + data.nonConsumedKeys[key] = false; + board.pushEvent(new Key(window, key, scancode, false)); + } + + private void onKeyPressed(Keyboard board, KeyData data) { + if(data.filter == null || data.filter.test(key, board)) { + if(board.pushEvent(new Key(window, key, scancode, true))) return; + data.nonConsumedKeys[key] = true; + } + } + } + + public record CharTyped(long window, int codepoint) implements KeyTask { + @Override + public void process(Keyboard board) { + board.pushEvent(new KeyEvent.Char(window, codepoint)); + } + } +} \ No newline at end of file diff --git a/src/main/java/speiger/src/coreengine/rendering/input/divices/Mouse.java b/src/main/java/speiger/src/coreengine/rendering/input/devices/Mouse.java similarity index 95% rename from src/main/java/speiger/src/coreengine/rendering/input/divices/Mouse.java rename to src/main/java/speiger/src/coreengine/rendering/input/devices/Mouse.java index 1f3bb4e..8fc002e 100644 --- a/src/main/java/speiger/src/coreengine/rendering/input/divices/Mouse.java +++ b/src/main/java/speiger/src/coreengine/rendering/input/devices/Mouse.java @@ -1,12 +1,12 @@ -package speiger.src.coreengine.rendering.input.divices; +package speiger.src.coreengine.rendering.input.devices; import org.lwjgl.glfw.GLFW; import speiger.src.collections.ints.sets.IntOpenHashSet; import speiger.src.collections.ints.sets.IntSet; import speiger.src.coreengine.math.vector.ints.Vec2i; -import speiger.src.coreengine.rendering.input.divices.Mouse.MouseData; -import speiger.src.coreengine.rendering.input.divices.Mouse.MouseTask; +import speiger.src.coreengine.rendering.input.devices.Mouse.MouseData; +import speiger.src.coreengine.rendering.input.devices.Mouse.MouseTask; import speiger.src.coreengine.rendering.input.events.MouseEvent; import speiger.src.coreengine.rendering.input.window.Window; diff --git a/src/main/java/speiger/src/coreengine/rendering/input/divices/Keyboard.java b/src/main/java/speiger/src/coreengine/rendering/input/divices/Keyboard.java deleted file mode 100644 index 0b1b3de..0000000 --- a/src/main/java/speiger/src/coreengine/rendering/input/divices/Keyboard.java +++ /dev/null @@ -1,51 +0,0 @@ -package speiger.src.coreengine.rendering.input.divices; - -import java.util.function.BiPredicate; - -import org.lwjgl.glfw.GLFW; - -import speiger.src.coreengine.rendering.input.divices.Keyboard.KeyTask; -import speiger.src.coreengine.rendering.input.divices.Keyboard.KeyboardData; -import speiger.src.coreengine.rendering.input.window.Window; - - -public class Keyboard extends AbstractDevice { - @Override - public void reset(long windowId) {} - @Override - protected KeyboardData createData(long windowId) { return new KeyboardData(); } - @Override - protected void process(KeyTask task) {} - - @Override - protected void registerCallbacks(Window window) { - super.registerCallbacks(window); - window.addCallback(this::keyPress, GLFW::glfwSetKeyCallback); - window.addCallback(this::charTyped, GLFW::glfwSetCharCallback); - } - - private void keyPress(long window, int key, int scancode, int action, int mods) { push(window, new KeyPressed(window, key, scancode, action, mods)); } - - private void charTyped(long window, int codepoint) { push(window, new CharTyped(window, codepoint)); } - - public static class KeyboardData { - BiPredicate filter; - boolean[] pressedKeys = new boolean[350]; - boolean[] nonConsumedKeys = new boolean[350]; - - } - - public static interface KeyTask { - public void process(Keyboard board); - } - - public record KeyPressed(long window, int key, int scancode, int action, int mods) implements KeyTask { - @Override - public void process(Keyboard board) {} - } - - public record CharTyped(long window, int codepoint) implements KeyTask { - @Override - public void process(Keyboard board) {} - } -} diff --git a/src/main/java/speiger/src/coreengine/rendering/input/events/KeyEvent.java b/src/main/java/speiger/src/coreengine/rendering/input/events/KeyEvent.java new file mode 100644 index 0000000..ad9a10f --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/input/events/KeyEvent.java @@ -0,0 +1,43 @@ +package speiger.src.coreengine.rendering.input.events; + +import speiger.src.coreengine.utils.eventbus.Event; + +public abstract class KeyEvent extends Event { + final long window; + + public KeyEvent(long window) { + this.window = window; + } + + @Override + public boolean isCancelable() { return true; } + public long window() { return window; } + + public static class Key extends KeyEvent { + final int key; + final int scancode; + final boolean press; + + public Key(long window, int key, int scancode, boolean press) { + super(window); + this.key = key; + this.scancode = scancode; + this.press = press; + } + + public int key() { return key; } + public int scancode() { return scancode; } + public boolean press() { return press; } + } + + public static class Char extends KeyEvent { + final int codepoint; + + public Char(long window, int codepoint) { + super(window); + this.codepoint = codepoint; + } + + public int codepoint() { return codepoint; } + } +} diff --git a/src/main/java/speiger/src/coreengine/rendering/input/window/WindowManager.java b/src/main/java/speiger/src/coreengine/rendering/input/window/WindowManager.java index 5ccbaea..ae96f87 100644 --- a/src/main/java/speiger/src/coreengine/rendering/input/window/WindowManager.java +++ b/src/main/java/speiger/src/coreengine/rendering/input/window/WindowManager.java @@ -10,7 +10,7 @@ import org.lwjgl.system.Callback; import speiger.src.collections.longs.maps.impl.concurrent.Long2ObjectConcurrentOpenHashMap; import speiger.src.collections.longs.maps.interfaces.Long2ObjectMap; import speiger.src.collections.objects.lists.ObjectArrayList; -import speiger.src.coreengine.rendering.input.divices.InputDevice; +import speiger.src.coreengine.rendering.input.devices.InputDevice; public class WindowManager { Long2ObjectMap monitors;