DEADSOFTWARE

fixed console on android
[d2df-sdl.git] / src / game / g_touch.pas
index 9256fb8e79411f362c9873beeb7dc11114c49086..7cdc3282c1dd25057ed91b4ef4a251c4c3f6f7a9 100644 (file)
@@ -22,9 +22,11 @@ interface
     SDL2;
 
   var
+    g_touch_enabled: Boolean = False;
     g_touch_size: Single = 1.0;
-    g_touch_offset: Single = 50;
+    g_touch_offset: Single = 50.0;
     g_touch_fire: Boolean = True;
+    g_touch_alt: Boolean = False;
 
   procedure g_Touch_Init;
   procedure g_Touch_ShowKeyboard(yes: Boolean);
@@ -35,99 +37,101 @@ implementation
 
   uses
     SysUtils,
-    e_log, e_graphics, e_input, g_options, g_game, g_main, g_weapons, g_console;
+    e_log, e_graphics, e_input, g_options, g_game, g_main, g_gui, g_weapons, g_console, g_window;
 
   var
-    jab: Boolean;
-    enabled: Boolean;
     angleFire: Boolean;
     keyFinger: array [VK_FIRSTKEY..VK_LASTKEY] of Integer;
 
-  procedure GetKeyRect(key: Word; out x, y, w, h: Integer; out founded: Boolean);
+  procedure GetKeyRect(key: Integer; out x, y, w, h: Integer; out founded: Boolean);
     var
       sw, sh, sz: Integer;
       dpi: Single;
+
+    procedure S (xx, yy, ww, hh: Single);
+    begin
+      x := Trunc(xx);
+      y := Trunc(yy);
+      w := Trunc(ww);
+      h := Trunc(hh);
+      founded := true;
+    end;
+
   begin
+    founded := false;
     if SDL_GetDisplayDPI(0, @dpi, nil, nil) <> 0 then
       dpi := 96;
 
-    founded := true;
-    sz := Trunc(g_touch_size * dpi);
-    sw := gScreenWidth; sh := gScreenHeight;
-    if jab then
-    begin
-      w := sz div 2; h := sz div 2;
+    sz := Trunc(g_touch_size * dpi); sw := gScreenWidth; sh := gScreenHeight;
+    x := 0; y := Round(sh * g_touch_offset / 100);
+    w := sz; h := sz;
+
+    if SDL_IsTextInputActive() = SDL_True then
       case key of
