DEADSOFTWARE

key binds and vars are stored in dfconfig.cfg
[d2df-sdl.git] / src / engine / e_input.pas
index 6e208d7ecd1920bf6901feb5534897f8452bf3be..6c4eb45d7528aefea11e1b1569305a7bba624750 100644 (file)
@@ -1,4 +1,4 @@
-(* Copyright (C)  DooM 2D:Forever Developers
+(* Copyright (C)  Doom 2D: Forever Developers
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,14 +29,16 @@ const
   e_MaxJoyBtns  = 32;
   e_MaxJoyAxes  = 8;
   e_MaxJoyHats  = 8;
+  e_MaxVirtKeys = 48;
 
   e_MaxJoyKeys = e_MaxJoyBtns + e_MaxJoyAxes*2 + e_MaxJoyHats*4;
 
-  e_MaxInputKeys = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys - 1;
+  e_MaxInputKeys = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys + e_MaxVirtKeys - 1;
   // $$$..$$$ -  321 Keyboard buttons/keys
   // $$$..$$$ - 4*32 Joystick buttons
   // $$$..$$$ -  8*2 Joystick axes (- and +)
   // $$$..$$$ -  4*4 Joystick hats (L U R D)
+  // $$$..$$$ -   48 Virtual buttons/keys
 
   // these are apparently used in g_gui and g_game and elsewhere
   IK_INVALID = 65535;
@@ -59,11 +61,23 @@ const
   IK_SPACE   = SDL_SCANCODE_SPACE;
   IK_CONTROL = SDL_SCANCODE_LCTRL;
   IK_SHIFT   = SDL_SCANCODE_LSHIFT;
+  IK_ALT     = SDL_SCANCODE_LALT;
   IK_TAB     = SDL_SCANCODE_TAB;
   IK_PAGEUP  = SDL_SCANCODE_PAGEUP;
   IK_KPPAGEUP= SDL_SCANCODE_KP_9;
   IK_PAGEDN  = SDL_SCANCODE_PAGEDOWN;
   IK_KPPAGEDN= SDL_SCANCODE_KP_3;
+  IK_0      = SDL_SCANCODE_0;
+  IK_1      = SDL_SCANCODE_1;
+  IK_2      = SDL_SCANCODE_2;
+  IK_3      = SDL_SCANCODE_3;
+  IK_4      = SDL_SCANCODE_4;
+  IK_5      = SDL_SCANCODE_5;
+  IK_6      = SDL_SCANCODE_6;
+  IK_7      = SDL_SCANCODE_7;
+  IK_8      = SDL_SCANCODE_8;
+  IK_9      = SDL_SCANCODE_9;
+  IK_F1      = SDL_SCANCODE_F1;
   IK_F2      = SDL_SCANCODE_F2;
   IK_F3      = SDL_SCANCODE_F3;
   IK_F4      = SDL_SCANCODE_F4;
@@ -73,6 +87,8 @@ const
   IK_F8      = SDL_SCANCODE_F8;
   IK_F9      = SDL_SCANCODE_F9;
   IK_F10     = SDL_SCANCODE_F10;
+  IK_F11     = SDL_SCANCODE_F11;
+  IK_F12     = SDL_SCANCODE_F12;
   IK_END     = SDL_SCANCODE_END;
   IK_KPEND   = SDL_SCANCODE_KP_1;
   IK_BACKSPACE = SDL_SCANCODE_BACKSPACE;
@@ -81,9 +97,56 @@ const
   IK_PAUSE   = SDL_SCANCODE_PAUSE;
   IK_Y       = SDL_SCANCODE_Y;
   IK_N       = SDL_SCANCODE_N;
+  IK_W       = SDL_SCANCODE_W;
+  IK_A       = SDL_SCANCODE_A;
+  IK_S       = SDL_SCANCODE_S;
+  IK_D       = SDL_SCANCODE_D;
+  IK_Q       = SDL_SCANCODE_Q;
+  IK_E       = SDL_SCANCODE_E;
+  IK_H       = SDL_SCANCODE_H;
+  IK_J       = SDL_SCANCODE_J;
+  IK_T       = SDL_SCANCODE_T;
+  IK_MINUS   = SDL_SCANCODE_MINUS;
   // TODO: think of something better than this shit
   IK_LASTKEY = SDL_NUM_SCANCODES-1;
 
+  VK_FIRSTKEY = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys;
+  VK_LEFT     = VK_FIRSTKEY + 0;
+  VK_RIGHT    = VK_FIRSTKEY + 1;
+  VK_UP       = VK_FIRSTKEY + 2;
+  VK_DOWN     = VK_FIRSTKEY + 3;
+  VK_FIRE     = VK_FIRSTKEY + 4;
+  VK_OPEN     = VK_FIRSTKEY + 5;
+  VK_JUMP     = VK_FIRSTKEY + 6;
+  VK_CHAT     = VK_FIRSTKEY + 7;
+  VK_ESCAPE   = VK_FIRSTKEY + 8;
+  VK_0        = VK_FIRSTKEY + 9;
+  VK_1        = VK_FIRSTKEY + 10;
+  VK_2        = VK_FIRSTKEY + 11;
+  VK_3        = VK_FIRSTKEY + 12;
+  VK_4        = VK_FIRSTKEY + 13;
+  VK_5        = VK_FIRSTKEY + 14;
+  VK_6        = VK_FIRSTKEY + 15;
+  VK_7        = VK_FIRSTKEY + 16;
+  VK_8        = VK_FIRSTKEY + 17;
+  VK_9        = VK_FIRSTKEY + 18;
+  VK_A        = VK_FIRSTKEY + 19;
+  VK_B        = VK_FIRSTKEY + 20;
+  VK_C        = VK_FIRSTKEY + 21;
+  VK_D        = VK_FIRSTKEY + 22;
+  VK_E        = VK_FIRSTKEY + 23;
+  VK_F        = VK_FIRSTKEY + 24;
+  VK_CONSOLE  = VK_FIRSTKEY + 25;
+  VK_STATUS   = VK_FIRSTKEY + 26;
+  VK_TEAM     = VK_FIRSTKEY + 27;
+  VK_PREV     = VK_FIRSTKEY + 28;
+  VK_NEXT     = VK_FIRSTKEY + 29;
+  VK_STRAFE   = VK_FIRSTKEY + 30;
+  VK_LSTRAFE  = VK_FIRSTKEY + 31;
+  VK_RSTRAFE  = VK_FIRSTKEY + 32;
+  VK_PRINTSCR = VK_FIRSTKEY + 33;
+  VK_LASTKEY  = e_MaxKbdKeys + e_MaxJoys*e_MaxJoyKeys + e_MaxVirtKeys - 1;
+
   AX_MINUS  = 0;
   AX_PLUS   = 1;
   HAT_LEFT  = 0;
@@ -94,7 +157,8 @@ const
 function  e_InitInput(): Boolean;
 procedure e_ReleaseInput();
 procedure e_ClearInputBuffer();
-function  e_PollInput(): Boolean;
+//function  e_PollInput(): Boolean;
+procedure e_PollJoysticks(); // call this from message loop to update joysticks
 function  e_KeyPressed(Key: Word): Boolean;
 function  e_AnyKeyPressed(): Boolean;
 function  e_GetFirstKeyPressed(): Word;
@@ -127,6 +191,8 @@ const
   JOYA_END = JOYA_BEG + e_MaxJoyAxes*2*e_MaxJoys;
   JOYH_BEG = JOYA_END;
   JOYH_END = JOYH_BEG + e_MaxJoyHats*4*e_MaxJoys;
+  VIRT_BEG = JOYH_END;
+  VIRT_END = VIRT_BEG + e_MaxVirtKeys;
 
 type
   TJoystick = record
@@ -143,6 +209,7 @@ type
 
 var
   KeyBuffer: array [0..e_MaxKbdKeys] of Boolean;
+  VirtBuffer: array [0..e_MaxVirtKeys] of Boolean;
   Joysticks: array of TJoystick = nil;
 
 function OpenJoysticks(): Byte;
@@ -161,7 +228,7 @@ begin
     begin
       Inc(c);
       e_WriteLog('Input: Opened SDL joystick ' + IntToStr(i) + ' (' + SDL_JoystickName(joy) +
-                 ') as joystick ' + IntToStr(c) + ':', MSG_NOTIFY);
+                 ') as joystick ' + IntToStr(c) + ':', TMsgType.Notify);
       SetLength(Joysticks, c);
       with Joysticks[c-1] do
       begin
@@ -173,7 +240,7 @@ begin
         // TODO: find proper solution for this xbox trigger shit
         for j := 0 to Axes do AxisZero[j] := SDL_JoystickGetAxis(joy, j);
         e_WriteLog('       ' + IntToStr(Axes) + ' axes, ' + IntToStr(Buttons) + ' buttons, ' +
-                   IntToStr(Hats) + ' hats.', MSG_NOTIFY);
+                   IntToStr(Hats) + ' hats.', TMsgType.Notify);
       end;
     end;
   end;
