hedgewars/uInputHandler.pas
changeset 7117 7df6386f7090
parent 7106 aacb90365d3d
child 7118 dcdbb6fcc924
equal deleted inserted replaced
7116:547b5be3a8e5 7117:7df6386f7090
    43 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    43 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    44 
    44 
    45 implementation
    45 implementation
    46 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    46 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    47 
    47 
    48 var tkbd, tkbdn: TKeyboardState;
    48 var tkbd: TKeyboardState;
    49     quitKeyCode: Byte;
    49     quitKeyCode: Byte;
    50     KeyNames: array [0..cKeyMaxIndex] of string[15];
    50     KeyNames: array [0..cKeyMaxIndex] of string[15];
    51     CurrentBinds: TBinds;
    51     CurrentBinds: TBinds;
    52 
    52 
    53 function KeyNameToCode(name: shortstring): word;
    53 function KeyNameToCode(name: shortstring): word;
    67 hideAmmoMenu:= false;
    67 hideAmmoMenu:= false;
    68 Trusted:= (CurrentTeam <> nil)
    68 Trusted:= (CurrentTeam <> nil)
    69           and (not CurrentTeam^.ExtDriven)
    69           and (not CurrentTeam^.ExtDriven)
    70           and (CurrentHedgehog^.BotLevel = 0);
    70           and (CurrentHedgehog^.BotLevel = 0);
    71 
    71 
    72 tkbdn[code]:= ord(KeyDown);
    72 tkbd[code]:= ord(KeyDown);
    73 
    73 
    74 // ctrl/cmd + q to close engine and frontend
    74 // ctrl/cmd + q to close engine and frontend
    75 if(KeyDown and (code = quitKeyCode)) then
    75 if(KeyDown and (code = quitKeyCode)) then
    76     begin
    76     begin
    77 {$IFDEF DARWIN}
    77 {$IFDEF DARWIN}
    78     if ((tkbdn[KeyNameToCode('left_meta')] = 1) or (tkbdn[KeyNameToCode('right_meta')] = 1)) then
    78     if ((tkbd[KeyNameToCode('left_meta')] = 1) or (tkbd[KeyNameToCode('right_meta')] = 1)) then
    79 {$ELSE}
    79 {$ELSE}
    80     if ((tkbdn[KeyNameToCode('left_ctrl')] = 1) or (tkbdn[KeyNameToCode('right_ctrl')] = 1)) then
    80     if ((tkbd[KeyNameToCode('left_ctrl')] = 1) or (tkbd[KeyNameToCode('right_ctrl')] = 1)) then
    81 {$ENDIF}
    81 {$ENDIF}
    82         ParseCommand('halt', true);    
    82         ParseCommand('halt', true);    
    83     end;
    83     end;
    84 
    84 
    85 if CurrentBinds[code][0] <> #0 then
    85 if CurrentBinds[code][0] <> #0 then
    86     begin
    86     begin
    87     if (code > 3) and (tkbdn[code] <> 0) and not ((CurrentBinds[code] = 'put') or (CurrentBinds[code] = 'ammomenu') or (CurrentBinds[code] = '+cur_u') or (CurrentBinds[code] = '+cur_d') or (CurrentBinds[code] = '+cur_l') or (CurrentBinds[code] = '+cur_r')) then hideAmmoMenu:= true;
    87     if (code > 3) and (tkbd[code] <> 0) and not ((CurrentBinds[code] = 'put') or (CurrentBinds[code] = 'ammomenu') or (CurrentBinds[code] = '+cur_u') or (CurrentBinds[code] = '+cur_d') or (CurrentBinds[code] = '+cur_l') or (CurrentBinds[code] = '+cur_r')) then hideAmmoMenu:= true;
    88     if (tkbd[code] = 0) and (tkbdn[code] <> 0) then
    88     if (KeyDown) then
    89         begin
    89         begin
    90         ParseCommand(CurrentBinds[code], Trusted);
    90         ParseCommand(CurrentBinds[code], Trusted);
    91         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
    91         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
    92             ParseCommand('gencmd R', true)
    92             ParseCommand('gencmd R', true)
    93         end
    93         end
    94     else if (CurrentBinds[code][1] = '+') and (tkbdn[code] = 0) and (tkbd[code] <> 0) then
    94     else if (CurrentBinds[code][1] = '+') and not KeyDown then
    95         begin
    95         begin
    96         s:= CurrentBinds[code];
    96         s:= CurrentBinds[code];
    97         s[1]:= '-';
    97         s[1]:= '-';
    98         ParseCommand(s, Trusted);
    98         ParseCommand(s, Trusted);
    99         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
    99         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
   100             ParseCommand('gencmd R', true)
   100             ParseCommand('gencmd R', true)
   101         end;
   101         end;
   102     tkbd[code]:= tkbdn[code]
       
   103     end
   102     end
   104 
   103 
   105 end;
   104 end;
   106 
   105 
   107 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   106 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   124         ProcessKey(KeyNameToCode('wheelup'), ButtonDown);
   123         ProcessKey(KeyNameToCode('wheelup'), ButtonDown);
   125     end;
   124     end;
   126 end;
   125 end;
   127 
   126 
   128 procedure ResetKbd;
   127 procedure ResetKbd;
   129 var j, t: LongInt;
   128 var t: LongInt;
   130     i: LongInt;
   129 begin
   131     pkbd: PByteArray;
       
   132 begin
       
   133 
       
   134 //k:= SDL_GetMouseState(nil, nil);
       
   135 pkbd:=SDL_GetKeyState(@j);
       
   136 
       
   137 //TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + IntToStr(j) + ')', true);
       
   138 
       
   139 for i:= 1 to pred(j) do
       
   140     tkbdn[i]:= pkbd^[i];
       
   141 
       
   142 (*
       
   143 // TODO: reimplement
       
   144 // Controller(s)
       
   145 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
       
   146 for j:= 0 to Pred(ControllerNumControllers) do
       
   147     begin
       
   148     for i:= 0 to Pred(ControllerNumAxes[j]) do
       
   149         begin
       
   150         if ControllerAxes[j][i] > 20000 then
       
   151             tkbdn[k + 0]:= 1
       
   152         else
       
   153             tkbdn[k + 0]:= 0;
       
   154         if ControllerAxes[j][i] < -20000 then
       
   155             tkbdn[k + 1]:= 1
       
   156         else
       
   157             tkbdn[k + 1]:= 0;
       
   158         inc(k, 2);
       
   159         end;
       
   160     for i:= 0 to Pred(ControllerNumHats[j]) do
       
   161         begin
       
   162         tkbdn[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP;
       
   163         tkbdn[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT;
       
   164         tkbdn[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN;
       
   165         tkbdn[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT;
       
   166         inc(k, 4);
       
   167         end;
       
   168     for i:= 0 to Pred(ControllerNumButtons[j]) do
       
   169         begin
       
   170         tkbdn[k]:= ControllerButtons[j][i];
       
   171         inc(k, 1);
       
   172         end;
       
   173     end;
       
   174 *)
       
   175 
       
   176 // what is this final loop for?
       
   177 for t:= 0 to cKeyMaxIndex do
   130 for t:= 0 to cKeyMaxIndex do
   178     tkbd[t]:= tkbdn[t]
   131     if(tkbd[t] <> 0) then
       
   132         ProcessKey(t, False);
   179 end;
   133 end;
   180 
   134 
   181 procedure InitKbdKeyTable;
   135 procedure InitKbdKeyTable;
   182 var i, j, k, t: LongInt;
   136 var i, j, k, t: LongInt;
   183     s: string[15];
   137     s: string[15];