-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSystem.cpp
More file actions
174 lines (147 loc) · 4.28 KB
/
InputSystem.cpp
File metadata and controls
174 lines (147 loc) · 4.28 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
165
166
167
168
169
170
171
172
#include "InputSystem.h"
InputSystem &InputSystem::GetInstance()
{
static InputSystem instance;
return instance;
}
void InputSystem::Initialize(HWND window)
{
mWindow = window;
}
// save the previous state of the keyboard and get the current state
void InputSystem::GetKeyboardState()
{
// update current keyboard state
for (unsigned int key = 0; key < NUM_KEYS; key++)
{
// save previous key state
mPreviousKeys[key] = mCurrentKeys[key];
// update current key state
unsigned int isPressed = GetAsyncKeyState(key) & 0x8000 ? 1 : 0; // GetAsyncKeyState polls the current state of the keyboard
mCurrentKeys[key] = isPressed;
}
}
// get the state of a key
InputSystem::KeyState InputSystem::GetKeyState(unsigned int key) const
{
if (mPreviousKeys[key] == 1) // key was pressed during previous frame
{
if (mCurrentKeys[key] == 1)
return KeyState::PRESSED;
else
return KeyState::JUST_RELEASED;
}
else // key wasn't pressed during the previous frame
{
if (mCurrentKeys[key] == 1)
return KeyState::JUST_PRESSED;
else
return KeyState::RELEASED;
}
}
// update mouse position
void InputSystem::GetMouseState()
{
mMousePreviousPosition = mMouseCurrentPosition; // save previous mouse position relative to client area
GetCursorPos(&mMouseCurrentPosition); // get mouse position in absolute (screen) coordinates
ScreenToClient(mWindow, &mMouseCurrentPosition); // get coordinates relative to window client area
}
POINT InputSystem::GetMouseDeltaPosition() const
{
POINT deltaPos;
deltaPos.x = mMouseCurrentPosition.x - mMousePreviousPosition.x;
deltaPos.y = mMouseCurrentPosition.y - mMousePreviousPosition.y;
return deltaPos;
}
POINT InputSystem::GetMouseAbsolutePosition() const
{
return mMouseCurrentPosition;
}
void InputSystem::PollInput()
{
// poll keyboard state
GetKeyboardState();
// poll mouse state
GetMouseState();
}
#include <ctype.h>
void InputSystem::ProcessInput()
{
if (GetMouseDeltaPosition().x != 0 || GetMouseDeltaPosition().y != 0)
mMouseMoveEvent.Invoke(mMouseCurrentPosition.x, mMouseCurrentPosition.y);
if (GetKeyState(VK_LBUTTON) == KeyState::JUST_PRESSED)
mMousePressEvent.Invoke(mMouseCurrentPosition.x, mMouseCurrentPosition.y);
if (GetKeyState(VK_LBUTTON) == KeyState::JUST_RELEASED)
mMouseReleaseEvent.Invoke(mMouseCurrentPosition.x, mMouseCurrentPosition.y);
for (int vk = 0; vk < NUM_KEYS; vk++)
if (GetKeyState(vk) == KeyState::JUST_PRESSED) // vk is the virtual key-code
{
char key;
if (vk == VK_LBUTTON || vk == VK_SHIFT || vk == VK_LSHIFT || vk == VK_RSHIFT)
continue;
else if (vk == VK_RETURN)
key = '\n';
else
key = MapVirtualKey(vk, MAPVK_VK_TO_CHAR);
if (GetKeyState(VK_SHIFT) != KeyState::PRESSED)
key = tolower(key);
mKeyPressEvent.Invoke(key);
}
else if (GetKeyState(vk) == KeyState::PRESSED) // vk is the virtual key-code
{
int key = MapVirtualKey(vk, MAPVK_VK_TO_CHAR);
if (vk == VK_LEFT || vk == VK_RIGHT || vk == VK_UP || vk == VK_DOWN || vk == VK_PRIOR || vk == VK_NEXT)
mKeyDownEvent.Invoke(vk);
else
mKeyDownEvent.Invoke(vk);
}
}
// input main callback function
LRESULT InputSystem::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_KEYDOWN:
case WM_KEYUP:
{
bool isDown = !(lParam & 1 << 31);
bool wasDown = lParam & 1 << 30;
if (wParam == 'O' && isDown && wasDown)
{
}
}
return 0;
case WM_CHAR:
// switch over keyboard characters
return 0;
case WM_LBUTTONDOWN:
//mMousePressedEvent.Invoke(mMouseCurrentPosition.x, mMouseCurrentPosition.y);
return 0;
case WM_RBUTTONDOWN:
return 0;
case WM_MBUTTONDOWN:
return 0;
case WM_MOUSEWHEEL:
return 0;
case WM_MOUSEMOVE:
//mMouseMovedEvent.Invoke(mMouseCurrentPosition.x, mMouseCurrentPosition.y);
return 0;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
// Windows event handler
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
if (MessageBox(hWnd, L"are you sure you want to quit?", L"Quit", MB_YESNO) == IDYES)
PostQuitMessage(0);
return 0;
default:
return InputSystem::GetInstance().WndProc(hWnd, msg, wParam, lParam); // delegate to input system class
}
}