author | displacer |
Wed, 19 Dec 2007 11:10:23 +0000 | |
changeset 681 | 7a20c50988ec |
parent 626 | a30171dbcd19 |
child 846 | 08c3f0bc766c |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
393 | 3 |
* Copyright (c) 2004-2007 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; |
|
29 |
procedure InitKbdKeyTable; |
|
30 |
||
167 | 31 |
procedure SetBinds(var binds: TBinds); |
32 |
procedure SetDefaultBinds; |
|
33 |
||
161 | 34 |
var KbdKeyPressed: boolean; |
35 |
||
4 | 36 |
implementation |
167 | 37 |
uses SDLh, uTeams, uConsole, uMisc; |
109 | 38 |
const KeyNumber = 1024; |
39 |
type TKeyboardState = array[0..cKeyMaxIndex] of Byte; |
|
167 | 40 |
|
4 | 41 |
var tkbd: TKeyboardState; |
42 |
KeyNames: array [0..cKeyMaxIndex] of string[15]; |
|
167 | 43 |
DefaultBinds, CurrentBinds: TBinds; |
4 | 44 |
|
45 |
function KeyNameToCode(name: string): word; |
|
351 | 46 |
var Result: Word; |
4 | 47 |
begin |
48 |
Result:= cKeyMaxIndex; |
|
351 | 49 |
while (Result > 0) and (KeyNames[Result] <> name) do dec(Result); |
50 |
KeyNameToCode:= Result |
|
4 | 51 |
end; |
52 |
||
53 |
procedure ProcessKbd; |
|
371 | 54 |
var i: LongInt; |
4 | 55 |
s: shortstring; |
56 |
pkbd: PByteArray; |
|
167 | 57 |
Trusted: boolean; |
4 | 58 |
begin |
167 | 59 |
KbdKeyPressed:= false; |
60 |
Trusted:= (CurrentTeam <> nil) |
|
351 | 61 |
and (not CurrentTeam^.ExtDriven) |
62 |
and (CurrentTeam^.Hedgehogs[CurrentTeam^.CurrHedgehog].BotLevel = 0); |
|
161 | 63 |
|
4 | 64 |
pkbd:= SDL_GetKeyState(nil); |
65 |
i:= SDL_GetMouseState(nil, nil); |
|
66 |
pkbd^[1]:= (i and 1); |
|
161 | 67 |
pkbd^[2]:= ((i shr 1) and 1); |
68 |
pkbd^[3]:= ((i shr 2) and 1); |
|
4 | 69 |
for i:= 1 to cKeyMaxIndex do |
167 | 70 |
if CurrentBinds[i][0] <> #0 then |
161 | 71 |
begin |
72 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true; |
|
167 | 73 |
if CurrentBinds[i][1] = '+' then |
4 | 74 |
begin |
167 | 75 |
if (pkbd^[i] <> 0)and(tkbd[i] = 0) then ParseCommand(CurrentBinds[i], Trusted) else |
4 | 76 |
if (pkbd^[i] = 0)and(tkbd[i] <> 0) then |
77 |
begin |
|
167 | 78 |
s:= CurrentBinds[i]; |
4 | 79 |
s[1]:= '-'; |
351 | 80 |
ParseCommand(s, true) |
4 | 81 |
end; |
82 |
end else |
|
167 | 83 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted); |
4 | 84 |
tkbd[i]:= pkbd^[i] |
85 |
end |
|
86 |
end; |
|
87 |
||
88 |
procedure ResetKbd; |
|
371 | 89 |
var i, t: LongInt; |
4 | 90 |
pkbd: PByteArray; |
91 |
begin |
|
92 |
pkbd:= PByteArray(SDL_GetKeyState(@i)); |
|
109 | 93 |
TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true); |
4 | 94 |
for t:= 0 to Pred(i) do |
95 |
tkbd[i]:= pkbd^[i] |
|
96 |
end; |
|
97 |
||
98 |
procedure InitKbdKeyTable; |
|
371 | 99 |
var i, t: LongInt; |
4 | 100 |
s: string[15]; |
101 |
begin |
|
102 |
KeyNames[1]:= 'mousel'; |
|
103 |
KeyNames[2]:= 'mousem'; |
|
104 |
KeyNames[3]:= 'mouser'; |
|
105 |
for i:= 4 to cKeyMaxIndex do |
|
106 |
begin |
|
107 |
s:= SDL_GetKeyName(i); |
|
108 |
if s = 'unknown key' then KeyNames[i]:= '' |
|
109 |
else begin |
|
110 |
for t:= 1 to Length(s) do |
|
111 |
if s[t] = ' ' then s[t]:= '_'; |
|
112 |
KeyNames[i]:= s |
|
113 |
end; |
|
167 | 114 |
end; |
115 |
||
116 |
DefaultBinds[ 27]:= 'quit'; |
|
175 | 117 |
DefaultBinds[ 48]:= '+volup'; |
118 |
DefaultBinds[ 57]:= '+voldown'; |
|
167 | 119 |
DefaultBinds[ 99]:= 'capture'; |
120 |
DefaultBinds[102]:= 'fullscr'; |
|
176 | 121 |
DefaultBinds[104]:= 'findhh'; |
299 | 122 |
DefaultBinds[112]:= 'pause'; |
626 | 123 |
DefaultBinds[115]:= '+speedup'; |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
393
diff
changeset
|
124 |
DefaultBinds[127]:= 'rotmask'; |
167 | 125 |
SetDefaultBinds |
4 | 126 |
end; |
127 |
||
167 | 128 |
procedure SetBinds(var binds: TBinds); |
129 |
begin |
|
130 |
CurrentBinds:= binds |
|
131 |
end; |
|
132 |
||
133 |
procedure SetDefaultBinds; |
|
134 |
begin |
|
135 |
CurrentBinds:= DefaultBinds |
|
136 |
end; |
|
137 |
||
138 |
||
4 | 139 |
initialization |
140 |
||
141 |
end. |