-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_handler.cpp
More file actions
135 lines (113 loc) · 4.16 KB
/
input_handler.cpp
File metadata and controls
135 lines (113 loc) · 4.16 KB
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
#include "input_handler.h"
#include <iostream>
#include <array>
#include <algorithm>
struct ButtonState {
InputHandler::MouseButton button = InputHandler::MouseButton::MaxCount;
bool is_holding = false;
};
ButtonState button_state;
struct KeyState {
int key = GLFW_KEY_LAST;
bool is_holding = false;
};
KeyState key_state;
std::array<std::function<void(double, double)>, (size_t)InputHandler::MouseButton::MaxCount> mouse_drag_handlers;
std::array<std::function<void(double, double)>, (size_t)InputHandler::MouseButton::MaxCount> mouse_press_handlers;
std::array<std::function<void(double, double)>, (size_t)InputHandler::MouseButton::MaxCount> mouse_release_handlers;
std::function<void(double, double, double)> scroll_handler;
std::function<bool(void)> mouse_filter = nullptr;
std::function<void(int)> key_press_handler;
std::function<void(int)> key_release_handler;
void InputHandler::set_mouse_input_filter(std::function<bool(void)> filter) {
mouse_filter = filter;
}
void InputHandler::set_mouse_drag_handler(std::function<void(double, double)> handler, MouseButton button) {
mouse_drag_handlers[(size_t)button] = handler;
}
void InputHandler::set_mouse_press_handler(std::function<void(double, double)> handler, MouseButton button) {
mouse_press_handlers[(size_t)button] = handler;
}
void InputHandler::set_mouse_release_handler(std::function<void(double, double)> handler, MouseButton button) {
mouse_release_handlers[(size_t)button] = handler;
}
void InputHandler::set_mouse_scroll_handler(std::function<void(double, double, double)> handle) {
scroll_handler = handle;
}
void InputHandler::set_key_press_handler(std::function<void(int)> handle) {
key_press_handler = handle;
}
void InputHandler::set_key_release_handler(std::function<void(int)> handle) {
key_release_handler = handle;
}
void mouse_click_callback(GLFWwindow* window, int button, int action, int mods) {
if (mouse_filter && !mouse_filter()) {
return;
}
if (button == GLFW_MOUSE_BUTTON_LEFT) {
button_state.button = InputHandler::MouseButton::Left;
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT) {
button_state.button = InputHandler::MouseButton::Right;
}
else if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
button_state.button = InputHandler::MouseButton::Mid;
}
else {}
double x, y;
glfwGetCursorPos(window, &x, &y);
if (action == GLFW_PRESS) {
if (mouse_press_handlers[(size_t)button_state.button]) {
mouse_press_handlers[(size_t)button_state.button](x, y);
}
button_state.is_holding = true;
}
else {
if (mouse_release_handlers[(size_t)button_state.button]) {
mouse_release_handlers[(size_t)button_state.button](x, y);
}
button_state.is_holding = false;
}
}
void cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
if (mouse_filter && !mouse_filter()) {
return;
}
if (button_state.is_holding && mouse_drag_handlers[(size_t)button_state.button]) {
mouse_drag_handlers[(size_t)button_state.button](xpos, ypos);
return;
}
if (!button_state.is_holding && mouse_drag_handlers[(size_t)InputHandler::MouseButton::None]) {
mouse_drag_handlers[(size_t)InputHandler::MouseButton::None](xpos, ypos);
return;
}
return;
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
if (mouse_filter && !mouse_filter()) {
return;
}
double x, y;
glfwGetCursorPos(window, &x, &y);
if (scroll_handler) {
scroll_handler(x, y, yoffset);
}
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key < 0 || key > GLFW_KEY_LAST) {
return;
}
if (action == GLFW_PRESS) {
key_press_handler(key);
}
else if (action == GLFW_RELEASE) {
key_release_handler(key);
}
}
InputHandler::InputHandler(GLFWwindow* window) {
_window = window;
glfwSetMouseButtonCallback(_window, mouse_click_callback);
glfwSetCursorPosCallback(_window, cursor_position_callback);
glfwSetScrollCallback(_window, scroll_callback);
glfwSetKeyCallback(_window, key_callback);
}