hedgewars/uInputHandler.pas
branchwebgl
changeset 9521 8054d9d775fd
parent 9127 e350500c4edb
parent 9466 8b48c27201af
child 9950 2759212a27de
equal deleted inserted replaced
9282:92af50454cf2 9521:8054d9d775fd
    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;
   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);
   317 
   318 
   318 SetDefaultBinds();
   319 loadBinds('dbind', cPathz[ptData] + '/settings.ini');
   319 end;
   320 end;
   320 
   321 
   321 procedure SetBinds(var binds: TBinds);
   322 procedure SetBinds(var binds: TBinds);
   322 {$IFNDEF MOBILE}
   323 {$IFNDEF MOBILE}
   323 var
   324 var
   447     SDL_GetKeyState(@k);
   448     SDL_GetKeyState(@k);
   448     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);
   449     ProcessKey(k +  ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + button, pressed);
   450     ProcessKey(k +  ControllerNumAxes[joy]*2 + ControllerNumHats[joy]*4 + button, pressed);
   450 end;
   451 end;
   451 
   452 
   452 // Bind that isn't a team bind, but overrides defaultbinds.
   453 procedure loadBinds(cmd, s: shortstring);
   453 // When first called, DefaultBinds is cleared, because we assume we are getting a full list of dbinds.
   454 var i: LongInt;
   454 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);
   455 var KeyName, Modifier, tmp: shortstring;
   508 var KeyName, Modifier, tmp: shortstring;
   456     b: LongInt;
   509     i, b: LongInt;
   457 begin
   510 begin
   458 KeyName:= '';
   511 KeyName:= '';
   459 Modifier:= '';
   512 Modifier:= '';
   460 
   513 
   461 if (not usingDBinds) then
   514 if(Pos('mod:', id) <> 0)then
   462     begin
       
   463     usingDBinds:= true;
       
   464     FillChar(DefaultBinds, SizeOf(DefaultBinds), 0);
       
   465     end;
       
   466 
       
   467 if (Pos('mod:', id) <> 0) then
       
   468     begin
   515     begin
   469     tmp:= '';
   516     tmp:= '';
   470     SplitBySpace(id, tmp);
   517     SplitBySpace(id, tmp);
   471     Modifier:= id;
   518     Modifier:= id;
   472     id:= tmp;
   519     id:= tmp;
   479     Delete(KeyName, byte(KeyName[0]), 1);
   526     Delete(KeyName, byte(KeyName[0]), 1);
   480 b:= KeyNameToCode(id, Modifier);
   527 b:= KeyNameToCode(id, Modifier);
   481 if b = 0 then
   528 if b = 0 then
   482     OutError(errmsgUnknownVariable + ' "' + id + '"', false)
   529     OutError(errmsgUnknownVariable + ' "' + id + '"', false)
   483 else
   530 else
   484     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)
   485 end;
   548 end;
   486 
   549 
   487 procedure initModule;
   550 procedure initModule;
   488 begin
   551 begin
   489     usingDBinds:= false;
       
   490     RegisterVariable('dbind', @chDefaultBind, true );
   552     RegisterVariable('dbind', @chDefaultBind, true );
   491 end;
   553 end;
   492 
   554 
   493 procedure freeModule;
   555 procedure freeModule;
   494 var j: LongInt;
   556 var j: LongInt;