- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGT_Input.cpp
More file actions
Latest commit
140 lines (115 loc) · 2.66 KB
/
Copy pathGT_Input.cpp
File metadata and controls
140 lines (115 loc) · 2.66 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
#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
voidCWinInput::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
voidCWinInput::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
voidCWinInput::setKey(int which, bool onOrOff)
{
assert(which >= 0 && which < kMaxKeys); // Safety first
mKeys[which] = onOrOff;
}
// Set a key's status
voidCWinInput::setKey(WPARAM which, bool onOrOff)
{
assert((unsignedint)which < kMaxKeys); // Safety first
mKeys[(unsignedint)which] = onOrOff;
}
// Sets the mouse's position in screen coordinates
voidCWinInput::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
voidCWinInput::setMousePos(constPOINT &p)
{
mPos = p;
SetCursorPos(p.x, p.y);
}
// Sets the mouse's position in client coordinates
voidCWinInput::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
boolCWinInput::isAlphaDown(char k)
{
if(!isalpha(k))
{
OutputDebugString("Not an ASCII alphabet character!");
returnfalse; // Not a alpha character
}
else
{
if(islower(k))
k = toupper(k);
returnmKeys[k];
}
}
// Returns true if the key is down, false otherwise
boolCWinInput::isNumericDown(char k)
{
if(!isdigit(k))
{
OutputDebugString("Not an ASCII numeric character!");
returnfalse; // Not a numeric character
}
else
{
returnmKeys[k];
}
}
// Returns true if the key down, false otherwise
boolCWinInput::isKeyDown(int k, bool useDelay)
{
assert(k >= 0 && k < kMaxKeys);
if(useDelay)
{
if(mKeys[k] && mKeyDelayTimes[k] <= 0.0f)
{
mKeyDelayTimes[k] = mDelayTime;
returntrue;
}
else
returnfalse;
}
else
returnmKeys[k];
}
// Return mouse position in client area of window passed in
POINTCWinInput::getMousePosInWindow(HWND hwnd)
{
POINT pos = mPos;
ScreenToClient(hwnd, &pos);
return pos;
}