forked from BernardCastello/Year-2
/
EventHandle.cpp
162 lines (137 loc) · 4.8 KB
/
EventHandle.cpp
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
#include "EventHandle.h"
// Static members need to be given a value at runtime...
EventHandle* EventHandle::sm_Self = nullptr;
std::vector<int> EventHandle::s_viKey(0), EventHandle::s_viScancode(0), EventHandle::s_viAction(0), EventHandle::s_viMods(0);
std::vector<int> EventHandle::s_viMButton(0), EventHandle::s_viMAction(0), EventHandle::s_viMMods(0);
double EventHandle::s_dXOffset = NULL, EventHandle::s_dYOffset = NULL;
double EventHandle::s_dCursorX = NULL, EventHandle::s_dCursorY = NULL;
double EventHandle::s_dPrevCursorX = NULL, EventHandle::s_dPrevCursorY = NULL;
double EventHandle::s_dXPos = NULL, EventHandle::s_dYPos = NULL;
// For Key Presses
void EventHandle::KeyCallback(GLFWwindow* a_Window, int a_iKey, int a_iScancode, int a_iAction, int a_iMods)
{
sm_Self->s_viKey.push_back(a_iKey);
sm_Self->s_viScancode.push_back(a_iScancode);
sm_Self->s_viAction.push_back(a_iAction);
sm_Self->s_viMods.push_back(a_iMods);
}
void EventHandle::MButtonCallback(GLFWwindow* a_Window, int a_iButton, int a_iAction, int a_iMods)
{
sm_Self->s_viMButton.push_back(a_iButton);
sm_Self->s_viMAction.push_back(a_iAction);
sm_Self->s_viMMods.push_back(a_iMods);
}
// For the ScrollWheel
void EventHandle::ScrollCallback(GLFWwindow* a_Window, double a_dXOffset, double a_dYOffset)
{
sm_Self->s_dXOffset = a_dXOffset;
sm_Self->s_dXOffset = a_dYOffset;
}
// For The Cursor's x and y position in the window
void EventHandle::CursorPosCallback(GLFWwindow* a_Window, double a_dXPos, double a_dYPos)
{
sm_Self->s_dXPos = a_dXPos;
sm_Self->s_dYPos = a_dYPos;
}
void EventHandle::ParseInput()
{
unsigned int i;
// Loops through the pending key presses
for (i = 0; i < sm_Self->s_viAction.size(); ++i)
{
switch (sm_Self->s_viAction[i])
{
case GLFW_PRESS: OnKeyPress(sm_Self->s_viKey[i], sm_Self->s_viScancode[i], sm_Self->s_viMods[i]); break;
case GLFW_RELEASE: OnKeyRelease(sm_Self->s_viKey[i], sm_Self->s_viScancode[i], sm_Self->s_viMods[i]); break;
default: break;
}
}
// Loops through the pending mouse button presses
for (i = 0; i < sm_Self->s_viMAction.size(); ++i)
{
switch (sm_Self->s_viMAction[i])
{
case GLFW_PRESS: OnMButtonPress(sm_Self->s_viMButton[i], sm_Self->s_viMMods[i]); break;
case GLFW_RELEASE: OnMButtonRelease(sm_Self->s_viMButton[i], sm_Self->s_viMMods[i]); break;
default: break;
}
}
// If the ScrollWheel was moved
if ((sm_Self->s_dXOffset != NULL) || (sm_Self->s_dYOffset != NULL))
OnScroll(sm_Self->s_dXOffset, sm_Self->s_dYOffset);
// If the Cursor is in the current window
if ((sm_Self->s_dXPos != NULL) || (sm_Self->s_dYPos != NULL)) {}
// Grabs the current x and y of the cursor and passes it on to the virtual funtion
glfwGetCursorPos(Graphics::GetWin(), &sm_Self->s_dCursorX, &sm_Self->s_dCursorY);
GetCursor(sm_Self->s_dCursorX, sm_Self->s_dCursorY);
// Sends the change in 's_dCursorX' and 's_dCursorY' rather than the current values to a separate function
GetRelativeCursor(sm_Self->s_dCursorX - sm_Self->s_dPrevCursorX, sm_Self->s_dCursorY - sm_Self->s_dPrevCursorY);
// Sets the new vales of the cursor to the previous
sm_Self->s_dPrevCursorX = s_dCursorX;
sm_Self->s_dPrevCursorY = s_dCursorY;
// Since all input has been parsed, resets all the values that need to be reset for next time
sm_Self->Clear();
}
void EventHandle::OnKeyPress(int a_iKey, int a_iScancode, int a_iMods)
{
// Virtual, Do Nothing...
}
void EventHandle::OnKeyRelease(int a_iKey, int a_iScancode, int a_iMods)
{
// Virtual, Do Nothing...
}
void EventHandle::OnMButtonPress(int a_iButton, int a_iMods)
{
// Virtual, Do Nothing...
}
void EventHandle::OnMButtonRelease(int a_iButton, int a_iMods)
{
// Virtual, Do Nothing...
}
void EventHandle::OnScroll(double a_dXOffset, double a_dYOffset)
{
// Virtual, Do Nothing...
}
void EventHandle::GetCursor(double a_dCursorX, double a_dCursorY)
{
// Virtual, Do Nothing...
}
void EventHandle::GetRelativeCursor(double a_dOffsetX, double a_dOffsetY)
{
// Virtual, Do Nothing...
}
void EventHandle::PollEvents()
{
glfwPollEvents();
}
void EventHandle::Clear()
{
sm_Self->s_viKey.clear();
sm_Self->s_viScancode.clear();
sm_Self->s_viAction.clear();
sm_Self->s_viMods.clear();
sm_Self->s_viMButton.clear();
sm_Self->s_viMAction.clear();
sm_Self->s_viMMods.clear();
sm_Self->s_dXOffset = NULL;
sm_Self->s_dYOffset = NULL;
sm_Self->s_dXPos = NULL;
sm_Self->s_dYPos = NULL;
}
//Initializes the staic member and links the KeyCallback functions for OpenGL to call automatically
int EventHandle::Init()
{
sm_Self = new EventHandle();
glfwSetKeyCallback(Graphics::GetWin(), KeyCallback);
glfwSetMouseButtonCallback(Graphics::GetWin(), MButtonCallback);
glfwSetScrollCallback(Graphics::GetWin(), ScrollCallback);
glfwSetCursorPosCallback(Graphics::GetWin(), CursorPosCallback);
return 0;
}
EventHandle::EventHandle()
{
}
EventHandle::~EventHandle()
{
// Virtual, Do Nothing...
}