Finished Keyboard Device, Started Joystick Support

This commit is contained in:
Speiger 2024-05-17 12:50:12 +02:00
parent ab9d11f165
commit 0caf508535
8 changed files with 165 additions and 57 deletions

View File

@ -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<T, E> implements InputDevice {
this.bus = bus;
}
protected LongIterable knownWindows() {
return queues.keySet();
}
public void register(Window window) {
registerCallbacks(window);
long id = window.id();

View File

@ -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;

View File

@ -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<JoyStickData, JoyStickTask> {
@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 {
}
}

View File

@ -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<KeyData, KeyTask> {
@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<Integer, Keyboard> 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));
}
}
}

View File

@ -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;

View File

@ -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<KeyboardData, KeyTask> {
@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<Integer, Keyboard> 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) {}
}
}

View File

@ -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; }
}
}

View File

@ -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<Monitor> monitors;