From 23453b4dbc7fcdf75c89145073d279c9c64c0a43 Mon Sep 17 00:00:00 2001 From: halotroop2288 Date: Tue, 4 Feb 2020 19:21:59 -0800 Subject: [PATCH] Added input handlers by @Valoeghese --- .../litecraft/input/InitialPressHandler.java | 21 ++++++++++++ .../halotroop/litecraft/input/Input.java | 34 +++++++++++++++++++ .../litecraft/input/KeyCallback.java | 9 +++++ .../litecraft/input/KeyCallbackHandler.java | 33 ++++++++++++++++++ .../litecraft/input/KeyListener.java | 9 +++++ .../halotroop/litecraft/input/Keybind.java | 30 ++++++++++++++++ .../litecraft/input/MouseCallbackHandler.java | 24 +++++++++++++ 7 files changed, 160 insertions(+) create mode 100644 src/main/java/com/github/halotroop/litecraft/input/InitialPressHandler.java create mode 100644 src/main/java/com/github/halotroop/litecraft/input/Input.java create mode 100644 src/main/java/com/github/halotroop/litecraft/input/KeyCallback.java create mode 100644 src/main/java/com/github/halotroop/litecraft/input/KeyCallbackHandler.java create mode 100644 src/main/java/com/github/halotroop/litecraft/input/KeyListener.java create mode 100644 src/main/java/com/github/halotroop/litecraft/input/Keybind.java create mode 100644 src/main/java/com/github/halotroop/litecraft/input/MouseCallbackHandler.java diff --git a/src/main/java/com/github/halotroop/litecraft/input/InitialPressHandler.java b/src/main/java/com/github/halotroop/litecraft/input/InitialPressHandler.java new file mode 100644 index 0000000..fddcb41 --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/input/InitialPressHandler.java @@ -0,0 +1,21 @@ +package com.github.halotroop.litecraft.input; + +/* + * Author: Valoeghese + */ +public final class InitialPressHandler implements KeyListener +{ + public InitialPressHandler(KeyCallback callback) + { this.callback = callback; } + + private boolean activatedPreviously = false; + private final KeyCallback callback; + + @Override + public void listen(boolean active) + { + if (!activatedPreviously && active) + { callback.onCallback(); } + activatedPreviously = active; + } +} diff --git a/src/main/java/com/github/halotroop/litecraft/input/Input.java b/src/main/java/com/github/halotroop/litecraft/input/Input.java new file mode 100644 index 0000000..2954eeb --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/input/Input.java @@ -0,0 +1,34 @@ +package com.github.halotroop.litecraft.input; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/* + * Author: Valoeghese + */ +public class Input +{ + private static final Map> CALLBACKS = new HashMap<>(); + private static final Map> LISTENERS = new HashMap<>(); + + public static void addPressCallback(Keybind key, KeyCallback callback) + { CALLBACKS.computeIfAbsent(key, listener -> new ArrayList<>()).add(callback); } + + public static void addListener(Keybind key, KeyListener callback) + { LISTENERS.computeIfAbsent(key, listener -> new ArrayList<>()).add(callback); } + + public static void invokeAllListeners() + { + CALLBACKS.forEach((keybind, listeners) -> + { + if (keybind.isActive()) + { listeners.forEach(callback -> callback.onCallback()); } + }); + LISTENERS.forEach((keybind, listeners) -> + { + listeners.forEach(listener -> listener.listen(keybind.isActive())); + }); + } +} diff --git a/src/main/java/com/github/halotroop/litecraft/input/KeyCallback.java b/src/main/java/com/github/halotroop/litecraft/input/KeyCallback.java new file mode 100644 index 0000000..9f86320 --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/input/KeyCallback.java @@ -0,0 +1,9 @@ +package com.github.halotroop.litecraft.input; + +/* + * Author: Valoeghese + */ +public interface KeyCallback +{ + public void onCallback(); +} diff --git a/src/main/java/com/github/halotroop/litecraft/input/KeyCallbackHandler.java b/src/main/java/com/github/halotroop/litecraft/input/KeyCallbackHandler.java new file mode 100644 index 0000000..7dfd84a --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/input/KeyCallbackHandler.java @@ -0,0 +1,33 @@ +package com.github.halotroop.litecraft.input; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWKeyCallback; + +/* + * Author: Valoeghese + */ +public class KeyCallbackHandler extends GLFWKeyCallback +{ + private KeyCallbackHandler() + {} + + private static final KeyCallbackHandler INSTANCE = new KeyCallbackHandler(); + + public static void trackWindow(long window) + { GLFW.glfwSetKeyCallback(window, INSTANCE); } + + public static boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST]; + + @Override + public void invoke(long window, int key, int scancode, int action, int mods) + { + try + { + keys[key] = action != GLFW.GLFW_RELEASE; + } + catch (ArrayIndexOutOfBoundsException e) + { + // Probably just changing the volume + } + } +} diff --git a/src/main/java/com/github/halotroop/litecraft/input/KeyListener.java b/src/main/java/com/github/halotroop/litecraft/input/KeyListener.java new file mode 100644 index 0000000..5a3f78c --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/input/KeyListener.java @@ -0,0 +1,9 @@ +package com.github.halotroop.litecraft.input; + +/* + * Author: Valoeghese + */ +public interface KeyListener +{ + public void listen(boolean active); +} diff --git a/src/main/java/com/github/halotroop/litecraft/input/Keybind.java b/src/main/java/com/github/halotroop/litecraft/input/Keybind.java new file mode 100644 index 0000000..9543206 --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/input/Keybind.java @@ -0,0 +1,30 @@ +package com.github.halotroop.litecraft.input; + +import org.lwjgl.glfw.GLFW; + +/* + * Author: Valoeghese + */ +public final class Keybind +{ + public int value; + public boolean mouse; + public static final Keybind MOVE_UP = new Keybind(GLFW.GLFW_KEY_W, false); + public static final Keybind MOVE_DOWN = new Keybind(GLFW.GLFW_KEY_S, false); + public static final Keybind MOVE_LEFT = new Keybind(GLFW.GLFW_KEY_A, false); + public static final Keybind MOVE_RIGHT = new Keybind(GLFW.GLFW_KEY_D, false); + public static final Keybind USE = new Keybind(GLFW.GLFW_MOUSE_BUTTON_1, true); + public static final Keybind SELECT_0 = new Keybind(GLFW.GLFW_KEY_1, false); + public static final Keybind SELECT_1 = new Keybind(GLFW.GLFW_KEY_2, false); + public static final Keybind SELECT_2 = new Keybind(GLFW.GLFW_KEY_3, false); + public static final Keybind EXIT = new Keybind(GLFW.GLFW_KEY_ESCAPE, false); + + public Keybind(int initValue, boolean isMouse) + { + this.value = initValue; + this.mouse = isMouse; + } + + public boolean isActive() + { return mouse ? MouseCallbackHandler.buttons[value] : KeyCallbackHandler.keys[value]; } +} diff --git a/src/main/java/com/github/halotroop/litecraft/input/MouseCallbackHandler.java b/src/main/java/com/github/halotroop/litecraft/input/MouseCallbackHandler.java new file mode 100644 index 0000000..9af299b --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/input/MouseCallbackHandler.java @@ -0,0 +1,24 @@ +package com.github.halotroop.litecraft.input; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWMouseButtonCallback; + +/* + * Author: Valoeghese + */ +public class MouseCallbackHandler extends GLFWMouseButtonCallback +{ + private MouseCallbackHandler() + {} + + private static final MouseCallbackHandler INSTANCE = new MouseCallbackHandler(); + + public static void trackWindow(long window) + { GLFW.glfwSetMouseButtonCallback(window, INSTANCE); } + + public static boolean[] buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST]; + + @Override + public void invoke(long window, int button, int action, int mods) + { buttons[button] = action != GLFW.GLFW_RELEASE; } +}