diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/window/Events.cpp | 157 | ||||
-rw-r--r-- | src/window/Events.hpp | 151 |
2 files changed, 298 insertions, 10 deletions
diff --git a/src/window/Events.cpp b/src/window/Events.cpp index 09f6acd..8799853 100644 --- a/src/window/Events.cpp +++ b/src/window/Events.cpp @@ -1,16 +1,21 @@ #include <string.h> -#include <GL/glew.h> #include <GLFW/glfw3.h> #include "Events.hpp" +#include "Window.hpp" +#include "../debug.hpp" -Events::Events() : cur_frame(0), delta_x(0.0), delta_y(0.0), - x(0.0), y(0.0), cursor_locked(false) +static int get_std_key(int glfw_key); + +Events::Events(Window *w) : master(w), cur_frame(0), delta_x(0.0), delta_y(0.0), + x(0.0), y(0.0), scroll_dx(0), scroll_dy(0), cursor_locked(false) { keys = new bool[MAX_KEYS]; memset(keys, false, MAX_KEYS * sizeof(bool)); frames = new unsigned int[MAX_KEYS]; memset(frames, 0, MAX_KEYS * sizeof(unsigned int)); + + glfwGetCursorPos(w->GetWin(), &x, &y); } Events::~Events() @@ -46,6 +51,10 @@ bool Events::Jclicked(int button) void Events::NextFrame() { + delta_x = 0; + delta_y = 0; + scroll_dx = 0; + scroll_dy = 0; cur_frame++; } @@ -53,6 +62,10 @@ void Events::NextFrame() void Events::KeyHandle(int key, int scancode, int action, int mode) { + key = get_std_key(key); + if(key == KEY_UNKNOWN) + return; + if(action == GLFW_PRESS) { keys[key] = true; frames[key] = cur_frame; @@ -83,3 +96,141 @@ void Events::MouseButtonHandle(int button, int action, int mode) frames[MOUSE_BUTTONS + button] = cur_frame; } } + +void Events::WindowResizeHandle(int width, int height) +{ + master->Resize(width, height); +} + +void Events::MouseScrollHandle(double xoffset, double yoffset) +{ + scroll_dx = xoffset; + scroll_dy = yoffset; +} + +int get_std_key(int glfw_key) +{ + switch(glfw_key) { + case GLFW_KEY_SPACE: return KEY_SPACE; + case GLFW_KEY_APOSTROPHE: return KEY_APOSTROPHE; + case GLFW_KEY_COMMA: return KEY_COMMA; + case GLFW_KEY_MINUS: return KEY_MINUS; + case KEY_PERIOD: return GLFW_KEY_PERIOD; + case KEY_SLASH: return GLFW_KEY_SLASH; + case KEY_0: return GLFW_KEY_0; + case KEY_1: return GLFW_KEY_1; + case KEY_2: return GLFW_KEY_2; + case KEY_3: return GLFW_KEY_3; + case KEY_4: return GLFW_KEY_4; + case KEY_5: return GLFW_KEY_5; + case KEY_6: return GLFW_KEY_6; + case KEY_7: return GLFW_KEY_7; + case KEY_8: return GLFW_KEY_8; + case KEY_9: return GLFW_KEY_9; + case KEY_KEY_SEMICOLON: return GLFW_KEY_SEMICOLON; + case GLFW_KEY_EQUAL: return KEY_EQUAL; + case GLFW_KEY_A: return KEY_A; + case GLFW_KEY_B: return KEY_B; + case GLFW_KEY_C: return KEY_C; + case GLFW_KEY_D: return KEY_D; + case GLFW_KEY_E: return KEY_E; + case GLFW_KEY_F: return KEY_F; + case GLFW_KEY_G: return KEY_G; + case GLFW_KEY_H: return KEY_H; + case GLFW_KEY_I: return KEY_I; + case GLFW_KEY_J: return KEY_J; + case GLFW_KEY_K: return KEY_K; + case GLFW_KEY_L: return KEY_L; + case GLFW_KEY_M: return KEY_M; + case GLFW_KEY_N: return KEY_N; + case GLFW_KEY_O: return KEY_O; + case GLFW_KEY_P: return KEY_P; + case GLFW_KEY_Q: return KEY_Q; + case GLFW_KEY_R: return KEY_R; + case GLFW_KEY_S: return KEY_S; + case GLFW_KEY_T: return KEY_T; + case GLFW_KEY_U: return KEY_U; + case GLFW_KEY_V: return KEY_V; + case GLFW_KEY_W: return KEY_W; + case GLFW_KEY_X: return KEY_X; + case GLFW_KEY_Y: return KEY_Y; + case GLFW_KEY_Z: return KEY_Z; + case GLFW_KEY_LEFT_BRACKET: return KEY_LEFT_BRACKET; + case GLFW_KEY_BACKSLASH: return KEY_BACKSLASH; + case GLFW_KEY_RIGHT_BRACKET: return KEY_RIGHT_BRACKET; + case GLFW_KEY_GRAVE_ACCENT: return KEY_GRAVE_ACCENT; + case GLFW_KEY_WORLD_1: return KEY_WORLD_1; + case GLFW_KEY_WORLD_2: return KEY_WORLD_2; + case GLFW_KEY_ESCAPE: return KEY_ESCAPE; + case GLFW_KEY_ENTER: return KEY_ENTER; + case GLFW_KEY_TAB: return KEY_TAB; + case GLFW_KEY_BACKSPACE: return KEY_BACKSPACE; + case GLFW_KEY_INSERT: return KEY_INSERT; + case GLFW_KEY_DELETE: return KEY_DELETE; + case GLFW_KEY_RIGHT: return KEY_RIGHT; + case GLFW_KEY_LEFT: return KEY_LEFT; + case GLFW_KEY_DOWN: return KEY_DOWN; + case GLFW_KEY_UP: return KEY_UP; + case GLFW_KEY_PAGE_UP: return KEY_PAGE_UP; + case GLFW_KEY_PAGE_DOWN: return KEY_PAGE_DOWN; + case GLFW_KEY_HOME: return KEY_HOME; + case GLFW_KEY_END: return KEY_END; + case GLFW_KEY_CAPS_LOCK: return KEY_CAPS_LOCK; + case GLFW_KEY_SCROLL_LOCK: return KEY_SCROLL_LOCK; + case GLFW_KEY_NUM_LOCK: return KEY_NUM_LOCK; + case GLFW_KEY_PRINT_SCREEN: return KEY_PRINT_SCREEN; + case GLFW_KEY_PAUSE: return KEY_PAUSE; + case GLFW_KEY_F1: return KEY_F1; + case GLFW_KEY_F2: return KEY_F2; + case GLFW_KEY_F3: return KEY_F3; + case GLFW_KEY_F4: return KEY_F4; + case GLFW_KEY_F5: return KEY_F5; + case GLFW_KEY_F6: return KEY_F6; + case GLFW_KEY_F7: return KEY_F7; + case GLFW_KEY_F8: return KEY_F8; + case GLFW_KEY_F9: return KEY_F9; + case GLFW_KEY_F10: return KEY_F10; + case GLFW_KEY_F11: return KEY_F11; + case GLFW_KEY_F12: return KEY_F12; + case GLFW_KEY_F13: return KEY_F13; + case GLFW_KEY_F14: return KEY_F14; + case GLFW_KEY_F15: return KEY_F15; + case GLFW_KEY_F16: return KEY_F16; + case GLFW_KEY_F17: return KEY_F17; + case GLFW_KEY_F18: return KEY_F18; + case GLFW_KEY_F19: return KEY_F19; + case GLFW_KEY_F20: return KEY_F20; + case GLFW_KEY_F21: return KEY_F21; + case GLFW_KEY_F22: return KEY_F22; + case GLFW_KEY_F23: return KEY_F23; + case GLFW_KEY_F24: return KEY_F24; + case GLFW_KEY_F25: return KEY_F25; + case GLFW_KEY_KP_0: return KEY_KP_0; + case GLFW_KEY_KP_1: return KEY_KP_1; + case GLFW_KEY_KP_2: return KEY_KP_2; + case GLFW_KEY_KP_3: return KEY_KP_3; + case GLFW_KEY_KP_4: return KEY_KP_4; + case GLFW_KEY_KP_5: return KEY_KP_5; + case GLFW_KEY_KP_6: return KEY_KP_6; + case GLFW_KEY_KP_7: return KEY_KP_7; + case GLFW_KEY_KP_8: return KEY_KP_8; + case GLFW_KEY_KP_9: return KEY_KP_9; + case GLFW_KEY_KP_DECIMAL: return KEY_KP_DECIMAL; + case GLFW_KEY_KP_DIVIDE: return KEY_KP_DIVIDE; + case GLFW_KEY_KP_MULTIPLY: return KEY_KP_MULTIPLY; + case GLFW_KEY_KP_SUBTRACT: return KEY_KP_SUBTRACT; + case GLFW_KEY_KP_ADD: return KEY_KP_ADD; + case GLFW_KEY_KP_ENTER: return KEY_KP_ENTER; + case GLFW_KEY_KP_EQUAL: return KEY_KP_EQUAL; + case GLFW_KEY_LEFT_SHIFT: return KEY_LEFT_SHIFT; + case GLFW_KEY_LEFT_CONTROL: return KEY_LEFT_CONTROL; + case GLFW_KEY_LEFT_ALT: return KEY_LEFT_ALT; + case GLFW_KEY_LEFT_SUPER: return KEY_LEFT_SUPER; + case GLFW_KEY_RIGHT_SHIFT: return KEY_RIGHT_SHIFT; + case GLFW_KEY_RIGHT_CONTROL: return KEY_RIGHT_CONTROL; + case GLFW_KEY_RIGHT_ALT: return KEY_RIGHT_ALT; + case GLFW_KEY_RIGHT_SUPER: return KEY_RIGHT_SUPER; + case GLFW_KEY_MENU: return KEY_MENU; + default: return KEY_UNKNOWN; + } +} diff --git a/src/window/Events.hpp b/src/window/Events.hpp index 6914dca..9374373 100644 --- a/src/window/Events.hpp +++ b/src/window/Events.hpp @@ -1,23 +1,158 @@ #ifndef ENGINE_EVENTS_H #define ENGINE_EVENTS_H -#define MAX_KEYS GLFW_KEY_LAST + 1 + GLFW_MOUSE_BUTTON_LAST + 1 -#define MOUSE_BUTTONS GLFW_KEY_LAST + 1 /* when start mouse buttons */ +enum keys_name { + KEY_UNKNOWN = -1, + KEY_SPACE, + KEY_APOSTROPHE, + KEY_COMMA, + KEY_MINUS, + KEY_PERIOD, + KEY_SLASH, + KEY_0, + KEY_1, + KEY_2, + KEY_3, + KEY_4, + KEY_5, + KEY_6, + KEY_7, + KEY_8, + KEY_9, + KEY_KEY_SEMICOLON, + KEY_EQUAL, + KEY_A, + KEY_B, + KEY_C, + KEY_D, + KEY_E, + KEY_F, + KEY_G, + KEY_H, + KEY_I, + KEY_J, + KEY_K, + KEY_L, + KEY_M, + KEY_N, + KEY_O, + KEY_P, + KEY_Q, + KEY_R, + KEY_S, + KEY_T, + KEY_U, + KEY_V, + KEY_W, + KEY_X, + KEY_Y, + KEY_Z, + KEY_LEFT_BRACKET, + KEY_BACKSLASH, + KEY_RIGHT_BRACKET, + KEY_GRAVE_ACCENT, + KEY_WORLD_1, + KEY_WORLD_2, + KEY_ESCAPE, + KEY_ENTER, + KEY_TAB, + KEY_BACKSPACE, + KEY_INSERT, + KEY_DELETE, + KEY_RIGHT, + KEY_LEFT, + KEY_DOWN, + KEY_UP, + KEY_PAGE_UP, + KEY_PAGE_DOWN, + KEY_HOME, + KEY_END, + KEY_CAPS_LOCK, + KEY_SCROLL_LOCK, + KEY_NUM_LOCK, + KEY_PRINT_SCREEN, + KEY_PAUSE, + KEY_F1, + KEY_F2, + KEY_F3, + KEY_F4, + KEY_F5, + KEY_F6, + KEY_F7, + KEY_F8, + KEY_F9, + KEY_F10, + KEY_F11, + KEY_F12, + KEY_F13, + KEY_F14, + KEY_F15, + KEY_F16, + KEY_F17, + KEY_F18, + KEY_F19, + KEY_F20, + KEY_F21, + KEY_F22, + KEY_F23, + KEY_F24, + KEY_F25, + KEY_KP_0, + KEY_KP_1, + KEY_KP_2, + KEY_KP_3, + KEY_KP_4, + KEY_KP_5, + KEY_KP_6, + KEY_KP_7, + KEY_KP_8, + KEY_KP_9, + KEY_KP_DECIMAL, + KEY_KP_DIVIDE, + KEY_KP_MULTIPLY, + KEY_KP_SUBTRACT, + KEY_KP_ADD, + KEY_KP_ENTER, + KEY_KP_EQUAL, + KEY_LEFT_SHIFT, + KEY_LEFT_CONTROL, + KEY_LEFT_ALT, + KEY_LEFT_SUPER, + KEY_RIGHT_SHIFT, + KEY_RIGHT_CONTROL, + KEY_RIGHT_ALT, + KEY_RIGHT_SUPER, + KEY_MENU, + KEY_LAST = KEY_MENU +}; + +#define MAX_KEYS KEY_LAST + 1 + GLFW_MOUSE_BUTTON_LAST + 1 +#define MOUSE_BUTTONS KEY_LAST + 1 /* when start mouse buttons */ + +class Window; class Events { + Window *master; bool *keys; unsigned int *frames; unsigned int cur_frame; bool update_frame; - float delta_x; - float delta_y; - float x; - float y; + double delta_x; + double delta_y; + double x; + double y; + double scroll_dx; + double scroll_dy; bool cursor_locked; public: - Events(); + Events(Window *w); ~Events(); + Window *GetWindow() { return master; } + double GetDX() { return delta_x; } + double GetDY() { return delta_y; } + double GetScrollDX() { return scroll_dx; } + double GetScrollDY() { return scroll_dy; } bool Pressed(int keycode); bool Jpressed(int keycode); @@ -28,6 +163,8 @@ public: void KeyHandle(int key, int scancode, int action, int mode); void CursorPosHandle(double xpos, double ypos); void MouseButtonHandle(int button, int action, int mode); + void WindowResizeHandle(int width, int height); + void MouseScrollHandle(double xoffset, double yoffset); }; #endif /* ENGINE_EVENTS_H */ |