hedgewars/uInputHandler.pas
branchsdl2transition
changeset 9691 a312103af4b1
parent 9682 aa2431ed87b2
child 9694 e8d0fe885169
equal deleted inserted replaced
9688:98024c99e58d 9691:a312103af4b1
   183             ParseCommand('gencmd R', true)
   183             ParseCommand('gencmd R', true)
   184         end;
   184         end;
   185     end
   185     end
   186 end;
   186 end;
   187 
   187 
       
   188 {$IFDEF SDL2}
       
   189 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
       
   190 var code: LongInt;
       
   191 begin
       
   192     code:= event.keysym.scancode;
       
   193 
       
   194     ProcessKey(code, event.type_ = SDL_KEYDOWN);
       
   195 end;
       
   196 {$ELSE}
   188 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   197 procedure ProcessKey(event: TSDL_KeyboardEvent); inline;
   189 var code: LongInt;
   198 var code: LongInt;
   190 begin
   199 begin
   191     code:= event.keysym.sym;
   200     code:= event.keysym.sym;
   192     //MaskModifier(code, event.keysym.modifier);
   201     //MaskModifier(code, event.keysym.modifier);
   193     ProcessKey(code, event.type_ = SDL_KEYDOWN);
   202     ProcessKey(code, event.type_ = SDL_KEYDOWN);
   194 end;
   203 end;
       
   204 {$ENDIF}
   195 
   205 
   196 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   206 procedure ProcessMouse(event: TSDL_MouseButtonEvent; ButtonDown: boolean);
   197 begin
   207 begin
   198 case event.button of
   208 case event.button of
   199     SDL_BUTTON_LEFT:
   209     SDL_BUTTON_LEFT:
   215 for t:= 0 to cKbdMaxIndex do
   225 for t:= 0 to cKbdMaxIndex do
   216     if tkbd[t] then
   226     if tkbd[t] then
   217         ProcessKey(t, False);
   227         ProcessKey(t, False);
   218 end;
   228 end;
   219 
   229 
       
   230 
       
   231 procedure InitDefaultBinds;
       
   232 var i: Longword;
       
   233 begin
       
   234     DefaultBinds[KeyNameToCode('escape')]:= 'quit';
       
   235     DefaultBinds[KeyNameToCode(_S'`')]:= 'history';
       
   236     DefaultBinds[KeyNameToCode('delete')]:= 'rotmask';
       
   237 
       
   238     //numpad
       
   239     //DefaultBinds[265]:= '+volup';
       
   240     //DefaultBinds[256]:= '+voldown';
       
   241 
       
   242     DefaultBinds[KeyNameToCode(_S'0')]:= '+volup';
       
   243     DefaultBinds[KeyNameToCode(_S'9')]:= '+voldown';
       
   244     DefaultBinds[KeyNameToCode(_S'8')]:= 'mute';
       
   245     DefaultBinds[KeyNameToCode(_S'c')]:= 'capture';
       
   246     DefaultBinds[KeyNameToCode(_S'r')]:= 'record';
       
   247     DefaultBinds[KeyNameToCode(_S'h')]:= 'findhh';
       
   248     DefaultBinds[KeyNameToCode(_S'p')]:= 'pause';
       
   249     DefaultBinds[KeyNameToCode(_S's')]:= '+speedup';
       
   250     DefaultBinds[KeyNameToCode(_S't')]:= 'chat';
       
   251     DefaultBinds[KeyNameToCode(_S'y')]:= 'confirm';
       
   252 
       
   253     DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
       
   254     DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomin';
       
   255     DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomout';
       
   256 
       
   257     DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
       
   258 
       
   259 
       
   260     DefaultBinds[KeyNameToCode('mousel')]:= '/put';
       
   261     DefaultBinds[KeyNameToCode('mouser')]:= 'ammomenu';
       
   262     DefaultBinds[KeyNameToCode('backspace')]:= 'hjump';
       
   263     DefaultBinds[KeyNameToCode('tab')]:= 'switch';
       
   264     DefaultBinds[KeyNameToCode('return')]:= 'ljump';
       
   265     DefaultBinds[KeyNameToCode('space')]:= '+attack';
       
   266     DefaultBinds[KeyNameToCode('up')]:= '+up';
       
   267     DefaultBinds[KeyNameToCode('down')]:= '+down';
       
   268     DefaultBinds[KeyNameToCode('left')]:= '+left';
       
   269     DefaultBinds[KeyNameToCode('right')]:= '+right';
       
   270     DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
       
   271 
       
   272 
       
   273     DefaultBinds[KeyNameToCode('j0a0u')]:= '+left';
       
   274     DefaultBinds[KeyNameToCode('j0a0d')]:= '+right';
       
   275     DefaultBinds[KeyNameToCode('j0a1u')]:= '+up';
       
   276     DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
       
   277     for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
       
   278     for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
       
   279 
       
   280     loadBinds('dbind', cPathz[ptData] + '/settings.ini');
       
   281 end;
       
   282 
       
   283 
       
   284 {$IFDEF SDL2}
       
   285 procedure InitKbdKeyTable;
       
   286 var i, j, k, t: LongInt;
       
   287     s: string[15];
       
   288 begin
       
   289     KeyNames[cKeyMaxIndex    ]:= 'mousel';
       
   290     KeyNames[cKeyMaxIndex - 1]:= 'mousem';
       
   291     KeyNames[cKeyMaxIndex - 2]:= 'mouser';
       
   292     KeyNames[cKeyMaxIndex - 3]:= 'wheelup';
       
   293     KeyNames[cKeyMaxIndex - 4]:= 'wheeldown';
       
   294 
       
   295     for i:= 0 to cKeyMaxIndex - 5 do
       
   296         begin
       
   297         s:= shortstring(SDL_GetScancodeName(i));
       
   298 
       
   299         for t:= 1 to Length(s) do
       
   300             if s[t] = ' ' then
       
   301                 s[t]:= '_';
       
   302         KeyNames[i]:= LowerCase(s)
       
   303         end;
       
   304 
       
   305 
       
   306     // get the size of keyboard array
       
   307     SDL_GetKeyState(@k);
       
   308 
       
   309     // Controller(s)
       
   310     for j:= 0 to Pred(ControllerNumControllers) do
       
   311         begin
       
   312         for i:= 0 to Pred(ControllerNumAxes[j]) do
       
   313             begin
       
   314             KeyNames[k + 0]:= 'j' + IntToStr(j) + 'a' + IntToStr(i) + 'u';
       
   315             KeyNames[k + 1]:= 'j' + IntToStr(j) + 'a' + IntToStr(i) + 'd';
       
   316             inc(k, 2);
       
   317             end;
       
   318         for i:= 0 to Pred(ControllerNumHats[j]) do
       
   319             begin
       
   320             KeyNames[k + 0]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'u';
       
   321             KeyNames[k + 1]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'r';
       
   322             KeyNames[k + 2]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'd';
       
   323             KeyNames[k + 3]:= 'j' + IntToStr(j) + 'h' + IntToStr(i) + 'l';
       
   324             inc(k, 4);
       
   325             end;
       
   326         for i:= 0 to Pred(ControllerNumButtons[j]) do
       
   327             begin
       
   328             KeyNames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
       
   329             inc(k, 1);
       
   330             end;
       
   331         end;
       
   332 
       
   333         InitDefaultBinds
       
   334 end;
       
   335 {$ELSE}
   220 procedure InitKbdKeyTable;
   336 procedure InitKbdKeyTable;
   221 var i, j, k, t: LongInt;
   337 var i, j, k, t: LongInt;
   222     s: string[15];
   338     s: string[15];
   223 begin
   339 begin
   224 //TODO in sdl13 this overrides some values (A and B) change indices to some other values at the back perhaps?
   340 //TODO in sdl13 this overrides some values (A and B) change indices to some other values at the back perhaps?
   268         keynames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
   384         keynames[k]:= 'j' + IntToStr(j) + 'b' + IntToStr(i);
   269         inc(k, 1);
   385         inc(k, 1);
   270         end;
   386         end;
   271     end;
   387     end;
   272 
   388 
   273 DefaultBinds[KeyNameToCode('escape')]:= 'quit';
   389     InitDefaultBinds
   274 DefaultBinds[KeyNameToCode(_S'`')]:= 'history';
   390 end;
   275 DefaultBinds[KeyNameToCode('delete')]:= 'rotmask';
   391 {$ENDIF}
   276 
   392 
   277 //numpad
   393 
   278 //DefaultBinds[265]:= '+volup';
   394 
   279 //DefaultBinds[256]:= '+voldown';
   395 {$IFNDEF MOBILE}
   280 
       
   281 DefaultBinds[KeyNameToCode(_S'0')]:= '+volup';
       
   282 DefaultBinds[KeyNameToCode(_S'9')]:= '+voldown';
       
   283 DefaultBinds[KeyNameToCode(_S'8')]:= 'mute';
       
   284 DefaultBinds[KeyNameToCode(_S'c')]:= 'capture';
       
   285 DefaultBinds[KeyNameToCode(_S'r')]:= 'record';
       
   286 DefaultBinds[KeyNameToCode(_S'h')]:= 'findhh';
       
   287 DefaultBinds[KeyNameToCode(_S'p')]:= 'pause';
       
   288 DefaultBinds[KeyNameToCode(_S's')]:= '+speedup';
       
   289 DefaultBinds[KeyNameToCode(_S't')]:= 'chat';
       
   290 DefaultBinds[KeyNameToCode(_S'y')]:= 'confirm';
       
   291 
       
   292 DefaultBinds[KeyNameToCode('mousem')]:= 'zoomreset';
       
   293 DefaultBinds[KeyNameToCode('wheelup')]:= 'zoomin';
       
   294 DefaultBinds[KeyNameToCode('wheeldown')]:= 'zoomout';
       
   295 
       
   296 DefaultBinds[KeyNameToCode('f12')]:= 'fullscr';
       
   297 
       
   298 
       
   299 DefaultBinds[KeyNameToCode('mousel')]:= '/put';
       
   300 DefaultBinds[KeyNameToCode('mouser')]:= 'ammomenu';
       
   301 DefaultBinds[KeyNameToCode('backspace')]:= 'hjump';
       
   302 DefaultBinds[KeyNameToCode('tab')]:= 'switch';
       
   303 DefaultBinds[KeyNameToCode('return')]:= 'ljump';
       
   304 DefaultBinds[KeyNameToCode('space')]:= '+attack';
       
   305 DefaultBinds[KeyNameToCode('up')]:= '+up';
       
   306 DefaultBinds[KeyNameToCode('down')]:= '+down';
       
   307 DefaultBinds[KeyNameToCode('left')]:= '+left';
       
   308 DefaultBinds[KeyNameToCode('right')]:= '+right';
       
   309 DefaultBinds[KeyNameToCode('left_shift')]:= '+precise';
       
   310 
       
   311 
       
   312 DefaultBinds[KeyNameToCode('j0a0u')]:= '+left';
       
   313 DefaultBinds[KeyNameToCode('j0a0d')]:= '+right';
       
   314 DefaultBinds[KeyNameToCode('j0a1u')]:= '+up';
       
   315 DefaultBinds[KeyNameToCode('j0a1d')]:= '+down';
       
   316 for i:= 1 to 10 do DefaultBinds[KeyNameToCode('f'+IntToStr(i))]:= 'slot '+IntToStr(i);
       
   317 for i:= 1 to 5  do DefaultBinds[KeyNameToCode(IntToStr(i))]:= 'timer '+IntToStr(i);
       
   318 
       
   319 loadBinds('dbind', cPathz[ptData] + '/settings.ini');
       
   320 end;
       
   321 
       
   322 procedure SetBinds(var binds: TBinds);
   396 procedure SetBinds(var binds: TBinds);
   323 {$IFNDEF MOBILE}
       
   324 var
   397 var
   325     t: LongInt;
   398     t: LongInt;
   326 {$ENDIF}
   399 begin
   327 begin
       
   328 {$IFDEF MOBILE}
       
   329     binds:= binds; // avoid hint
       
   330     CurrentBinds:= DefaultBinds;
       
   331 {$ELSE}
       
   332     for t:= 0 to cKbdMaxIndex do
   400     for t:= 0 to cKbdMaxIndex do
   333         if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
   401         if (CurrentBinds[t] <> binds[t]) and tkbd[t] then
   334             ProcessKey(t, False);
   402             ProcessKey(t, False);
   335 
   403 
   336     CurrentBinds:= binds;
   404     CurrentBinds:= binds;
       
   405 end;
       
   406 {$ELSE}
       
   407 procedure SetBinds(var binds: TBinds);
       
   408 begin
       
   409     binds:= binds; // avoid hint
       
   410     CurrentBinds:= DefaultBinds;
       
   411 end;
   337 {$ENDIF}
   412 {$ENDIF}
   338 end;
       
   339 
   413 
   340 procedure SetDefaultBinds;
   414 procedure SetDefaultBinds;
   341 begin
   415 begin
   342     CurrentBinds:= DefaultBinds;
   416     CurrentBinds:= DefaultBinds;
   343 end;
   417 end;