author | nemo |
Mon, 12 Oct 2009 19:40:01 +0000 | |
changeset 2435 | 7afc9edac4cf |
parent 2428 | 6800f8aa0184 |
child 2436 | 246ef6271470 |
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 |
|
4 | 61 |
var tkbd: TKeyboardState; |
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 |
||
2428 | 73 |
|
4 | 74 |
procedure ProcessKbd; |
2428 | 75 |
var i, j, k: LongInt; |
4 | 76 |
pkbd: PByteArray; |
167 | 77 |
Trusted: boolean; |
2428 | 78 |
s: shortstring; |
4 | 79 |
begin |
2428 | 80 |
|
81 |
hideAmmoMenu:= false; |
|
167 | 82 |
Trusted:= (CurrentTeam <> nil) |
351 | 83 |
and (not CurrentTeam^.ExtDriven) |
846 | 84 |
and (CurrentHedgehog^.BotLevel = 0); |
161 | 85 |
|
2428 | 86 |
// move cursor/camera |
87 |
// TODO: Scale on screen dimensions and/or axis value (game controller)? |
|
88 |
movecursor(5 * CursorMovementX, 5 * CursorMovementY); |
|
2152 | 89 |
{$IFDEF SDL13} |
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
|
90 |
pkbd := SDL_GetKeyboardState(nil); |
2328 | 91 |
i := SDL_GetMouseState(0, nil, nil); |
2152 | 92 |
{$ELSE} |
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
|
93 |
pkbd := SDL_GetKeyState(nil); |
2328 | 94 |
i := SDL_GetMouseState(nil, nil); |
2152 | 95 |
{$ENDIF} |
2379 | 96 |
// mouse buttons |
2152 | 97 |
{$IFDEF DARWIN} |
2328 | 98 |
pkbd^[1]:= ((i and 1) and not (pkbd^[306] or pkbd^[305])); |
99 |
pkbd^[3]:= ((i and 1) and (pkbd^[306] or pkbd^[305])) or (i and 4); |
|
2152 | 100 |
{$ELSE} |
2328 | 101 |
pkbd^[1]:= (i and 1); |
161 | 102 |
pkbd^[3]:= ((i shr 2) and 1); |
2152 | 103 |
{$ENDIF} |
2328 | 104 |
pkbd^[2]:= ((i shr 1) and 1); |
105 |
||
2379 | 106 |
// mouse wheels (see event loop in project file) |
107 |
pkbd^[4]:= ord(wheelDown); |
|
108 |
pkbd^[5]:= ord(wheelUp); |
|
109 |
wheelUp:= false; |
|
110 |
wheelDown:= false; |
|
2152 | 111 |
|
2428 | 112 |
// Controller(s) |
113 |
k:= 500; // should we test k for hitting the limit? sounds rather unlikely to ever reach it |
|
114 |
for j:= 0 to Pred(ControllerNumControllers) do |
|
115 |
begin |
|
116 |
for i:= 0 to Pred(ControllerNumAxes[j]) do |
|
117 |
begin |
|
118 |
if ControllerAxes[j][i] > 20000 then pkbd^[k + 0]:= 1 else pkbd^[k + 0]:= 0; |
|
119 |
if ControllerAxes[j][i] < -20000 then pkbd^[k + 1]:= 1 else pkbd^[k + 1]:= 0; |
|
120 |
inc(k, 2); |
|
121 |
end; |
|
122 |
for i:= 0 to Pred(ControllerNumHats[j]) do |
|
123 |
begin |
|
124 |
pkbd^[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP; |
|
125 |
pkbd^[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT; |
|
126 |
pkbd^[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN; |
|
127 |
pkbd^[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT; |
|
128 |
inc(k, 4); |
|
129 |
end; |
|
130 |
for i:= 0 to Pred(ControllerNumButtons[j]) do |
|
131 |
begin |
|
132 |
pkbd^[k]:= ControllerButtons[j][i]; |
|
133 |
inc(k, 1); |
|
134 |
end; |
|
135 |
end; |
|
136 |
||
2379 | 137 |
// now process strokes |
4 | 138 |
for i:= 1 to cKeyMaxIndex do |
947 | 139 |
if CurrentBinds[i][0] <> #0 then |
140 |
begin |
|
2428 | 141 |
if (i > 3) and (pkbd^[i] <> 0) and not (hideAmmoMenu or (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; |
947 | 142 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted) |
143 |
else if (CurrentBinds[i][1] = '+') |
|
948 | 144 |
and (pkbd^[i] = 0) |
145 |
and (tkbd[i] <> 0) then |
|
947 | 146 |
begin |
147 |
s:= CurrentBinds[i]; |
|
148 |
s[1]:= '-'; |
|
149 |
ParseCommand(s, Trusted) |
|
150 |
end; |
|
151 |
tkbd[i]:= pkbd^[i] |
|
2428 | 152 |
end; |
4 | 153 |
end; |
154 |
||
155 |
procedure ResetKbd; |
|
2428 | 156 |
var i, j, k, t: LongInt; |
4 | 157 |
pkbd: PByteArray; |
158 |
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
|
159 |
|
2152 | 160 |
{$IFDEF SDL13} |
161 |
pkbd:= PByteArray(SDL_GetKeyboardState(@i)); |
|
162 |
{$ELSE} |
|
4 | 163 |
pkbd:= PByteArray(SDL_GetKeyState(@i)); |
2152 | 164 |
{$ENDIF} |
109 | 165 |
TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true); |
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
|
166 |
|
2428 | 167 |
k:= 500; |
168 |
for j:= 0 to Pred(ControllerNumControllers) do |
|
169 |
begin |
|
170 |
for i:= 0 to Pred(ControllerNumAxes[j]) do |
|
171 |
begin |
|
172 |
pkbd^[k + 0]:= 0; |
|
173 |
pkbd^[k + 1]:= 0; |
|
174 |
inc(k, 2); |
|
175 |
end; |
|
176 |
for i:= 0 to Pred(ControllerNumHats[j]) do |
|
177 |
begin |
|
178 |
pkbd^[k + 0]:= 0; |
|
179 |
pkbd^[k + 1]:= 0; |
|
180 |
pkbd^[k + 2]:= 0; |
|
181 |
pkbd^[k + 3]:= 0; |
|
182 |
inc(k, 4); |
|
183 |
end; |
|
184 |
for i:= 0 to Pred(ControllerNumButtons[j]) do |
|
185 |
begin |
|
186 |
pkbd^[k]:= 0; |
|
187 |
inc(k, 1); |
|
188 |
end; |
|
189 |
end; |
|
190 |
||
4 | 191 |
for t:= 0 to Pred(i) do |
2284 | 192 |
tkbd[i]:= pkbd^[i] |
4 | 193 |
end; |
194 |
||
195 |
procedure InitKbdKeyTable; |
|
2428 | 196 |
var i, j, k, t: LongInt; |
4 | 197 |
s: string[15]; |
198 |
begin |
|
199 |
KeyNames[1]:= 'mousel'; |
|
200 |
KeyNames[2]:= 'mousem'; |
|
201 |
KeyNames[3]:= 'mouser'; |
|
2379 | 202 |
KeyNames[4]:= 'wheelup'; |
203 |
KeyNames[5]:= 'wheeldown'; |
|
204 |
||
205 |
for i:= 6 to cKeyMaxIndex do |
|
4 | 206 |
begin |
207 |
s:= SDL_GetKeyName(i); |
|
2379 | 208 |
//addfilelog(inttostr(i) + ' ' + s); |
4 | 209 |
if s = 'unknown key' then KeyNames[i]:= '' |
210 |
else begin |
|
211 |
for t:= 1 to Length(s) do |
|
212 |
if s[t] = ' ' then s[t]:= '_'; |
|
213 |
KeyNames[i]:= s |
|
214 |
end; |
|
167 | 215 |
end; |
216 |
||
2428 | 217 |
// Controller(s) |
218 |
k:= 500; |
|
219 |
for j:= 0 to Pred(ControllerNumControllers) do |
|
220 |
begin |
|
221 |
for i:= 0 to Pred(ControllerNumAxes[j]) do |
|
222 |
begin |
|
223 |
keynames[k + 0]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'u'; |
|
224 |
keynames[k + 1]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'd'; |
|
225 |
inc(k, 2); |
|
226 |
end; |
|
227 |
for i:= 0 to Pred(ControllerNumHats[j]) do |
|
228 |
begin |
|
229 |
keynames[k + 0]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'u'; |
|
230 |
keynames[k + 1]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'r'; |
|
231 |
keynames[k + 2]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'd'; |
|
232 |
keynames[k + 3]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'l'; |
|
233 |
inc(k, 4); |
|
234 |
end; |
|
235 |
for i:= 0 to Pred(ControllerNumButtons[j]) do |
|
236 |
begin |
|
237 |
keynames[k]:= 'j' + inttostr(j) + 'b' + inttostr(i); |
|
238 |
inc(k, 1); |
|
239 |
end; |
|
240 |
end; |
|
241 |
||
2379 | 242 |
DefaultBinds[ 27]:= 'quit'; |
243 |
DefaultBinds[ 96]:= 'history'; |
|
244 |
DefaultBinds[127]:= 'rotmask'; |
|
245 |
||
246 |
DefaultBinds[KeyNameToCode('0')]:= '+volup'; |
|
247 |
DefaultBinds[KeyNameToCode('9')]:= '+voldown'; |
|
248 |
DefaultBinds[KeyNameToCode('c')]:= 'capture'; |
|
249 |
DefaultBinds[KeyNameToCode('h')]:= 'findhh'; |
|
250 |
DefaultBinds[KeyNameToCode('p')]:= 'pause'; |
|
251 |
DefaultBinds[KeyNameToCode('s')]:= '+speedup'; |
|
252 |
DefaultBinds[KeyNameToCode('t')]:= 'chat'; |
|
253 |
DefaultBinds[KeyNameToCode('y')]:= 'confirm'; |
|
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
254 |
|
2407
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
255 |
DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset'; |
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
256 |
DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomout'; |
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
257 |
DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomin'; |
9f413bd5150e
- Fix mouse cursor bugs in net game (still has a bug near water)
unc0rr
parents:
2379
diff
changeset
|
258 |
|
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
259 |
DefaultBinds[KeyNameToCode('f12')]:= 'fullscr'; |
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
260 |
|
167 | 261 |
SetDefaultBinds |
4 | 262 |
end; |
263 |
||
167 | 264 |
procedure SetBinds(var binds: TBinds); |
265 |
begin |
|
266 |
CurrentBinds:= binds |
|
267 |
end; |
|
268 |
||
269 |
procedure SetDefaultBinds; |
|
270 |
begin |
|
271 |
CurrentBinds:= DefaultBinds |
|
272 |
end; |
|
273 |
||
948 | 274 |
procedure FreezeEnterKey; |
275 |
begin |
|
276 |
tkbd[13]:= 1; |
|
277 |
tkbd[271]:= 1 |
|
278 |
end; |
|
167 | 279 |
|
2428 | 280 |
var Controller: array [0..5] of PSDLJoystick; |
281 |
||
282 |
procedure ControllerInit; |
|
283 |
var i, j: Integer; |
|
284 |
begin |
|
285 |
ControllerEnabled:= 0; |
|
286 |
ControllerNumControllers:= SDL_NumJoysticks; |
|
287 |
||
288 |
if ControllerNumControllers > 6 then ControllerNumControllers:= 6; |
|
289 |
||
290 |
WriteLnToConsole('Number of game controllers: ' + inttostr(ControllerNumControllers)); |
|
291 |
||
292 |
if ControllerNumControllers > 0 then |
|
293 |
begin |
|
294 |
for j:= 0 to pred(ControllerNumControllers) do |
|
295 |
begin |
|
296 |
WriteLnToConsole('Using game controller: ' + SDL_JoystickName(j)); |
|
297 |
Controller[j]:= SDL_JoystickOpen(j); |
|
298 |
if Controller[j] = nil then |
|
299 |
WriteLnToConsole('* Failed to open game controller!') |
|
300 |
else |
|
301 |
begin |
|
302 |
ControllerNumAxes[j]:= SDL_JoystickNumAxes(Controller[j]); |
|
303 |
//ControllerNumBalls[j]:= SDL_JoystickNumBalls(Controller[j]); |
|
304 |
ControllerNumHats[j]:= SDL_JoystickNumHats(Controller[j]); |
|
305 |
ControllerNumButtons[j]:= SDL_JoystickNumButtons(Controller[j]); |
|
306 |
WriteLnToConsole('* Number of axes: ' + inttostr(ControllerNumAxes[j])); |
|
307 |
//WriteLnToConsole('* Number of balls: ' + inttostr(ControllerNumBalls[j])); |
|
308 |
WriteLnToConsole('* Number of hats: ' + inttostr(ControllerNumHats[j])); |
|
309 |
WriteLnToConsole('* Number of buttons: ' + inttostr(ControllerNumButtons[j])); |
|
310 |
ControllerEnabled:= 1; |
|
311 |
||
312 |
if ControllerNumAxes[j] > 20 then ControllerNumAxes[j]:= 20; |
|
313 |
//if ControllerNumBalls[j] > 20 then ControllerNumBalls[j]:= 20; |
|
314 |
if ControllerNumHats[j] > 20 then ControllerNumHats[j]:= 20; |
|
315 |
if ControllerNumButtons[j] > 20 then ControllerNumButtons[j]:= 20; |
|
316 |
||
317 |
// reset all buttons/axes |
|
318 |
for i:= 0 to pred(ControllerNumAxes[j]) do |
|
319 |
ControllerAxes[j][i]:= 0; |
|
320 |
(*for i:= 0 to pred(ControllerNumBalls[j]) do |
|
321 |
begin |
|
322 |
ControllerBalls[j][i][0]:= 0; |
|
323 |
ControllerBalls[j][i][1]:= 0; |
|
324 |
end;*) |
|
325 |
for i:= 0 to pred(ControllerNumHats[j]) do |
|
326 |
ControllerHats[j][i]:= SDL_HAT_CENTERED; |
|
327 |
for i:= 0 to pred(ControllerNumButtons[j]) do |
|
328 |
ControllerButtons[j][i]:= 0; |
|
329 |
end; |
|
330 |
end; |
|
331 |
// enable event generation/controller updating |
|
332 |
SDL_JoystickEventState(1); |
|
333 |
end |
|
334 |
else |
|
335 |
WriteLnToConsole('Not using any game controller'); |
|
336 |
end; |
|
337 |
||
338 |
procedure ControllerClose; |
|
339 |
var j: Integer; |
|
340 |
begin |
|
341 |
if ControllerEnabled > 0 then |
|
342 |
for j:= 0 to pred(ControllerNumControllers) do |
|
343 |
SDL_JoystickClose(Controller[j]); |
|
344 |
end; |
|
345 |
||
346 |
procedure ControllerAxisEvent(joy, axis: Byte; value: Integer); |
|
347 |
begin |
|
348 |
ControllerAxes[joy][axis]:= value; |
|
349 |
end; |
|
350 |
||
351 |
procedure ControllerHatEvent(joy, hat, value: Byte); |
|
352 |
begin |
|
353 |
ControllerHats[joy][hat]:= value; |
|
354 |
end; |
|
355 |
||
356 |
procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean); |
|
357 |
begin |
|
358 |
if pressed then ControllerButtons[joy][button]:= 1 else ControllerButtons[joy][button]:= 0; |
|
359 |
end; |
|
360 |
||
4 | 361 |
initialization |
362 |
||
363 |
end. |