hedgewars/uKeys.pas
changeset 2436 246ef6271470
parent 2428 6800f8aa0184
child 2438 6df2e58b6ab2
equal deleted inserted replaced
2435:7afc9edac4cf 2436:246ef6271470
    56 implementation
    56 implementation
    57 uses SDLh, uTeams, uConsole, uMisc, uStore;
    57 uses SDLh, uTeams, uConsole, uMisc, uStore;
    58 const KeyNumber = 1024;
    58 const KeyNumber = 1024;
    59 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
    59 type TKeyboardState = array[0..cKeyMaxIndex] of Byte;
    60 
    60 
    61 var tkbd: TKeyboardState;
    61 var tkbd, tkbdn: TKeyboardState;
    62     KeyNames: array [0..cKeyMaxIndex] of string[15];
    62     KeyNames: array [0..cKeyMaxIndex] of string[15];
    63     DefaultBinds, CurrentBinds: TBinds;
    63     DefaultBinds, CurrentBinds: TBinds;
    64 
    64 
    65 function KeyNameToCode(name: string): word;
    65 function KeyNameToCode(name: string): word;
    66 var Result: Word;
    66 var Result: Word;
    68 Result:= cKeyMaxIndex;
    68 Result:= cKeyMaxIndex;
    69 while (Result > 0) and (KeyNames[Result] <> name) do dec(Result);
    69 while (Result > 0) and (KeyNames[Result] <> name) do dec(Result);
    70 KeyNameToCode:= Result
    70 KeyNameToCode:= Result
    71 end;
    71 end;
    72 
    72 
    73 
       
    74 procedure ProcessKbd;
    73 procedure ProcessKbd;
    75 var  i, j, k: LongInt;
    74 var  i, j, k: LongInt;
       
    75      s: shortstring;
    76      pkbd: PByteArray;
    76      pkbd: PByteArray;
    77      Trusted: boolean;
    77      Trusted: boolean;
    78      s: shortstring;
    78 begin
    79 begin
       
    80 
       
    81 hideAmmoMenu:= false;
    79 hideAmmoMenu:= false;
    82 Trusted:= (CurrentTeam <> nil)
    80 Trusted:= (CurrentTeam <> nil)
    83           and (not CurrentTeam^.ExtDriven)
    81           and (not CurrentTeam^.ExtDriven)
    84           and (CurrentHedgehog^.BotLevel = 0);
    82           and (CurrentHedgehog^.BotLevel = 0);
    85 
    83 
    86 // move cursor/camera
    84 // move cursor/camera
    87 // TODO: Scale on screen dimensions and/or axis value (game controller)?
    85 // TODO: Scale on screen dimensions and/or axis value (game controller)?
    88 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
    86 movecursor(5 * CursorMovementX, 5 * CursorMovementY);
       
    87 		  
    89 {$IFDEF SDL13}
    88 {$IFDEF SDL13}
    90 pkbd := SDL_GetKeyboardState(nil);
    89 pkbd := SDL_GetKeyboardState(@j);
    91 i    := SDL_GetMouseState(0, nil, nil);
    90 k    := SDL_GetMouseState(0, nil, nil);
    92 {$ELSE}
    91 {$ELSE}
    93 pkbd := SDL_GetKeyState(nil);
    92 pkbd := SDL_GetKeyState(@j);
    94 i    := SDL_GetMouseState(nil, nil);
    93 k    := SDL_GetMouseState(nil, nil);
    95 {$ENDIF}
    94 {$ENDIF}
       
    95 
       
    96 for i:= 6 to pred(j) do // first 6 will be overwritten
       
    97 	tkbdn[i]:= pkbd^[i];
       
    98 
    96 // mouse buttons
    99 // mouse buttons
    97 {$IFDEF DARWIN}
   100 {$IFDEF DARWIN}
    98 pkbd^[1]:= ((i and 1) and not (pkbd^[306] or pkbd^[305]));
   101 tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
    99 pkbd^[3]:= ((i and 1) and (pkbd^[306] or pkbd^[305])) or (i and 4);
   102 tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
   100 {$ELSE}
   103 {$ELSE}
   101 pkbd^[1]:= (i and 1);
   104 tkbdn[1]:= (k and 1);
   102 pkbd^[3]:= ((i shr 2) and 1);
   105 tkbdn[3]:= ((k shr 2) and 1);
   103 {$ENDIF}
   106 {$ENDIF}
   104 pkbd^[2]:= ((i shr 1) and 1);
   107 tkbdn[2]:= ((k shr 1) and 1);
   105 
   108 
   106 // mouse wheels (see event loop in project file)
   109 // mouse wheels (see event loop in project file)
   107 pkbd^[4]:= ord(wheelDown);
   110 tkbdn[4]:= ord(wheelDown);
   108 pkbd^[5]:= ord(wheelUp);
   111 tkbdn[5]:= ord(wheelUp);
   109 wheelUp:= false;
   112 wheelUp:= false;
   110 wheelDown:= false;
   113 wheelDown:= false;
   111 
   114 
   112 // Controller(s)
   115 // Controller(s)
   113 k:= 500; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   116 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   114 for j:= 0 to Pred(ControllerNumControllers) do
   117 for j:= 0 to Pred(ControllerNumControllers) do
   115 	begin
   118 	begin
   116 	for i:= 0 to Pred(ControllerNumAxes[j]) do
   119 	for i:= 0 to Pred(ControllerNumAxes[j]) do
   117 		begin
   120 		begin
   118 		if ControllerAxes[j][i] > 20000 then pkbd^[k + 0]:= 1 else pkbd^[k + 0]:= 0;
   121 		if ControllerAxes[j][i] > 20000 then tkbdn[k + 0]:= 1 else tkbdn[k + 0]:= 0;
   119 		if ControllerAxes[j][i] < -20000 then pkbd^[k + 1]:= 1 else pkbd^[k + 1]:= 0;
   122 		if ControllerAxes[j][i] < -20000 then tkbdn[k + 1]:= 1 else tkbdn[k + 1]:= 0;
   120 		inc(k, 2);
   123 		inc(k, 2);
   121 		end;
   124 		end;
   122 	for i:= 0 to Pred(ControllerNumHats[j]) do
   125 	for i:= 0 to Pred(ControllerNumHats[j]) do
   123 		begin
   126 		begin
   124 		pkbd^[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP;
   127 		tkbdn[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP;
   125 		pkbd^[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT;
   128 		tkbdn[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT;
   126 		pkbd^[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN;
   129 		tkbdn[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN;
   127 		pkbd^[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT;
   130 		tkbdn[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT;
   128 		inc(k, 4);
   131 		inc(k, 4);
   129 		end;
   132 		end;
   130 	for i:= 0 to Pred(ControllerNumButtons[j]) do
   133 	for i:= 0 to Pred(ControllerNumButtons[j]) do
   131 		begin
   134 		begin
   132 		pkbd^[k]:= ControllerButtons[j][i];
   135 		tkbdn[k]:= ControllerButtons[j][i];
   133 		inc(k, 1);
   136 		inc(k, 1);
   134 		end;
   137 		end;
   135 	end;
   138 	end;
   136 
   139 
   137 // now process strokes
   140 // now process strokes
   138 for i:= 1 to cKeyMaxIndex do
   141 for i:= 0 to cKeyMaxIndex do
   139 if CurrentBinds[i][0] <> #0 then
   142 if CurrentBinds[i][0] <> #0 then
   140 	begin
   143 	begin
   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;
   144 	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;
   142 	if (tkbd[i] = 0) and (pkbd^[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted)
   145 	if (tkbd[i] = 0) and (tkbdn[i] <> 0) then ParseCommand(CurrentBinds[i], Trusted)
   143 	else if (CurrentBinds[i][1] = '+')
   146 	else if (CurrentBinds[i][1] = '+')
   144 			and (pkbd^[i] = 0)
   147 			and (tkbdn[i] = 0)
   145 			and (tkbd[i] <> 0) then
   148 			and (tkbd[i] <> 0) then
   146 			begin
   149 			begin
   147 			s:= CurrentBinds[i];
   150 			s:= CurrentBinds[i];
   148 			s[1]:= '-';
   151 			s[1]:= '-';
   149 			ParseCommand(s, Trusted)
   152 			ParseCommand(s, Trusted)
   150 			end;
   153 			end;
   151 	tkbd[i]:= pkbd^[i]
   154 	tkbd[i]:= tkbdn[i]
   152 	end;
   155 	end
   153 end;
   156 end;
   154 
   157 
   155 procedure ResetKbd;
   158 procedure ResetKbd;
   156 var i, j, k, t: LongInt;
   159 var i, j, k, t: LongInt;
   157     pkbd: PByteArray;
   160     pkbd: PByteArray;
   158 begin
   161 begin
   159 
   162 
   160 {$IFDEF SDL13}
   163 {$IFDEF SDL13}
   161 pkbd:= PByteArray(SDL_GetKeyboardState(@i));
   164 pkbd:= SDL_GetKeyboardState(@j);
   162 {$ELSE}
   165 k    := SDL_GetMouseState(0, nil, nil);
   163 pkbd:= PByteArray(SDL_GetKeyState(@i));
   166 {$ELSE}
   164 {$ENDIF}
   167 pkbd:= SDL_GetKeyState(@j);
   165 TryDo(i < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(i) + ')', true);
   168 k    := SDL_GetMouseState(nil, nil);
   166 
   169 {$ENDIF}
   167 k:= 500;
   170 TryDo(j < cKeyMaxIndex, 'SDL keys number is more than expected (' + inttostr(j) + ')', true);
       
   171 
       
   172 for i:= 1 to pred(j) do
       
   173 	tkbdn[i]:= pkbd^[i];
       
   174 
       
   175 // mouse buttons
       
   176 {$IFDEF DARWIN}
       
   177 tkbdn[1]:= ((k and 1) and not (tkbdn[306] or tkbdn[305]));
       
   178 tkbdn[3]:= ((k and 1) and (tkbdn[306] or tkbdn[305])) or (k and 4);
       
   179 {$ELSE}
       
   180 tkbdn[1]:= (k and 1);
       
   181 tkbdn[3]:= ((k shr 2) and 1);
       
   182 {$ENDIF}
       
   183 tkbdn[2]:= ((k shr 1) and 1);
       
   184 
       
   185 // mouse wheels (see event loop in project file)
       
   186 tkbdn[4]:= ord(wheelDown);
       
   187 tkbdn[5]:= ord(wheelUp);
       
   188 wheelUp:= false;
       
   189 wheelDown:= false;
       
   190 
       
   191 // Controller(s)
       
   192 k:= j; // should we test k for hitting the limit? sounds rather unlikely to ever reach it
   168 for j:= 0 to Pred(ControllerNumControllers) do
   193 for j:= 0 to Pred(ControllerNumControllers) do
   169 	begin
   194 	begin
   170 	for i:= 0 to Pred(ControllerNumAxes[j]) do
   195 	for i:= 0 to Pred(ControllerNumAxes[j]) do
   171 		begin
   196 		begin
   172 		pkbd^[k + 0]:= 0;
   197 		if ControllerAxes[j][i] > 20000 then tkbdn[k + 0]:= 1 else tkbdn[k + 0]:= 0;
   173 		pkbd^[k + 1]:= 0;
   198 		if ControllerAxes[j][i] < -20000 then tkbdn[k + 1]:= 1 else tkbdn[k + 1]:= 0;
   174 		inc(k, 2);
   199 		inc(k, 2);
   175 		end;
   200 		end;
   176 	for i:= 0 to Pred(ControllerNumHats[j]) do
   201 	for i:= 0 to Pred(ControllerNumHats[j]) do
   177 		begin
   202 		begin
   178 		pkbd^[k + 0]:= 0;
   203 		tkbdn[k + 0]:= ControllerHats[j][i] and SDL_HAT_UP;
   179 		pkbd^[k + 1]:= 0;
   204 		tkbdn[k + 1]:= ControllerHats[j][i] and SDL_HAT_RIGHT;
   180 		pkbd^[k + 2]:= 0;
   205 		tkbdn[k + 2]:= ControllerHats[j][i] and SDL_HAT_DOWN;
   181 		pkbd^[k + 3]:= 0;
   206 		tkbdn[k + 3]:= ControllerHats[j][i] and SDL_HAT_LEFT;
   182 		inc(k, 4);
   207 		inc(k, 4);
   183 		end;
   208 		end;
   184 	for i:= 0 to Pred(ControllerNumButtons[j]) do
   209 	for i:= 0 to Pred(ControllerNumButtons[j]) do
   185 		begin
   210 		begin
   186 		pkbd^[k]:= 0;
   211 		tkbdn[k]:= ControllerButtons[j][i];
   187 		inc(k, 1);
   212 		inc(k, 1);
   188 		end;
   213 		end;
   189 	end;
   214 	end;
   190 
   215 	
   191 for t:= 0 to Pred(i) do
   216 for t:= 0 to cKeyMaxIndex do
   192     tkbd[i]:= pkbd^[i]
   217     tkbd[i]:= tkbdn[i]
   193 end;
   218 end;
   194 
   219 
   195 procedure InitKbdKeyTable;
   220 procedure InitKbdKeyTable;
   196 var i, j, k, t: LongInt;
   221 var i, j, k, t: LongInt;
   197     s: string[15];
   222     s: string[15];
   212            if s[t] = ' ' then s[t]:= '_';
   237            if s[t] = ' ' then s[t]:= '_';
   213        KeyNames[i]:= s
   238        KeyNames[i]:= s
   214        end;
   239        end;
   215     end;
   240     end;
   216 
   241 
       
   242 {$IFDEF SDL13}
       
   243 PByteArray(SDL_GetKeyboardState(@i);
       
   244 {$ELSE}
       
   245 SDL_GetKeyState(@i);
       
   246 {$ENDIF}
       
   247 
   217 // Controller(s)
   248 // Controller(s)
   218 k:= 500;
   249 k:= i;
   219 for j:= 0 to Pred(ControllerNumControllers) do
   250 for j:= 0 to Pred(ControllerNumControllers) do
   220 	begin
   251 	begin
   221 	for i:= 0 to Pred(ControllerNumAxes[j]) do
   252 	for i:= 0 to Pred(ControllerNumAxes[j]) do
   222 		begin
   253 		begin
   223 		keynames[k + 0]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'u';
   254 		keynames[k + 0]:= 'j' + inttostr(j) + 'a' + inttostr(i) + 'u';