author | nemo |
Sun, 26 Jul 2009 19:19:03 +0000 | |
changeset 2275 | 3f56c99a70f8 |
parent 2222 | 2e98698b76aa |
child 2284 | bd091b6b128d |
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 |
2222 | 38 |
uses SDLh, uTeams, uConsole, uMisc, uStore; |
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} |
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
|
66 |
pkbd := SDL_GetKeyboardState(nil); |
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset
|
67 |
i := SDL_GetMouseState(0, nil, nil); |
2152 | 68 |
{$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
|
69 |
pkbd := SDL_GetKeyState(nil); |
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset
|
70 |
i := SDL_GetMouseState(nil, nil); |
2152 | 71 |
{$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
|
72 |
|
4 | 73 |
pkbd^[1]:= (i and 1); |
161 | 74 |
pkbd^[2]:= ((i shr 1) and 1); |
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
|
75 |
|
2152 | 76 |
{$IFDEF DARWIN} |
77 |
// normal right click || ctrl (left/right) + left click |
|
78 |
pkbd^[3]:= ((i shr 2) and 1) or ((i and 1) and (pkbd^[306] or pkbd^[305])); |
|
79 |
{$ELSE} |
|
161 | 80 |
pkbd^[3]:= ((i shr 2) and 1); |
2152 | 81 |
{$ENDIF} |
82 |
||
4 | 83 |
for i:= 1 to cKeyMaxIndex do |
947 | 84 |
if CurrentBinds[i][0] <> #0 then |
85 |
begin |
|
86 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true; |
|
87 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted) |
|
88 |
else if (CurrentBinds[i][1] = '+') |
|
948 | 89 |
and (pkbd^[i] = 0) |
90 |
and (tkbd[i] <> 0) then |
|
947 | 91 |
begin |
92 |
s:= CurrentBinds[i]; |
|
93 |
s[1]:= '-'; |
|
94 |
ParseCommand(s, Trusted) |
|
95 |
end; |
|
96 |
tkbd[i]:= pkbd^[i] |
|
97 |
end |
|
4 | 98 |
end; |
99 |
||
100 |
procedure ResetKbd; |
|
371 | 101 |
var i, t: LongInt; |
4 | 102 |
pkbd: PByteArray; |
103 |
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
|
104 |
|
2152 | 105 |
{$IFDEF SDL13} |
106 |
pkbd:= PByteArray(SDL_GetKeyboardState(@i)); |
|
107 |
{$ELSE} |
|
4 | 108 |
pkbd:= PByteArray(SDL_GetKeyState(@i)); |
2152 | 109 |
{$ENDIF} |
109 | 110 |
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
|
111 |
|
4 | 112 |
for t:= 0 to Pred(i) do |
2222 | 113 |
tkbd[i]:= pkbd^[i]; |
114 |
||
115 |
AddProgress; |
|
4 | 116 |
end; |
117 |
||
118 |
procedure InitKbdKeyTable; |
|
371 | 119 |
var i, t: LongInt; |
4 | 120 |
s: string[15]; |
121 |
begin |
|
122 |
KeyNames[1]:= 'mousel'; |
|
123 |
KeyNames[2]:= 'mousem'; |
|
124 |
KeyNames[3]:= 'mouser'; |
|
125 |
for i:= 4 to cKeyMaxIndex do |
|
126 |
begin |
|
127 |
s:= SDL_GetKeyName(i); |
|
128 |
if s = 'unknown key' then KeyNames[i]:= '' |
|
129 |
else begin |
|
130 |
for t:= 1 to Length(s) do |
|
131 |
if s[t] = ' ' then s[t]:= '_'; |
|
132 |
KeyNames[i]:= s |
|
133 |
end; |
|
167 | 134 |
end; |
135 |
||
136 |
DefaultBinds[ 27]:= 'quit'; |
|
175 | 137 |
DefaultBinds[ 48]:= '+volup'; |
138 |
DefaultBinds[ 57]:= '+voldown'; |
|
2008 | 139 |
DefaultBinds[ 44]:= 'history'; // , instead of ` |
991 | 140 |
DefaultBinds[ 96]:= 'history'; |
167 | 141 |
DefaultBinds[ 99]:= 'capture'; |
176 | 142 |
DefaultBinds[104]:= 'findhh'; |
299 | 143 |
DefaultBinds[112]:= 'pause'; |
626 | 144 |
DefaultBinds[115]:= '+speedup'; |
948 | 145 |
DefaultBinds[116]:= 'chat'; |
1022 | 146 |
DefaultBinds[121]:= 'confirm'; |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
393
diff
changeset
|
147 |
DefaultBinds[127]:= 'rotmask'; |
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
148 |
|
2162 | 149 |
DefaultBinds[ 61]:= 'zoomout'; |
150 |
DefaultBinds[ 45]:= 'zoomin'; |
|
151 |
||
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
152 |
DefaultBinds[KeyNameToCode('f12')]:= 'fullscr'; |
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
153 |
|
167 | 154 |
SetDefaultBinds |
4 | 155 |
end; |
156 |
||
167 | 157 |
procedure SetBinds(var binds: TBinds); |
158 |
begin |
|
159 |
CurrentBinds:= binds |
|
160 |
end; |
|
161 |
||
162 |
procedure SetDefaultBinds; |
|
163 |
begin |
|
164 |
CurrentBinds:= DefaultBinds |
|
165 |
end; |
|
166 |
||
948 | 167 |
procedure FreezeEnterKey; |
168 |
begin |
|
169 |
tkbd[13]:= 1; |
|
170 |
tkbd[271]:= 1 |
|
171 |
end; |
|
167 | 172 |
|
4 | 173 |
initialization |
174 |
||
175 |
end. |