hedgewars/uInputHandler.pas
changeset 7191 9419294e5f33
parent 7184 211ab9e2cb15
child 7192 e6c379b486d5
equal deleted inserted replaced
7190:aa8d68817c32 7191:9419294e5f33
    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 
       
    63     MaskModifier(Modifier, code);
       
    64     WriteLnToConsole(inttostr(code));
       
    65 
    59     KeyNameToCode:= code;
    66     KeyNameToCode:= code;
       
    67 end;
       
    68 
       
    69 procedure MaskModifier(var code: LongInt; Modifier: LongWord);
       
    70 begin
       
    71     WriteLnToConsole(inttostr(code));
       
    72     code:= code or (modifier shl 10);
       
    73     WriteLnToConsole(inttostr(code));
       
    74 end;
       
    75 
       
    76 procedure MaskModifier(Modifier: shortstring; var code: LongInt);
       
    77 var mod_ : shortstring;
       
    78     ModifierCount, i: LongInt;
       
    79     c : char;
       
    80 begin
       
    81 if Modifier = '' then exit;
       
    82 ModifierCount:= 0;
       
    83 for c in Modifier do
       
    84     if(c = ':') then inc(ModifierCount);
       
    85 
       
    86 SplitByChar(Modifier, mod_, ':');//remove the first mod: part
       
    87 Modifier:= mod_;
       
    88 for i:= 0 to ModifierCount do
       
    89     begin 
       
    90     mod_:= '';
       
    91     SplitByChar(Modifier, mod_, ':');
       
    92     WriteLnToConsole(Modifier + ' baaaaa' );
       
    93     if (Modifier = 'lshift')                    then code:= code or (KMOD_LSHIFT shl 10);
       
    94     if (Modifier = 'rshift')                    then code:= code or (KMOD_RSHIFT shl 10);
       
    95     if (Modifier = 'lalt')                      then code:= code or (KMOD_LALT   shl 10);
       
    96     if (Modifier = 'ralt')                      then code:= code or (KMOD_RALT   shl 10);
       
    97     if (Modifier = 'lctrl') or (mod_ = 'lmeta') then code:= code or (KMOD_LCTRL  shl 10);
       
    98     if (Modifier = 'rctrl') or (mod_ = 'rmeta') then code:= code or (KMOD_RCTRL  shl 10);
       
    99     Modifier:= mod_;
       
   100     end;
    60 end;
   101 end;
    61 
   102 
    62 procedure ProcessKey(code: LongInt; KeyDown: boolean);
   103 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    63 var
   104 var
    64     Trusted: boolean;
   105     Trusted: boolean;
    65     s      : string;
   106     s      : string;
    66 begin
   107 begin
    67 
   108 WriteLnToConsole(inttostr(code) + ' KeyDown:' + inttostr(ord(keydown)) + CurrentBinds[code]) ; 
    68 if not(tkbd[code] xor KeyDown) then exit;
   109 if not(tkbd[code] xor KeyDown) then exit;
    69 tkbd[code]:= KeyDown;
   110 tkbd[code]:= KeyDown;
    70 
       
    71 
   111 
    72 hideAmmoMenu:= false;
   112 hideAmmoMenu:= false;
    73 Trusted:= (CurrentTeam <> nil)
   113 Trusted:= (CurrentTeam <> nil)
    74           and (not CurrentTeam^.ExtDriven)
   114           and (not CurrentTeam^.ExtDriven)
    75           and (CurrentHedgehog^.BotLevel = 0);
   115           and (CurrentHedgehog^.BotLevel = 0);
    76 
       
    77 
       
    78 
   116 
    79 // ctrl/cmd + q to close engine and frontend
   117 // ctrl/cmd + q to close engine and frontend
    80 if(KeyDown and (code = quitKeyCode)) then
   118 if(KeyDown and (code = quitKeyCode)) then
    81     begin
   119     begin
    82 {$IFDEF DARWIN}
   120 {$IFDEF DARWIN}
   107         end;
   145         end;
   108     end
   146     end
   109 end;
   147 end;
   110 
   148 
   111 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   149 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   112 begin
   150 var code: LongInt;
   113     ProcessKey(event.keysym.sym, event.type_ = SDL_KEYDOWN);
   151 begin
       
   152     code:= event.keysym.sym;
       
   153     MaskModifier(code, event.keysym.modifier);
       
   154     
       
   155     ProcessKey(code, event.type_ = SDL_KEYDOWN);
   114 end;
   156 end;
   115 
   157 
   116 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   158 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   117 begin
   159 begin
   118 case event.button of
   160 case event.button of
   130 end;
   172 end;
   131 
   173 
   132 procedure ResetKbd;
   174 procedure ResetKbd;
   133 var t: LongInt;
   175 var t: LongInt;
   134 begin
   176 begin
   135 for t:= 0 to cKeyMaxIndex do
   177 for t:= 0 to cKbdMaxIndex do
   136     if tkbd[t] then
   178     if tkbd[t] then
   137         ProcessKey(t, False);
   179         ProcessKey(t, False);
   138 end;
   180 end;
   139 
   181 
   140 procedure InitKbdKeyTable;
   182 procedure InitKbdKeyTable;
   246 begin
   288 begin
   247 {$IFDEF MOBILE}
   289 {$IFDEF MOBILE}
   248     binds:= binds; // avoid hint
   290     binds:= binds; // avoid hint
   249     CurrentBinds:= DefaultBinds;
   291     CurrentBinds:= DefaultBinds;
   250 {$ELSE}
   292 {$ELSE}
   251 for t:= 0 to cKeyMaxIndex do
   293 for t:= 0 to cKbdMaxIndex do
   252     if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
   294     if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
   253         ProcessKey(t, False);
   295         ProcessKey(t, False);
   254 
   296 
   255     CurrentBinds:= binds;
   297     CurrentBinds:= binds;
   256 {$ENDIF}
   298 {$ENDIF}