author | unc0rr |
Thu, 18 Sep 2008 13:39:05 +0000 | |
changeset 1261 | 903058d382c7 |
parent 1066 | 1f1b3686a2b0 |
child 2008 | fc2fb5c938c3 |
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 |
|
4 | 65 |
pkbd:= SDL_GetKeyState(nil); |
66 |
i:= SDL_GetMouseState(nil, nil); |
|
67 |
pkbd^[1]:= (i and 1); |
|
161 | 68 |
pkbd^[2]:= ((i shr 1) and 1); |
69 |
pkbd^[3]:= ((i shr 2) and 1); |
|
4 | 70 |
for i:= 1 to cKeyMaxIndex do |
947 | 71 |
if CurrentBinds[i][0] <> #0 then |
72 |
begin |
|
73 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true; |
|
74 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted) |
|
75 |
else if (CurrentBinds[i][1] = '+') |
|
948 | 76 |
and (pkbd^[i] = 0) |
77 |
and (tkbd[i] <> 0) then |
|
947 | 78 |
begin |
79 |
s:= CurrentBinds[i]; |
|
80 |
s[1]:= '-'; |
|
81 |
ParseCommand(s, Trusted) |
|
82 |
end; |
|
83 |
tkbd[i]:= pkbd^[i] |
|
84 |
end |
|
4 | 85 |
end; |
86 |
||
87 |
procedure ResetKbd; |
|
371 | 88 |
var i, t: LongInt; |
4 | 89 |
pkbd: PByteArray; |
90 |
begin |
|
91 |
pkbd:= PByteArray(SDL_GetKeyState(@i)); |
|
109 | 92 |
TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true); |
4 | 93 |
for t:= 0 to Pred(i) do |
94 |
tkbd[i]:= pkbd^[i] |
|
95 |
end; |
|
96 |
||
97 |
procedure InitKbdKeyTable; |
|
371 | 98 |
var i, t: LongInt; |
4 | 99 |
s: string[15]; |
100 |
begin |
|
101 |
KeyNames[1]:= 'mousel'; |
|
102 |
KeyNames[2]:= 'mousem'; |
|
103 |
KeyNames[3]:= 'mouser'; |
|
104 |
for i:= 4 to cKeyMaxIndex do |
|
105 |
begin |
|
106 |
s:= SDL_GetKeyName(i); |
|
107 |
if s = 'unknown key' then KeyNames[i]:= '' |
|
108 |
else begin |
|
109 |
for t:= 1 to Length(s) do |
|
110 |
if s[t] = ' ' then s[t]:= '_'; |
|
111 |
KeyNames[i]:= s |
|
112 |
end; |
|
167 | 113 |
end; |
114 |
||
115 |
DefaultBinds[ 27]:= 'quit'; |
|
175 | 116 |
DefaultBinds[ 48]:= '+volup'; |
117 |
DefaultBinds[ 57]:= '+voldown'; |
|
991 | 118 |
DefaultBinds[ 96]:= 'history'; |
167 | 119 |
DefaultBinds[ 99]:= 'capture'; |
176 | 120 |
DefaultBinds[104]:= 'findhh'; |
299 | 121 |
DefaultBinds[112]:= 'pause'; |
626 | 122 |
DefaultBinds[115]:= '+speedup'; |
948 | 123 |
DefaultBinds[116]:= 'chat'; |
1022 | 124 |
DefaultBinds[121]:= 'confirm'; |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
393
diff
changeset
|
125 |
DefaultBinds[127]:= 'rotmask'; |
1051
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
126 |
|
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
127 |
DefaultBinds[KeyNameToCode('f12')]:= 'fullscr'; |
dfdd5dfe97d4
Enable fullscreen switching back, now it's bound on F12
unc0rr
parents:
1022
diff
changeset
|
128 |
|
167 | 129 |
SetDefaultBinds |
4 | 130 |
end; |
131 |
||
167 | 132 |
procedure SetBinds(var binds: TBinds); |
133 |
begin |
|
134 |
CurrentBinds:= binds |
|
135 |
end; |
|
136 |
||
137 |
procedure SetDefaultBinds; |
|
138 |
begin |
|
139 |
CurrentBinds:= DefaultBinds |
|
140 |
end; |
|
141 |
||
948 | 142 |
procedure FreezeEnterKey; |
143 |
begin |
|
144 |
tkbd[13]:= 1; |
|
145 |
tkbd[271]:= 1 |
|
146 |
end; |
|
167 | 147 |
|
4 | 148 |
initialization |
149 |
||
150 |
end. |