-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
164 lines (138 loc) · 3.53 KB
/
Copy pathInput.cpp
File metadata and controls
164 lines (138 loc) · 3.53 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "Input.h"
//only used once
namespace Input {
//use for private methods and variables
namespace {
GLFWwindow* window;
std::vector<int> keysPressed;
std::vector<int> keysReleased;
std::vector<int> keysHeld;
std::vector<int> mouseButtonPressed;
std::vector<int> mouseButtonReleased;
std::vector<int> mouseButtonHeld;
glm::vec2 scrollDelta;
glm::vec2 mousePosition;
bool mouseOverUI = false;
void MouseCallback(GLFWwindow* window, double xpos, double ypos) {
mousePosition = glm::vec2(xpos, ypos);
}
void ScrollCallback(GLFWwindow* window, double xOffset, double yOffset) {
scrollDelta = glm::vec2(xOffset, yOffset);
}
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
keysPressed.push_back(key);
keysHeld.push_back(key);
}
else if (action == GLFW_RELEASE) {
keysReleased.push_back(key);
//TODO:Make faster
for (unsigned int i = 0; i < keysHeld.size(); ++i) {
if (keysHeld[i] == key) {
keysHeld.erase(keysHeld.begin() + i);
}
}
}
}
void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
if (action == GLFW_PRESS) {
mouseButtonPressed.push_back(button);
mouseButtonHeld.push_back(button);
}
else if (action == GLFW_RELEASE) {
mouseButtonReleased.push_back(button);
//TODO:Make faster
for (unsigned int i = 0; i < mouseButtonHeld.size(); ++i) {
if (mouseButtonHeld[i] == button) {
mouseButtonHeld.erase(mouseButtonHeld.begin() + i);
}
}
}
}
}
//put public methods and variables here
void InitInput(GLFWwindow* window) {
Input::window = window;
scrollDelta = glm::vec2(0.0f, 0.0f);
//init callbacks
glfwSetCursorPosCallback(window, MouseCallback);
glfwSetKeyCallback(window, KeyCallback);
glfwSetScrollCallback(window, ScrollCallback);
glfwSetMouseButtonCallback(window, MouseButtonCallback);
}
void ProcessInput() {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true);
}
}
glm::vec2 GetScrollDelta() {
return scrollDelta;
}
glm::vec2 GetMousePosition() {
return mousePosition;
}
void ClearOldInputs() {
keysPressed.clear();
keysReleased.clear();
mouseButtonPressed.clear();
mouseButtonReleased.clear();
scrollDelta = glm::vec2(0.0f, 0.0f);
}
bool GetKeyPressed(int key) {
for (unsigned int i = 0; i < keysPressed.size(); ++i) {
if (key == keysPressed[i]) {
return true;
}
}
return false;
}
bool GetKeyReleased(int key) {
for (unsigned int i = 0; i < keysReleased.size(); ++i) {
if (key == keysReleased[i]) {
return true;
}
}
return false;
}
bool GetKeyHeld(int key) {
for (unsigned int i = 0; i < keysHeld.size(); ++i) {
if (key == keysHeld[i]) {
return true;
}
}
return false;
}
bool GetMouseButtonPressed(int button) {
for (unsigned int i = 0; i < mouseButtonPressed.size(); ++i) {
if (button == mouseButtonPressed[i]) {
return true;
}
}
return false;
}
bool GetMouseButtonReleased(int button) {
for (unsigned int i = 0; i < mouseButtonReleased.size(); ++i) {
if (button == mouseButtonReleased[i]) {
return true;
}
}
return false;
}
bool GetMouseButtonHeld(int button) {
for (unsigned int i = 0; i < mouseButtonHeld.size(); ++i) {
if (button == mouseButtonHeld[i]) {
return true;
}
}
return false;
}
void MouseIsOverUI() {
mouseOverUI = true;
}
void MouseNotOverUI() {
mouseOverUI = false;
}
bool GetMouseOverUI() {
return mouseOverUI;
}
}