-        VK_CONSOLE: begin x := 0; y := 0 end;
-        VK_ESCAPE:  begin x := sw - w - 1; y := 0 end;
-        VK_CHAT:    begin x := sw div 2 - w div 2 - w; y := 0 end;
-        VK_STATUS:  begin x := sw div 2 - w div 2 + 0; y := 0 end;
-        VK_TEAM:    begin x := sw div 2 - w div 2 + w; y := 0 end;
-        VK_PREV:    begin x := 0; y := sh - 4*sz - 1; w := sz end;
-        VK_NEXT:    begin x := sw - w - 1; y := sh - 4*sz - 1; w := sz end;
-      else
-        w := sz; h := sz * 3;
-        case key of
-          VK_LEFT:  begin x := 0; y := sh - h - 1 end;
-          VK_RIGHT: begin x := w; y := sh - h - 1 end;
-        else
-          w := sz; h := sz;
-          case key of
-            VK_UP:   begin x := sw - 2*w - 1; y := sh - 3*h - 1 end;
-            VK_FIRE: begin x := sw - 2*w - 1; y := sh - 2*h - 1 end;
-            VK_DOWN: begin x := sw - 2*w - 1; y := sh - 1*h - 1 end;
-            VK_OPEN: begin x := sw - 1*w - 1; y := sh - 1*h - h div 2 - 1 end;
-            VK_JUMP: begin x := sw - 1*w - 1; y := sh - 2*h - h div 2 - 1 end;
-          else
-            founded := false
-          end
-        end
+        VK_HIDEKBD: S(sw - (sz/2), 0, sz / 2, sz / 2);
+      end
+    else if g_touch_alt then
+      case key of
+        (* top ------- x ------------------------------- y  w ----- h -- *)
+        VK_CONSOLE:  S(0,                                0, sz / 2, sz / 2);
+        VK_ESCAPE:   S(sw - 1*(sz/2) - 1,                0, sz / 2, sz / 2);
+        VK_SHOWKBD:  S(sw - 2*(sz/2) - 1,                0, sz / 2, sz / 2);
+        VK_CHAT:     S(sw / 2 - (sz/2) / 2 - (sz/2) - 1, 0, sz / 2, sz / 2);
+        VK_STATUS:   S(sw / 2 - (sz/2) / 2 - 1,          0, sz / 2, sz / 2);
+        VK_TEAM:     S(sw / 2 - (sz/2) / 2 + (sz/2) - 1, 0, sz / 2, sz / 2);
+        (* left --- x - y -------------- w - h --- *)
+        VK_PREV:  S(0,  sh - 3.0*sz - 1, sz, sz / 2);
+        VK_LEFT:  S(0,  sh - 2.0*sz - 1, sz, sz * 2);
+        VK_RIGHT: S(sz, sh - 2.0*sz - 1, sz, sz * 2);
+        (* right - x ------------ y -------------- w - h -- *)
+        VK_NEXT: S(sw - 1*sz - 1, sh - 3.0*sz - 1, sz, sz / 2);
+        VK_UP:   S(sw - 2*sz - 1, sh - 2.0*sz - 1, sz, sz / 2);
+        VK_FIRE: S(sw - 2*sz - 1, sh - 1.5*sz - 1, sz, sz);
+        VK_DOWN: S(sw - 2*sz - 1, sh - 0.5*sz - 1, sz, sz / 2);
+        VK_JUMP: S(sw - 1*sz - 1, sh - 2.0*sz - 1, sz, sz);
+        VK_OPEN: S(sw - 1*sz - 1, sh - 1.0*sz - 1, sz, sz);
       end
-    end
     else
-    begin
-      x := 0; y := Round(sh * g_touch_offset / 100); w := sz; h := sz;
       case key of