@@ -197,22 +264,27 @@ var
   i: Integer;
 begin
   for i := 0 to High(KeyBuffer) do KeyBuffer[i] := False;
+  for i := 0 to High(VirtBuffer) do VirtBuffer[i] := False;
 end;
 
 
 procedure e_KeyUpDown (key: Word; down: Boolean);
 begin
-  if (key > 0) and (key < Length(KeyBuffer)) then KeyBuffer[key] := down;
+  if (key > 0) and (key < Length(KeyBuffer)) then KeyBuffer[key] := down
+  else if (key >= VIRT_BEG) and (key < VIRT_END) then VirtBuffer[key - VIRT_BEG] := down
 end;
 
 
 function PollKeyboard(): Boolean;
+{
 var
   Keys: PByte;
   NKeys: Integer;
   i: NativeUInt;
+}
 begin
   Result := False;
+  {
   Keys := SDL_GetKeyboardState(@NKeys);
   if (Keys = nil) or (NKeys < 1) then Exit;
   for i := 0 to NKeys do
@@ -220,23 +292,23 @@ begin
     if ((PByte(NativeUInt(Keys) + i)^) <> 0) then KeyBuffer[i] := false;
   end;
   for i := NKeys to High(KeyBuffer) do KeyBuffer[i] := False;
+  }
 end;
 
