hedgewars/uKeys.pas
changeset 6900 26873e68034f
parent 6863 4d1ce4b4f7c6
child 6906 6f5e1e64f106
equal deleted inserted replaced
6899:c3d44a836086 6900:26873e68034f
    39 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    39 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    40 procedure ControllerHatEvent(joy, hat, value: Byte);
    40 procedure ControllerHatEvent(joy, hat, value: Byte);
    41 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    41 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    42 
    42 
    43 {$IFDEF MOBILE}
    43 {$IFDEF MOBILE}
    44 procedure setiPhoneBinds;
    44 procedure setTouchWidgetStates;
    45 {$ENDIF}
    45 {$ENDIF}
    46 
    46 
    47 implementation
    47 implementation
    48 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    48 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    49 
    49 
    61 
    61 
    62 procedure ProcessKbd;
    62 procedure ProcessKbd;
    63 var  i, j, k: LongInt;
    63 var  i, j, k: LongInt;
    64      s: shortstring;
    64      s: shortstring;
    65      Trusted: boolean;
    65      Trusted: boolean;
    66 {$IFNDEF MOBILE}pkbd: PByteArray;{$ENDIF}
    66      pkbd: PByteArray;
    67 begin
    67 begin
    68 hideAmmoMenu:= false;
    68 hideAmmoMenu:= false;
    69 Trusted:= (CurrentTeam <> nil)
    69 Trusted:= (CurrentTeam <> nil)
    70           and (not CurrentTeam^.ExtDriven)
    70           and (not CurrentTeam^.ExtDriven)
    71           and (CurrentHedgehog^.BotLevel = 0);
    71           and (CurrentHedgehog^.BotLevel = 0);
    72 
    72 
    73 // move cursor/camera
    73 // move cursor/camera
    74 // TODO: Scale on screen dimensions and/or axis value (game controller)?
    74 // TODO: Scale on screen dimensions and/or axis value (game controller)?
    75 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
    75 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
    76 
    76 
       
    77 pkbd:= SDL_GetKeyState(@j);
       
    78 for i:= 1 to pred(j) do 
       
    79     tkbdn[i]:= pkbd^[i];
       
    80 
    77 k:= SDL_GetMouseState(nil, nil);
    81 k:= SDL_GetMouseState(nil, nil);
    78 
       
    79 {$IFDEF MOBILE}
       
    80 SDL_GetKeyState(@j);
       
    81 {$ELSE}
       
    82 pkbd:= SDL_GetKeyState(@j);
       
    83 for i:= 6 to pred(j) do // first 6 will be overwritten
       
    84     tkbdn[i]:= pkbd^[i];
       
    85 {$ENDIF}
       
    86 
       
    87 // mouse buttons
    82 // mouse buttons
    88 {$IFDEF DARWIN}
    83 {$IFDEF DARWIN}
    89 tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
    84 tkbdn[SDL_SCANCODE_MOUSEL]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
    90 tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
    85 tkbdni[SDL_SCANCODE_MOUSER]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
    91 {$ELSE}
    86 {$ELSE}
    92 tkbdn[1]:= (k and 1);
    87 tkbdn[SDL_SCANCODE_MOUSEL]:= (k and 1);
    93 tkbdn[3]:= ((k shr 2) and 1);
    88 tkbdn[SDL_SCANCODE_MOUSER]:= ((k shr 2) and 1);
    94 {$ENDIF}
    89 {$ENDIF}
    95 tkbdn[2]:= ((k shr 1) and 1);
    90 tkbdn[SDL_SCANCODE_MOUSEM]:= ((k shr 1) and 1);
    96 
    91 
    97 // mouse wheels
    92 // mouse wheels
    98 tkbdn[4]:= ord(wheelDown);
    93 tkbdn[SDL_SCANCODE_WHEELDOWN]:= ord(wheelDown);
    99 tkbdn[5]:= ord(wheelUp);
    94 tkbdn[SDL_SCANCODE_WHEELUP]:= ord(wheelUp);
   100 wheelUp:= false;
    95 wheelUp:= false;
   101 wheelDown:= false;
    96 wheelDown:= false;
   102 
    97 
   103 {$IFDEF MOBILE}
    98 {$IFDEF MOBILE}
   104 setiPhoneBinds();
    99 setTouchWidgetStates();
   105 {$ELSE}
   100 {$ENDIF}
       
   101 
       
   102 {$IFNDEF MOBILE}
   106 // Controller(s)
   103 // Controller(s)
   107 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   104 k:= SDL_SCANCODE_CONTROLLER; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   108 for j:= 0 to Pred(ControllerNumControllers) do
   105 for j:= 0 to Pred(ControllerNumControllers) do
   109     begin
   106     begin
   110     for i:= 0 to Pred(ControllerNumAxes[j]) do
   107     for i:= 0 to Pred(ControllerNumAxes[j]) do
   111         begin
   108         begin
   112         if ControllerAxes[j][i] > 20000 then
   109         if ControllerAxes[j][i] > 20000 then
   135     end;
   132     end;
   136 {$ENDIF}
   133 {$ENDIF}
   137 
   134 
   138 // ctrl/cmd + q to close engine and frontend
   135 // ctrl/cmd + q to close engine and frontend
   139 {$IFDEF DARWIN}
   136 {$IFDEF DARWIN}
   140     if ((tkbdn[KeyNameToCode('left_meta')] = 1) or (tkbdn[KeyNameToCode('right_meta')] = 1)) then
   137     if ((tkbdn[SDL_SCANCODE_LGUI] = 1) or (tkbdn[SDL_SCANCODE_RGUI] = 1)) then
   141 {$ELSE}
   138 {$ELSE}
   142     if ((tkbdn[KeyNameToCode('left_ctrl')] = 1) or (tkbdn[KeyNameToCode('right_ctrl')] = 1)) then
   139     if ((tkbdn[SDL_SCANCODE_LCTRL] = 1) or (tkbdn[SDL_SCANCODE_RCTRL] = 1)) then
   143 {$ENDIF}
   140 {$ENDIF}
   144     begin
   141     begin
   145         if tkbdn[KeyNameToCode('q')] = 1 then ParseCommand ('halt', true)
   142         if tkbdn[SDL_SCANCODE_Q] = 1 then ParseCommand ('halt', true)
   146     end;
   143     end;
   147 
   144 
   148 // now process strokes
   145 // now process strokes
   149 for i:= 0 to cKeyMaxIndex do
   146 for i:= 0 to cKeyMaxIndex do
   150 if CurrentBinds[i][0] <> #0 then
   147 if CurrentBinds[i][0] <> #0 then
   168     end
   165     end
   169 end;
   166 end;
   170 
   167 
   171 procedure ResetKbd;
   168 procedure ResetKbd;
   172 var j, k, t: LongInt;
   169 var j, k, t: LongInt;
   173 {$IFNDEF MOBILE}
       
   174     i: LongInt;
   170     i: LongInt;
   175     pkbd: PByteArray;
   171     pkbd: PByteArray;
   176 {$ENDIF}
       
   177 begin
   172 begin
   178 
   173 
   179 k:= SDL_GetMouseState(nil, nil);
   174 k:= SDL_GetMouseState(nil, nil);
   180 {$IFNDEF MOBILE}pkbd:={$ENDIF}SDL_GetKeyState(@j);
   175 pkbd:=SDL_GetKeyState(@j);
   181 
   176 
   182 TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + IntToStr(j) + ')', true);
   177 TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + IntToStr(j) + ')', true);
   183 
   178 
   184 {$IFNDEF MOBILE}
       
   185 for i:= 1 to pred(j) do
   179 for i:= 1 to pred(j) do
   186     tkbdn[i]:= pkbd^[i];
   180     tkbdn[i]:= pkbd^[i];
   187 {$ENDIF}
       
   188 
   181 
   189 // mouse buttons
   182 // mouse buttons
   190 {$IFDEF DARWIN}
   183 {$IFDEF DARWIN}
   191 tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
   184 tkbdn[SDL_SCANCODE_MOUSEL]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
   192 tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
   185 tkbdn[SDL_SCANCODE_MOUSER]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
   193 {$ELSE}
   186 {$ELSE}
   194 tkbdn[1]:= (k and 1);
   187 tkbdn[SDL_SCANCODE_MOUSEL]:= (k and 1);
   195 tkbdn[3]:= ((k shr 2) and 1);
   188 tkbdn[SDL_SCANCODE_MOUSER]:= ((k shr 2) and 1);
   196 {$ENDIF}
   189 {$ENDIF}
   197 tkbdn[2]:= ((k shr 1) and 1);
   190 tkbdn[SDL_SCANCODE_MOUSEM]:= ((k shr 1) and 1);
   198 
   191 
   199 // mouse wheels
   192 // mouse wheels
   200 tkbdn[4]:= ord(wheelDown);
   193 tkbdn[SDL_SCANCODE_WHEELDOWN]:= ord(wheelDown);
   201 tkbdn[5]:= ord(wheelUp);
   194 tkbdn[SDL_SCANCODE_WHEELUP]:= ord(wheelUp);
   202 wheelUp:= false;
   195 wheelUp:= false;
   203 wheelDown:= false;
   196 wheelDown:= false;
   204 
   197 
   205 {$IFDEF MOBILE}
   198 {$IFDEF MOBILE}
   206 setiPhoneBinds();
   199 setTouchWidgetStates();
   207 {$ELSE}
   200 {$ENDIF}
       
   201 
       
   202 {$IFNDEF MOBILE}
   208 // Controller(s)
   203 // Controller(s)
   209 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   204 k:= SDL_SCANCODE_CONTROLLER; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   210 for j:= 0 to Pred(ControllerNumControllers) do
   205 for j:= 0 to Pred(ControllerNumControllers) do
   211     begin
   206     begin
   212     for i:= 0 to Pred(ControllerNumAxes[j]) do
   207     for i:= 0 to Pred(ControllerNumAxes[j]) do
   213         begin
   208         begin
   214         if ControllerAxes[j][i] > 20000 then
   209         if ControllerAxes[j][i] > 20000 then
   244 
   239 
   245 procedure InitKbdKeyTable;
   240 procedure InitKbdKeyTable;
   246 var i, j, k, t: LongInt;
   241 var i, j, k, t: LongInt;
   247     s: string[15];
   242     s: string[15];
   248 begin
   243 begin
   249 KeyNames[1]:= 'mousel';
   244 for i:= 0 to cKeyMaxIndex do
   250 KeyNames[2]:= 'mousem';
       
   251 KeyNames[3]:= 'mouser';
       
   252 KeyNames[4]:= 'wheelup';
       
   253 KeyNames[5]:= 'wheeldown';
       
   254 
       
   255 for i:= 6 to cKeyMaxIndex do
       
   256     begin
   245     begin
   257     s:= shortstring(sdl_getkeyname(i));
   246     s:= shortstring(sdl_getkeyname(i));
   258     //writeln(stdout,IntToStr(i) + ': ' + s);
   247     //WriteToConsole(IntToStr(i) + ': ' + s);
   259     if s = 'unknown key' then KeyNames[i]:= ''
   248     if s = 'unknown key' then KeyNames[i]:= ''
   260     else 
   249     else 
   261         begin
   250         begin
   262         for t:= 1 to Length(s) do
   251         for t:= 1 to Length(s) do
   263             if s[t] = ' ' then
   252             if s[t] = ' ' then
   264                 s[t]:= '_';
   253                 s[t]:= '_';
   265         KeyNames[i]:= s
   254         KeyNames[i]:= s
   266         end;
   255         end;
   267     end;
   256     end;
   268 
   257 
       
   258 KeyNames[SDL_SCANCODE_MOUSEL]:= 'mousel';
       
   259 KeyNames[SDL_SCANCODE_MOUSEM]:= 'mousem';
       
   260 KeyNames[SDL_SCANCODE_MOUSER]:= 'mouser';
       
   261 KeyNames[SDL_SCANCODE_WHEELUP]:= 'wheelup';
       
   262 KeyNames[SDL_SCANCODE_WHEELDOWN]:= 'wheeldown';
   269 //for i:= 0 to cKeyMaxIndex do writeln(stdout,IntToStr(i) + ': ' + KeyNames[i]);
   263 //for i:= 0 to cKeyMaxIndex do writeln(stdout,IntToStr(i) + ': ' + KeyNames[i]);
   270 
   264 
   271 // get the size of keyboard array
   265 k:= SDL_SCANCODE_CONTROLLER;
   272 SDL_GetKeyState(@k);
       
   273 
       
   274 // Controller(s)
   266 // Controller(s)
   275 for j:= 0 to Pred(ControllerNumControllers) do
   267 for j:= 0 to Pred(ControllerNumControllers) do
   276     begin
   268     begin
   277     for i:= 0 to Pred(ControllerNumAxes[j]) do
   269     for i:= 0 to Pred(ControllerNumAxes[j]) do
   278         begin
   270         begin
   293         keynames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
   285         keynames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
   294         inc(k, 1);
   286         inc(k, 1);
   295         end;
   287         end;
   296     end;
   288     end;
   297 
   289 
   298 DefaultBinds[ 27]:= 'quit';
   290 DefaultBinds[SDL_SCANCODE_ESCAPE]:= 'quit';
   299 DefaultBinds[ 96]:= 'history';
   291 DefaultBinds[SDL_SCANCODE_GRAVE]:= 'history';
   300 DefaultBinds[127]:= 'rotmask';
   292 DefaultBinds[127]:= 'rotmask';
   301 
   293 
   302 //numpad
   294 //numpad
   303 //DefaultBinds[265]:= '+volup';
   295 //DefaultBinds[265]:= '+volup';
   304 //DefaultBinds[256]:= '+voldown';
   296 //DefaultBinds[256]:= '+voldown';
   305 
   297 
   306 DefaultBinds[KeyNameToCode('0')]:= '+volup';
   298 DefaultBinds[SDL_SCANCODE_0]:= '+volup';
   307 DefaultBinds[KeyNameToCode('9')]:= '+voldown';
   299 DefaultBinds[SDL_SCANCODE_9]:= '+voldown';
   308 DefaultBinds[KeyNameToCode('c')]:= 'capture';
   300 DefaultBinds[SDL_SCANCODE_C]:= 'capture';
   309 DefaultBinds[KeyNameToCode('h')]:= 'findhh';
   301 DefaultBinds[SDL_SCANCODE_H]:= 'findhh';
   310 DefaultBinds[KeyNameToCode('p')]:= 'pause';
   302 DefaultBinds[SDL_SCANCODE_P]:= 'pause';
   311 DefaultBinds[KeyNameToCode('s')]:= '+speedup';
   303 DefaultBinds[SDL_SCANCODE_S]:= '+speedup';
   312 DefaultBinds[KeyNameToCode('t')]:= 'chat';
   304 DefaultBinds[SDL_SCANCODE_T]:= 'chat';
   313 DefaultBinds[KeyNameToCode('y')]:= 'confirm';
   305 DefaultBinds[SDL_SCANCODE_Y]:= 'confirm';
   314 
   306 
   315 DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
   307 DefaultBinds[SDL_SCANCODE_MOUSEM]:= 'zoomreset';
   316 DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomout';
   308 DefaultBinds[SDL_SCANCODE_WHEELUP]:= 'zoomout';
   317 DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomin';
   309 DefaultBinds[SDL_SCANCODE_WHEELDOWN]:= 'zoomin';
   318 
   310 
   319 DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
   311 DefaultBinds[SDL_SCANCODE_F12]:= 'fullscr';
   320 
   312 
   321 
   313 
   322 DefaultBinds[ 1]:= '/put';
   314 DefaultBinds[SDL_SCANCODE_MOUSEL]:= '/put';
   323 DefaultBinds[ 3]:= 'ammomenu';
   315 DefaultBinds[SDL_SCANCODE_MOUSER]:= 'ammomenu';
   324 DefaultBinds[ 8]:= 'hjump';
   316 DefaultBinds[SDL_SCANCODE_BACKSPACE]:= 'hjump';
   325 DefaultBinds[ 9]:= 'switch';
   317 DefaultBinds[SDL_SCANCODE_TAB]:= 'switch';
   326 DefaultBinds[13]:= 'ljump';
   318 DefaultBinds[SDL_SCANCODE_RETURN]:= 'ljump';
   327 DefaultBinds[32]:= '+attack';
   319 DefaultBinds[SDL_SCANCODE_SPACE]:= '+attack';
   328 {$IFDEF MOBILE}
   320 
   329 DefaultBinds[23]:= '+up';
   321 DefaultBinds[SDL_SCANCODE_UP]:= '+up';
   330 DefaultBinds[24]:= '+down';
   322 DefaultBinds[SDL_SCANCODE_DOWN]:= '+down';
   331 DefaultBinds[25]:= '+left';
   323 DefaultBinds[SDL_SCANCODE_LEFT]:= '+left';
   332 DefaultBinds[26]:= '+right';
   324 DefaultBinds[SDL_SCANCODE_RIGHT]:= '+right';
   333 DefaultBinds[27]:= '+precise';
   325 DefaultBinds[SDL_SCANCODE_LSHIFT]:= '+precise';
   334 DefaultBinds[44]:= 'chat';
   326 
   335 DefaultBinds[55]:= 'pause';
   327 DefaultBinds[SDL_SCANCODE_F1]:= 'slot 1';
   336 {$ELSE}
   328 DefaultBinds[SDL_SCANCODE_F2]:= 'slot 2';
   337 DefaultBinds[KeyNameToCode('up')]:= '+up';
   329 DefaultBinds[SDL_SCANCODE_F3]:= 'slot 3';
   338 DefaultBinds[KeyNameToCode('down')]:= '+down';
   330 DefaultBinds[SDL_SCANCODE_F4]:= 'slot 4';
   339 DefaultBinds[KeyNameToCode('left')]:= '+left';
   331 DefaultBinds[SDL_SCANCODE_F5]:= 'slot 5';
   340 DefaultBinds[KeyNameToCode('right')]:= '+right';
   332 DefaultBinds[SDL_SCANCODE_F6]:= 'slot 6';
   341 DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
   333 DefaultBinds[SDL_SCANCODE_F7]:= 'slot 7';
   342 {$ENDIF}
   334 DefaultBinds[SDL_SCANCODE_F8]:= 'slot 8';
   343 
   335 DefaultBinds[SDL_SCANCODE_F9]:= 'slot 9';
   344 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
   336 DefaultBinds[SDL_SCANCODE_F10]:= 'slot 10';
   345 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
   337 
       
   338 DefaultBinds[SDL_SCANCODE_1]:= 'timer 1';
       
   339 DefaultBinds[SDL_SCANCODE_2]:= 'timer 2';
       
   340 DefaultBinds[SDL_SCANCODE_3]:= 'timer 3';
       
   341 DefaultBinds[SDL_SCANCODE_4]:= 'timer 4';
       
   342 DefaultBinds[SDL_SCANCODE_5]:= 'timer 5';
   346 
   343 
   347 SetDefaultBinds();
   344 SetDefaultBinds();
   348 end;
   345 end;
   349 
   346 
   350 procedure SetBinds(var binds: TBinds);
   347 procedure SetBinds(var binds: TBinds);
   361 begin
   358 begin
   362     CurrentBinds:= DefaultBinds;
   359     CurrentBinds:= DefaultBinds;
   363 end;
   360 end;
   364 
   361 
   365 {$IFDEF MOBILE}
   362 {$IFDEF MOBILE}
   366 procedure setiPhoneBinds;
   363 procedure setTouchWidgetStates;
   367 begin
   364 begin
   368     tkbdn[ 1]:= ord(leftClick);
   365     tkbdn[SDL_SCANCODE_MOUSEL]:= tkbdn[SDL_SCANCODE_MOUSEL] or ord(leftClick);
   369     tkbdn[ 2]:= ord(middleClick);
   366     tkbdn[SDL_SCANCODE_MOUSEM]:= tkbdn[SDL_SCANCODE_MOUSEM] or ord(middleClick);
   370     tkbdn[ 3]:= ord(rightClick);
   367     tkbdn[SDL_SCANCODE_MOUSER]:= tkbdn[SDL_SCANCODE_MOUSER] or ord(rightClick);
   371 
   368 
   372     tkbdn[23]:= ord(upKey);
   369     tkbdn[SDL_SCANCODE_UP]    := tkbdn[SDL_SCANCODE_UP] or ord(upKey);
   373     tkbdn[24]:= ord(downKey);
   370     tkbdn[SDL_SCANCODE_DOWN]  := tkbdn[SDL_SCANCODE_DOWN] or ord(downKey);
   374     tkbdn[25]:= ord(leftKey);
   371     tkbdn[SDL_SCANCODE_LEFT]  := tkbdn[SDL_SCANCODE_LEFT] or ord(leftKey);
   375     tkbdn[26]:= ord(rightKey);
   372     tkbdn[SDL_SCANCODE_RIGHT] := tkbdn[SDL_SCANCODE_RIGHT] or ord(rightKey);
   376     tkbdn[27]:= ord(preciseKey);
   373     tkbdn[SDL_SCANCODE_LSHIFT]:= tkbdn[SDL_SCANCODE_LSHIFT] or ord(preciseKey);
   377 
   374 
   378     tkbdn[ 8]:= ord(backspaceKey);
   375     tkbdn[SDL_SCANCODE_BACKSPACE]:= tkbdn[SDL_SCANCODE_BACKSPACE] or ord(backspaceKey);
   379     tkbdn[ 9]:= ord(tabKey);
   376     tkbdn[SDL_SCANCODE_TAB]:= tkbdn[SDL_SCANCODE_TAB] or ord(tabKey);
   380     tkbdn[13]:= ord(enterKey);
   377     tkbdn[SDL_SCANCODE_RETURN]:= ord(enterKey);
   381     tkbdn[32]:= ord(spaceKey);
   378     tkbdn[SDL_SCANCODE_SPACE]:= ord(spaceKey);
   382 
   379 
   383     tkbdn[44]:= ord(chatAction);
   380     tkbdn[SDL_SCANCODE_T]:= tkbdn[SDL_SCANCODE_T] or ord(chatAction);
   384     tkbdn[55]:= ord(pauseAction);
   381     tkbdn[SDL_SCANCODE_PAUSE]:= ord(pauseAction);
   385 
   382 
   386     // set to false the keys that only need one stoke
   383     // set to false the keys that only need one stoke
   387     leftClick:= false;
   384     leftClick:= false;
   388     middleClick:= false;
   385     middleClick:= false;
   389     rightClick:= false;
   386     rightClick:= false;