hedgewars/uKeys.pas
branchexperimental3D
changeset 4812 f924be23ffb4
parent 4531 4ea193b0e378
child 4744 ecc2c757d0df
equal deleted inserted replaced
4347:0ddb100fea61 4812:f924be23ffb4
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uKeys;
    21 unit uKeys;
    22 interface
    22 interface
    23 uses uConsts, SDLh;
    23 uses SDLh, uTypes;
    24 
       
    25 type TBinds = array[0..cKeyMaxIndex] of shortstring;
       
    26 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
       
    27 
    24 
    28 procedure initModule;
    25 procedure initModule;
    29 procedure freeModule;
    26 procedure freeModule;
    30 
    27 
    31 function  KeyNameToCode(name: shortstring): word;
    28 function  KeyNameToCode(name: shortstring): word;
    83 {$IFDEF IPHONEOS}
    80 {$IFDEF IPHONEOS}
    84 procedure setiPhoneBinds;
    81 procedure setiPhoneBinds;
    85 {$ENDIF}
    82 {$ENDIF}
    86 {$ENDIF}
    83 {$ENDIF}
    87 implementation
    84 implementation
    88 uses uTeams, uConsole, uMisc;
    85 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    89 //const KeyNumber = 1024;
       
    90 
    86 
    91 var tkbd, tkbdn: TKeyboardState;
    87 var tkbd, tkbdn: TKeyboardState;
    92     KeyNames: array [0..cKeyMaxIndex] of string[15];
    88     KeyNames: array [0..cKeyMaxIndex] of string[15];
    93 
    89 
    94 function KeyNameToCode(name: shortstring): word;
    90 function KeyNameToCode(name: shortstring): word;
   173 // now process strokes
   169 // now process strokes
   174 for i:= 0 to cKeyMaxIndex do
   170 for i:= 0 to cKeyMaxIndex do
   175 if CurrentBinds[i][0] <> #0 then
   171 if CurrentBinds[i][0] <> #0 then
   176     begin
   172     begin
   177     if (i > 3) and (tkbdn[i] <> 0) and not ((CurrentBinds[i] = 'put') or (CurrentBinds[i] = 'ammomenu') or (CurrentBinds[i] = '+cur_u') or (CurrentBinds[i] = '+cur_d') or (CurrentBinds[i] = '+cur_l') or (CurrentBinds[i] = '+cur_r')) then hideAmmoMenu:= true;
   173     if (i > 3) and (tkbdn[i] <> 0) and not ((CurrentBinds[i] = 'put') or (CurrentBinds[i] = 'ammomenu') or (CurrentBinds[i] = '+cur_u') or (CurrentBinds[i] = '+cur_d') or (CurrentBinds[i] = '+cur_l') or (CurrentBinds[i] = '+cur_r')) then hideAmmoMenu:= true;
   178     if (tkbd[i] = 0) and (tkbdn[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted)
   174     if (tkbd[i] = 0) and (tkbdn[i] <> 0) then
       
   175          begin
       
   176          ParseCommand(CurrentBinds[i], Trusted);
       
   177          if (CurrentTeam <> nil) and not CurrentTeam^.ExtDriven and (ReadyTimeLeft > 1) then ParseCommand('gencmd R', true)
       
   178          end
   179     else if (CurrentBinds[i][1] = '+')
   179     else if (CurrentBinds[i][1] = '+')
   180             and (tkbdn[i] = 0)
   180             and (tkbdn[i] = 0)
   181             and (tkbd[i] <> 0) then
   181             and (tkbd[i] <> 0) then
   182             begin
   182             begin
   183             s:= CurrentBinds[i];
   183             s:= CurrentBinds[i];
   184             s[1]:= '-';
   184             s[1]:= '-';
   185             ParseCommand(s, Trusted)
   185             ParseCommand(s, Trusted);
       
   186             if (CurrentTeam <> nil) and not CurrentTeam^.ExtDriven and (ReadyTimeLeft > 1) then ParseCommand('gencmd R', true)
   186             end;
   187             end;
   187     tkbd[i]:= tkbdn[i]
   188     tkbd[i]:= tkbdn[i]
   188     end
   189     end
   189 end;
   190 end;
   190 
   191 
   194 begin
   195 begin
   195 
   196 
   196 k:= SDL_GetMouseState(nil, nil);
   197 k:= SDL_GetMouseState(nil, nil);
   197 {$IFNDEF IPHONEOS}pkbd:={$ENDIF}SDL_GetKeyState(@j);
   198 {$IFNDEF IPHONEOS}pkbd:={$ENDIF}SDL_GetKeyState(@j);
   198 
   199 
   199 TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(j) + ')', true);
   200 TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + IntToStr(j) + ')', true);
   200 
   201 
   201 {$IFNDEF IPHONEOS}
   202 {$IFNDEF IPHONEOS}
   202 for i:= 1 to pred(j) do
   203 for i:= 1 to pred(j) do
   203     tkbdn[i]:= pkbd^[i];
   204     tkbdn[i]:= pkbd^[i];
   204 {$ENDIF}
   205 {$ENDIF}
   263 KeyNames[5]:= 'wheeldown';
   264 KeyNames[5]:= 'wheeldown';
   264 
   265 
   265 for i:= 6 to cKeyMaxIndex do
   266 for i:= 6 to cKeyMaxIndex do
   266     begin
   267     begin
   267     s:= shortstring(sdl_getkeyname(i));
   268     s:= shortstring(sdl_getkeyname(i));
   268     //writeln(stdout,inttostr(i) + ': ' + s);
   269     //writeln(stdout,IntToStr(i) + ': ' + s);
   269     if s = 'unknown key' then KeyNames[i]:= ''
   270     if s = 'unknown key' then KeyNames[i]:= ''
   270     else 
   271     else 
   271         begin
   272         begin
   272         for t:= 1 to Length(s) do
   273         for t:= 1 to Length(s) do
   273             if s[t] = ' ' then s[t]:= '_';
   274             if s[t] = ' ' then s[t]:= '_';
   274         KeyNames[i]:= s
   275         KeyNames[i]:= s
   275         end;
   276         end;
   276     end;
   277     end;
   277 
   278 
   278 //for i:= 0 to cKeyMaxIndex do writeln(stdout,inttostr(i) + ': ' + KeyNames[i]);
   279 //for i:= 0 to cKeyMaxIndex do writeln(stdout,IntToStr(i) + ': ' + KeyNames[i]);
   279 
   280 
   280 // get the size of keyboard array
   281 // get the size of keyboard array
   281 SDL_GetKeyState(@k);
   282 SDL_GetKeyState(@k);
   282 
   283 
   283 // Controller(s)
   284 // Controller(s)
   284 for j:= 0 to Pred(ControllerNumControllers) do
   285 for j:= 0 to Pred(ControllerNumControllers) do
   285     begin
   286     begin
   286     for i:= 0 to Pred(ControllerNumAxes[j]) do
   287     for i:= 0 to Pred(ControllerNumAxes[j]) do
   287         begin
   288         begin
   288         keynames[k + 0]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'u';
   289         keynames[k + 0]:= 'j' + IntToStr(j) + 'a' + IntToStr(i) + 'u';
   289         keynames[k + 1]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'd';
   290         keynames[k + 1]:= 'j' + IntToStr(j) + 'a' + IntToStr(i) + 'd';
   290         inc(k, 2);
   291         inc(k, 2);
   291         end;
   292         end;
   292     for i:= 0 to Pred(ControllerNumHats[j]) do
   293     for i:= 0 to Pred(ControllerNumHats[j]) do
   293         begin
   294         begin
   294         keynames[k + 0]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'u';
   295         keynames[k + 0]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'u';
   295         keynames[k + 1]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'r';
   296         keynames[k + 1]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'r';
   296         keynames[k + 2]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'd';
   297         keynames[k + 2]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'd';
   297         keynames[k + 3]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'l';
   298         keynames[k + 3]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'l';
   298         inc(k, 4);
   299         inc(k, 4);
   299         end;
   300         end;
   300     for i:= 0 to Pred(ControllerNumButtons[j]) do
   301     for i:= 0 to Pred(ControllerNumButtons[j]) do
   301         begin
   302         begin
   302         keynames[k]:= 'j' + inttostr(j) + 'b' + inttostr(i);
   303         keynames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
   303         inc(k, 1);
   304         inc(k, 1);
   304         end;
   305         end;
   305     end;
   306     end;
   306 
   307 
   307 DefaultBinds[ 27]:= 'quit';
   308 DefaultBinds[ 27]:= 'quit';
   348 DefaultBinds[KeyNameToCode('left')]:= '+left';
   349 DefaultBinds[KeyNameToCode('left')]:= '+left';
   349 DefaultBinds[KeyNameToCode('right')]:= '+right';
   350 DefaultBinds[KeyNameToCode('right')]:= '+right';
   350 DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
   351 DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
   351 {$ENDIF}
   352 {$ENDIF}
   352 
   353 
   353 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+inttostr(i))]:= 'slot '+inttostr(i);
   354 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
   354 
   355 
   355 SetDefaultBinds();
   356 SetDefaultBinds();
   356 end;
   357 end;
   357 
   358 
   358 procedure SetBinds(var binds: TBinds);
   359 procedure SetBinds(var binds: TBinds);
   423 
   424 
   424 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
   425 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
   425 ControllerNumControllers:= SDL_NumJoysticks();
   426 ControllerNumControllers:= SDL_NumJoysticks();
   426 if ControllerNumControllers > 6 then ControllerNumControllers:= 6;
   427 if ControllerNumControllers > 6 then ControllerNumControllers:= 6;
   427 
   428 
   428 WriteLnToConsole('Number of game controllers: ' + inttostr(ControllerNumControllers));
   429 WriteLnToConsole('Number of game controllers: ' + IntToStr(ControllerNumControllers));
   429 
   430 
   430 if ControllerNumControllers > 0 then
   431 if ControllerNumControllers > 0 then
   431     begin
   432     begin
   432     for j:= 0 to pred(ControllerNumControllers) do
   433     for j:= 0 to pred(ControllerNumControllers) do
   433         begin
   434         begin
   439             begin
   440             begin
   440             ControllerNumAxes[j]:= SDL_JoystickNumAxes(Controller[j]);
   441             ControllerNumAxes[j]:= SDL_JoystickNumAxes(Controller[j]);
   441             //ControllerNumBalls[j]:= SDL_JoystickNumBalls(Controller[j]);
   442             //ControllerNumBalls[j]:= SDL_JoystickNumBalls(Controller[j]);
   442             ControllerNumHats[j]:= SDL_JoystickNumHats(Controller[j]);
   443             ControllerNumHats[j]:= SDL_JoystickNumHats(Controller[j]);
   443             ControllerNumButtons[j]:= SDL_JoystickNumButtons(Controller[j]);
   444             ControllerNumButtons[j]:= SDL_JoystickNumButtons(Controller[j]);
   444             WriteLnToConsole('* Number of axes: ' + inttostr(ControllerNumAxes[j]));
   445             WriteLnToConsole('* Number of axes: ' + IntToStr(ControllerNumAxes[j]));
   445             //WriteLnToConsole('* Number of balls: ' + inttostr(ControllerNumBalls[j]));
   446             //WriteLnToConsole('* Number of balls: ' + IntToStr(ControllerNumBalls[j]));
   446             WriteLnToConsole('* Number of hats: ' + inttostr(ControllerNumHats[j]));
   447             WriteLnToConsole('* Number of hats: ' + IntToStr(ControllerNumHats[j]));
   447             WriteLnToConsole('* Number of buttons: ' + inttostr(ControllerNumButtons[j]));
   448             WriteLnToConsole('* Number of buttons: ' + IntToStr(ControllerNumButtons[j]));
   448             ControllerEnabled:= 1;
   449             ControllerEnabled:= 1;
   449 
   450 
   450             if ControllerNumAxes[j] > 20 then ControllerNumAxes[j]:= 20;
   451             if ControllerNumAxes[j] > 20 then ControllerNumAxes[j]:= 20;
   451             //if ControllerNumBalls[j] > 20 then ControllerNumBalls[j]:= 20;
   452             //if ControllerNumBalls[j] > 20 then ControllerNumBalls[j]:= 20;
   452             if ControllerNumHats[j] > 20 then ControllerNumHats[j]:= 20;
   453             if ControllerNumHats[j] > 20 then ControllerNumHats[j]:= 20;