author | koda |
Wed, 19 Aug 2009 18:55:40 +0000 | |
changeset 2328 | e74f2db28b21 |
parent 2312 | 3b59f1251ead |
child 2379 | d62b1f224982 |
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); |
2328 | 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); |
2328 | 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 |
|
8208946331ba
Smaxx refactor of LoadImage to use flags, iphone changes by koda (mostly use of rgba instead of rgb)
nemo
parents:
2163
diff
changeset
|
73 |
|
2152 | 74 |
{$IFDEF DARWIN} |
2328 | 75 |
pkbd^[1]:= ((i and 1) and not (pkbd^[306] or pkbd^[305])); |
76 |
pkbd^[3]:= ((i and 1) and (pkbd^[306] or pkbd^[305])) or (i and 4); |
|
2152 | 77 |
{$ELSE} |
2328 | 78 |
pkbd^[1]:= (i and 1); |
161 | 79 |
pkbd^[3]:= ((i shr 2) and 1); |
2152 | 80 |
{$ENDIF} |
2328 | 81 |
pkbd^[2]:= ((i shr 1) and 1); |
82 |
||
2152 | 83 |
|
4 | 84 |
for i:= 1 to cKeyMaxIndex do |
947 | 85 |
if CurrentBinds[i][0] <> #0 then |
86 |
begin |
|
87 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true; |
|
88 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted) |
|
89 |
else if (CurrentBinds[i][1] = '+') |
|
948 | 90 |
and (pkbd^[i] = 0) |
91 |
and (tkbd[i] <> 0) then |
|
947 | 92 |
begin |
93 |
s:= CurrentBinds[i]; |
|
94 |
s[1]:= '-'; |
|
95 |
ParseCommand(s, Trusted) |
|
96 |
end; |
|
97 |
tkbd[i]:= pkbd^[i] |
|
98 |
end |
|
4 | 99 |
end; |
100 |
||
101 |
procedure ResetKbd; |
|
371 | 102 |
var i, t: LongInt; |
4 | 103 |
pkbd: PByteArray; |
104 |
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
|
105 |
|
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); |
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
|
112 |
|
4 | 113 |
for t:= 0 to Pred(i) do |
2284 | 114 |
tkbd[i]:= pkbd^[i] |
4 | 115 |
end; |
116 |
||
117 |
procedure InitKbdKeyTable; |
|
371 | 118 |
var i, t: LongInt; |
4 | 119 |
s: string[15]; |
120 |
begin |
|
121 |
KeyNames[1]:= 'mousel'; |
|
122 |
KeyNames[2]:= 'mousem'; |
|
123 |
KeyNames[3]:= 'mouser'; |
|
124 |
for i:= 4 to cKeyMaxIndex do |
|
125 |
begin |
|
126 |
s:= SDL_GetKeyName(i); |
|
127 |
if s = 'unknown key' then KeyNames[i]:= '' |
|
128 |
else begin |
|
129 |
for t:= 1 to Length(s) do |
|
130 |
if s[t] = ' ' then s[t]:= '_'; |
|
131 |
KeyNames[i]:= s |
|
132 |
end; |
|
167 | 133 |
end; |
134 |
||
2328 | 135 |
DefaultBinds[ 27]:= 'quit'; // esc |
136 |
DefaultBinds[ 48]:= '+volup'; // 0 |
|
137 |
DefaultBinds[ 57]:= '+voldown'; // 9 |
|
138 |
DefaultBinds[ 96]:= 'history'; // ` |
|
139 |
DefaultBinds[ 99]:= 'capture'; // c |
|
140 |
DefaultBinds[104]:= 'findhh'; // h |
|
141 |
DefaultBinds[112]:= 'pause'; // p |
|
142 |
DefaultBinds[115]:= '+speedup'; // s |
|
143 |
DefaultBinds[116]:= 'chat'; // t |
|
144 |
DefaultBinds[121]:= 'confirm'; // y |
|
145 |
DefaultBinds[127]:= 'rotmask'; // canc |
|
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
146 |
|
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
147 |
DefaultBinds[KeyNameToCode('f12')]:= 'fullscr'; |
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
148 |
|
167 | 149 |
SetDefaultBinds |
4 | 150 |
end; |
151 |
||
167 | 152 |
procedure SetBinds(var binds: TBinds); |
153 |
begin |
|
154 |
CurrentBinds:= binds |
|
155 |
end; |
|
156 |
||
157 |
procedure SetDefaultBinds; |
|
158 |
begin |
|
159 |
CurrentBinds:= DefaultBinds |
|
160 |
end; |
|
161 |
||
948 | 162 |
procedure FreezeEnterKey; |
163 |
begin |
|
164 |
tkbd[13]:= 1; |
|
165 |
tkbd[271]:= 1 |
|
166 |
end; |
|
167 | 167 |
|
4 | 168 |
initialization |
169 |
||
170 |
end. |