From 180aad05decc7eefa87e4e45d6747c48f40e5361 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 17 Apr 2023 12:12:01 -0400 Subject: save --- .../net/tylermurphy/Minecraft/Input/Input.java | 154 +++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100755 src/main/java/net/tylermurphy/Minecraft/Input/Input.java (limited to 'src/main/java/net/tylermurphy/Minecraft/Input/Input.java') 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 inputs = new ArrayList(); + + 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; + } + } + } + + +} -- cgit v1.2.3-freya