From 61d48be57945839617ef603a85225b26ecfa7d96 Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 17 May 2024 18:46:56 +0200 Subject: [PATCH] Finished Input Devices --- .../speiger/src/coreengine/NewInputTest.java | 24 ++++++++-- .../rendering/input/devices/FileDrop.java | 47 +++++++++++++++++++ .../rendering/input/devices/Keyboard.java | 1 + .../rendering/input/devices/Mouse.java | 4 +- .../rendering/input/events/FileEvents.java | 28 +++++++++++ .../rendering/input/events/MouseEvent.java | 2 + .../rendering/input/window/Window.java | 4 ++ .../rendering/input/window/WindowManager.java | 19 +++++++- .../coreengine/utils/eventbus/EventBus.java | 5 +- 9 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/rendering/input/devices/FileDrop.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/input/events/FileEvents.java diff --git a/src/main/java/speiger/src/coreengine/NewInputTest.java b/src/main/java/speiger/src/coreengine/NewInputTest.java index e54c75a..396c0d0 100644 --- a/src/main/java/speiger/src/coreengine/NewInputTest.java +++ b/src/main/java/speiger/src/coreengine/NewInputTest.java @@ -2,13 +2,18 @@ package speiger.src.coreengine; import org.lwjgl.glfw.GLFW; +import speiger.src.coreengine.rendering.input.devices.FileDrop; import speiger.src.coreengine.rendering.input.devices.Joystick; -import speiger.src.coreengine.rendering.input.devices.Joystick.ButtonData; +import speiger.src.coreengine.rendering.input.devices.Keyboard; +import speiger.src.coreengine.rendering.input.devices.Mouse; +import speiger.src.coreengine.rendering.input.window.Window; +import speiger.src.coreengine.rendering.input.window.WindowManager; import speiger.src.coreengine.utils.eventbus.EventBus; public class NewInputTest { EventBus bus = new EventBus(); + WindowManager manager = new WindowManager(); public static void main(String[] args) { new NewInputTest().run(); @@ -16,10 +21,21 @@ public class NewInputTest { public void run() { GLFW.glfwInit(); - Joystick.INSTANCE.init(bus); - ButtonData data = new ButtonData(); + manager.initialize(); + Mouse.INSTANCE.init(bus); + Keyboard.INSTANCE.init(bus); + Joystick.INSTANCE.init(manager, bus); + FileDrop.INSTANCE.init(bus); + manager.addDevices(Mouse.INSTANCE, Keyboard.INSTANCE, Joystick.INSTANCE, FileDrop.INSTANCE); + Window window = manager.builder().title("Testing Engine").build(); + window.visible(true); + //TODO implement window close requests while(true) { - Joystick.INSTANCE.handleJoystick(0, GLFW.GLFW_JOYSTICK_1, data); + GLFW.glfwPollEvents(); + window.update(); + window.handleInput(); + window.finishFrame(); + try { Thread.sleep(100); } diff --git a/src/main/java/speiger/src/coreengine/rendering/input/devices/FileDrop.java b/src/main/java/speiger/src/coreengine/rendering/input/devices/FileDrop.java new file mode 100644 index 0000000..15dda30 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/input/devices/FileDrop.java @@ -0,0 +1,47 @@ +package speiger.src.coreengine.rendering.input.devices; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWDropCallback; + +import speiger.src.collections.objects.lists.ObjectArrayList; +import speiger.src.coreengine.rendering.input.devices.FileDrop.DropTask; +import speiger.src.coreengine.rendering.input.events.FileEvents; +import speiger.src.coreengine.rendering.input.window.Window; + +public class FileDrop extends AbstractDevice { + public static final FileDrop INSTANCE = new FileDrop(); + + @Override + protected void registerCallbacks(Window window) { + super.registerCallbacks(window); + window.addCallback(this::drop, GLFW::glfwSetDropCallback); + } + + private void drop(long windowId, int count, long names) { + List paths = new ObjectArrayList<>(); + for(int i = 0;i { + public static final Keyboard INSTANCE = new Keyboard(); @Override public void reset(long windowId) {} @Override diff --git a/src/main/java/speiger/src/coreengine/rendering/input/devices/Mouse.java b/src/main/java/speiger/src/coreengine/rendering/input/devices/Mouse.java index 8fc002e..c3e4b00 100644 --- a/src/main/java/speiger/src/coreengine/rendering/input/devices/Mouse.java +++ b/src/main/java/speiger/src/coreengine/rendering/input/devices/Mouse.java @@ -59,12 +59,12 @@ public class Mouse extends AbstractDevice { public int x(long windowId) { MouseData data = get(windowId); - return data == null ? 0 : data.position.x(); + return data == null ? 0 : data.x(); } public int y(long windowId) { MouseData data = get(windowId); - return data == null ? 0 : data.position.y(); + return data == null ? 0 : data.y(); } public boolean pressed(long windowId, int button) { diff --git a/src/main/java/speiger/src/coreengine/rendering/input/events/FileEvents.java b/src/main/java/speiger/src/coreengine/rendering/input/events/FileEvents.java new file mode 100644 index 0000000..9ac0eee --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/input/events/FileEvents.java @@ -0,0 +1,28 @@ +package speiger.src.coreengine.rendering.input.events; + +import java.nio.file.Path; + +public class FileEvents { + public static class Drop extends MouseEvent { + Path[] files; + String[] extension; + String[] name; + + public Drop(long window, int x, int y, Path[] files) { + super(window, x, y); + this.files = files; + this.extension = new String[files.length]; + this.name = new String[files.length]; + for(int i = 0,m=files.length;i, Listeners> listeners = new ConcurrentHashMap<>(); + Map, Listeners> listeners = new Object2ObjectOpenHashMap, Listeners>().synchronize(); Object2ObjectMap> instances = new Object2ObjectConcurrentOpenHashMap<>(); public void register(Class event, Consumer listener) { @@ -86,6 +86,7 @@ public class EventBus public void post(Event event) { Consumer[] listeners = getListeners(event.getClass()).getListeners(); + System.out.println("Event: "+event); if(listeners.length <= 0) return; int index = 0; try {