hedgewars/uInputHandler.pas
changeset 9466 8b48c27201af
parent 9464 901e363d5837
child 9521 8054d9d775fd
child 9678 aa349a876c93
child 9682 aa2431ed87b2
equal deleted inserted replaced
9464:901e363d5837 9466:8b48c27201af
    38 procedure InitKbdKeyTable;
    38 procedure InitKbdKeyTable;
    39 
    39 
    40 procedure SetBinds(var binds: TBinds);
    40 procedure SetBinds(var binds: TBinds);
    41 procedure SetDefaultBinds;
    41 procedure SetDefaultBinds;
    42 procedure chDefaultBind(var id: shortstring);
    42 procedure chDefaultBind(var id: shortstring);
       
    43 procedure loadBinds(cmd, s: shortstring);
       
    44 procedure addBind(var binds: TBinds; var id: shortstring);
    43 
    45 
    44 procedure ControllerInit;
    46 procedure ControllerInit;
    45 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    47 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
    46 procedure ControllerHatEvent(joy, hat, value: Byte);
    48 procedure ControllerHatEvent(joy, hat, value: Byte);
    47 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    49 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
    48 
    50 
    49 implementation
    51 implementation
    50 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug;
    52 uses uConsole, uCommands, uMisc, uVariables, uConsts, uUtils, uDebug, uPhysFSLayer;
    51 
    53 
    52 const
    54 const
    53     LSHIFT = $0200;
    55     LSHIFT = $0200;
    54     RSHIFT = $0400;
    56     RSHIFT = $0400;
    55     LALT   = $0800;
    57     LALT   = $0800;
    68     ControllerNumButtons: array[0..5] of Integer;
    70     ControllerNumButtons: array[0..5] of Integer;
    69     //ControllerAxes: array[0..5] of array[0..19] of Integer;
    71     //ControllerAxes: array[0..5] of array[0..19] of Integer;
    70     //ControllerBalls: array[0..5] of array[0..19] of array[0..1] of Integer;
    72     //ControllerBalls: array[0..5] of array[0..19] of array[0..1] of Integer;
    71     //ControllerHats: array[0..5] of array[0..19] of Byte;
    73     //ControllerHats: array[0..5] of array[0..19] of Byte;
    72     //ControllerButtons: array[0..5] of array[0..19] of Byte;
    74     //ControllerButtons: array[0..5] of array[0..19] of Byte;
    73     usingDBinds: boolean;
       
    74 
    75 
    75 function  KeyNameToCode(name: shortstring): LongInt; inline;
    76 function  KeyNameToCode(name: shortstring): LongInt; inline;
    76 begin
    77 begin
    77     KeyNameToCode:= KeyNameToCode(name, '');
    78     KeyNameToCode:= KeyNameToCode(name, '');
    78 end;
    79 end;
   312 DefaultBinds[KeyNameToCode('j0a0d')]:= '+right';
   313 DefaultBinds[KeyNameToCode('j0a0d')]:= '+right';
   313 DefaultBinds[KeyNameToCode('j0a1u')]:= '+up';
   314 DefaultBinds[KeyNameToCode('j0a1u')]:= '+up';
   314 DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
   315 DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
   315 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
   316 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
   316 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
   317 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
       
   318 
       
   319 loadBinds('dbind', cPathz[ptData] + '/settings.ini');
   317 end;
   320 end;
   318 
   321 
   319 procedure SetBinds(var binds: TBinds);
   322 procedure SetBinds(var binds: TBinds);
   320 {$IFNDEF MOBILE}
   323 {$IFNDEF MOBILE}
   321 var
   324 var
   445     SDL_GetKeyState(@k);
   448     SDL_GetKeyState(@k);
   446     k:= k + joy * (ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + ControllerNumButtons[joy]*2);
   449     k:= k + joy * (ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + ControllerNumButtons[joy]*2);
   447     ProcessKey(k +  ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + button, pressed);
   450     ProcessKey(k +  ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + button, pressed);
   448 end;
   451 end;
   449 
   452 
   450 // Bind that isn't a team bind, but overrides defaultbinds.
   453 procedure loadBinds(cmd, s: shortstring);
   451 // When first called, DefaultBinds is cleared, because we assume we are getting a full list of dbinds.
   454 var i: LongInt;
   452 procedure chDefaultBind(var id: shortstring);
   455     f: PFSFile;
       
   456     p, l: shortstring;
       
   457     b: byte;
       
   458 begin
       
   459     AddFileLog('[BINDS] Loading binds from: ' + s);
       
   460 
       
   461     l:= '';
       
   462     if pfsExists(s) then
       
   463         begin
       
   464         f:= pfsOpenRead(s);
       
   465         while (not pfsEOF(f)) and (l <> '[Binds]') do
       
   466             pfsReadLn(f, l);
       
   467 
       
   468         while (not pfsEOF(f)) and (l <> '') do
       
   469             begin
       
   470             pfsReadLn(f, l);
       
   471 
       
   472             p:= '';
       
   473             i:= 1;
       
   474             while (i <= length(l)) and (l[i] <> '=') do
       
   475                 begin
       
   476                 if l[i] <> '%' then
       
   477                     begin
       
   478                     p:= p + l[i];
       
   479                     inc(i)
       
   480                     end else
       
   481                     begin
       
   482                     l[i]:= '$';
       
   483                     val(copy(l, i, 3), b);
       
   484                     p:= p + char(b);
       
   485                     inc(i, 3)
       
   486                     end;
       
   487                 end;
       
   488 
       
   489             if i < length(l) then
       
   490                 begin
       
   491                 l:= copy(l, i + 1, length(l) - i);
       
   492                 if l <> 'default' then
       
   493                     begin
       
   494                     p:= cmd + ' ' + l + ' ' + p;
       
   495                     ParseCommand(p, true)
       
   496                     end
       
   497                 end
       
   498             end;
       
   499 
       
   500         pfsClose(f)
       
   501         end 
       
   502         else
       
   503             AddFileLog('[BINDS] file not found');
       
   504 end;
       
   505 
       
   506 
       
   507 procedure addBind(var binds: TBinds; var id: shortstring);
   453 var KeyName, Modifier, tmp: shortstring;
   508 var KeyName, Modifier, tmp: shortstring;
   454     b: LongInt;
   509     i, b: LongInt;
   455 begin
   510 begin
   456 KeyName:= '';
   511 KeyName:= '';
   457 Modifier:= '';
   512 Modifier:= '';
   458 
   513 
   459 if (not usingDBinds) then
   514 if(Pos('mod:', id) <> 0)then
   460     begin
       
   461     usingDBinds:= true;
       
   462     FillByte(DefaultBinds, SizeOf(DefaultBinds), 0);
       
   463     end;
       
   464 
       
   465 if (Pos('mod:', id) <> 0) then
       
   466     begin
   515     begin
   467     tmp:= '';
   516     tmp:= '';
   468     SplitBySpace(id, tmp);
   517     SplitBySpace(id, tmp);
   469     Modifier:= id;
   518     Modifier:= id;
   470     id:= tmp;
   519     id:= tmp;
   477     Delete(KeyName, byte(KeyName[0]), 1);
   526     Delete(KeyName, byte(KeyName[0]), 1);
   478 b:= KeyNameToCode(id, Modifier);
   527 b:= KeyNameToCode(id, Modifier);
   479 if b = 0 then
   528 if b = 0 then
   480     OutError(errmsgUnknownVariable + ' "' + id + '"', false)
   529     OutError(errmsgUnknownVariable + ' "' + id + '"', false)
   481 else
   530 else
   482     DefaultBinds[b]:= KeyName;
   531     begin 
       
   532     // add bind: first check if this cmd is already bound, and remove old bind
       
   533     i:= cKbdMaxIndex;
       
   534     repeat
       
   535         dec(i)
       
   536     until (i < 0) or (binds[i] = KeyName);
       
   537     if (i >= 0) then
       
   538         binds[i]:= '';
       
   539 
       
   540     binds[b]:= KeyName;
       
   541     end
       
   542 end;
       
   543 
       
   544 // Bind that isn't a team bind, but overrides defaultbinds.
       
   545 procedure chDefaultBind(var id: shortstring);
       
   546 begin
       
   547     addBind(DefaultBinds, id)
   483 end;
   548 end;
   484 
   549 
   485 procedure initModule;
   550 procedure initModule;
   486 begin
   551 begin
   487     usingDBinds:= false;
       
   488     RegisterVariable('dbind', @chDefaultBind, true );
   552     RegisterVariable('dbind', @chDefaultBind, true );
   489 end;
   553 end;
   490 
   554 
   491 procedure freeModule;
   555 procedure freeModule;
   492 var j: LongInt;
   556 var j: LongInt;