hedgewars/uInputHandler.pas
changeset 7110 c91d33837b0d
parent 7106 aacb90365d3d
child 7117 7df6386f7090
equal deleted inserted replaced
7020:846cea23aea2 7110:c91d33837b0d
    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): word;
    29 procedure ProcessKbd;
       
    30 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
    29 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
    31 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
    30 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
    32 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    31 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    33 
    32 
    34 procedure ResetKbd;
    33 procedure ResetKbd;
    37 
    36 
    38 procedure SetBinds(var binds: TBinds);
    37 procedure SetBinds(var binds: TBinds);
    39 procedure SetDefaultBinds;
    38 procedure SetDefaultBinds;
    40 
    39 
    41 procedure ControllerInit;
    40 procedure ControllerInit;
    42 procedure ControllerClose;
       
    43 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    41 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    44 procedure ControllerHatEvent(joy, hat, value: Byte);
    42 procedure ControllerHatEvent(joy, hat, value: Byte);
    45 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    43 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    46 
    44 
    47 implementation
    45 implementation
    58     name:= LowerCase(name);
    56     name:= LowerCase(name);
    59     code:= cKeyMaxIndex;
    57     code:= cKeyMaxIndex;
    60     while (code > 0) and (KeyNames[code] <> name) do dec(code);
    58     while (code > 0) and (KeyNames[code] <> name) do dec(code);
    61     KeyNameToCode:= code;
    59     KeyNameToCode:= code;
    62 end;
    60 end;
    63 
       
    64 procedure ProcessKbd;
       
    65 //var  i, j, k: LongInt;
       
    66 begin
       
    67 
       
    68 // move cursor/camera
       
    69 // TODO: Scale on screen dimensions and/or axis value (game controller)?
       
    70 //TODO what is this for?
       
    71 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
       
    72 
       
    73 
       
    74 (* 
       
    75 TODO reimplement
       
    76 $IFNDEF MOBILE
       
    77 // Controller(s)
       
    78 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
       
    79 for j:= 0 to Pred(ControllerNumControllers) do
       
    80     begin
       
    81     for i:= 0 to Pred(ControllerNumAxes[j]) do
       
    82         begin
       
    83         if ControllerAxes[j][i] > 20000 then
       
    84             tkbdn[k + 0]:= 1
       
    85         else
       
    86             tkbdn[k + 0]:= 0;
       
    87         if ControllerAxes[j][i] < -20000 then
       
    88             tkbdn[k + 1]:= 1
       
    89         else
       
    90             tkbdn[k + 1]:= 0;
       
    91         inc(k, 2);
       
    92         end;
       
    93     for i:= 0 to Pred(ControllerNumHats[j]) do
       
    94         begin
       
    95         tkbdn[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP;
       
    96         tkbdn[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT;
       
    97         tkbdn[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN;
       
    98         tkbdn[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT;
       
    99         inc(k, 4);
       
   100         end;
       
   101     for i:= 0 to Pred(ControllerNumButtons[j]) do
       
   102         begin
       
   103         tkbdn[k]:= ControllerButtons[j][i];
       
   104         inc(k, 1);
       
   105         end;
       
   106     end;
       
   107 $ENDIF *)
       
   108 
       
   109 end;
       
   110 
       
   111 
    61 
   112 procedure ProcessKey(code: LongInt; KeyDown: boolean);
    62 procedure ProcessKey(code: LongInt; KeyDown: boolean);
   113 var
    63 var
   114     Trusted: boolean;
    64     Trusted: boolean;
   115     s      : string;
    65     s      : string;
   174         ProcessKey(KeyNameToCode('wheelup'), ButtonDown);
   124         ProcessKey(KeyNameToCode('wheelup'), ButtonDown);
   175     end;
   125     end;
   176 end;
   126 end;
   177 
   127 
   178 procedure ResetKbd;
   128 procedure ResetKbd;
   179 var j, k, t: LongInt;
   129 var j, t: LongInt;
   180     i: LongInt;
   130     i: LongInt;
   181     pkbd: PByteArray;
   131     pkbd: PByteArray;
   182 begin
   132 begin
   183 
   133 
   184 k:= SDL_GetMouseState(nil, nil);
   134 //k:= SDL_GetMouseState(nil, nil);
   185 pkbd:=SDL_GetKeyState(@j);
   135 pkbd:=SDL_GetKeyState(@j);
   186 
   136 
   187 //TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + IntToStr(j) + ')', true);
   137 //TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + IntToStr(j) + ')', true);
   188 
   138 
   189 for i:= 1 to pred(j) do
   139 for i:= 1 to pred(j) do
   190     tkbdn[i]:= pkbd^[i];
   140     tkbdn[i]:= pkbd^[i];
   191 
   141 
   192 {$IFNDEF MOBILE}
   142 (*
       
   143 // TODO: reimplement
   193 // Controller(s)
   144 // Controller(s)
   194 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   145 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   195 for j:= 0 to Pred(ControllerNumControllers) do
   146 for j:= 0 to Pred(ControllerNumControllers) do
   196     begin
   147     begin
   197     for i:= 0 to Pred(ControllerNumAxes[j]) do
   148     for i:= 0 to Pred(ControllerNumAxes[j]) do
   218         begin
   169         begin
   219         tkbdn[k]:= ControllerButtons[j][i];
   170         tkbdn[k]:= ControllerButtons[j][i];
   220         inc(k, 1);
   171         inc(k, 1);
   221         end;
   172         end;
   222     end;
   173     end;
   223 {$ENDIF}
   174 *)
   224 
   175 
   225 // what is this final loop for?
   176 // what is this final loop for?
   226 for t:= 0 to cKeyMaxIndex do
   177 for t:= 0 to cKeyMaxIndex do
   227     tkbd[t]:= tkbdn[t]
   178     tkbd[t]:= tkbdn[t]
   228 end;
   179 end;
   238 KeyNames[4]:= 'wheelup';
   189 KeyNames[4]:= 'wheelup';
   239 KeyNames[5]:= 'wheeldown';
   190 KeyNames[5]:= 'wheeldown';
   240 
   191 
   241 for i:= 6 to cKeyMaxIndex do
   192 for i:= 6 to cKeyMaxIndex do
   242     begin
   193     begin
   243 {$IFDEF SDL13}
       
   244     s:= shortstring(SDL_GetScancodeName(i));
       
   245 {$ELSE}
       
   246     s:= shortstring(sdl_getkeyname(i));
   194     s:= shortstring(sdl_getkeyname(i));
   247 {$ENDIF}
   195     //WriteLnToConsole(IntToStr(i) + ': ' + s + ' ' + IntToStr(cKeyMaxIndex));
   248     WriteToConsole(IntToStr(i) + ': ' + s + ' ' + IntToStr(cKeyMaxIndex));
       
   249     if s = 'unknown key' then KeyNames[i]:= ''
   196     if s = 'unknown key' then KeyNames[i]:= ''
   250     else 
   197     else 
   251         begin
   198         begin
   252         for t:= 1 to Length(s) do
   199         for t:= 1 to Length(s) do
   253             if s[t] = ' ' then
   200             if s[t] = ' ' then
   254                 s[t]:= '_';
   201                 s[t]:= '_';
   255         KeyNames[i]:= LowerCase(s)
   202         KeyNames[i]:= LowerCase(s)
   256         end;
   203         end;
   257     end;
   204     end;
   258 
   205 
   259 quitKeyCode:= KeyNameToCode('q');
   206 quitKeyCode:= KeyNameToCode(_S'q');
   260 
   207 
   261 // get the size of keyboard array
   208 // get the size of keyboard array
   262 SDL_GetKeyState(@k);
   209 SDL_GetKeyState(@k);
   263 
   210 
   264 // Controller(s)
   211 // Controller(s)
   291 
   238 
   292 //numpad
   239 //numpad
   293 //DefaultBinds[265]:= '+volup';
   240 //DefaultBinds[265]:= '+volup';
   294 //DefaultBinds[256]:= '+voldown';
   241 //DefaultBinds[256]:= '+voldown';
   295 
   242 
   296 DefaultBinds[KeyNameToCode('0')]:= '+volup';
   243 DefaultBinds[KeyNameToCode(_S'0')]:= '+volup';
   297 DefaultBinds[KeyNameToCode('9')]:= '+voldown';
   244 DefaultBinds[KeyNameToCode(_S'9')]:= '+voldown';
   298 DefaultBinds[KeyNameToCode('c')]:= 'capture';
   245 DefaultBinds[KeyNameToCode(_S'c')]:= 'capture';
   299 DefaultBinds[KeyNameToCode('h')]:= 'findhh';
   246 DefaultBinds[KeyNameToCode(_S'h')]:= 'findhh';
   300 DefaultBinds[KeyNameToCode('p')]:= 'pause';
   247 DefaultBinds[KeyNameToCode(_S'p')]:= 'pause';
   301 DefaultBinds[KeyNameToCode('s')]:= '+speedup';
   248 DefaultBinds[KeyNameToCode(_S's')]:= '+speedup';
   302 DefaultBinds[KeyNameToCode('t')]:= 'chat';
   249 DefaultBinds[KeyNameToCode(_S't')]:= 'chat';
   303 DefaultBinds[KeyNameToCode('y')]:= 'confirm';
   250 DefaultBinds[KeyNameToCode(_S'y')]:= 'confirm';
   304 
   251 
   305 DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
   252 DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
   306 DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomout';
   253 DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomin';
   307 DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomin';
   254 DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomout';
   308 
   255 
   309 DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
   256 DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
   310 
   257 
   311 
   258 
   312 DefaultBinds[KeyNameToCode('mousel')]:= '/put';
   259 DefaultBinds[KeyNameToCode('mousel')]:= '/put';
   319 DefaultBinds[KeyNameToCode('down')]:= '+down';
   266 DefaultBinds[KeyNameToCode('down')]:= '+down';
   320 DefaultBinds[KeyNameToCode('left')]:= '+left';
   267 DefaultBinds[KeyNameToCode('left')]:= '+left';
   321 DefaultBinds[KeyNameToCode('right')]:= '+right';
   268 DefaultBinds[KeyNameToCode('right')]:= '+right';
   322 DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
   269 DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
   323 
   270 
       
   271 
       
   272 DefaultBinds[KeyNameToCode('j0a0u')]:= '+left';
       
   273 DefaultBinds[KeyNameToCode('j0a0d')]:= '+right';
       
   274 DefaultBinds[KeyNameToCode('j0a1u')]:= '+up';
       
   275 DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
   324 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
   276 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
   325 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
   277 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
   326 
   278 
   327 SetDefaultBinds();
   279 SetDefaultBinds();
   328 end;
   280 end;
   354 
   306 
   355 procedure ControllerInit;
   307 procedure ControllerInit;
   356 var i, j: Integer;
   308 var i, j: Integer;
   357 begin
   309 begin
   358 ControllerEnabled:= 0;
   310 ControllerEnabled:= 0;
   359 {$IFDEF MOBILE}
   311 {$IFDEF IPHONE}
   360 exit; // joystick subsystem disabled on iPhone
   312 exit; // joystick subsystem disabled on iPhone
   361 {$ENDIF}
   313 {$ENDIF}
   362 
   314 
   363 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
   315 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
   364 ControllerNumControllers:= SDL_NumJoysticks();
   316 ControllerNumControllers:= SDL_NumJoysticks();
   417     end
   369     end
   418 else
   370 else
   419     WriteLnToConsole('Not using any game controller');
   371     WriteLnToConsole('Not using any game controller');
   420 end;
   372 end;
   421 
   373 
   422 procedure ControllerClose;
   374 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
   423 var j: Integer;
   375 var
   424 begin
   376     k: LongInt;
       
   377 begin
       
   378     SDL_GetKeyState(@k);
       
   379     k:= k + joy * (ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + ControllerNumButtons[joy]*2);
       
   380     ProcessKey(k +  axis*2, value > 20000);
       
   381     ProcessKey(k + (axis*2)+1, value < -20000);
       
   382 end;
       
   383 
       
   384 procedure ControllerHatEvent(joy, hat, value: Byte);
       
   385 var
       
   386     k: LongInt;
       
   387 begin
       
   388     SDL_GetKeyState(@k);
       
   389     k:= k + joy * (ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + ControllerNumButtons[joy]*2);
       
   390     ProcessKey(k +  ControllerNumAxes[joy]*2 + hat*4 + 0, (value and SDL_HAT_UP)   <> 0);
       
   391     ProcessKey(k +  ControllerNumAxes[joy]*2 + hat*4 + 1, (value and SDL_HAT_RIGHT)<> 0);
       
   392     ProcessKey(k +  ControllerNumAxes[joy]*2 + hat*4 + 2, (value and SDL_HAT_DOWN) <> 0);
       
   393     ProcessKey(k +  ControllerNumAxes[joy]*2 + hat*4 + 3, (value and SDL_HAT_LEFT) <> 0);
       
   394 end;
       
   395 
       
   396 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
       
   397 var
       
   398     k: LongInt;
       
   399 begin
       
   400     SDL_GetKeyState(@k);
       
   401     k:= k + joy * (ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + ControllerNumButtons[joy]*2);
       
   402     ProcessKey(k +  ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + button, pressed);
       
   403 end;
       
   404 
       
   405 procedure initModule;
       
   406 begin
       
   407     wheelUp:= false;
       
   408     wheelDown:= false;
       
   409 end;
       
   410 
       
   411 procedure freeModule;
       
   412 var j: LongInt;
       
   413 begin
       
   414     // close gamepad controllers
   425     if ControllerEnabled > 0 then
   415     if ControllerEnabled > 0 then
   426         for j:= 0 to pred(ControllerNumControllers) do
   416         for j:= 0 to pred(ControllerNumControllers) do
   427             SDL_JoystickClose(Controller[j]);
   417             SDL_JoystickClose(Controller[j]);
   428 end;
   418 end;
   429 
   419 
   430 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
       
   431 begin
       
   432     ControllerAxes[joy][axis]:= value;
       
   433 end;
       
   434 
       
   435 procedure ControllerHatEvent(joy, hat, value: Byte);
       
   436 begin
       
   437     ControllerHats[joy][hat]:= value;
       
   438 end;
       
   439 
       
   440 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
       
   441 begin
       
   442     if pressed then
       
   443         ControllerButtons[joy][button]:= 1
       
   444     else
       
   445         ControllerButtons[joy][button]:= 0;
       
   446 end;
       
   447 
       
   448 procedure initModule;
       
   449 begin
       
   450     wheelUp:= false;
       
   451     wheelDown:= false;
       
   452 end;
       
   453 
       
   454 procedure freeModule;
       
   455 begin
       
   456 
       
   457 end;
       
   458 
       
   459 end.
   420 end.