-
Notifications
You must be signed in to change notification settings - Fork 1
/
GT_Input.cpp
140 lines (115 loc) · 2.66 KB
/
GT_Input.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
#include <assert.h>
#include "GT_Input.h"
// Static variables
bool CWinInput::mKeys[kMaxKeys] = {0};
float CWinInput::mKeyDelayTimes[kMaxKeys] = {0};
POINT CWinInput::mPos = {0};
float CWinInput::mDelayTime = kDefaultKeyDelayTime;
// Poll the controls
void CWinInput::poll()
{
// Grab the state of each key
for(int i = 0; i < kMaxKeys; ++i)
mKeys[i] = (GetAsyncKeyState(i) & 0x8000) != 0;
GetCursorPos(&mPos); // Get cursor position
}
// Poll the controls, record time slice
void CWinInput::poll(float dt)
{
// Grab the state of each key
for(int i = 0; i < kMaxKeys; ++i)
{
mKeys[i] = (GetAsyncKeyState(i) & 0x8000) != 0;
if(mKeyDelayTimes[i] > 0.0f)
mKeyDelayTimes[i] -= dt;
}
GetCursorPos(&mPos); // Get cursor position
}
// Set a key's status
void CWinInput::setKey(int which, bool onOrOff)
{
assert(which >= 0 && which < kMaxKeys); // Safety first
mKeys[which] = onOrOff;
}
// Set a key's status
void CWinInput::setKey(WPARAM which, bool onOrOff)
{
assert((unsigned int)which < kMaxKeys); // Safety first
mKeys[(unsigned int)which] = onOrOff;
}
// Sets the mouse's position in screen coordinates
void CWinInput::setMousePos(int x, int y)
{
assert(x >= 0); // Should always be positive
assert(y >= 0); // Should always be positive
mPos.x = x;
mPos.y = y;
SetCursorPos(x, y);
}
// Sets the mouse's position in screen coordinates
void CWinInput::setMousePos(const POINT &p)
{
mPos = p;
SetCursorPos(p.x, p.y);
}
// Sets the mouse's position in client coordinates
void CWinInput::setMousePosInWindow(HWND hwnd, int x, int y)
{
assert(hwnd != NULL);
assert(x >= 0 && y >= 0);
POINT p = { x, y };
if(ClientToScreen(hwnd, &p))
mPos = p;
}
// Returns true if the key is down, false otherwise
bool CWinInput::isAlphaDown(char k)
{
if(!isalpha(k))
{
OutputDebugString("Not an ASCII alphabet character!");
return false; // Not a alpha character
}
else
{
if(islower(k))
k = toupper(k);
return mKeys[k];
}
}
// Returns true if the key is down, false otherwise
bool CWinInput::isNumericDown(char k)
{
if(!isdigit(k))
{
OutputDebugString("Not an ASCII numeric character!");
return false; // Not a numeric character
}
else
{
return mKeys[k];
}
}
// Returns true if the key down, false otherwise
bool CWinInput::isKeyDown(int k, bool useDelay)
{
assert(k >= 0 && k < kMaxKeys);
if(useDelay)
{
if(mKeys[k] && mKeyDelayTimes[k] <= 0.0f)
{
mKeyDelayTimes[k] = mDelayTime;
return true;
}
else
return false;
}
else
return mKeys[k];
}
// Return mouse position in client area of window passed in
POINT CWinInput::getMousePosInWindow(HWND hwnd)
{
POINT pos = mPos;
ScreenToClient(hwnd, &pos);
return pos;
}