author | unc0rr |
Thu, 17 Sep 2009 06:37:10 +0000 | |
changeset 2386 | f462ceff8abe |
parent 2379 | d62b1f224982 |
child 2407 | 9f413bd5150e |
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 |
||
161 | 35 |
var KbdKeyPressed: boolean; |
2379 | 36 |
wheelUp: boolean = false; |
37 |
wheelDown: boolean = false; |
|
161 | 38 |
|
4 | 39 |
implementation |
2222 | 40 |
uses SDLh, uTeams, uConsole, uMisc, uStore; |
109 | 41 |
const KeyNumber = 1024; |
42 |
type TKeyboardState = array[0..cKeyMaxIndex] of Byte; |
|
167 | 43 |
|
4 | 44 |
var tkbd: TKeyboardState; |
45 |
KeyNames: array [0..cKeyMaxIndex] of string[15]; |
|
167 | 46 |
DefaultBinds, CurrentBinds: TBinds; |
4 | 47 |
|
48 |
function KeyNameToCode(name: string): word; |
|
351 | 49 |
var Result: Word; |
4 | 50 |
begin |
51 |
Result:= cKeyMaxIndex; |
|
351 | 52 |
while (Result > 0) and (KeyNames[Result] <> name) do dec(Result); |
53 |
KeyNameToCode:= Result |
|
4 | 54 |
end; |
55 |
||
56 |
procedure ProcessKbd; |
|
371 | 57 |
var i: LongInt; |
4 | 58 |
s: shortstring; |
59 |
pkbd: PByteArray; |
|
167 | 60 |
Trusted: boolean; |
4 | 61 |
begin |
167 | 62 |
KbdKeyPressed:= false; |
63 |
Trusted:= (CurrentTeam <> nil) |
|
351 | 64 |
and (not CurrentTeam^.ExtDriven) |
846 | 65 |
and (CurrentHedgehog^.BotLevel = 0); |
161 | 66 |
|
2152 | 67 |
{$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
|
68 |
pkbd := SDL_GetKeyboardState(nil); |
2328 | 69 |
i := SDL_GetMouseState(0, nil, nil); |
2152 | 70 |
{$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
|
71 |
pkbd := SDL_GetKeyState(nil); |
2328 | 72 |
i := SDL_GetMouseState(nil, nil); |
2152 | 73 |
{$ENDIF} |
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
|
74 |
|
2379 | 75 |
// mouse buttons |
2152 | 76 |
{$IFDEF DARWIN} |
2328 | 77 |
pkbd^[1]:= ((i and 1) and not (pkbd^[306] or pkbd^[305])); |
78 |
pkbd^[3]:= ((i and 1) and (pkbd^[306] or pkbd^[305])) or (i and 4); |
|
2152 | 79 |
{$ELSE} |
2328 | 80 |
pkbd^[1]:= (i and 1); |
161 | 81 |
pkbd^[3]:= ((i shr 2) and 1); |
2152 | 82 |
{$ENDIF} |
2328 | 83 |
pkbd^[2]:= ((i shr 1) and 1); |
84 |
||
2379 | 85 |
// mouse wheels (see event loop in project file) |
86 |
pkbd^[4]:= ord(wheelDown); |
|
87 |
pkbd^[5]:= ord(wheelUp); |
|
88 |
wheelUp:= false; |
|
89 |
wheelDown:= false; |
|
2152 | 90 |
|
2379 | 91 |
// now process strokes |
4 | 92 |
for i:= 1 to cKeyMaxIndex do |
947 | 93 |
if CurrentBinds[i][0] <> #0 then |
94 |
begin |
|
95 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true; |
|
96 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted) |
|
97 |
else if (CurrentBinds[i][1] = '+') |
|
948 | 98 |
and (pkbd^[i] = 0) |
99 |
and (tkbd[i] <> 0) then |
|
947 | 100 |
begin |
101 |
s:= CurrentBinds[i]; |
|
102 |
s[1]:= '-'; |
|
103 |
ParseCommand(s, Trusted) |
|
104 |
end; |
|
105 |
tkbd[i]:= pkbd^[i] |
|
106 |
end |
|
4 | 107 |
end; |
108 |
||
109 |
procedure ResetKbd; |
|
371 | 110 |
var i, t: LongInt; |
4 | 111 |
pkbd: PByteArray; |
112 |
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
|
113 |
|
2152 | 114 |
{$IFDEF SDL13} |
115 |
pkbd:= PByteArray(SDL_GetKeyboardState(@i)); |
|
116 |
{$ELSE} |
|
4 | 117 |
pkbd:= PByteArray(SDL_GetKeyState(@i)); |
2152 | 118 |
{$ENDIF} |
109 | 119 |
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
|
120 |
|
4 | 121 |
for t:= 0 to Pred(i) do |
2284 | 122 |
tkbd[i]:= pkbd^[i] |
4 | 123 |
end; |
124 |
||
125 |
procedure InitKbdKeyTable; |
|
371 | 126 |
var i, t: LongInt; |
4 | 127 |
s: string[15]; |
128 |
begin |
|
129 |
KeyNames[1]:= 'mousel'; |
|
130 |
KeyNames[2]:= 'mousem'; |
|
131 |
KeyNames[3]:= 'mouser'; |
|
2379 | 132 |
KeyNames[4]:= 'wheelup'; |
133 |
KeyNames[5]:= 'wheeldown'; |
|
134 |
||
135 |
for i:= 6 to cKeyMaxIndex do |
|
4 | 136 |
begin |
137 |
s:= SDL_GetKeyName(i); |
|
2379 | 138 |
//addfilelog(inttostr(i) + ' ' + s); |
4 | 139 |
if s = 'unknown key' then KeyNames[i]:= '' |
140 |
else begin |
|
141 |
for t:= 1 to Length(s) do |
|
142 |
if s[t] = ' ' then s[t]:= '_'; |
|
143 |
KeyNames[i]:= s |
|
144 |
end; |
|
167 | 145 |
end; |
146 |
||
2379 | 147 |
DefaultBinds[ 27]:= 'quit'; |
148 |
DefaultBinds[ 96]:= 'history'; |
|
149 |
DefaultBinds[127]:= 'rotmask'; |
|
150 |
||
151 |
DefaultBinds[KeyNameToCode('0')]:= '+volup'; |
|
152 |
DefaultBinds[KeyNameToCode('9')]:= '+voldown'; |
|
153 |
DefaultBinds[KeyNameToCode('c')]:= 'capture'; |
|
154 |
DefaultBinds[KeyNameToCode('h')]:= 'findhh'; |
|
155 |
DefaultBinds[KeyNameToCode('p')]:= 'pause'; |
|
156 |
DefaultBinds[KeyNameToCode('s')]:= '+speedup'; |
|
157 |
DefaultBinds[KeyNameToCode('t')]:= 'chat'; |
|
158 |
DefaultBinds[KeyNameToCode('y')]:= 'confirm'; |
|
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
159 |
|
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
160 |
DefaultBinds[KeyNameToCode('f12')]:= 'fullscr'; |
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
161 |
|
167 | 162 |
SetDefaultBinds |
4 | 163 |
end; |
164 |
||
167 | 165 |
procedure SetBinds(var binds: TBinds); |
166 |
begin |
|
167 |
CurrentBinds:= binds |
|
168 |
end; |
|
169 |
||
170 |
procedure SetDefaultBinds; |
|
171 |
begin |
|
172 |
CurrentBinds:= DefaultBinds |
|
173 |
end; |
|
174 |
||
948 | 175 |
procedure FreezeEnterKey; |
176 |
begin |
|
177 |
tkbd[13]:= 1; |
|
178 |
tkbd[271]:= 1 |
|
179 |
end; |
|
167 | 180 |
|
4 | 181 |
initialization |
182 |
||
183 |
end. |