hedgewars/uKeys.pas
changeset 2428 6800f8aa0184
parent 2407 9f413bd5150e
child 2436 246ef6271470
equal deleted inserted replaced
2427:241e3bb6a146 2428:6800f8aa0184
    30 procedure InitKbdKeyTable;
    30 procedure InitKbdKeyTable;
    31 
    31 
    32 procedure SetBinds(var binds: TBinds);
    32 procedure SetBinds(var binds: TBinds);
    33 procedure SetDefaultBinds;
    33 procedure SetDefaultBinds;
    34 
    34 
    35 var KbdKeyPressed: boolean;
    35 procedure ControllerInit;
       
    36 procedure ControllerClose;
       
    37 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
       
    38 procedure ControllerHatEvent(joy, hat, value: Byte);
       
    39 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
       
    40 
       
    41 var hideAmmoMenu: boolean;
    36 	wheelUp: boolean = false;
    42 	wheelUp: boolean = false;
    37 	wheelDown: boolean = false;
    43 	wheelDown: boolean = false;
       
    44 
       
    45 	ControllerNumControllers: Integer;
       
    46     ControllerEnabled: Integer;
       
    47     ControllerNumAxes: array[0..5] of Integer;
       
    48     //ControllerNumBalls: array[0..5] of Integer;
       
    49 	ControllerNumHats: array[0..5] of Integer;
       
    50 	ControllerNumButtons: array[0..5] of Integer;
       
    51 	ControllerAxes: array[0..5] of array[0..19] of Integer;
       
    52 	//ControllerBalls: array[0..5] of array[0..19] of array[0..1] of Integer;
       
    53 	ControllerHats: array[0..5] of array[0..19] of Byte;
       
    54 	ControllerButtons: array[0..5] of array[0..19] of Byte;
    38 
    55 
    39 implementation
    56 implementation
    40 uses SDLh, uTeams, uConsole, uMisc, uStore;
    57 uses SDLh, uTeams, uConsole, uMisc, uStore;
    41 const KeyNumber = 1024;
    58 const KeyNumber = 1024;
    42 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
    59 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
    51 Result:= cKeyMaxIndex;
    68 Result:= cKeyMaxIndex;
    52 while (Result > 0) and (KeyNames[Result] <> name) do dec(Result);
    69 while (Result > 0) and (KeyNames[Result] <> name) do dec(Result);
    53 KeyNameToCode:= Result
    70 KeyNameToCode:= Result
    54 end;
    71 end;
    55 
    72 
       
    73 
    56 procedure ProcessKbd;
    74 procedure ProcessKbd;
    57 var  i: LongInt;
    75 var  i, j, k: LongInt;
    58      s: shortstring;
       
    59      pkbd: PByteArray;
    76      pkbd: PByteArray;
    60      Trusted: boolean;
    77      Trusted: boolean;
    61 begin
    78      s: shortstring;
    62 KbdKeyPressed:= false;
    79 begin
       
    80 
       
    81 hideAmmoMenu:= false;
    63 Trusted:= (CurrentTeam <> nil)
    82 Trusted:= (CurrentTeam <> nil)
    64           and (not CurrentTeam^.ExtDriven)
    83           and (not CurrentTeam^.ExtDriven)
    65           and (CurrentHedgehog^.BotLevel = 0);
    84           and (CurrentHedgehog^.BotLevel = 0);
    66 
    85 
       
    86 // move cursor/camera
       
    87 // TODO: Scale on screen dimensions and/or axis value (game controller)?
       
    88 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
    67 {$IFDEF SDL13}
    89 {$IFDEF SDL13}
    68 pkbd := SDL_GetKeyboardState(nil);
    90 pkbd := SDL_GetKeyboardState(nil);
    69 i    := SDL_GetMouseState(0, nil, nil);
    91 i    := SDL_GetMouseState(0, nil, nil);
    70 {$ELSE}
    92 {$ELSE}
    71 pkbd := SDL_GetKeyState(nil);
    93 pkbd := SDL_GetKeyState(nil);
    72 i    := SDL_GetMouseState(nil, nil);
    94 i    := SDL_GetMouseState(nil, nil);
    73 {$ENDIF}
    95 {$ENDIF}
    74 
       
    75 // mouse buttons
    96 // mouse buttons
    76 {$IFDEF DARWIN}
    97 {$IFDEF DARWIN}
    77 pkbd^[1]:= ((i and 1) and not (pkbd^[306] or pkbd^[305]));
    98 pkbd^[1]:= ((i and 1) and not (pkbd^[306] or pkbd^[305]));
    78 pkbd^[3]:= ((i and 1) and (pkbd^[306] or pkbd^[305])) or (i and 4);
    99 pkbd^[3]:= ((i and 1) and (pkbd^[306] or pkbd^[305])) or (i and 4);
    79 {$ELSE}
   100 {$ELSE}
    86 pkbd^[4]:= ord(wheelDown);
   107 pkbd^[4]:= ord(wheelDown);
    87 pkbd^[5]:= ord(wheelUp);
   108 pkbd^[5]:= ord(wheelUp);
    88 wheelUp:= false;
   109 wheelUp:= false;
    89 wheelDown:= false;
   110 wheelDown:= false;
    90 
   111 
       
   112 // Controller(s)
       
   113 k:= 500; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
       
   114 for j:= 0 to Pred(ControllerNumControllers) do
       
   115 	begin
       
   116 	for i:= 0 to Pred(ControllerNumAxes[j]) do
       
   117 		begin
       
   118 		if ControllerAxes[j][i] > 20000 then pkbd^[k + 0]:= 1 else pkbd^[k + 0]:= 0;
       
   119 		if ControllerAxes[j][i] < -20000 then pkbd^[k + 1]:= 1 else pkbd^[k + 1]:= 0;
       
   120 		inc(k, 2);
       
   121 		end;
       
   122 	for i:= 0 to Pred(ControllerNumHats[j]) do
       
   123 		begin
       
   124 		pkbd^[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP;
       
   125 		pkbd^[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT;
       
   126 		pkbd^[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN;
       
   127 		pkbd^[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT;
       
   128 		inc(k, 4);
       
   129 		end;
       
   130 	for i:= 0 to Pred(ControllerNumButtons[j]) do
       
   131 		begin
       
   132 		pkbd^[k]:= ControllerButtons[j][i];
       
   133 		inc(k, 1);
       
   134 		end;
       
   135 	end;
       
   136 
    91 // now process strokes
   137 // now process strokes
    92 for i:= 1 to cKeyMaxIndex do
   138 for i:= 1 to cKeyMaxIndex do
    93 if CurrentBinds[i][0] <> #0 then
   139 if CurrentBinds[i][0] <> #0 then
    94 	begin
   140 	begin
    95 	if (i > 3) and (pkbd^[i] <> 0) then KbdKeyPressed:= true;
   141 	if (i > 3) and (pkbd^[i] <> 0) and not (hideAmmoMenu or (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;
    96 	if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted)
   142 	if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted)
    97 	else if (CurrentBinds[i][1] = '+')
   143 	else if (CurrentBinds[i][1] = '+')
    98 			and (pkbd^[i] = 0)
   144 			and (pkbd^[i] = 0)
    99 			and (tkbd[i] <> 0) then
   145 			and (tkbd[i] <> 0) then
   100 			begin
   146 			begin
   101 			s:= CurrentBinds[i];
   147 			s:= CurrentBinds[i];
   102 			s[1]:= '-';
   148 			s[1]:= '-';
   103 			ParseCommand(s, Trusted)
   149 			ParseCommand(s, Trusted)
   104 			end;
   150 			end;
   105 	tkbd[i]:= pkbd^[i]
   151 	tkbd[i]:= pkbd^[i]
   106 	end
   152 	end;
   107 end;
   153 end;
   108 
   154 
   109 procedure ResetKbd;
   155 procedure ResetKbd;
   110 var i, t: LongInt;
   156 var i, j, k, t: LongInt;
   111     pkbd: PByteArray;
   157     pkbd: PByteArray;
   112 begin
   158 begin
   113 
   159 
   114 {$IFDEF SDL13}
   160 {$IFDEF SDL13}
   115 pkbd:= PByteArray(SDL_GetKeyboardState(@i));
   161 pkbd:= PByteArray(SDL_GetKeyboardState(@i));
   116 {$ELSE}
   162 {$ELSE}
   117 pkbd:= PByteArray(SDL_GetKeyState(@i));
   163 pkbd:= PByteArray(SDL_GetKeyState(@i));
   118 {$ENDIF}
   164 {$ENDIF}
   119 TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true);
   165 TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true);
   120 
   166 
       
   167 k:= 500;
       
   168 for j:= 0 to Pred(ControllerNumControllers) do
       
   169 	begin
       
   170 	for i:= 0 to Pred(ControllerNumAxes[j]) do
       
   171 		begin
       
   172 		pkbd^[k + 0]:= 0;
       
   173 		pkbd^[k + 1]:= 0;
       
   174 		inc(k, 2);
       
   175 		end;
       
   176 	for i:= 0 to Pred(ControllerNumHats[j]) do
       
   177 		begin
       
   178 		pkbd^[k + 0]:= 0;
       
   179 		pkbd^[k + 1]:= 0;
       
   180 		pkbd^[k + 2]:= 0;
       
   181 		pkbd^[k + 3]:= 0;
       
   182 		inc(k, 4);
       
   183 		end;
       
   184 	for i:= 0 to Pred(ControllerNumButtons[j]) do
       
   185 		begin
       
   186 		pkbd^[k]:= 0;
       
   187 		inc(k, 1);
       
   188 		end;
       
   189 	end;
       
   190 
   121 for t:= 0 to Pred(i) do
   191 for t:= 0 to Pred(i) do
   122     tkbd[i]:= pkbd^[i]
   192     tkbd[i]:= pkbd^[i]
   123 end;
   193 end;
   124 
   194 
   125 procedure InitKbdKeyTable;
   195 procedure InitKbdKeyTable;
   126 var i, t: LongInt;
   196 var i, j, k, t: LongInt;
   127     s: string[15];
   197     s: string[15];
   128 begin
   198 begin
   129 KeyNames[1]:= 'mousel';
   199 KeyNames[1]:= 'mousel';
   130 KeyNames[2]:= 'mousem';
   200 KeyNames[2]:= 'mousem';
   131 KeyNames[3]:= 'mouser';
   201 KeyNames[3]:= 'mouser';
   142            if s[t] = ' ' then s[t]:= '_';
   212            if s[t] = ' ' then s[t]:= '_';
   143        KeyNames[i]:= s
   213        KeyNames[i]:= s
   144        end;
   214        end;
   145     end;
   215     end;
   146 
   216 
       
   217 // Controller(s)
       
   218 k:= 500;
       
   219 for j:= 0 to Pred(ControllerNumControllers) do
       
   220 	begin
       
   221 	for i:= 0 to Pred(ControllerNumAxes[j]) do
       
   222 		begin
       
   223 		keynames[k + 0]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'u';
       
   224 		keynames[k + 1]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'd';
       
   225 		inc(k, 2);
       
   226 		end;
       
   227 	for i:= 0 to Pred(ControllerNumHats[j]) do
       
   228 		begin
       
   229 		keynames[k + 0]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'u';
       
   230 		keynames[k + 1]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'r';
       
   231 		keynames[k + 2]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'd';
       
   232 		keynames[k + 3]:= 'j' + inttostr(j) + 'h' + inttostr(i) + 'l';
       
   233 		inc(k, 4);
       
   234 		end;
       
   235 	for i:= 0 to Pred(ControllerNumButtons[j]) do
       
   236 		begin
       
   237 		keynames[k]:= 'j' + inttostr(j) + 'b' + inttostr(i);
       
   238 		inc(k, 1);
       
   239 		end;
       
   240 	end;
       
   241 	
   147 DefaultBinds[ 27]:= 'quit';
   242 DefaultBinds[ 27]:= 'quit';
   148 DefaultBinds[ 96]:= 'history';
   243 DefaultBinds[ 96]:= 'history';
   149 DefaultBinds[127]:= 'rotmask';
   244 DefaultBinds[127]:= 'rotmask';
   150 
   245 
   151 DefaultBinds[KeyNameToCode('0')]:= '+volup';
   246 DefaultBinds[KeyNameToCode('0')]:= '+volup';
   180 begin
   275 begin
   181 tkbd[13]:= 1;
   276 tkbd[13]:= 1;
   182 tkbd[271]:= 1
   277 tkbd[271]:= 1
   183 end;
   278 end;
   184 
   279 
       
   280 var Controller: array [0..5] of PSDLJoystick;
       
   281 	
       
   282 procedure ControllerInit;
       
   283 var i, j: Integer;
       
   284 begin
       
   285 ControllerEnabled:= 0;
       
   286 ControllerNumControllers:= SDL_NumJoysticks;
       
   287 
       
   288 if ControllerNumControllers > 6 then ControllerNumControllers:= 6;
       
   289 
       
   290 WriteLnToConsole('Number of game controllers: ' + inttostr(ControllerNumControllers));
       
   291 
       
   292 if ControllerNumControllers > 0 then
       
   293 	begin
       
   294 	for j:= 0 to pred(ControllerNumControllers) do
       
   295 		begin
       
   296 		WriteLnToConsole('Using game controller: ' + SDL_JoystickName(j));
       
   297 		Controller[j]:= SDL_JoystickOpen(j);
       
   298 		if Controller[j] = nil then
       
   299 			WriteLnToConsole('* Failed to open game controller!')
       
   300 		else
       
   301 			begin
       
   302 			ControllerNumAxes[j]:= SDL_JoystickNumAxes(Controller[j]);
       
   303 			//ControllerNumBalls[j]:= SDL_JoystickNumBalls(Controller[j]);
       
   304 			ControllerNumHats[j]:= SDL_JoystickNumHats(Controller[j]);
       
   305 			ControllerNumButtons[j]:= SDL_JoystickNumButtons(Controller[j]);
       
   306 			WriteLnToConsole('* Number of axes: ' + inttostr(ControllerNumAxes[j]));
       
   307 			//WriteLnToConsole('* Number of balls: ' + inttostr(ControllerNumBalls[j]));
       
   308 			WriteLnToConsole('* Number of hats: ' + inttostr(ControllerNumHats[j]));
       
   309 			WriteLnToConsole('* Number of buttons: ' + inttostr(ControllerNumButtons[j]));
       
   310 			ControllerEnabled:= 1;
       
   311 			
       
   312 			if ControllerNumAxes[j] > 20 then ControllerNumAxes[j]:= 20;
       
   313 			//if ControllerNumBalls[j] > 20 then ControllerNumBalls[j]:= 20;
       
   314 			if ControllerNumHats[j] > 20 then ControllerNumHats[j]:= 20;
       
   315 			if ControllerNumButtons[j] > 20 then ControllerNumButtons[j]:= 20;
       
   316 			
       
   317 			// reset all buttons/axes
       
   318 			for i:= 0 to pred(ControllerNumAxes[j]) do
       
   319 				ControllerAxes[j][i]:= 0;
       
   320 			(*for i:= 0 to pred(ControllerNumBalls[j]) do
       
   321 				begin
       
   322 				ControllerBalls[j][i][0]:= 0;
       
   323 				ControllerBalls[j][i][1]:= 0;
       
   324 				end;*)
       
   325 			for i:= 0 to pred(ControllerNumHats[j]) do
       
   326 				ControllerHats[j][i]:= SDL_HAT_CENTERED;
       
   327 			for i:= 0 to pred(ControllerNumButtons[j]) do
       
   328 				ControllerButtons[j][i]:= 0;
       
   329 			end;
       
   330 		end;
       
   331 	// enable event generation/controller updating
       
   332 	SDL_JoystickEventState(1);
       
   333 	end
       
   334 else	
       
   335 	WriteLnToConsole('Not using any game controller');
       
   336 end;
       
   337 
       
   338 procedure ControllerClose;
       
   339 var j: Integer;
       
   340 begin
       
   341 if ControllerEnabled > 0 then
       
   342 	for j:= 0 to pred(ControllerNumControllers) do
       
   343 		SDL_JoystickClose(Controller[j]);
       
   344 end;
       
   345 
       
   346 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
       
   347 begin
       
   348 	ControllerAxes[joy][axis]:= value;
       
   349 end;
       
   350 
       
   351 procedure ControllerHatEvent(joy, hat, value: Byte);
       
   352 begin
       
   353 	ControllerHats[joy][hat]:= value;
       
   354 end;
       
   355 
       
   356 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
       
   357 begin
       
   358 	if pressed then ControllerButtons[joy][button]:= 1 else ControllerButtons[joy][button]:= 0;
       
   359 end;
       
   360 
   185 initialization
   361 initialization
   186 
   362 
   187 end.
   363 end.