-        VK_LSTRAFE: begin x := 0;             y := y - h div 2; w := w div 2 end;
-        VK_LEFT:    begin x := w div 2;       y := y - h div 2 end;
-        VK_RIGHT:   begin x := w div 2 + 1*w; y := y - h div 2 end;
-        VK_RSTRAFE: begin x := w div 2 + 2*w; y := y - h div 2; w := w div 2 end;
-        VK_UP:      begin x := sw - w - 1;    y := y - h div 2 - h end;
-        VK_FIRE:    begin x := sw - 1*w - 1;  y := y - h div 2 end;
-        VK_DOWN:    begin x := sw - w - 1;    y := y - h div 2 + h end;
-        VK_NEXT:    begin x := sw - 2*w - 1;  y := y - h div 2 - h end;
-        VK_JUMP:    begin x := sw - 2*w - 1;  y := y - h div 2 end;
-        VK_PREV:    begin x := sw - 3*w - 1;  y := y - h div 2 - h end;
-        VK_OPEN:    begin x := sw - 3*w - 1;  y := y - h div 2 end;
-      else
-        x := 0; y := 0; w := sz div 2; h := sz div 2;
-        case key of
-          VK_0:       begin x := sw div 2 - w div 2 - 5*w - 1; y := sh - 1*h - 1 end;
-          VK_1:       begin x := sw div 2 - w div 2 - 4*w - 1; y := sh - 1*h - 1 end;
-          VK_2:       begin x := sw div 2 - w div 2 - 3*w - 1; y := sh - 1*h - 1 end;
-          VK_3:       begin x := sw div 2 - w div 2 - 2*w - 1; y := sh - 1*h - 1 end;
-          VK_4:       begin x := sw div 2 - w div 2 - 1*w - 1; y := sh - 1*h - 1 end;
-          VK_5:       begin x := sw div 2 - w div 2 + 0*w - 1; y := sh - 1*h - 1 end;
-          VK_6:       begin x := sw div 2 - w div 2 + 1*w - 1; y := sh - 1*h - 1 end;
-          VK_7:       begin x := sw div 2 - w div 2 + 2*w - 1; y := sh - 1*h - 1 end;
-          VK_8:       begin x := sw div 2 - w div 2 + 3*w - 1; y := sh - 1*h - 1 end;
-          VK_9:       begin x := sw div 2 - w div 2 + 4*w - 1; y := sh - 1*h - 1 end;
-          VK_A:       begin x := sw div 2 - w div 2 + 5*w - 1; y := sh - 1*h - 1 end;
-          VK_CHAT:    begin x := sw div 2 - w div 2 - 2*w - 1; y := sh - 2*h - 1 end;
-          VK_ESCAPE:  begin x := sw div 2 - w div 2 - 1*w - 1; y := sh - 2*h - 1 end;
-          VK_CONSOLE: begin x := sw div 2 - w div 2 + 0*w - 1; y := sh - 2*h - 1 end;
-          VK_STATUS:  begin x := sw div 2 - w div 2 + 1*w - 1; y := sh - 2*h - 1 end;
-          VK_TEAM:    begin x := sw div 2 - w div 2 + 2*w - 1; y := sh - 2*h - 1 end;
-        else
-          founded := false
-        end
+        (* left ----- x ----- y -------------- w ----- h -- *)
+        VK_ESCAPE:  S(0.0*sz, y - 1*sz - sz/2, sz,     sz / 2);
+        VK_LSTRAFE: S(0.0*sz, y - 0*sz - sz/2, sz / 2, sz);
+        VK_LEFT:    S(0.5*sz, y - 0*sz - sz/2, sz,     sz);
+        VK_RIGHT:   S(1.5*sz, y - 0*sz - sz/2, sz,     sz);
+        VK_RSTRAFE: S(2.5*sz, y - 0*sz - sz/2, sz / 2, sz);
+        (* right - x ------------ y --------------- w - h *)
+        VK_UP:   S(sw - 1*sz - 1, y -  1*sz - sz/2, sz, sz);
+        VK_FIRE: S(sw - 1*sz - 1, y -  0*sz - sz/2, sz, sz);
+        VK_DOWN: S(sw - 1*sz - 1, y - -1*sz - sz/2, sz, sz);
+        VK_NEXT: S(sw - 2*sz - 1, y -  1*sz - sz/2, sz, sz);
+        VK_JUMP: S(sw - 2*sz - 1, y -  0*sz - sz/2, sz, sz);
+        VK_PREV: S(sw - 3*sz - 1, y -  1*sz - sz/2, sz, sz);
+        VK_OPEN: S(sw - 3*sz - 1, y -  0*sz - sz/2, sz, sz);
+        (* bottom ---- x -------------------------- y ---------------- w ----- h -- *)
+        VK_CHAT:     S(sw/2 - sz/4 -  2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
+        VK_CONSOLE:  S(sw/2 - sz/4 -  1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
+        VK_STATUS:   S(sw/2 - sz/4 -  0*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
+        VK_TEAM:     S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
+        VK_SHOWKBD:  S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 2*(sz/2) - 1, sz / 2, sz / 2);
+        VK_0:        S(sw/2 - sz/4 -  5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_1:        S(sw/2 - sz/4 -  4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_2:        S(sw/2 - sz/4 -  3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_3:        S(sw/2 - sz/4 -  2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_4:        S(sw/2 - sz/4 -  1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_5:        S(sw/2 - sz/4 -  0*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_6:        S(sw/2 - sz/4 - -1*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_7:        S(sw/2 - sz/4 - -2*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_8:        S(sw/2 - sz/4 - -3*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_9:        S(sw/2 - sz/4 - -4*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
+        VK_A:        S(sw/2 - sz/4 - -5*(sz/2) - 1, sh - 1*(sz/2) - 1, sz / 2, sz / 2);
       end
-    end
   end;
 
-  function GetKeyName(key: Word): String;
+  function GetKeyName(key: Integer): String;
   begin
     case key of
+      VK_SHOWKBD: result := 'KBD';
+      VK_HIDEKBD: result := 'KBD';
       VK_LEFT:    result := 'LEFT';
       VK_RIGHT:   result := 'RIGHT';
       VK_UP:      result := 'UP';
@@ -168,11 +172,6 @@ implementation
     end
   end;
 
-  procedure DrawRect(x, y, w, h: Integer);
-  begin
-    e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31);
-  end;
-
   function IntersectControl(ctl, xx, yy: Integer): Boolean;
     var
       x, y, w, h: Integer;
@@ -185,14 +184,14 @@ implementation
   procedure g_Touch_Init;
   begin
 {$IFNDEF HEADLESS}
-    enabled := SDL_GetNumTouchDevices() > 0
+    g_touch_enabled := SDL_GetNumTouchDevices() > 0
 {$ENDIF}
   end;
 
   procedure g_Touch_ShowKeyboard(yes: Boolean);
   begin
 {$IFNDEF HEADLESS}
-    if not enabled then
+    if not g_touch_enabled then
       Exit;
 
     if yes then
@@ -205,10 +204,69 @@ implementation
   procedure g_Touch_HandleEvent(const ev: TSDL_TouchFingerEvent);
     var
       x, y, i, finger: Integer;
+
+    procedure KeyUp (finger, i: Integer);
+    begin
+      if g_dbg_input then
+        e_LogWritefln('Input Debug: g_touch.KeyUp, finger=%s, key=%s', [finger, i]);
+
+      keyFinger[i] := 0;
+      e_KeyUpDown(i, False);
+      g_Console_ProcessBind(i, False);
+
+      (* up/down + fire hack *)
+      if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) and angleFire then
+      begin
+        if (i = VK_UP) or (i = VK_DOWN) then
+        begin
+          angleFire := False;
+          keyFinger[VK_FIRE] := 0;
+          e_KeyUpDown(VK_FIRE, False);
+          g_Console_ProcessBind(VK_FIRE, False)
+        end
+      end
+    end;
+
+    procedure KeyDown (finger, i: Integer);
+    begin
+      if g_dbg_input then
+        e_LogWritefln('Input Debug: g_touch.KeyDown, finger=%s, key=%s', [finger, i]);
+
+      keyFinger[i] := finger;
+      e_KeyUpDown(i, True);
+      g_Console_ProcessBind(i, True);
+
+      (* up/down + fire hack *)
+      if g_touch_fire and (gGameSettings.GameType <> GT_NONE) and (g_ActiveWindow = nil) then
+      begin
+        if i = VK_UP then
+        begin
+          angleFire := True;
+          keyFinger[VK_FIRE] := -1;
+          e_KeyUpDown(VK_FIRE, True);
+          g_Console_ProcessBind(VK_FIRE, True)
+        end
+        else if i = VK_DOWN then
+        begin
+          angleFire := True;
+          keyFinger[VK_FIRE] := -1;
+          e_KeyUpDown(VK_FIRE, True);
+          g_Console_ProcessBind(VK_FIRE, True)
+        end
+      end
+    end;
+
+    procedure KeyMotion (finger, i: Integer);
+    begin
+      if keyFinger[i] <> finger then
+      begin
+        KeyUp(finger, i);
+        KeyDown(finger, i)
+      end
+    end;
+
   begin
-    if not enabled then
-      Exit;
-    if SDL_IsTextInputActive() = SDL_True then
+    if not g_touch_enabled then
       Exit;
 
     finger := ev.fingerId + 2;
@@ -220,87 +278,27 @@ implementation
       if IntersectControl(i, x, y) then
       begin
         if ev.type_ = SDL_FINGERUP then
-          keyFinger[i] := 0
+          KeyUp(finger, i)
         else if ev.type_ = SDL_FINGERMOTION then
-          keyFinger[i] := finger
+          KeyMotion(finger, i)
         else if ev.type_ = SDL_FINGERDOWN then
-          begin
-            KeyPress(i); // Menu events
-            keyFinger[i] := finger;
-          end
+          keyDown(finger, i)
       end
       else if keyFinger[i] = finger then
       begin
         if ev.type_ = SDL_FINGERUP then
-          keyFinger[i] := 0
+          KeyUp(finger, i)
         else if ev.type_ = SDL_FINGERMOTION then
-          keyFinger[i] := 0
-      end;
-
-      e_KeyUpDown(i, keyFinger[i] <> 0);
-    end;
-
-    (* emulate up+fire / donw+fire *)
-    if g_touch_fire and (gGameSettings.GameType <> GT_NONE) then
-    begin
-      if keyFinger[VK_UP] <> 0 then
-      begin
-        angleFire := true;
-        keyFinger[VK_FIRE] := keyFinger[VK_UP];
-        e_KeyUpDown(VK_FIRE, true);
-      end
-      else if keyFinger[VK_DOWN] <> 0 then
-      begin
-        angleFire := true;
-        keyFinger[VK_FIRE] := keyFinger[VK_DOWN];
-        e_KeyUpDown(VK_FIRE, true);
-      end
-      else if angleFire then
-      begin
-        angleFire := false;
-        keyFinger[VK_FIRE] := 0;
-        e_KeyUpDown(VK_FIRE, false);
-      end
-    end;
-
-    (* left/right strafe *)
-    if gGameSettings.GameType <> GT_NONE then
-    begin
-      if keyFinger[VK_LSTRAFE] <> 0 then
-      begin
-        keyFinger[VK_LEFT] := finger;
-        keyFinger[VK_RIGHT] := 0;
-        keyFinger[VK_STRAFE] := finger;
-        e_KeyUpDown(VK_LEFT, true);
-        e_KeyUpDown(VK_RIGHT, false);
-        e_KeyUpDown(VK_STRAFE, true);
-      end
-      else if keyFinger[VK_RSTRAFE] <> 0 then
-      begin
-        keyFinger[VK_LEFT] := 0;
-        keyFinger[VK_RIGHT] := finger;
-        keyFinger[VK_STRAFE] := finger;
-        e_KeyUpDown(VK_LEFT, false);
-        e_KeyUpDown(VK_RIGHT, true);
-        e_KeyUpDown(VK_STRAFE, true);
-      end
-      else
-      begin
-        keyFinger[VK_STRAFE] := 0;
-        e_KeyUpDown(VK_STRAFE, false);
+          KeyUp(finger, i)
       end
-    end;
+    end
   end;
 
   procedure g_Touch_Draw;
-    var
-      i, x, y, w, h: Integer;
-      founded: Boolean;
+    var i, x, y, w, h: Integer; founded: Boolean;
   begin
 {$IFNDEF HEADLESS}
-    if not enabled then
-      Exit;
-    if SDL_IsTextInputActive() = SDL_True then
+    if not g_touch_enabled then
       Exit;
 
     for i := VK_FIRSTKEY to VK_LASTKEY do
@@ -308,17 +306,17 @@ implementation
       GetKeyRect(i, x, y, w, h, founded);
       if founded then
       begin
-        DrawRect(x, y, w, h);
-        e_TextureFontPrint(x, y, GetKeyName(i), gStdFont)
-      end;
-    end;
+        e_DrawQuad(x, y, x + w, y + h, 0, 255, 0, 31);
+        e_TextureFontPrintEx(x, y, GetKeyName(i), gStdFont, 255, 255, 255, 1, True)
+      end
+    end
 {$ENDIF}
   end;
 
 initialization
-  conRegVar('touch_enable', @enabled, 'enable/disable virtual buttons', 'draw buttons');
+  conRegVar('touch_enable', @g_touch_enabled, 'enable/disable virtual buttons', 'draw buttons');
   conRegVar('touch_fire', @g_touch_fire, 'enable/disable fire when press virtual up/down', 'fire when press up/down');
   conRegVar('touch_size', @g_touch_size, 0.1, 10, 'size of virtual buttons', 'button size');
   conRegVar('touch_offset', @g_touch_offset, 0, 100, '', '');
-  conRegVar('touch_alt', @jab, 'althernative virtual buttons layout', 'althernative layout');
+  conRegVar('touch_alt', @g_touch_alt, 'althernative virtual buttons layout', 'althernative layout');
 end.