author | unc0rr |
Wed, 18 Jun 2008 21:53:54 +0000 | |
changeset 1015 | 6f4a1890c60d |
parent 991 | d17c71ee074e |
child 1022 | f6a3fcb329ca |
permissions | -rw-r--r-- |
4 | 1 |
(* |
2 |
* Hedgewars, a worms-like 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'; |
120 |
DefaultBinds[102]:= 'fullscr'; |
|
176 | 121 |
DefaultBinds[104]:= 'findhh'; |
299 | 122 |
DefaultBinds[112]:= 'pause'; |
626 | 123 |
DefaultBinds[115]:= '+speedup'; |
948 | 124 |
DefaultBinds[116]:= 'chat'; |
539
6a9bf1852bbc
Ability to choose which info is shown above hedgehogs
unc0rr
parents:
393
diff
changeset
|
125 |
DefaultBinds[127]:= 'rotmask'; |
167 | 126 |
SetDefaultBinds |
4 | 127 |
end; |
128 |
||
167 | 129 |
procedure SetBinds(var binds: TBinds); |
130 |
begin |
|
131 |
CurrentBinds:= binds |
|
132 |
end; |
|
133 |
||
134 |
procedure SetDefaultBinds; |
|
135 |
begin |
|
136 |
CurrentBinds:= DefaultBinds |
|
137 |
end; |
|
138 |
||
948 | 139 |
procedure FreezeEnterKey; |
140 |
begin |
|
141 |
tkbd[13]:= 1; |
|
142 |
tkbd[271]:= 1 |
|
143 |
end; |
|
167 | 144 |
|
4 | 145 |
initialization |
146 |
||
147 |
end. |