hedgewars/uKeys.pas
changeset 167 805fa9a27e9e
parent 161 d8870bbf960e
child 175 d226d976d836
equal deleted inserted replaced
166:2920ab2bf329 167:805fa9a27e9e
    32  *)
    32  *)
    33 
    33 
    34 unit uKeys;
    34 unit uKeys;
    35 interface
    35 interface
    36 {$INCLUDE options.inc}
    36 {$INCLUDE options.inc}
       
    37 uses uConsts;
       
    38 
       
    39 type TBinds = array[0..cKeyMaxIndex] of shortstring;
    37 
    40 
    38 function KeyNameToCode(name: string): word;
    41 function KeyNameToCode(name: string): word;
    39 procedure ProcessKbd;
    42 procedure ProcessKbd;
    40 procedure ResetKbd;
    43 procedure ResetKbd;
    41 procedure ProcessKbdDemo;
       
    42 procedure InitKbdKeyTable;
    44 procedure InitKbdKeyTable;
       
    45 
       
    46 procedure SetBinds(var binds: TBinds);
       
    47 procedure SetDefaultBinds;
    43 
    48 
    44 var KbdKeyPressed: boolean;
    49 var KbdKeyPressed: boolean;
    45 
    50 
    46 implementation
    51 implementation
    47 uses SDLh, uTeams, uConsole, uConsts, uMisc;
    52 uses SDLh, uTeams, uConsole, uMisc;
    48 const KeyNumber = 1024;
    53 const KeyNumber = 1024;
    49 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
    54 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
       
    55 
    50 var tkbd: TKeyboardState;
    56 var tkbd: TKeyboardState;
    51     KeyNames: array [0..cKeyMaxIndex] of string[15];
    57     KeyNames: array [0..cKeyMaxIndex] of string[15];
       
    58     DefaultBinds, CurrentBinds: TBinds;
    52 
    59 
    53 function KeyNameToCode(name: string): word;
    60 function KeyNameToCode(name: string): word;
    54 begin
    61 begin
    55 Result:= cKeyMaxIndex;
    62 Result:= cKeyMaxIndex;
    56 while (Result > 0) and (KeyNames[Result] <> name) do dec(Result)
    63 while (Result > 0) and (KeyNames[Result] <> name) do dec(Result)
    58 
    65 
    59 procedure ProcessKbd;
    66 procedure ProcessKbd;
    60 var  i: integer;
    67 var  i: integer;
    61      s: shortstring;
    68      s: shortstring;
    62      pkbd: PByteArray;
    69      pkbd: PByteArray;
       
    70      Trusted: boolean;
    63 begin
    71 begin
    64 if (CurrentTeam = nil)
    72 KbdKeyPressed:= false;
    65    or (GameState = gsConsole)
    73 Trusted:= (CurrentTeam <> nil)
    66    or (CurrentTeam.ExtDriven)
    74           and (not CurrentTeam.ExtDriven)
    67    or (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].BotLevel <> 0) then exit;
    75           and (CurrentTeam.Hedgehogs[CurrentTeam.CurrHedgehog].BotLevel = 0);
    68 
    76 
    69 KbdKeyPressed:= false;
       
    70 pkbd:= SDL_GetKeyState(nil);
    77 pkbd:= SDL_GetKeyState(nil);
    71 i:= SDL_GetMouseState(nil, nil);
    78 i:= SDL_GetMouseState(nil, nil);
    72 pkbd^[1]:= (i and 1);
    79 pkbd^[1]:= (i and 1);
    73 pkbd^[2]:= ((i shr 1) and 1);
    80 pkbd^[2]:= ((i shr 1) and 1);
    74 pkbd^[3]:= ((i shr 2) and 1);
    81 pkbd^[3]:= ((i shr 2) and 1);
    75 for i:= 1 to cKeyMaxIndex do
    82 for i:= 1 to cKeyMaxIndex do
    76     if CurrentTeam.Aliases[i][0] <> #0 then
    83     if CurrentBinds[i][0] <> #0 then
    77       begin
    84       begin
    78       if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true;
    85       if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true;
    79       if CurrentTeam.Aliases[i][1] = '+' then
    86       if CurrentBinds[i][1] = '+' then
    80           begin
    87           begin
    81           if (pkbd^[i] <> 0)and(tkbd[i]  = 0) then ParseCommand(CurrentTeam.Aliases[i]) else
    88           if (pkbd^[i] <> 0)and(tkbd[i]  = 0) then ParseCommand(CurrentBinds[i], Trusted) else
    82           if (pkbd^[i] =  0)and(tkbd[i] <> 0) then
    89           if (pkbd^[i] =  0)and(tkbd[i] <> 0) then
    83              begin
    90              begin
    84              s:= CurrentTeam.Aliases[i];
    91              s:= CurrentBinds[i];
    85              s[1]:= '-';
    92              s[1]:= '-';
    86              ParseCommand(s)
    93              ParseCommand(s)
    87              end;
    94              end;
    88           end else
    95           end else
    89           if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentTeam.Aliases[i]);
    96           if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted);
    90        tkbd[i]:= pkbd^[i]
    97        tkbd[i]:= pkbd^[i]
    91        end
    98        end
    92 end;
       
    93 
       
    94 procedure ProcessKbdDemo;
       
    95 var pkbd: PByteArray;
       
    96 begin
       
    97 pkbd:= PByteArray(SDL_GetKeyState(nil));
       
    98 if pkbd^[27] <> 0 then
       
    99    begin
       
   100    ParseCommand('/quit');
       
   101    end;
       
   102 end;
    99 end;
   103 
   100 
   104 procedure ResetKbd;
   101 procedure ResetKbd;
   105 var i, t: integer;
   102 var i, t: integer;
   106     pkbd: PByteArray;
   103     pkbd: PByteArray;
   125        else begin
   122        else begin
   126        for t:= 1 to Length(s) do
   123        for t:= 1 to Length(s) do
   127            if s[t] = ' ' then s[t]:= '_';
   124            if s[t] = ' ' then s[t]:= '_';
   128        KeyNames[i]:= s
   125        KeyNames[i]:= s
   129        end;
   126        end;
   130     end
   127     end;
       
   128 
       
   129 DefaultBinds[ 27]:= 'quit';
       
   130 DefaultBinds[ 99]:= 'capture';
       
   131 DefaultBinds[102]:= 'fullscr';
       
   132 SetDefaultBinds
   131 end;
   133 end;
       
   134 
       
   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 
   132 
   145 
   133 initialization
   146 initialization
   134 
   147 
   135 end.
   148 end.