hedgewars/uInputHandler.pas
changeset 7288 5d0704f23a2a
parent 7202 2d78dc517c91
child 7231 f484455dd055
child 7403 e8d0b21efa82
equal deleted inserted replaced
7188:580cd247511e 7288:5d0704f23a2a
    23 uses SDLh, uTypes;
    23 uses SDLh, uTypes;
    24 
    24 
    25 procedure initModule;
    25 procedure initModule;
    26 procedure freeModule;
    26 procedure freeModule;
    27 
    27 
    28 function  KeyNameToCode(name: shortstring): word;
    28 function  KeyNameToCode(name: shortstring; Modifier: shortstring = ''): LongInt;
       
    29 procedure MaskModifier(var code: LongInt; modifier: LongWord);
       
    30 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
    29 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
    31 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
    30 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
    32 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
    31 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    33 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    32 
    34 
    33 procedure ResetKbd;
    35 procedure ResetKbd;
    43 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    45 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    44 
    46 
    45 implementation
    47 implementation
    46 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    48 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    47 
    49 
    48 var tkbd: array[0..cKeyMaxIndex] of boolean;
    50 const
       
    51     LSHIFT = $0200;
       
    52     RSHIFT = $0400;
       
    53     LALT   = $0800;
       
    54     RALT   = $1000;
       
    55     LCTRL  = $2000;
       
    56     RCTRL  = $4000; 
       
    57 
       
    58 var tkbd: array[0..cKbdMaxIndex] of boolean;
    49     quitKeyCode: Byte;
    59     quitKeyCode: Byte;
    50     KeyNames: array [0..cKeyMaxIndex] of string[15];
    60     KeyNames: array [0..cKeyMaxIndex] of string[15];
    51     CurrentBinds: TBinds;
    61     CurrentBinds: TBinds;
    52 
    62 
    53 function KeyNameToCode(name: shortstring): word;
    63 function KeyNameToCode(name: shortstring; Modifier: shortstring): LongInt;
    54 var code: Word;
    64 var code: LongInt;
    55 begin
    65 begin
    56     name:= LowerCase(name);
    66     name:= LowerCase(name);
    57     code:= cKeyMaxIndex;
    67     code:= cKeyMaxIndex;
    58     while (code > 0) and (KeyNames[code] <> name) do dec(code);
    68     while (code > 0) and (KeyNames[code] <> name) do dec(code);
       
    69 
       
    70     MaskModifier(Modifier, code);
    59     KeyNameToCode:= code;
    71     KeyNameToCode:= code;
       
    72 end;
       
    73 
       
    74 procedure MaskModifier(var code: LongInt; Modifier: LongWord);
       
    75 begin
       
    76     if(Modifier and KMOD_LSHIFT) <> 0 then code:= code or LSHIFT; 
       
    77     if(Modifier and KMOD_RSHIFT) <> 0 then code:= code or LSHIFT; 
       
    78     if(Modifier and KMOD_LALT) <> 0 then code:= code or LALT; 
       
    79     if(Modifier and KMOD_RALT) <> 0 then code:= code or LALT; 
       
    80     if(Modifier and KMOD_LCTRL) <> 0 then code:= code or LCTRL; 
       
    81     if(Modifier and KMOD_RCTRL) <> 0 then code:= code or LCTRL; 
       
    82 end;
       
    83 
       
    84 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
       
    85 var mod_ : shortstring;
       
    86     ModifierCount, i: LongInt;
       
    87 begin
       
    88 if Modifier = '' then exit;
       
    89 ModifierCount:= 0;
       
    90 
       
    91 for i:= 1 to Length(Modifier) do
       
    92     if(Modifier[i] = ':') then inc(ModifierCount);
       
    93 
       
    94 SplitByChar(Modifier, mod_, ':');//remove the first mod: part
       
    95 Modifier:= mod_;
       
    96 for i:= 0 to ModifierCount do
       
    97     begin 
       
    98     mod_:= '';
       
    99     SplitByChar(Modifier, mod_, ':');
       
   100     if (Modifier = 'lshift')                    then code:= code or LSHIFT;
       
   101     if (Modifier = 'rshift')                    then code:= code or RSHIFT;
       
   102     if (Modifier = 'lalt')                      then code:= code or LALT;
       
   103     if (Modifier = 'ralt')                      then code:= code or RALT;
       
   104     if (Modifier = 'lctrl') or (mod_ = 'lmeta') then code:= code or LCTRL;
       
   105     if (Modifier = 'rctrl') or (mod_ = 'rmeta') then code:= code or RCTRL;
       
   106     Modifier:= mod_;
       
   107     end;
    60 end;
   108 end;
    61 
   109 
    62 procedure ProcessKey(code: LongInt; KeyDown: boolean);
   110 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    63 var
   111 var
    64     Trusted: boolean;
   112     Trusted: boolean;
    65     s      : string;
   113     s      : string;
    66 begin
   114 begin
    67 
       
    68 if not(tkbd[code] xor KeyDown) then exit;
   115 if not(tkbd[code] xor KeyDown) then exit;
    69 tkbd[code]:= KeyDown;
   116 tkbd[code]:= KeyDown;
    70 
       
    71 
   117 
    72 hideAmmoMenu:= false;
   118 hideAmmoMenu:= false;
    73 Trusted:= (CurrentTeam <> nil)
   119 Trusted:= (CurrentTeam <> nil)
    74           and (not CurrentTeam^.ExtDriven)
   120           and (not CurrentTeam^.ExtDriven)
    75           and (CurrentHedgehog^.BotLevel = 0);
   121           and (CurrentHedgehog^.BotLevel = 0);
    76 
       
    77 
       
    78 
   122 
    79 // ctrl/cmd + q to close engine and frontend
   123 // ctrl/cmd + q to close engine and frontend
    80 if(KeyDown and (code = quitKeyCode)) then
   124 if(KeyDown and (code = quitKeyCode)) then
    81     begin
   125     begin
    82 {$IFDEF DARWIN}
   126 {$IFDEF DARWIN}
   107         end;
   151         end;
   108     end
   152     end
   109 end;
   153 end;
   110 
   154 
   111 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   155 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   112 begin
   156 var code: LongInt;
   113     ProcessKey(event.keysym.sym, event.type_ = SDL_KEYDOWN);
   157 begin
       
   158     code:= event.keysym.sym;
       
   159     //MaskModifier(code, event.keysym.modifier);
       
   160     ProcessKey(code, event.type_ = SDL_KEYDOWN);
   114 end;
   161 end;
   115 
   162 
   116 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   163 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   117 begin
   164 begin
   118 case event.button of
   165 case event.button of
   130 end;
   177 end;
   131 
   178 
   132 procedure ResetKbd;
   179 procedure ResetKbd;
   133 var t: LongInt;
   180 var t: LongInt;
   134 begin
   181 begin
   135 for t:= 0 to cKeyMaxIndex do
   182 for t:= 0 to cKbdMaxIndex do
   136     if tkbd[t] then
   183     if tkbd[t] then
   137         ProcessKey(t, False);
   184         ProcessKey(t, False);
   138 end;
   185 end;
   139 
   186 
   140 procedure InitKbdKeyTable;
   187 procedure InitKbdKeyTable;
   246 begin
   293 begin
   247 {$IFDEF MOBILE}
   294 {$IFDEF MOBILE}
   248     binds:= binds; // avoid hint
   295     binds:= binds; // avoid hint
   249     CurrentBinds:= DefaultBinds;
   296     CurrentBinds:= DefaultBinds;
   250 {$ELSE}
   297 {$ELSE}
   251 for t:= 0 to cKeyMaxIndex do
   298 for t:= 0 to cKbdMaxIndex do
   252     if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
   299     if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
   253         ProcessKey(t, False);
   300         ProcessKey(t, False);
   254 
   301 
   255     CurrentBinds:= binds;
   302     CurrentBinds:= binds;
   256 {$ENDIF}
   303 {$ENDIF}