summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/Input/Input.java
blob: 86deb124c34b4ed64f99024ec579d48125a08f2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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;
			}
		}
	}

	
}