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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
AUTHOR: John DiCamillo
OVERVIEW
========
Keyboard Input class
*/
#include "Keyboard.h"
#include "Game.h"
// +--------------------------------------------------------------------+
static Keyboard* instance = 0;
int Keyboard::map[KEY_MAP_SIZE];
int Keyboard::alt[KEY_MAP_SIZE];
Keyboard::Keyboard()
: x(0), y(0), z(0), p(0), r(0), w(0), c(0), p1(0), r1(0), w1(0), t(0)
{
instance = this;
sensitivity = 25;
dead_zone = 100;
for (int i = 0; i < MotionController::MaxActions; i++)
action[i] = 0;
memset(map, 0, sizeof(map));
memset(alt, 0, sizeof(alt));
map[KEY_PLUS_X] = 'R';
map[KEY_MINUS_X] = 'E';
map[KEY_PLUS_Y] = VK_HOME;
map[KEY_MINUS_Y] = VK_END;
map[KEY_PLUS_Z] = VK_PRIOR; // page up
map[KEY_MINUS_Z] = VK_NEXT; // page down
map[KEY_PITCH_UP] = VK_DOWN;
map[KEY_PITCH_DOWN] = VK_UP;
map[KEY_YAW_LEFT] = VK_LEFT;
map[KEY_YAW_RIGHT] = VK_RIGHT;
map[KEY_ROLL_ENABLE] = 0; // used to be VK_CONTROL;
}
Keyboard::~Keyboard()
{
instance = 0;
}
Keyboard*
Keyboard::GetInstance()
{
return instance;
}
// +--------------------------------------------------------------------+
void
Keyboard::MapKeys(KeyMapEntry* mapping, int nkeys)
{
for (int i = 0; i < nkeys; i++) {
KeyMapEntry k = mapping[i];
if (k.act >= KEY_MAP_FIRST && k.act <= KEY_MAP_LAST) {
if (k.key == 0 || k.key > VK_MBUTTON && k.key < KEY_JOY_1) {
map[k.act] = k.key;
alt[k.act] = k.alt;
}
}
}
}
// +--------------------------------------------------------------------+
bool Keyboard::KeyDown(int key)
{
if (key) {
short k = GetAsyncKeyState(key);
return (k<0)||(k&1);
}
return 0;
}
// +--------------------------------------------------------------------+
bool Keyboard::KeyDownMap(int key)
{
if (key >= KEY_MAP_FIRST && key <= KEY_MAP_LAST && map[key]) {
short k = GetAsyncKeyState(map[key]);
short a = -1;
if (alt[key] > 0 && alt[key] < KEY_JOY_1) {
a = GetAsyncKeyState(alt[key]);
}
else {
a = !GetAsyncKeyState(VK_SHIFT) &&
!GetAsyncKeyState(VK_MENU);
}
return ((k<0)||(k&1)) && ((a<0)||(a&1));
}
return 0;
}
// +--------------------------------------------------------------------+
void
Keyboard::FlushKeys()
{
for (int i = 0; i < 255; i++)
GetAsyncKeyState(i);
}
// +--------------------------------------------------------------------+
static inline double sqr(double a) { return a; } //*a; }
void
Keyboard::Acquire()
{
t = x = y = z = p = r = w = c = 0;
for (int i = 0; i < MotionController::MaxActions; i++)
action[i] = 0;
int speed = 10;
// lateral translations:
if (KeyDownMap(KEY_PLUS_Y)) y = 1;
else if (KeyDownMap(KEY_MINUS_Y)) y = -1;
if (KeyDownMap(KEY_PLUS_Z)) z = 1;
else if (KeyDownMap(KEY_MINUS_Z)) z = -1;
if (KeyDownMap(KEY_MINUS_X)) x = -1;
else if (KeyDownMap(KEY_PLUS_X)) x = 1;
const double steps=10;
// if roll and yaw are swapped --------------------------
if (swapped) {
// yaw:
if (KeyDownMap(KEY_ROLL_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); }
else if (KeyDownMap(KEY_ROLL_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); }
// another way to yaw:
if (KeyDownMap(KEY_ROLL_ENABLE)) {
if (KeyDownMap(KEY_YAW_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); }
else if (KeyDownMap(KEY_YAW_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); }
else w1 = 0;
}
// roll:
else {
if (KeyDownMap(KEY_YAW_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); }
else if (KeyDownMap(KEY_YAW_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); }
else r1 = 0;
}
}
// else roll and yaw are NOT swapped ---------------------
else {
// roll:
if (KeyDownMap(KEY_ROLL_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); }
else if (KeyDownMap(KEY_ROLL_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); }
// another way to roll:
if (KeyDownMap(KEY_ROLL_ENABLE)) {
if (KeyDownMap(KEY_YAW_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); }
else if (KeyDownMap(KEY_YAW_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); }
else r1 = 0;
}
// yaw left-right
else {
if (KeyDownMap(KEY_YAW_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); }
else if (KeyDownMap(KEY_YAW_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); }
else w1 = 0;
}
}
// if pitch is inverted ----------------------------------
if (inverted) {
if (KeyDownMap(KEY_PITCH_DOWN)) { if (p1<steps) p1+=1; p = -sqr(p1/steps); }
else if (KeyDownMap(KEY_PITCH_UP)) { if (p1<steps) p1+=1; p = sqr(p1/steps); }
else p1 = 0;
}
// else pitch is NOT inverted ----------------------------
else {
if (KeyDownMap(KEY_PITCH_UP)) { if (p1<steps) p1+=1; p = -sqr(p1/steps); }
else if (KeyDownMap(KEY_PITCH_DOWN)) { if (p1<steps) p1+=1; p = sqr(p1/steps); }
else p1 = 0;
}
if (KeyDownMap(KEY_CENTER)) c = 1;
// actions
if (KeyDownMap(KEY_ACTION_0)) action[0] = 1;
if (KeyDownMap(KEY_ACTION_1)) action[1] = 1;
if (KeyDownMap(KEY_ACTION_2)) action[2] = 1;
if (KeyDownMap(KEY_ACTION_3)) action[3] = 1;
}
// +--------------------------------------------------------------------+
|