4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
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
|
|
21 |
{$INCLUDE options.inc}
|
167
|
22 |
uses uConsts;
|
|
23 |
|
|
24 |
type TBinds = array[0..cKeyMaxIndex] of shortstring;
|
4
|
25 |
|
|
26 |
function KeyNameToCode(name: string): word;
|
|
27 |
procedure ProcessKbd;
|
|
28 |
procedure ResetKbd;
|
|
29 |
procedure InitKbdKeyTable;
|
|
30 |
|
167
|
31 |
procedure SetBinds(var binds: TBinds);
|
|
32 |
procedure SetDefaultBinds;
|
|
33 |
|
161
|
34 |
var KbdKeyPressed: boolean;
|
|
35 |
|
4
|
36 |
implementation
|
167
|
37 |
uses SDLh, uTeams, uConsole, uMisc;
|
109
|
38 |
const KeyNumber = 1024;
|
|
39 |
type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
|
167
|
40 |
|
4
|
41 |
var tkbd: TKeyboardState;
|
|
42 |
KeyNames: array [0..cKeyMaxIndex] of string[15];
|
167
|
43 |
DefaultBinds, CurrentBinds: TBinds;
|
4
|
44 |
|
|
45 |
function KeyNameToCode(name: string): word;
|
|
46 |
begin
|
|
47 |
Result:= cKeyMaxIndex;
|
161
|
48 |
while (Result > 0) and (KeyNames[Result] <> name) do dec(Result)
|
4
|
49 |
end;
|
|
50 |
|
|
51 |
procedure ProcessKbd;
|
|
52 |
var i: integer;
|
|
53 |
s: shortstring;
|
|
54 |
pkbd: PByteArray;
|
167
|
55 |
Trusted: boolean;
|
4
|
56 |
begin
|
167
|
57 |
KbdKeyPressed:= false;
|
|
58 |
Trusted:= (CurrentTeam <> nil)
|
|
59 |
and (not CurrentTeam.ExtDriven)
|
|
60 |
and (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].BotLevel = 0);
|
161
|
61 |
|
4
|
62 |
pkbd:= SDL_GetKeyState(nil);
|
|
63 |
i:= SDL_GetMouseState(nil, nil);
|
|
64 |
pkbd^[1]:= (i and 1);
|
161
|
65 |
pkbd^[2]:= ((i shr 1) and 1);
|
|
66 |
pkbd^[3]:= ((i shr 2) and 1);
|
4
|
67 |
for i:= 1 to cKeyMaxIndex do
|
167
|
68 |
if CurrentBinds[i][0] <> #0 then
|
161
|
69 |
begin
|
|
70 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true;
|
167
|
71 |
if CurrentBinds[i][1] = '+' then
|
4
|
72 |
begin
|
167
|
73 |
if (pkbd^[i] <> 0)and(tkbd[i] = 0) then ParseCommand(CurrentBinds[i], Trusted) else
|
4
|
74 |
if (pkbd^[i] = 0)and(tkbd[i] <> 0) then
|
|
75 |
begin
|
167
|
76 |
s:= CurrentBinds[i];
|
4
|
77 |
s[1]:= '-';
|
|
78 |
ParseCommand(s)
|
|
79 |
end;
|
|
80 |
end else
|
167
|
81 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted);
|
4
|
82 |
tkbd[i]:= pkbd^[i]
|
|
83 |
end
|
|
84 |
end;
|
|
85 |
|
|
86 |
procedure ResetKbd;
|
|
87 |
var i, t: integer;
|
|
88 |
pkbd: PByteArray;
|
|
89 |
begin
|
|
90 |
pkbd:= PByteArray(SDL_GetKeyState(@i));
|
109
|
91 |
TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true);
|
4
|
92 |
for t:= 0 to Pred(i) do
|
|
93 |
tkbd[i]:= pkbd^[i]
|
|
94 |
end;
|
|
95 |
|
|
96 |
procedure InitKbdKeyTable;
|
|
97 |
var i, t: integer;
|
|
98 |
s: string[15];
|
|
99 |
begin
|
|
100 |
KeyNames[1]:= 'mousel';
|
|
101 |
KeyNames[2]:= 'mousem';
|
|
102 |
KeyNames[3]:= 'mouser';
|
|
103 |
for i:= 4 to cKeyMaxIndex do
|
|
104 |
begin
|
|
105 |
s:= SDL_GetKeyName(i);
|
|
106 |
if s = 'unknown key' then KeyNames[i]:= ''
|
|
107 |
else begin
|
|
108 |
for t:= 1 to Length(s) do
|
|
109 |
if s[t] = ' ' then s[t]:= '_';
|
|
110 |
KeyNames[i]:= s
|
|
111 |
end;
|
167
|
112 |
end;
|
|
113 |
|
|
114 |
DefaultBinds[ 27]:= 'quit';
|
175
|
115 |
DefaultBinds[ 48]:= '+volup';
|
|
116 |
DefaultBinds[ 57]:= '+voldown';
|
167
|
117 |
DefaultBinds[ 99]:= 'capture';
|
|
118 |
DefaultBinds[102]:= 'fullscr';
|
176
|
119 |
DefaultBinds[104]:= 'findhh';
|
299
|
120 |
DefaultBinds[112]:= 'pause';
|
167
|
121 |
SetDefaultBinds
|
4
|
122 |
end;
|
|
123 |
|
167
|
124 |
procedure SetBinds(var binds: TBinds);
|
|
125 |
begin
|
|
126 |
CurrentBinds:= binds
|
|
127 |
end;
|
|
128 |
|
|
129 |
procedure SetDefaultBinds;
|
|
130 |
begin
|
|
131 |
CurrentBinds:= DefaultBinds
|
|
132 |
end;
|
|
133 |
|
|
134 |
|
4
|
135 |
initialization
|
|
136 |
|
|
137 |
end.
|