summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/Input/Input.java
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-04-17 12:12:01 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-04-17 12:12:01 -0400
commit180aad05decc7eefa87e4e45d6747c48f40e5361 (patch)
tree51545197f7c94b4022acab880772c9f4fc65db0e /src/main/java/net/tylermurphy/Minecraft/Input/Input.java
downloadminecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.gz
minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.bz2
minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.zip
save
Diffstat (limited to '')
-rwxr-xr-xsrc/main/java/net/tylermurphy/Minecraft/Input/Input.java154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/main/java/net/tylermurphy/Minecraft/Input/Input.java b/src/main/java/net/tylermurphy/Minecraft/Input/Input.java
new file mode 100755
index 0000000..86deb12
--- /dev/null
+++ b/src/main/java/net/tylermurphy/Minecraft/Input/Input.java
@@ -0,0 +1,154 @@
+package net.tylermurphy.Minecraft.Input;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.lwjgl.glfw.GLFW;
+import org.lwjgl.glfw.GLFWCursorPosCallback;
+import org.lwjgl.glfw.GLFWKeyCallback;
+import org.lwjgl.glfw.GLFWMouseButtonCallback;
+
+import org.lwjgl.glfw.GLFWCharCallback;
+
+public class Input {
+
+ private static List<IInput> inputs = new ArrayList<IInput>();
+
+ private static boolean[] keys = new boolean[GLFW.GLFW_KEY_LAST];
+ private static boolean[] mouseButtons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
+ private static double mouseX, mouseY;
+ private static double mouseX_last, mouseY_last;
+
+ private static GLFWKeyCallback keyboard;
+ private static GLFWCursorPosCallback mouseMove;
+ private static GLFWMouseButtonCallback mouseButton;
+ private static GLFWCharCallback character;
+
+ public static void init() {
+
+ keyboard = new GLFWKeyCallback() {
+ public void invoke(long window, int key, int scancode, int action, int mods) {
+ switch(action) {
+ case GLFW.GLFW_PRESS:
+ keys[key] = true;
+ for(IInput input : inputs) {
+ if(!input.enabled) continue;
+ input.keyPressed(key);
+ }
+ break;
+ case GLFW.GLFW_RELEASE:
+ keys[key] = false;
+ for(IInput input : inputs) {
+ if(!input.enabled) continue;
+ input.keyRelesed(key);
+ }
+ break;
+ }
+
+ }
+ };
+
+ mouseMove = new GLFWCursorPosCallback() {
+ public void invoke(long window, double xPos, double yPos) {
+ mouseX = xPos; mouseY = yPos;
+ }
+ };
+
+ mouseButton = new GLFWMouseButtonCallback() {
+ public void invoke(long window, int button, int action, int mods) {
+ switch(action) {
+ case GLFW.GLFW_PRESS:
+ mouseButtons[button] = true;
+ for(IInput input : inputs) {
+ if(!input.enabled) continue;
+ input.mousePressed(button);
+ }
+ break;
+ case GLFW.GLFW_RELEASE:
+ mouseButtons[button] = false;
+ for(IInput input : inputs) {
+ if(!input.enabled) continue;
+ input.mouseRelesed(button);
+ }
+ break;
+ }
+ }
+ };
+
+ character = new GLFWCharCallback() {
+ public void invoke(long window, int key) {
+ for(IInput input : inputs) {
+ if(!input.enabled) continue;
+ input.charAction((char)key);
+ }
+ }
+ };
+ }
+
+ public static void addInput(IInput input) {
+ inputs.add(input);
+ }
+
+ public static void destroy() {
+ keyboard.free();
+ mouseMove.free();
+ mouseButton.free();
+ character.free();
+ }
+
+ public static boolean isKeyDown(int key) {
+ return keys[key];
+ }
+
+ public static boolean isButtonDown(int button) {
+ return mouseButtons[button];
+ }
+
+ public static double getMouseX() {
+ return mouseX;
+ }
+
+ public static double getMouseY() {
+ return mouseY;
+ }
+
+ public static double getMouseDX() {
+ double d = -(mouseX_last - mouseX);
+ mouseX_last = mouseX;
+ return d;
+ }
+
+ public static double getMouseDY() {
+ double d = mouseY_last - mouseY;
+ mouseY_last = mouseY;
+ return d;
+ }
+
+ public static GLFWKeyCallback getKeyboardCallback() {
+ return keyboard;
+ }
+
+ public static GLFWCursorPosCallback getMouseMoveCallback() {
+ return mouseMove;
+ }
+
+ public static GLFWMouseButtonCallback getMouseButtonCallback() {
+ return mouseButton;
+ }
+
+ public static GLFWCharCallback getCharCallback() {
+ return character;
+ }
+
+ public static void setEnabled(String className, boolean enabled) {
+ for(IInput input : inputs) {
+ String fullName = input.getClass().getTypeName();
+ fullName = fullName.substring(fullName.lastIndexOf(".")+1,fullName.length());
+ if(className.equals(fullName)) {
+ input.enabled = enabled;
+ }
+ }
+ }
+
+
+}