hedgewars/uInputHandler.pas
changeset 7194 d8e68cbca7ee
parent 7180 53ffc8853008
parent 7192 e6c379b486d5
child 7196 4fba5519c37f
equal deleted inserted replaced
7180:53ffc8853008 7194:d8e68cbca7ee
    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 var tkbd: array[0..cKbdMaxIndex] of boolean;
    49     quitKeyCode: Byte;
    51     quitKeyCode: Byte;
    50     KeyNames: array [0..cKeyMaxIndex] of string[15];
    52     KeyNames: array [0..cKeyMaxIndex] of string[15];
    51     CurrentBinds: TBinds;
    53     CurrentBinds: TBinds;
    52 
    54 
    53 function KeyNameToCode(name: shortstring): word;
    55 function KeyNameToCode(name: shortstring; Modifier: shortstring): LongInt;
    54 var code: Word;
    56 var code: LongInt;
    55 begin
    57 begin
    56     name:= LowerCase(name);
    58     name:= LowerCase(name);
    57     code:= cKeyMaxIndex;
    59     code:= cKeyMaxIndex;
    58     while (code > 0) and (KeyNames[code] <> name) do dec(code);
    60     while (code > 0) and (KeyNames[code] <> name) do dec(code);
       
    61 
       
    62     MaskModifier(Modifier, code);
    59     KeyNameToCode:= code;
    63     KeyNameToCode:= code;
       
    64 end;
       
    65 
       
    66 procedure MaskModifier(var code: LongInt; Modifier: LongWord);
       
    67 begin
       
    68     code:= code or (modifier shl 10);
       
    69 end;
       
    70 
       
    71 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
       
    72 var mod_ : shortstring;
       
    73     ModifierCount, i: LongInt;
       
    74     c : char;
       
    75 begin
       
    76 if Modifier = '' then exit;
       
    77 ModifierCount:= 0;
       
    78 for c in Modifier do
       
    79     if(c = ':') then inc(ModifierCount);
       
    80 
       
    81 SplitByChar(Modifier, mod_, ':');//remove the first mod: part
       
    82 Modifier:= mod_;
       
    83 for i:= 0 to ModifierCount do
       
    84     begin 
       
    85     mod_:= '';
       
    86     SplitByChar(Modifier, mod_, ':');
       
    87     if (Modifier = 'lshift')                    then code:= code or (KMOD_LSHIFT shl 10);
       
    88     if (Modifier = 'rshift')                    then code:= code or (KMOD_RSHIFT shl 10);
       
    89     if (Modifier = 'lalt')                      then code:= code or (KMOD_LALT   shl 10);
       
    90     if (Modifier = 'ralt')                      then code:= code or (KMOD_RALT   shl 10);
       
    91     if (Modifier = 'lctrl') or (mod_ = 'lmeta') then code:= code or (KMOD_LCTRL  shl 10);
       
    92     if (Modifier = 'rctrl') or (mod_ = 'rmeta') then code:= code or (KMOD_RCTRL  shl 10);
       
    93     Modifier:= mod_;
       
    94     end;
    60 end;
    95 end;
    61 
    96 
    62 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    97 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    63 var
    98 var
    64     Trusted: boolean;
    99     Trusted: boolean;
    65     s      : string;
   100     s      : string;
    66 begin
   101 begin
    67 
       
    68 if not(tkbd[code] xor KeyDown) then exit;
   102 if not(tkbd[code] xor KeyDown) then exit;
    69 tkbd[code]:= KeyDown;
   103 tkbd[code]:= KeyDown;
    70 
       
    71 
   104 
    72 hideAmmoMenu:= false;
   105 hideAmmoMenu:= false;
    73 Trusted:= (CurrentTeam <> nil)
   106 Trusted:= (CurrentTeam <> nil)
    74           and (not CurrentTeam^.ExtDriven)
   107           and (not CurrentTeam^.ExtDriven)
    75           and (CurrentHedgehog^.BotLevel = 0);
   108           and (CurrentHedgehog^.BotLevel = 0);
    76 
       
    77 
       
    78 
   109 
    79 // ctrl/cmd + q to close engine and frontend
   110 // ctrl/cmd + q to close engine and frontend
    80 if(KeyDown and (code = quitKeyCode)) then
   111 if(KeyDown and (code = quitKeyCode)) then
    81     begin
   112     begin
    82 {$IFDEF DARWIN}
   113 {$IFDEF DARWIN}
   107         end;
   138         end;
   108     end
   139     end
   109 end;
   140 end;
   110 
   141 
   111 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   142 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   112 begin
   143 var code: LongInt;
   113     ProcessKey(event.keysym.sym, event.type_ = SDL_KEYDOWN);
   144 begin
       
   145     code:= event.keysym.sym;
       
   146     MaskModifier(code, event.keysym.modifier);
       
   147     
       
   148     ProcessKey(code, event.type_ = SDL_KEYDOWN);
   114 end;
   149 end;
   115 
   150 
   116 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   151 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   117 begin
   152 begin
   118 case event.button of
   153 case event.button of
   130 end;
   165 end;
   131 
   166 
   132 procedure ResetKbd;
   167 procedure ResetKbd;
   133 var t: LongInt;
   168 var t: LongInt;
   134 begin
   169 begin
   135 for t:= 0 to cKeyMaxIndex do
   170 for t:= 0 to cKbdMaxIndex do
   136     if tkbd[t] then
   171     if tkbd[t] then
   137         ProcessKey(t, False);
   172         ProcessKey(t, False);
   138 end;
   173 end;
   139 
   174 
   140 procedure InitKbdKeyTable;
   175 procedure InitKbdKeyTable;
   238 
   273 
   239 SetDefaultBinds();
   274 SetDefaultBinds();
   240 end;
   275 end;
   241 
   276 
   242 procedure SetBinds(var binds: TBinds);
   277 procedure SetBinds(var binds: TBinds);
       
   278 {$IFNDEF MOBILE}
       
   279 var
       
   280     t: LongInt;
       
   281 {$ENDIF}
   243 begin
   282 begin
   244 {$IFDEF MOBILE}
   283 {$IFDEF MOBILE}
   245     binds:= binds; // avoid hint
   284     binds:= binds; // avoid hint
   246     CurrentBinds:= DefaultBinds;
   285     CurrentBinds:= DefaultBinds;
   247 {$ELSE}
   286 {$ELSE}
       
   287 for t:= 0 to cKbdMaxIndex do
       
   288     if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
       
   289         ProcessKey(t, False);
       
   290 
   248     CurrentBinds:= binds;
   291     CurrentBinds:= binds;
   249 {$ENDIF}
   292 {$ENDIF}
   250 end;
   293 end;
   251 
   294 
   252 procedure SetDefaultBinds;
   295 procedure SetDefaultBinds;