1 module Engine.Input; 2 3 import Engine.Core; 4 import derelict.glfw3.glfw3; 5 6 public static class Input 7 { 8 package static state[int] keysState; 9 package static __gshared vec2 mousePos = vec2i(0,0); 10 package static __gshared vec2 mouseScroll = vec2(0,0); 11 12 package enum state { 13 notPressed = 0, 14 press = 1, 15 wasPressed = 2, 16 firstPress = press | wasPressed, 17 } 18 19 package static void Initialize() { 20 glfwSetKeyCallback(Core.window,&KeyCallback); 21 glfwSetMouseButtonCallback(Core.window,&MouseKeyCallback); 22 glfwSetScrollCallback(Core.window,&MouseScrollCallback); 23 double x,y; 24 glfwGetCursorPos(Core.window, &x, &y); 25 mousePos.x = x; 26 mousePos.y = y; 27 } 28 29 extern (C) package static void MouseScrollCallback(GLFWwindow* window, double x, double y) nothrow { 30 mouseScroll.x = x; 31 mouseScroll.y = y; 32 } 33 34 extern (C) package static void MouseKeyCallback(GLFWwindow* window, int key, int action, int mods) nothrow { 35 switch (action) { 36 case GLFW_PRESS: 37 keysState[key] = state.firstPress; 38 break; 39 case GLFW_RELEASE: 40 keysState[key] = state.wasPressed; 41 break; 42 default: 43 break; 44 } 45 } 46 47 extern (C) package static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) nothrow { 48 switch (action) { 49 case GLFW_PRESS: 50 keysState[key] = state.firstPress; 51 break; 52 case GLFW_RELEASE: 53 keysState[key] = state.wasPressed; 54 break; 55 default: 56 break; 57 } 58 } 59 60 61 package static void Update() { 62 foreach (ref state s; keysState) { 63 switch (s) { 64 case state.firstPress: 65 s = state.press; 66 break; 67 case state.wasPressed: 68 s = state.notPressed; 69 break; 70 default: 71 break; 72 } 73 } 74 double x,y; 75 glfwGetCursorPos(Core.window, &x, &y); 76 mousePos.x = x; 77 mousePos.y = y; 78 79 mouseScroll = vec2(0,0); 80 } 81 82 private static state getState(int key) { 83 auto s = key in keysState; 84 if (s is null) 85 return state.notPressed; 86 return *s; 87 } 88 89 public static bool KeyDown(Key key) { 90 return (getState(key) & state.press) == state.press; 91 } 92 93 public static bool KeyUp(Key key) { 94 return !KeyDown(key); 95 } 96 97 public static bool KeyPressDown(Key key) { 98 return (getState(key) & state.firstPress) == state.firstPress; 99 } 100 101 public static bool KeyPressUp(Key key) { 102 return (getState(key) & state.wasPressed) == state.wasPressed; 103 } 104 105 106 public static vec2 MousePosition() { 107 return vec2(mousePos.x, mousePos.y); 108 } 109 110 public static vec2 MouseScroll() { 111 return mouseScroll; 112 } 113 114 } 115 116 public enum Key { 117 //UNKNOWN = GLFW_KEY_UNKNOWN , 118 SPACE = GLFW_KEY_SPACE , 119 APOSTROPHE = GLFW_KEY_APOSTROPHE , 120 COMMA = GLFW_KEY_COMMA , 121 MINUS = GLFW_KEY_MINUS , 122 PERIOD = GLFW_KEY_PERIOD , 123 SLASH = GLFW_KEY_SLASH , 124 KEY_0 = GLFW_KEY_0 , 125 KEY_1 = GLFW_KEY_1 , 126 KEY_2 = GLFW_KEY_2 , 127 KEY_3 = GLFW_KEY_3 , 128 KEY_4 = GLFW_KEY_4 , 129 KEY_5 = GLFW_KEY_5 , 130 KEY_6 = GLFW_KEY_6 , 131 KEY_7 = GLFW_KEY_7 , 132 KEY_8 = GLFW_KEY_8 , 133 KEY_9 = GLFW_KEY_9 , 134 SEMICOLON = GLFW_KEY_SEMICOLON , 135 EQUAL = GLFW_KEY_EQUAL , 136 A = GLFW_KEY_A , 137 B = GLFW_KEY_B , 138 C = GLFW_KEY_C , 139 D = GLFW_KEY_D , 140 E = GLFW_KEY_E , 141 F = GLFW_KEY_F , 142 G = GLFW_KEY_G , 143 H = GLFW_KEY_H , 144 I = GLFW_KEY_I , 145 J = GLFW_KEY_J , 146 K = GLFW_KEY_K , 147 L = GLFW_KEY_L , 148 M = GLFW_KEY_M , 149 N = GLFW_KEY_N , 150 O = GLFW_KEY_O , 151 P = GLFW_KEY_P , 152 Q = GLFW_KEY_Q , 153 R = GLFW_KEY_R , 154 S = GLFW_KEY_S , 155 T = GLFW_KEY_T , 156 U = GLFW_KEY_U , 157 V = GLFW_KEY_V , 158 W = GLFW_KEY_W , 159 X = GLFW_KEY_X , 160 Y = GLFW_KEY_Y , 161 Z = GLFW_KEY_Z , 162 LEFT_BRACKET = GLFW_KEY_LEFT_BRACKET , 163 BACKSLASH = GLFW_KEY_BACKSLASH , 164 RIGHT_BRACKET = GLFW_KEY_RIGHT_BRACKET , 165 GRAVE_ACCENT = GLFW_KEY_GRAVE_ACCENT , 166 WORLD_1 = GLFW_KEY_WORLD_1 , 167 WORLD_2 = GLFW_KEY_WORLD_2 , 168 169 ESCAPE = GLFW_KEY_ESCAPE , 170 ENTER = GLFW_KEY_ENTER , 171 TAB = GLFW_KEY_TAB , 172 BACKSPACE = GLFW_KEY_BACKSPACE , 173 INSERT = GLFW_KEY_INSERT , 174 DELETE = GLFW_KEY_DELETE , 175 RIGHT = GLFW_KEY_RIGHT , 176 LEFT = GLFW_KEY_LEFT , 177 DOWN = GLFW_KEY_DOWN , 178 UP = GLFW_KEY_UP , 179 PAGE_UP = GLFW_KEY_PAGE_UP , 180 PAGE_DOWN = GLFW_KEY_PAGE_DOWN , 181 HOME = GLFW_KEY_HOME , 182 END = GLFW_KEY_END , 183 CAPS_LOCK = GLFW_KEY_CAPS_LOCK , 184 SCROLL_LOCK = GLFW_KEY_SCROLL_LOCK , 185 NUM_LOCK = GLFW_KEY_NUM_LOCK , 186 PRINT_SCREEN = GLFW_KEY_PRINT_SCREEN , 187 PAUSE = GLFW_KEY_PAUSE , 188 F1 = GLFW_KEY_F1 , 189 F2 = GLFW_KEY_F2 , 190 F3 = GLFW_KEY_F3 , 191 F4 = GLFW_KEY_F4 , 192 F5 = GLFW_KEY_F5 , 193 F6 = GLFW_KEY_F6 , 194 F7 = GLFW_KEY_F7 , 195 F8 = GLFW_KEY_F8 , 196 F9 = GLFW_KEY_F9 , 197 F10 = GLFW_KEY_F10 , 198 F11 = GLFW_KEY_F11 , 199 F12 = GLFW_KEY_F12 , 200 F13 = GLFW_KEY_F13 , 201 F14 = GLFW_KEY_F14 , 202 F15 = GLFW_KEY_F15 , 203 F16 = GLFW_KEY_F16 , 204 F17 = GLFW_KEY_F17 , 205 F18 = GLFW_KEY_F18 , 206 F19 = GLFW_KEY_F19 , 207 F20 = GLFW_KEY_F20 , 208 F21 = GLFW_KEY_F21 , 209 F22 = GLFW_KEY_F22 , 210 F23 = GLFW_KEY_F23 , 211 F24 = GLFW_KEY_F24 , 212 F25 = GLFW_KEY_F25 , 213 KP_0 = GLFW_KEY_KP_0 , 214 KP_1 = GLFW_KEY_KP_1 , 215 KP_2 = GLFW_KEY_KP_2 , 216 KP_3 = GLFW_KEY_KP_3 , 217 KP_4 = GLFW_KEY_KP_4 , 218 KP_5 = GLFW_KEY_KP_5 , 219 KP_6 = GLFW_KEY_KP_6 , 220 KP_7 = GLFW_KEY_KP_7 , 221 KP_8 = GLFW_KEY_KP_8 , 222 KP_9 = GLFW_KEY_KP_9 , 223 KP_DECIMAL = GLFW_KEY_KP_DECIMAL , 224 KP_DIVIDE = GLFW_KEY_KP_DIVIDE , 225 KP_MULTIPLY = GLFW_KEY_KP_MULTIPLY , 226 KP_SUBTRACT = GLFW_KEY_KP_SUBTRACT , 227 KP_ADD = GLFW_KEY_KP_ADD , 228 KP_ENTER = GLFW_KEY_KP_ENTER , 229 KP_EQUAL = GLFW_KEY_KP_EQUAL , 230 LEFT_SHIFT = GLFW_KEY_LEFT_SHIFT , 231 LEFT_CONTROL = GLFW_KEY_LEFT_CONTROL , 232 LEFT_ALT = GLFW_KEY_LEFT_ALT , 233 LEFT_SUPER = GLFW_KEY_LEFT_SUPER , 234 RIGHT_SHIFT = GLFW_KEY_RIGHT_SHIFT , 235 RIGHT_CONTROL = GLFW_KEY_RIGHT_CONTROL , 236 RIGHT_ALT = GLFW_KEY_RIGHT_ALT , 237 RIGHT_SUPER = GLFW_KEY_RIGHT_SUPER , 238 MENU = GLFW_KEY_MENU , 239 LAST = GLFW_KEY_LAST, 240 241 ESC = GLFW_KEY_ESCAPE, 242 DEL = GLFW_KEY_DELETE, 243 PAGEUP = GLFW_KEY_PAGE_UP, 244 PAGEDOWN = GLFW_KEY_PAGE_DOWN, 245 KP_NUM_LOCK = GLFW_KEY_NUM_LOCK, 246 LCTRL = GLFW_KEY_LEFT_CONTROL, 247 LSHIFT = GLFW_KEY_LEFT_SHIFT, 248 LALT = GLFW_KEY_LEFT_ALT, 249 LSUPER = GLFW_KEY_LEFT_SUPER, 250 RCTRL = GLFW_KEY_RIGHT_CONTROL, 251 RSHIFT = GLFW_KEY_RIGHT_SHIFT, 252 RALT = GLFW_KEY_RIGHT_ALT, 253 RSUPER = GLFW_KEY_RIGHT_SUPER, 254 255 MOD_SHIFT = GLFW_MOD_SHIFT , 256 MOD_CONTROL = GLFW_MOD_CONTROL, 257 MOD_ALT = GLFW_MOD_ALT , 258 MOD_SUPER = GLFW_MOD_SUPER , 259 260 MOUSE_BUTTON_1 = GLFW_MOUSE_BUTTON_1 , 261 MOUSE_BUTTON_2 = GLFW_MOUSE_BUTTON_2 , 262 MOUSE_BUTTON_3 = GLFW_MOUSE_BUTTON_3 , 263 MOUSE_BUTTON_4 = GLFW_MOUSE_BUTTON_4 , 264 MOUSE_BUTTON_5 = GLFW_MOUSE_BUTTON_5 , 265 MOUSE_BUTTON_6 = GLFW_MOUSE_BUTTON_6 , 266 MOUSE_BUTTON_7 = GLFW_MOUSE_BUTTON_7 , 267 MOUSE_BUTTON_8 = GLFW_MOUSE_BUTTON_8 , 268 MOUSE_BUTTON_LAST = GLFW_MOUSE_BUTTON_LAST , 269 MOUSE_BUTTON_LEFT = GLFW_MOUSE_BUTTON_LEFT , 270 MOUSE_BUTTON_RIGHT = GLFW_MOUSE_BUTTON_RIGHT , 271 MOUSE_BUTTON_MIDDLE = GLFW_MOUSE_BUTTON_MIDDLE, 272 273 JOYSTICK_1 = GLFW_JOYSTICK_1 , 274 JOYSTICK_2 = GLFW_JOYSTICK_2 , 275 JOYSTICK_3 = GLFW_JOYSTICK_3 , 276 JOYSTICK_4 = GLFW_JOYSTICK_4 , 277 JOYSTICK_5 = GLFW_JOYSTICK_5 , 278 JOYSTICK_6 = GLFW_JOYSTICK_6 , 279 JOYSTICK_7 = GLFW_JOYSTICK_7 , 280 JOYSTICK_8 = GLFW_JOYSTICK_8 , 281 JOYSTICK_9 = GLFW_JOYSTICK_9 , 282 JOYSTICK_10 = GLFW_JOYSTICK_10 , 283 JOYSTICK_11 = GLFW_JOYSTICK_11 , 284 JOYSTICK_12 = GLFW_JOYSTICK_12 , 285 JOYSTICK_13 = GLFW_JOYSTICK_13 , 286 JOYSTICK_14 = GLFW_JOYSTICK_14 , 287 JOYSTICK_15 = GLFW_JOYSTICK_15 , 288 JOYSTICK_16 = GLFW_JOYSTICK_16 , 289 JOYSTICK_LAST = GLFW_JOYSTICK_LAST , 290 }