hedgewars/uKeys.pas
author unc0rr
Thu, 05 Oct 2006 16:33:18 +0000
changeset 183 57c2ef19f719
parent 176 533d03041dcd
child 299 098c722e13e1
permissions -rw-r--r--
Relicense to GPL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     1
(*
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     2
 * Hedgewars, a worms-like game
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     3
 * Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     4
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     8
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
    12
 * GNU General Public License for more details.
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    13
 *
183
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
    14
 * You should have received a copy of the GNU General Public License
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
    15
 * along with this program; if not, write to the Free Software
57c2ef19f719 Relicense to GPL
unc0rr
parents: 176
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    17
 *)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    18
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    19
unit uKeys;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    20
interface
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    21
{$INCLUDE options.inc}
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    22
uses uConsts;
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    23
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    24
type TBinds = array[0..cKeyMaxIndex] of shortstring;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    25
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    26
function KeyNameToCode(name: string): word;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    27
procedure ProcessKbd;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    28
procedure ResetKbd;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    29
procedure InitKbdKeyTable;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    30
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    31
procedure SetBinds(var binds: TBinds);
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    32
procedure SetDefaultBinds;
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    33
161
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    34
var KbdKeyPressed: boolean;
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    35
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    36
implementation
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    37
uses SDLh, uTeams, uConsole, uMisc;
109
ab0340f580c2 - Initialize translations earlier
unc0rr
parents: 42
diff changeset
    38
const KeyNumber = 1024;
ab0340f580c2 - Initialize translations earlier
unc0rr
parents: 42
diff changeset
    39
type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    40
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    41
var tkbd: TKeyboardState;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    42
    KeyNames: array [0..cKeyMaxIndex] of string[15];
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    43
    DefaultBinds, CurrentBinds: TBinds;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    44
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
function KeyNameToCode(name: string): word;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    46
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    47
Result:= cKeyMaxIndex;
161
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    48
while (Result > 0) and (KeyNames[Result] <> name) do dec(Result)
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    49
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    50
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    51
procedure ProcessKbd;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    52
var  i: integer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    53
     s: shortstring;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    54
     pkbd: PByteArray;
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    55
     Trusted: boolean;
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    56
begin
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    57
KbdKeyPressed:= false;
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    58
Trusted:= (CurrentTeam <> nil)
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    59
          and (not CurrentTeam.ExtDriven)
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    60
          and (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].BotLevel = 0);
161
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    61
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    62
pkbd:= SDL_GetKeyState(nil);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    63
i:= SDL_GetMouseState(nil, nil);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    64
pkbd^[1]:= (i and 1);
161
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    65
pkbd^[2]:= ((i shr 1) and 1);
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    66
pkbd^[3]:= ((i shr 2) and 1);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
for i:= 1 to cKeyMaxIndex do
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    68
    if CurrentBinds[i][0] <> #0 then
161
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    69
      begin
d8870bbf960e - AmmoMenu
unc0rr
parents: 109
diff changeset
    70
      if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true;
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    71
      if CurrentBinds[i][1] = '+' then
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    72
          begin
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    73
          if (pkbd^[i] <> 0)and(tkbd[i]  = 0) then ParseCommand(CurrentBinds[i], Trusted) else
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    74
          if (pkbd^[i] =  0)and(tkbd[i] <> 0) then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    75
             begin
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    76
             s:= CurrentBinds[i];
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    77
             s[1]:= '-';
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    78
             ParseCommand(s)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    79
             end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    80
          end else
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
    81
          if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    82
       tkbd[i]:= pkbd^[i]
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    83
       end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    84
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    85
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    86
procedure ResetKbd;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    87
var i, t: integer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    88
    pkbd: PByteArray;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    89
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    90
pkbd:= PByteArray(SDL_GetKeyState(@i));
109
ab0340f580c2 - Initialize translations earlier
unc0rr
parents: 42
diff changeset
    91
TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true);
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    92
for t:= 0 to Pred(i) do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    93
    tkbd[i]:= pkbd^[i]
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    94
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    95
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    96
procedure InitKbdKeyTable;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    97
var i, t: integer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    98
    s: string[15];
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    99
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   100
KeyNames[1]:= 'mousel';
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   101
KeyNames[2]:= 'mousem';
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   102
KeyNames[3]:= 'mouser';
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   103
for i:= 4 to cKeyMaxIndex do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   104
    begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   105
    s:= SDL_GetKeyName(i);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   106
    if s = 'unknown key' then KeyNames[i]:= ''
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   107
       else begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   108
       for t:= 1 to Length(s) do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   109
           if s[t] = ' ' then s[t]:= '_';
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   110
       KeyNames[i]:= s
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   111
       end;
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   112
    end;
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   113
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   114
DefaultBinds[ 27]:= 'quit';
175
d226d976d836 - Some improvements to volume control
unc0rr
parents: 167
diff changeset
   115
DefaultBinds[ 48]:= '+volup';
d226d976d836 - Some improvements to volume control
unc0rr
parents: 167
diff changeset
   116
DefaultBinds[ 57]:= '+voldown';
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   117
DefaultBinds[ 99]:= 'capture';
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   118
DefaultBinds[102]:= 'fullscr';
176
533d03041dcd 'h' marks out current hedgehog
unc0rr
parents: 175
diff changeset
   119
DefaultBinds[104]:= 'findhh';
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   120
SetDefaultBinds
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   121
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   122
167
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   123
procedure SetBinds(var binds: TBinds);
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   124
begin
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   125
CurrentBinds:= binds
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   126
end;
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   127
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   128
procedure SetDefaultBinds;
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   129
begin
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   130
CurrentBinds:= DefaultBinds
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   131
end;
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   132
805fa9a27e9e "Trusted" binds
unc0rr
parents: 161
diff changeset
   133
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   134
initialization
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   135
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   136
end.