4
|
1 |
(*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*)
|
|
33 |
|
|
34 |
unit uKeys;
|
|
35 |
interface
|
|
36 |
{$INCLUDE options.inc}
|
167
|
37 |
uses uConsts;
|
|
38 |
|
|
39 |
type TBinds = array[0..cKeyMaxIndex] of shortstring;
|
4
|
40 |
|
|
41 |
function KeyNameToCode(name: string): word;
|
|
42 |
procedure ProcessKbd;
|
|
43 |
procedure ResetKbd;
|
|
44 |
procedure InitKbdKeyTable;
|
|
45 |
|
167
|
46 |
procedure SetBinds(var binds: TBinds);
|
|
47 |
procedure SetDefaultBinds;
|
|
48 |
|
161
|
49 |
var KbdKeyPressed: boolean;
|
|
50 |
|
4
|
51 |
implementation
|
167
|
52 |
uses SDLh, uTeams, uConsole, uMisc;
|
109
|
53 |
const KeyNumber = 1024;
|
|
54 |
type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
|
167
|
55 |
|
4
|
56 |
var tkbd: TKeyboardState;
|
|
57 |
KeyNames: array [0..cKeyMaxIndex] of string[15];
|
167
|
58 |
DefaultBinds, CurrentBinds: TBinds;
|
4
|
59 |
|
|
60 |
function KeyNameToCode(name: string): word;
|
|
61 |
begin
|
|
62 |
Result:= cKeyMaxIndex;
|
161
|
63 |
while (Result > 0) and (KeyNames[Result] <> name) do dec(Result)
|
4
|
64 |
end;
|
|
65 |
|
|
66 |
procedure ProcessKbd;
|
|
67 |
var i: integer;
|
|
68 |
s: shortstring;
|
|
69 |
pkbd: PByteArray;
|
167
|
70 |
Trusted: boolean;
|
4
|
71 |
begin
|
167
|
72 |
KbdKeyPressed:= false;
|
|
73 |
Trusted:= (CurrentTeam <> nil)
|
|
74 |
and (not CurrentTeam.ExtDriven)
|
|
75 |
and (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].BotLevel = 0);
|
161
|
76 |
|
4
|
77 |
pkbd:= SDL_GetKeyState(nil);
|
|
78 |
i:= SDL_GetMouseState(nil, nil);
|
|
79 |
pkbd^[1]:= (i and 1);
|
161
|
80 |
pkbd^[2]:= ((i shr 1) and 1);
|
|
81 |
pkbd^[3]:= ((i shr 2) and 1);
|
4
|
82 |
for i:= 1 to cKeyMaxIndex do
|
167
|
83 |
if CurrentBinds[i][0] <> #0 then
|
161
|
84 |
begin
|
|
85 |
if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true;
|
167
|
86 |
if CurrentBinds[i][1] = '+' then
|
4
|
87 |
begin
|
167
|
88 |
if (pkbd^[i] <> 0)and(tkbd[i] = 0) then ParseCommand(CurrentBinds[i], Trusted) else
|
4
|
89 |
if (pkbd^[i] = 0)and(tkbd[i] <> 0) then
|
|
90 |
begin
|
167
|
91 |
s:= CurrentBinds[i];
|
4
|
92 |
s[1]:= '-';
|
|
93 |
ParseCommand(s)
|
|
94 |
end;
|
|
95 |
end else
|
167
|
96 |
if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted);
|
4
|
97 |
tkbd[i]:= pkbd^[i]
|
|
98 |
end
|
|
99 |
end;
|
|
100 |
|
|
101 |
procedure ResetKbd;
|
|
102 |
var i, t: integer;
|
|
103 |
pkbd: PByteArray;
|
|
104 |
begin
|
|
105 |
pkbd:= PByteArray(SDL_GetKeyState(@i));
|
109
|
106 |
TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true);
|
4
|
107 |
for t:= 0 to Pred(i) do
|
|
108 |
tkbd[i]:= pkbd^[i]
|
|
109 |
end;
|
|
110 |
|
|
111 |
procedure InitKbdKeyTable;
|
|
112 |
var i, t: integer;
|
|
113 |
s: string[15];
|
|
114 |
begin
|
|
115 |
KeyNames[1]:= 'mousel';
|
|
116 |
KeyNames[2]:= 'mousem';
|
|
117 |
KeyNames[3]:= 'mouser';
|
|
118 |
for i:= 4 to cKeyMaxIndex do
|
|
119 |
begin
|
|
120 |
s:= SDL_GetKeyName(i);
|
|
121 |
if s = 'unknown key' then KeyNames[i]:= ''
|
|
122 |
else begin
|
|
123 |
for t:= 1 to Length(s) do
|
|
124 |
if s[t] = ' ' then s[t]:= '_';
|
|
125 |
KeyNames[i]:= s
|
|
126 |
end;
|
167
|
127 |
end;
|
|
128 |
|
|
129 |
DefaultBinds[ 27]:= 'quit';
|
|
130 |
DefaultBinds[ 99]:= 'capture';
|
|
131 |
DefaultBinds[102]:= 'fullscr';
|
|
132 |
SetDefaultBinds
|
4
|
133 |
end;
|
|
134 |
|
167
|
135 |
procedure SetBinds(var binds: TBinds);
|
|
136 |
begin
|
|
137 |
CurrentBinds:= binds
|
|
138 |
end;
|
|
139 |
|
|
140 |
procedure SetDefaultBinds;
|
|
141 |
begin
|
|
142 |
CurrentBinds:= DefaultBinds
|
|
143 |
end;
|
|
144 |
|
|
145 |
|
4
|
146 |
initialization
|
|
147 |
|
|
148 |
end.
|