hedgewars/uInputHandler.pas
branchsdl2transition
changeset 11342 ed5a6478e710
parent 11338 7a7611adf715
parent 11046 47a8c19ecb60
child 11356 b8b208501475
equal deleted inserted replaced
11340:31570b766315 11342:ed5a6478e710
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     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
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12  * GNU General Public License for more details.
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    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
    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
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uInputHandler;
    21 unit uInputHandler;
    48 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    48 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    49 procedure ControllerHatEvent(joy, hat, value: Byte);
    49 procedure ControllerHatEvent(joy, hat, value: Byte);
    50 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    50 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    51 
    51 
    52 implementation
    52 implementation
    53 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug, uPhysFSLayer;
    53 uses uConsole, uCommands, uVariables, uConsts, uUtils, uDebug, uPhysFSLayer;
    54 
    54 
    55 const
    55 const
    56     LSHIFT = $0200;
    56     LSHIFT = $0200;
    57     RSHIFT = $0400;
    57     RSHIFT = $0400;
    58     LALT   = $0800;
    58     LALT   = $0800;
    59     RALT   = $1000;
    59     RALT   = $1000;
    60     LCTRL  = $2000;
    60     LCTRL  = $2000;
    61     RCTRL  = $4000; 
    61     RCTRL  = $4000;
    62 
    62 
    63 var tkbd: array[0..cKbdMaxIndex] of boolean;
    63 var tkbd: array[0..cKbdMaxIndex] of boolean;
    64     KeyNames: array [0..cKeyMaxIndex] of string[15];
    64     KeyNames: array [0..cKeyMaxIndex] of string[15];
    65     CurrentBinds: TBinds;
    65     CurrentBinds: TBinds;
    66     ControllerNumControllers: Integer;
    66     ControllerNumControllers: Integer;
    90     KeyNameToCode:= code;
    90     KeyNameToCode:= code;
    91 end;
    91 end;
    92 (*
    92 (*
    93 procedure MaskModifier(var code: LongInt; Modifier: LongWord);
    93 procedure MaskModifier(var code: LongInt; Modifier: LongWord);
    94 begin
    94 begin
    95     if(Modifier and KMOD_LSHIFT) <> 0 then code:= code or LSHIFT; 
    95     if(Modifier and KMOD_LSHIFT) <> 0 then code:= code or LSHIFT;
    96     if(Modifier and KMOD_RSHIFT) <> 0 then code:= code or LSHIFT; 
    96     if(Modifier and KMOD_RSHIFT) <> 0 then code:= code or LSHIFT;
    97     if(Modifier and KMOD_LALT) <> 0 then code:= code or LALT; 
    97     if(Modifier and KMOD_LALT) <> 0 then code:= code or LALT;
    98     if(Modifier and KMOD_RALT) <> 0 then code:= code or LALT; 
    98     if(Modifier and KMOD_RALT) <> 0 then code:= code or LALT;
    99     if(Modifier and KMOD_LCTRL) <> 0 then code:= code or LCTRL; 
    99     if(Modifier and KMOD_LCTRL) <> 0 then code:= code or LCTRL;
   100     if(Modifier and KMOD_RCTRL) <> 0 then code:= code or LCTRL; 
   100     if(Modifier and KMOD_RCTRL) <> 0 then code:= code or LCTRL;
   101 end;
   101 end;
   102 *)
   102 *)
   103 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
   103 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
   104 var mod_ : shortstring;
   104 var mod_ : shortstring = '';
   105     ModifierCount, i: LongInt;
   105     ModifierCount, i: LongInt;
   106 begin
   106 begin
   107 if Modifier = '' then exit;
   107 if Modifier = '' then exit;
   108 ModifierCount:= 0;
   108 ModifierCount:= 0;
   109 
   109 
   111     if(Modifier[i] = ':') then inc(ModifierCount);
   111     if(Modifier[i] = ':') then inc(ModifierCount);
   112 
   112 
   113 SplitByChar(Modifier, mod_, ':');//remove the first mod: part
   113 SplitByChar(Modifier, mod_, ':');//remove the first mod: part
   114 Modifier:= mod_;
   114 Modifier:= mod_;
   115 for i:= 0 to ModifierCount do
   115 for i:= 0 to ModifierCount do
   116     begin 
   116     begin
   117     mod_:= '';
   117     mod_:= '';
   118     SplitByChar(Modifier, mod_, ':');
   118     SplitByChar(Modifier, mod_, ':');
   119     if (Modifier = 'lshift')                    then code:= code or LSHIFT;
   119     if (Modifier = 'lshift')                    then code:= code or LSHIFT;
   120     if (Modifier = 'rshift')                    then code:= code or RSHIFT;
   120     if (Modifier = 'rshift')                    then code:= code or RSHIFT;
   121     if (Modifier = 'lalt')                      then code:= code or LALT;
   121     if (Modifier = 'lalt')                      then code:= code or LALT;
   176         and (not CurrentTeam^.ExtDriven) 
   176         and (not CurrentTeam^.ExtDriven) 
   177         then bShowAmmoMenu:= false;
   177         then bShowAmmoMenu:= false;
   178 
   178 
   179     if KeyDown then
   179     if KeyDown then
   180         begin
   180         begin
       
   181         Trusted:= Trusted and (not isPaused); //releasing keys during pause should be allowed on the other hand
       
   182 
   181         if CurrentBinds[code] = 'switch' then
   183         if CurrentBinds[code] = 'switch' then
   182             LocalMessage:= LocalMessage or gmSwitch
   184             LocalMessage:= LocalMessage or gmSwitch
   183         else if CurrentBinds[code] = '+precise' then
   185         else if CurrentBinds[code] = '+precise' then
   184             LocalMessage:= LocalMessage or gmPrecise;
   186             LocalMessage:= LocalMessage or gmPrecise;
   185             
   187 
   186         ParseCommand(CurrentBinds[code], Trusted);
   188         ParseCommand(CurrentBinds[code], Trusted);
   187         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
   189         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
   188             ParseCommand('gencmd R', true)
   190             ParseCommand('gencmd R', true)
   189         end
   191         end
   190     else if (CurrentBinds[code][1] = '+') then
   192     else if (CurrentBinds[code][1] = '+') then
   191         begin
   193         begin
   192         if CurrentBinds[code] = '+precise' then
   194         if CurrentBinds[code] = '+precise' then
   193             LocalMessage:= LocalMessage and not(gmPrecise);
   195             LocalMessage:= LocalMessage and (not gmPrecise);
   194         s:= CurrentBinds[code];
   196         s:= CurrentBinds[code];
   195         s[1]:= '-';
   197         s[1]:= '-';
   196         ParseCommand(s, Trusted);
   198         ParseCommand(s, Trusted);
   197         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
   199         if (CurrentTeam <> nil) and (not CurrentTeam^.ExtDriven) and (ReadyTimeLeft > 1) then
   198             ParseCommand('gencmd R', true)
   200             ParseCommand('gencmd R', true)
   199         end
   201         end
   200     else
   202     else
   201         begin
   203         begin
   202         if CurrentBinds[code] = 'switch' then
   204         if CurrentBinds[code] = 'switch' then
   203             LocalMessage:= LocalMessage and not(gmSwitch)
   205             LocalMessage:= LocalMessage and (not gmSwitch)
   204         end
   206         end
   205     end
   207     end
   206 end;
   208 end;
   207 
   209 
   208 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   210 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   425             ControllerEnabled:= 1;
   427             ControllerEnabled:= 1;
   426 
   428 
   427             if ControllerNumAxes[j] > 20 then
   429             if ControllerNumAxes[j] > 20 then
   428                 ControllerNumAxes[j]:= 20;
   430                 ControllerNumAxes[j]:= 20;
   429             //if ControllerNumBalls[j] > 20 then ControllerNumBalls[j]:= 20;
   431             //if ControllerNumBalls[j] > 20 then ControllerNumBalls[j]:= 20;
   430             
   432 
   431             if ControllerNumHats[j] > 20 then
   433             if ControllerNumHats[j] > 20 then
   432                 ControllerNumHats[j]:= 20;
   434                 ControllerNumHats[j]:= 20;
   433                 
   435 
   434             if ControllerNumButtons[j] > 20 then
   436             if ControllerNumButtons[j] > 20 then
   435                 ControllerNumButtons[j]:= 20;
   437                 ControllerNumButtons[j]:= 20;
   436 
   438 
   437             (*// reset all buttons/axes
   439             (*// reset all buttons/axes
   438             for i:= 0 to pred(ControllerNumAxes[j]) do
   440             for i:= 0 to pred(ControllerNumAxes[j]) do
   490 var i: LongInt;
   492 var i: LongInt;
   491     f: PFSFile;
   493     f: PFSFile;
   492     p, l: shortstring;
   494     p, l: shortstring;
   493     b: byte;
   495     b: byte;
   494 begin
   496 begin
       
   497     if cOnlyStats then exit;
       
   498 
   495     AddFileLog('[BINDS] Loading binds from: ' + s);
   499     AddFileLog('[BINDS] Loading binds from: ' + s);
   496 
   500 
   497     l:= '';
   501     l:= '';
   498     if pfsExists(s) then
   502     if pfsExists(s) then
   499         begin
   503         begin
   507 
   511 
   508             p:= '';
   512             p:= '';
   509             i:= 1;
   513             i:= 1;
   510             while (i <= length(l)) and (l[i] <> '=') do
   514             while (i <= length(l)) and (l[i] <> '=') do
   511                 begin
   515                 begin
   512                 if l[i] <> '%' then
   516                 if l[i] = '%' then
   513                     begin
       
   514                     p:= p + l[i];
       
   515                     inc(i)
       
   516                     end else
       
   517                     begin
   517                     begin
   518                     l[i]:= '$';
   518                     l[i]:= '$';
   519                     val(copy(l, i, 3), b);
   519                     val(copy(l, i, 3), b);
   520                     p:= p + char(b);
   520                     p:= p + char(b);
   521                     inc(i, 3)
   521                     inc(i, 3)
       
   522                     end
       
   523                 else
       
   524                     begin
       
   525                     p:= p + l[i];
       
   526                     inc(i)
   522                     end;
   527                     end;
   523                 end;
   528                 end;
   524 
   529 
   525             if i < length(l) then
   530             if i < length(l) then
   526                 begin
   531                 begin
   527                 l:= copy(l, i + 1, length(l) - i);
   532                 l:= copy(l, i + 1, length(l) - i);
   528                 if l <> 'default' then
   533                 if l <> 'default' then
   529                     begin
   534                     begin
       
   535                     if (length(l) = 2) and (l[1] = '\') then
       
   536                         l:= l[1] + ''
       
   537                     else if (l[1] = '"') and (l[length(l)] = '"') then
       
   538                         l:= copy(l, 2, length(l) - 2);
       
   539 
   530                     p:= cmd + ' ' + l + ' ' + p;
   540                     p:= cmd + ' ' + l + ' ' + p;
   531                     ParseCommand(p, true)
   541                     ParseCommand(p, true)
   532                     end
   542                     end
   533                 end
   543                 end
   534             end;
   544             end;
   535 
   545 
   536         pfsClose(f)
   546         pfsClose(f)
   537         end 
   547         end
   538         else
   548         else
   539             AddFileLog('[BINDS] file not found');
   549             AddFileLog('[BINDS] file not found');
   540 end;
   550 end;
   541 
   551 
   542 
   552 
   562     Delete(KeyName, byte(KeyName[0]), 1);
   572     Delete(KeyName, byte(KeyName[0]), 1);
   563 b:= KeyNameToCode(id, Modifier);
   573 b:= KeyNameToCode(id, Modifier);
   564 if b = 0 then
   574 if b = 0 then
   565     OutError(errmsgUnknownVariable + ' "' + id + '"', false)
   575     OutError(errmsgUnknownVariable + ' "' + id + '"', false)
   566 else
   576 else
   567     begin 
   577     begin
   568     // add bind: first check if this cmd is already bound, and remove old bind
   578     // add bind: first check if this cmd is already bound, and remove old bind
   569     i:= cKbdMaxIndex;
   579     i:= cKbdMaxIndex;
   570     repeat
   580     repeat
   571         dec(i)
   581         dec(i)
   572     until (i < 0) or (binds[i] = KeyName);
   582     until (i < 0) or (binds[i] = KeyName);