author | nemo |
Thu, 18 Jun 2009 17:35:34 +0000 | |
changeset 2170 | aa77f2a6b4f1 |
parent 2163 | 12730f5e79b9 |
child 2171 | 8208946331ba |
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; |
36 |
||
4 | 37 |
implementation |
167 | 38 |
uses SDLh, uTeams, uConsole, uMisc; |
109 | 39 |
const KeyNumber = 1024; |
40 |
type TKeyboardState = array[0..cKeyMaxIndex] of Byte; |
|
167 | 41 |
|
4 | 42 |
var tkbd: TKeyboardState; |
43 |
KeyNames: array [0..cKeyMaxIndex] of string[15]; |
|
167 | 44 |
DefaultBinds, CurrentBinds: TBinds; |
4 | 45 |
|
46 |
function KeyNameToCode(name: string): word; |
|
351 | 47 |
var Result: Word; |
4 | 48 |
begin |
49 |
Result:= cKeyMaxIndex; |
|
351 | 50 |
while (Result > 0) and (KeyNames[Result] <> name) do dec(Result); |
51 |
KeyNameToCode:= Result |
|
4 | 52 |
end; |
53 |
||
54 |
procedure ProcessKbd; |
|
371 | 55 |
var i: LongInt; |
4 | 56 |
s: shortstring; |
57 |
pkbd: PByteArray; |
|
167 | 58 |
Trusted: boolean; |
4 | 59 |
begin |
167 | 60 |
KbdKeyPressed:= false; |
61 |
Trusted:= (CurrentTeam <> nil) |
|
351 | 62 |
and (not CurrentTeam^.ExtDriven) |
846 | 63 |
and (CurrentHedgehog^.BotLevel = 0); |
161 | 64 |
|
2152 | 65 |
{$IFDEF SDL13} |
66 |
pkbd:= SDL_GetKeyboardState(nil); |
|
67 |
{$ELSE} |
|
4 | 68 |
pkbd:= SDL_GetKeyState(nil); |
2152 | 69 |
{$ENDIF} |
2163
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset
|
70 |
{$IFDEF IPHONEOS} |
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset
|
71 |
//SDL_GetMouseState currently broken in sdl13 |
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset
|
72 |
i:=1; |
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset
|
73 |
{$ELSE} |
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset
|
74 |
i:=SDL_GetMouseState(nil, nil); |
12730f5e79b9
koda's patch fixing some iphone port troubles (color, mouse)
unc0rr
parents:
2162
diff
changeset
|
75 |
{$ENDIF} |
4 | 76 |
pkbd^[1]:= (i and 1); |
161 | 77 |
pkbd^[2]:= ((i shr 1) and 1); |
2152 | 78 |
{$IFDEF DARWIN} |
79 |
// normal right click || ctrl (left/right) + left click |
|
80 |
pkbd^[3]:= ((i shr 2) and 1) or ((i and 1) and (pkbd^[306] or pkbd^[305])); |
|
81 |
{$ELSE} |
|
161 | 82 |
pkbd^[3]:= ((i shr 2) and 1); |
2152 | 83 |
{$ENDIF} |
84 |
||
4 | 85 |
for i:= 1 to cKeyMaxIndex do |
947 | 86 |
if CurrentBinds[i][0] <> #0 then |
87 |
begin |
|
88 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true; |
|
89 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted) |
|
90 |
else if (CurrentBinds[i][1] = '+') |
|
948 | 91 |
and (pkbd^[i] = 0) |
92 |
and (tkbd[i] <> 0) then |
|
947 | 93 |
begin |
94 |
s:= CurrentBinds[i]; |
|
95 |
s[1]:= '-'; |
|
96 |
ParseCommand(s, Trusted) |
|
97 |
end; |
|
98 |
tkbd[i]:= pkbd^[i] |
|
99 |
end |
|
4 | 100 |
end; |
101 |
||
102 |
procedure ResetKbd; |
|
371 | 103 |
var i, t: LongInt; |
4 | 104 |
pkbd: PByteArray; |
105 |
begin |
|
2152 | 106 |
{$IFDEF SDL13} |
107 |
pkbd:= PByteArray(SDL_GetKeyboardState(@i)); |
|
108 |
{$ELSE} |
|
4 | 109 |
pkbd:= PByteArray(SDL_GetKeyState(@i)); |
2152 | 110 |
{$ENDIF} |
109 | 111 |
TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true); |
4 | 112 |
for t:= 0 to Pred(i) do |
113 |
tkbd[i]:= pkbd^[i] |
|
114 |
end; |
|
115 |
||
116 |
procedure InitKbdKeyTable; |
|
371 | 117 |
var i, t: LongInt; |
4 | 118 |
s: string[15]; |
119 |
begin |
|
120 |
KeyNames[1]:= 'mousel'; |
|
121 |
KeyNames[2]:= 'mousem'; |
|
122 |
KeyNames[3]:= 'mouser'; |
|
123 |
for i:= 4 to cKeyMaxIndex do |
|
124 |
begin |
|
125 |
s:= SDL_GetKeyName(i); |
|
126 |
if s = 'unknown key' then KeyNames[i]:= '' |
|
127 |
else begin |
|
128 |
for t:= 1 to Length(s) do |
|
129 |
if s[t] = ' ' then s[t]:= '_'; |
|
130 |
KeyNames[i]:= s |
|
131 |
end; |
|
167 | 132 |
end; |
133 |
||
134 |
DefaultBinds[ 27]:= 'quit'; |
|
175 | 135 |
DefaultBinds[ 48]:= '+volup'; |
136 |
DefaultBinds[ 57]:= '+voldown'; |
|
2008 | 137 |
DefaultBinds[ 44]:= 'history'; // , instead of ` |
991 | 138 |
DefaultBinds[ 96]:= 'history'; |
167 | 139 |
DefaultBinds[ 99]:= 'capture'; |
176 | 140 |
DefaultBinds[104]:= 'findhh'; |
299 | 141 |
DefaultBinds[112]:= 'pause'; |
626 | 142 |
DefaultBinds[115]:= '+speedup'; |
948 | 143 |
DefaultBinds[116]:= 'chat'; |
1022 | 144 |
DefaultBinds[121]:= 'confirm'; |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
393
diff
changeset
|
145 |
DefaultBinds[127]:= 'rotmask'; |
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
146 |
|
2162 | 147 |
DefaultBinds[ 61]:= 'zoomout'; |
148 |
DefaultBinds[ 45]:= 'zoomin'; |
|
149 |
||
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
150 |
DefaultBinds[KeyNameToCode('f12')]:= 'fullscr'; |
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
151 |
|
167 | 152 |
SetDefaultBinds |
4 | 153 |
end; |
154 |
||
167 | 155 |
procedure SetBinds(var binds: TBinds); |
156 |
begin |
|
157 |
CurrentBinds:= binds |
|
158 |
end; |
|
159 |
||
160 |
procedure SetDefaultBinds; |
|
161 |
begin |
|
162 |
CurrentBinds:= DefaultBinds |
|
163 |
end; |
|
164 |
||
948 | 165 |
procedure FreezeEnterKey; |
166 |
begin |
|
167 |
tkbd[13]:= 1; |
|
168 |
tkbd[271]:= 1 |
|
169 |
end; |
|
167 | 170 |
|
4 | 171 |
initialization |
172 |
||
173 |
end. |