-function PollJoysticks(): Boolean;
+procedure e_PollJoysticks();
 var
   i, j: Word;
   hat: Byte;
 begin
-  Result := False;
+  //Result := False;
   if (Joysticks = nil) or (e_JoysticksAvailable = 0) then Exit;
   SDL_JoystickUpdate();
   for j := Low(Joysticks) to High(Joysticks) do
+  begin
     with Joysticks[j] do
     begin
-      for i := 0 to Buttons do
-        ButtBuf[i] := SDL_JoystickGetButton(Handle, i) <> 0;
-      for i := 0 to Axes do
-        AxisBuf[i] := SDL_JoystickGetAxis(Handle, i);
+      for i := 0 to Buttons do ButtBuf[i] := SDL_JoystickGetButton(Handle, i) <> 0;
+      for i := 0 to Axes do AxisBuf[i] := SDL_JoystickGetAxis(Handle, i);
       for i := 0 to Hats do
       begin
         hat := SDL_JoystickGetHat(Handle, i);
@@ -246,6 +318,7 @@ begin
         HatBuf[i, HAT_RIGHT] := LongBool(hat and SDL_HAT_RIGHT);
       end;
     end;
+  end;
 end;
 
 procedure GenerateKeyNames();
@@ -280,6 +353,10 @@ begin
       e_KeyNames[k + i*4 + 3] := Format('JOY%d D%dD', [j, i]);
     end;
   end;
+
+  // vitrual keys
+  for i := 0 to e_MaxVirtKeys-1 do
+    e_KeyNames[VIRT_BEG + i] := 'VIRTUAL ' + IntToStr(i);
 end;
 
 function e_InitInput(): Boolean;
@@ -316,17 +393,21 @@ begin
       for d := Low(Joysticks[i].HatBuf[j]) to High(Joysticks[i].HatBuf[j]) do
         Joysticks[i].HatBuf[j, d] := False;
   end;
+  for i := Low(VirtBuffer) to High(VirtBuffer) do
+    VirtBuffer[i] := False;
 end;
 
+{
 function e_PollInput(): Boolean;
 var
   kb, js: Boolean;
 begin
   kb := PollKeyboard();
-  js := PollJoysticks();
+  js := e_PollJoysticks();
 
   Result := kb or js;
 end;
+}
 
 function e_KeyPressed(Key: Word): Boolean;
 var
@@ -381,7 +462,10 @@ begin
       dir := Key mod 4;
       Result := Joysticks[JoyI].HatBuf[Key div 4, dir];
     end;
-  end;
+  end
+
+  else if (Key >= VIRT_BEG) and (Key < VIRT_END) then
+    Result := VirtBuffer[Key - VIRT_BEG]
 end;
 
 procedure e_SetKeyState(key: Word; state: Integer);
@@ -430,7 +514,10 @@ begin
       dir := Key mod 4;
       Joysticks[JoyI].HatBuf[Key div 4, dir] := (state <> 0);
     end;
-  end;
+  end
+
+  else if (Key >= VIRT_BEG) and (Key < VIRT_END) then
+    VirtBuffer[Key - VIRT_BEG] := (state <> 0)
 end;
 
 function e_AnyKeyPressed(): Boolean;
@@ -485,21 +572,21 @@ end;
 function e_JoyButtonToKey(id: Word; btn: Byte): Word;
 begin
   Result := 0;
-  if id >= Length(Joysticks) then Exit;
+  if id >= e_MaxJoys then Exit;
   Result := JOYK_BEG + id*e_MaxJoyBtns + btn;
 end;
 
 function e_JoyAxisToKey(id: Word; ax: Byte; dir: Byte): Word;
 begin
   Result := 0;
-  if id >= Length(Joysticks) then Exit;
+  if id >= e_MaxJoys then Exit;
   Result := JOYA_BEG + id*e_MaxJoyAxes*2 + ax*2 + dir;
 end;
 
 function e_JoyHatToKey(id: Word; hat: Byte; dir: Byte): Word;
 begin
   Result := 0;
-  if id >= Length(Joysticks) then Exit;
+  if id >= e_MaxJoys then Exit;
   Result := JOYH_BEG + id*e_MaxJoyHats*4 + hat*4 + dir;
 end;