author | koda |
Wed, 14 Oct 2009 22:21:25 +0000 | |
changeset 2444 | ace11b7d8eab |
parent 2438 | 6df2e58b6ab2 |
child 2567 | 02ff5f9510b5 |
permissions | -rw-r--r-- |
4 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2004-2008 Andrey Korotaev <unC0Rr@gmail.com> |
4 | 4 |
* |
183 | 5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
4 | 8 |
* |
183 | 9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
4 | 13 |
* |
183 | 14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
4 | 17 |
*) |
18 |
||
19 |
unit uKeys; |
|
20 |
interface |
|
345 | 21 |
uses uConsts; |
4 | 22 |
{$INCLUDE options.inc} |
167 | 23 |
|
24 |
type TBinds = array[0..cKeyMaxIndex] of shortstring; |
|
4 | 25 |
|
26 |
function KeyNameToCode(name: string): word; |
|
27 |
procedure ProcessKbd; |
|
28 |
procedure ResetKbd; |
|
948 | 29 |
procedure FreezeEnterKey; |
4 | 30 |
procedure InitKbdKeyTable; |
31 |
||
167 | 32 |
procedure SetBinds(var binds: TBinds); |
33 |
procedure SetDefaultBinds; |
|
34 |
||
2428 | 35 |
procedure ControllerInit; |
36 |
procedure ControllerClose; |
|
37 |
procedure ControllerAxisEvent(joy, axis: Byte; value: Integer); |
|
38 |
procedure ControllerHatEvent(joy, hat, value: Byte); |
|
39 |
procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean); |
|
40 |
||
41 |
var hideAmmoMenu: boolean; |
|
2379 | 42 |
wheelUp: boolean = false; |
43 |
wheelDown: boolean = false; |
|
161 | 44 |
|
2428 | 45 |
ControllerNumControllers: Integer; |
46 |
ControllerEnabled: Integer; |
|
47 |
ControllerNumAxes: array[0..5] of Integer; |
|
48 |
//ControllerNumBalls: array[0..5] of Integer; |
|
49 |
ControllerNumHats: array[0..5] of Integer; |
|
50 |
ControllerNumButtons: array[0..5] of Integer; |
|
51 |
ControllerAxes: array[0..5] of array[0..19] of Integer; |
|
52 |
//ControllerBalls: array[0..5] of array[0..19] of array[0..1] of Integer; |
|
53 |
ControllerHats: array[0..5] of array[0..19] of Byte; |
|
54 |
ControllerButtons: array[0..5] of array[0..19] of Byte; |
|
55 |
||
4 | 56 |
implementation |
2222 | 57 |
uses SDLh, uTeams, uConsole, uMisc, uStore; |
109 | 58 |
const KeyNumber = 1024; |
59 |
type TKeyboardState = array[0..cKeyMaxIndex] of Byte; |
|
167 | 60 |
|
2436 | 61 |
var tkbd, tkbdn: TKeyboardState; |
4 | 62 |
KeyNames: array [0..cKeyMaxIndex] of string[15]; |
167 | 63 |
DefaultBinds, CurrentBinds: TBinds; |
4 | 64 |
|
65 |
function KeyNameToCode(name: string): word; |
|
351 | 66 |
var Result: Word; |
4 | 67 |
begin |
68 |
Result:= cKeyMaxIndex; |
|
351 | 69 |
while (Result > 0) and (KeyNames[Result] <> name) do dec(Result); |
70 |
KeyNameToCode:= Result |
|
4 | 71 |
end; |
72 |
||
73 |
procedure ProcessKbd; |
|
2428 | 74 |
var i, j, k: LongInt; |
2436 | 75 |
s: shortstring; |
4 | 76 |
pkbd: PByteArray; |
167 | 77 |
Trusted: boolean; |
4 | 78 |
begin |
2428 | 79 |
hideAmmoMenu:= false; |
167 | 80 |
Trusted:= (CurrentTeam <> nil) |
351 | 81 |
and (not CurrentTeam^.ExtDriven) |
846 | 82 |
and (CurrentHedgehog^.BotLevel = 0); |
161 | 83 |
|
2428 | 84 |
// move cursor/camera |
85 |
// TODO: Scale on screen dimensions and/or axis value (game controller)? |
|
86 |
movecursor(5 * CursorMovementX, 5 * CursorMovementY); |
|
2436 | 87 |
|
2152 | 88 |
{$IFDEF SDL13} |
2436 | 89 |
pkbd := SDL_GetKeyboardState(@j); |
90 |
k := SDL_GetMouseState(0, nil, nil); |
|
2152 | 91 |
{$ELSE} |
2436 | 92 |
pkbd := SDL_GetKeyState(@j); |
93 |
k := SDL_GetMouseState(nil, nil); |
|
2152 | 94 |
{$ENDIF} |
2436 | 95 |
|
96 |
for i:= 6 to pred(j) do // first 6 will be overwritten |
|
97 |
tkbdn[i]:= pkbd^[i]; |
|
98 |
||
2379 | 99 |
// mouse buttons |
2152 | 100 |
{$IFDEF DARWIN} |
2436 | 101 |
tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305])); |
102 |
tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4); |
|
2152 | 103 |
{$ELSE} |
2436 | 104 |
tkbdn[1]:= (k and 1); |
105 |
tkbdn[3]:= ((k shr 2) and 1); |
|
2152 | 106 |
{$ENDIF} |
2436 | 107 |
tkbdn[2]:= ((k shr 1) and 1); |
2328 | 108 |
|
2379 | 109 |
// mouse wheels (see event loop in project file) |
2436 | 110 |
tkbdn[4]:= ord(wheelDown); |
111 |
tkbdn[5]:= ord(wheelUp); |
|
2379 | 112 |
wheelUp:= false; |
113 |
wheelDown:= false; |
|
2152 | 114 |
|
2428 | 115 |
// Controller(s) |
2436 | 116 |
k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it |
2428 | 117 |
for j:= 0 to Pred(ControllerNumControllers) do |
118 |
begin |
|
119 |
for i:= 0 to Pred(ControllerNumAxes[j]) do |
|
120 |
begin |
|
2436 | 121 |
if ControllerAxes[j][i] > 20000 then tkbdn[k + 0]:= 1 else tkbdn[k + 0]:= 0; |
122 |
if ControllerAxes[j][i] < -20000 then tkbdn[k + 1]:= 1 else tkbdn[k + 1]:= 0; |
|
2428 | 123 |
inc(k, 2); |
124 |
end; |
|
125 |
for i:= 0 to Pred(ControllerNumHats[j]) do |
|
126 |
begin |
|
2436 | 127 |
tkbdn[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP; |
128 |
tkbdn[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT; |
|
129 |
tkbdn[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN; |
|
130 |
tkbdn[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT; |
|
2428 | 131 |
inc(k, 4); |
132 |
end; |
|
133 |
for i:= 0 to Pred(ControllerNumButtons[j]) do |
|
134 |
begin |
|
2436 | 135 |
tkbdn[k]:= ControllerButtons[j][i]; |
2428 | 136 |
inc(k, 1); |
137 |
end; |
|
138 |
end; |
|
139 |
||
2379 | 140 |
// now process strokes |
2436 | 141 |
for i:= 0 to cKeyMaxIndex do |
947 | 142 |
if CurrentBinds[i][0] <> #0 then |
143 |
begin |
|
2436 | 144 |
if (i > 3) and (tkbdn[i] <> 0) and not ((CurrentBinds[i] = 'put') or (CurrentBinds[i] = 'ammomenu') or (CurrentBinds[i] = '+cur_u') or (CurrentBinds[i] = '+cur_d') or (CurrentBinds[i] = '+cur_l') or (CurrentBinds[i] = '+cur_r')) then hideAmmoMenu:= true; |
145 |
if (tkbd[i] = 0) and (tkbdn[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted) |
|
947 | 146 |
else if (CurrentBinds[i][1] = '+') |
2436 | 147 |
and (tkbdn[i] = 0) |
948 | 148 |
and (tkbd[i] <> 0) then |
947 | 149 |
begin |
150 |
s:= CurrentBinds[i]; |
|
151 |
s[1]:= '-'; |
|
152 |
ParseCommand(s, Trusted) |
|
153 |
end; |
|
2436 | 154 |
tkbd[i]:= tkbdn[i] |
155 |
end |
|
4 | 156 |
end; |
157 |
||
158 |
procedure ResetKbd; |
|
2428 | 159 |
var i, j, k, t: LongInt; |
4 | 160 |
pkbd: PByteArray; |
161 |
begin |
|
2171
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset
|
162 |
|
2152 | 163 |
{$IFDEF SDL13} |
2436 | 164 |
pkbd:= SDL_GetKeyboardState(@j); |
165 |
k := SDL_GetMouseState(0, nil, nil); |
|
2152 | 166 |
{$ELSE} |
2436 | 167 |
pkbd:= SDL_GetKeyState(@j); |
168 |
k := SDL_GetMouseState(nil, nil); |
|
2152 | 169 |
{$ENDIF} |
2436 | 170 |
TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(j) + ')', true); |
171 |
||
172 |
for i:= 1 to pred(j) do |
|
173 |
tkbdn[i]:= pkbd^[i]; |
|
2171
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset
|
174 |
|
2436 | 175 |
// mouse buttons |
176 |
{$IFDEF DARWIN} |
|
177 |
tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305])); |
|
178 |
tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4); |
|
179 |
{$ELSE} |
|
180 |
tkbdn[1]:= (k and 1); |
|
181 |
tkbdn[3]:= ((k shr 2) and 1); |
|
182 |
{$ENDIF} |
|
183 |
tkbdn[2]:= ((k shr 1) and 1); |
|
184 |
||
185 |
// mouse wheels (see event loop in project file) |
|
186 |
tkbdn[4]:= ord(wheelDown); |
|
187 |
tkbdn[5]:= ord(wheelUp); |
|
188 |
wheelUp:= false; |
|
189 |
wheelDown:= false; |
|
190 |
||
191 |
// Controller(s) |
|
192 |
k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it |
|
2428 | 193 |
for j:= 0 to Pred(ControllerNumControllers) do |
194 |
begin |
|
195 |
for i:= 0 to Pred(ControllerNumAxes[j]) do |
|
196 |
begin |
|
2436 | 197 |
if ControllerAxes[j][i] > 20000 then tkbdn[k + 0]:= 1 else tkbdn[k + 0]:= 0; |
198 |
if ControllerAxes[j][i] < -20000 then tkbdn[k + 1]:= 1 else tkbdn[k + 1]:= 0; |
|
2428 | 199 |
inc(k, 2); |
200 |
end; |
|
201 |
for i:= 0 to Pred(ControllerNumHats[j]) do |
|
202 |
begin |
|
2436 | 203 |
tkbdn[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP; |
204 |
tkbdn[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT; |
|
205 |
tkbdn[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN; |
|
206 |
tkbdn[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT; |
|
2428 | 207 |
inc(k, 4); |
208 |
end; |
|
209 |
for i:= 0 to Pred(ControllerNumButtons[j]) do |
|
210 |
begin |
|
2436 | 211 |
tkbdn[k]:= ControllerButtons[j][i]; |
2428 | 212 |
inc(k, 1); |
213 |
end; |
|
214 |
end; |
|
2436 | 215 |
|
216 |
for t:= 0 to cKeyMaxIndex do |
|
217 |
tkbd[i]:= tkbdn[i] |
|
4 | 218 |
end; |
219 |
||
220 |
procedure InitKbdKeyTable; |
|
2428 | 221 |
var i, j, k, t: LongInt; |
4 | 222 |
s: string[15]; |
223 |
begin |
|
224 |
KeyNames[1]:= 'mousel'; |
|
225 |
KeyNames[2]:= 'mousem'; |
|
226 |
KeyNames[3]:= 'mouser'; |
|
2379 | 227 |
KeyNames[4]:= 'wheelup'; |
228 |
KeyNames[5]:= 'wheeldown'; |
|
229 |
||
230 |
for i:= 6 to cKeyMaxIndex do |
|
4 | 231 |
begin |
232 |
s:= SDL_GetKeyName(i); |
|
2379 | 233 |
//addfilelog(inttostr(i) + ' ' + s); |
4 | 234 |
if s = 'unknown key' then KeyNames[i]:= '' |
235 |
else begin |
|
236 |
for t:= 1 to Length(s) do |
|
237 |
if s[t] = ' ' then s[t]:= '_'; |
|
238 |
KeyNames[i]:= s |
|
239 |
end; |
|
167 | 240 |
end; |
241 |
||
2436 | 242 |
{$IFDEF SDL13} |
2438 | 243 |
PByteArray(SDL_GetKeyboardState(@i)); |
2436 | 244 |
{$ELSE} |
245 |
SDL_GetKeyState(@i); |
|
246 |
{$ENDIF} |
|
247 |
||
2428 | 248 |
// Controller(s) |
2436 | 249 |
k:= i; |
2428 | 250 |
for j:= 0 to Pred(ControllerNumControllers) do |
251 |
begin |
|
252 |
for i:= 0 to Pred(ControllerNumAxes[j]) do |
|
253 |
begin |
|
254 |
keynames[k + 0]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'u'; |
|
255 |
keynames[k + 1]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'd'; |
|
256 |
inc(k, 2); |
|
257 |
end; |
|
258 |
for i:= 0 to Pred(ControllerNumHats[j]) do |
|
259 |
begin |
|
260 |
keynames[k + 0]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'u'; |
|
261 |
keynames[k + 1]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'r'; |
|
262 |
keynames[k + 2]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'd'; |
|
263 |
keynames[k + 3]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'l'; |
|
264 |
inc(k, 4); |
|
265 |
end; |
|
266 |
for i:= 0 to Pred(ControllerNumButtons[j]) do |
|
267 |
begin |
|
268 |
keynames[k]:= 'j' + inttostr(j) + 'b' + inttostr(i); |
|
269 |
inc(k, 1); |
|
270 |
end; |
|
271 |
end; |
|
272 |
||
2379 | 273 |
DefaultBinds[ 27]:= 'quit'; |
274 |
DefaultBinds[ 96]:= 'history'; |
|
275 |
DefaultBinds[127]:= 'rotmask'; |
|
276 |
||
277 |
DefaultBinds[KeyNameToCode('0')]:= '+volup'; |
|
278 |
DefaultBinds[KeyNameToCode('9')]:= '+voldown'; |
|
279 |
DefaultBinds[KeyNameToCode('c')]:= 'capture'; |
|
280 |
DefaultBinds[KeyNameToCode('h')]:= 'findhh'; |
|
281 |
DefaultBinds[KeyNameToCode('p')]:= 'pause'; |
|
282 |
DefaultBinds[KeyNameToCode('s')]:= '+speedup'; |
|
283 |
DefaultBinds[KeyNameToCode('t')]:= 'chat'; |
|
284 |
DefaultBinds[KeyNameToCode('y')]:= 'confirm'; |
|
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
285 |
|
2407
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
286 |
DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset'; |
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
287 |
DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomout'; |
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
288 |
DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomin'; |
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
289 |
|
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
290 |
DefaultBinds[KeyNameToCode('f12')]:= 'fullscr'; |
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
291 |
|
167 | 292 |
SetDefaultBinds |
4 | 293 |
end; |
294 |
||
167 | 295 |
procedure SetBinds(var binds: TBinds); |
296 |
begin |
|
297 |
CurrentBinds:= binds |
|
298 |
end; |
|
299 |
||
300 |
procedure SetDefaultBinds; |
|
301 |
begin |
|
302 |
CurrentBinds:= DefaultBinds |
|
303 |
end; |
|
304 |
||
948 | 305 |
procedure FreezeEnterKey; |
306 |
begin |
|
307 |
tkbd[13]:= 1; |
|
308 |
tkbd[271]:= 1 |
|
309 |
end; |
|
167 | 310 |
|
2428 | 311 |
var Controller: array [0..5] of PSDLJoystick; |
312 |
||
313 |
procedure ControllerInit; |
|
314 |
var i, j: Integer; |
|
315 |
begin |
|
316 |
ControllerEnabled:= 0; |
|
317 |
ControllerNumControllers:= SDL_NumJoysticks; |
|
318 |
||
319 |
if ControllerNumControllers > 6 then ControllerNumControllers:= 6; |
|
320 |
||
321 |
WriteLnToConsole('Number of game controllers: ' + inttostr(ControllerNumControllers)); |
|
322 |
||
323 |
if ControllerNumControllers > 0 then |
|
324 |
begin |
|
325 |
for j:= 0 to pred(ControllerNumControllers) do |
|
326 |
begin |
|
327 |
WriteLnToConsole('Using game controller: ' + SDL_JoystickName(j)); |
|
328 |
Controller[j]:= SDL_JoystickOpen(j); |
|
329 |
if Controller[j] = nil then |
|
330 |
WriteLnToConsole('* Failed to open game controller!') |
|
331 |
else |
|
332 |
begin |
|
333 |
ControllerNumAxes[j]:= SDL_JoystickNumAxes(Controller[j]); |
|
334 |
//ControllerNumBalls[j]:= SDL_JoystickNumBalls(Controller[j]); |
|
335 |
ControllerNumHats[j]:= SDL_JoystickNumHats(Controller[j]); |
|
336 |
ControllerNumButtons[j]:= SDL_JoystickNumButtons(Controller[j]); |
|
337 |
WriteLnToConsole('* Number of axes: ' + inttostr(ControllerNumAxes[j])); |
|
338 |
//WriteLnToConsole('* Number of balls: ' + inttostr(ControllerNumBalls[j])); |
|
339 |
WriteLnToConsole('* Number of hats: ' + inttostr(ControllerNumHats[j])); |
|
340 |
WriteLnToConsole('* Number of buttons: ' + inttostr(ControllerNumButtons[j])); |
|
341 |
ControllerEnabled:= 1; |
|
342 |
||
343 |
if ControllerNumAxes[j] > 20 then ControllerNumAxes[j]:= 20; |
|
344 |
//if ControllerNumBalls[j] > 20 then ControllerNumBalls[j]:= 20; |
|
345 |
if ControllerNumHats[j] > 20 then ControllerNumHats[j]:= 20; |
|
346 |
if ControllerNumButtons[j] > 20 then ControllerNumButtons[j]:= 20; |
|
347 |
||
348 |
// reset all buttons/axes |
|
349 |
for i:= 0 to pred(ControllerNumAxes[j]) do |
|
350 |
ControllerAxes[j][i]:= 0; |
|
351 |
(*for i:= 0 to pred(ControllerNumBalls[j]) do |
|
352 |
begin |
|
353 |
ControllerBalls[j][i][0]:= 0; |
|
354 |
ControllerBalls[j][i][1]:= 0; |
|
355 |
end;*) |
|
356 |
for i:= 0 to pred(ControllerNumHats[j]) do |
|
357 |
ControllerHats[j][i]:= SDL_HAT_CENTERED; |
|
358 |
for i:= 0 to pred(ControllerNumButtons[j]) do |
|
359 |
ControllerButtons[j][i]:= 0; |
|
360 |
end; |
|
361 |
end; |
|
362 |
// enable event generation/controller updating |
|
363 |
SDL_JoystickEventState(1); |
|
364 |
end |
|
365 |
else |
|
366 |
WriteLnToConsole('Not using any game controller'); |
|
367 |
end; |
|
368 |
||
369 |
procedure ControllerClose; |
|
370 |
var j: Integer; |
|
371 |
begin |
|
372 |
if ControllerEnabled > 0 then |
|
373 |
for j:= 0 to pred(ControllerNumControllers) do |
|
374 |
SDL_JoystickClose(Controller[j]); |
|
375 |
end; |
|
376 |
||
377 |
procedure ControllerAxisEvent(joy, axis: Byte; value: Integer); |
|
378 |
begin |
|
379 |
ControllerAxes[joy][axis]:= value; |
|
380 |
end; |
|
381 |
||
382 |
procedure ControllerHatEvent(joy, hat, value: Byte); |
|
383 |
begin |
|
384 |
ControllerHats[joy][hat]:= value; |
|
385 |
end; |
|
386 |
||
387 |
procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean); |
|
388 |
begin |
|
389 |
if pressed then ControllerButtons[joy][button]:= 1 else ControllerButtons[joy][button]:= 0; |
|
390 |
end; |
|
391 |
||
4 | 392 |
initialization |
393 |
||
394 |
end. |