LCOV - code coverage report
Current view: top level - source/ps - TouchInput.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 11 95 11.6 %
Date: 2026-12-03 01:19:39 Functions: 3 9 33.3 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "TouchInput.h"
      21             : 
      22             : #include "graphics/Camera.h"
      23             : #include "graphics/GameView.h"
      24             : #include "lib/debug.h"
      25             : #include "lib/external_libraries/libsdl.h"
      26             : #include "lib/sysdep/os.h"
      27             : #include "lib/timer.h"
      28             : #include "maths/Matrix3D.h"
      29             : #include "ps/Game.h"
      30             : #include "ps/VideoMode.h"
      31             : 
      32             : #include <SDL_events.h>
      33             : #include <SDL_mouse.h>
      34             : #include <cinttypes>
      35             : 
      36             : // When emulation is enabled:
      37             : // Left-click to put finger 0 down.
      38             : // Then left-click-and-drag to move finger 0.
      39             : // Then left-click to put finger 0 up.
      40             : // Same with right-click for finger 1.
      41             : #define EMULATE_FINGERS_WITH_MOUSE 0
      42             : 
      43             : // NOTE: All this code is currently just a basic prototype for testing;
      44             : // it might need significant redesigning for proper usage.
      45             : 
      46           1 : CTouchInput::CTouchInput() :
      47           7 :     m_State(STATE_INACTIVE)
      48             : {
      49           3 :     for (size_t i = 0; i < MAX_FINGERS; ++i)
      50           2 :         m_Down[i] = false;
      51             : 
      52           3 :     for (size_t i = 0; i < MAX_MOUSE; ++i)
      53           2 :         m_MouseEmulateState[i] = MOUSE_INACTIVE;
      54           1 : }
      55             : 
      56           1 : CTouchInput::~CTouchInput()
      57             : {
      58           1 : }
      59             : 
      60           0 : bool CTouchInput::IsEnabled()
      61             : {
      62             : #if OS_ANDROID || EMULATE_FINGERS_WITH_MOUSE
      63             :     return true;
      64             : #else
      65           0 :     return false;
      66             : #endif
      67             : }
      68             : 
      69           0 : void CTouchInput::OnFingerDown(int id, int x, int y)
      70             : {
      71           0 :     debug_printf("finger down %d %d %d; state %d\n", id, x, y, m_State);
      72           0 :     m_Down[id] = true;
      73           0 :     m_Prev[id] = m_Pos[id] = CVector2D(x, y);
      74             : 
      75           0 :     if (m_State == STATE_INACTIVE && id == 0)
      76             :     {
      77           0 :         m_State = STATE_FIRST_TOUCH;
      78           0 :         m_FirstTouchTime = timer_Time();
      79           0 :         m_FirstTouchPos = CVector2D(x, y);
      80             :     }
      81           0 :     else if ((m_State == STATE_FIRST_TOUCH || m_State == STATE_PANNING) && id == 1)
      82             :     {
      83           0 :         m_State = STATE_ZOOMING;
      84             :     }
      85           0 : }
      86             : 
      87           0 : void CTouchInput::OnFingerUp(int id, int x, int y)
      88             : {
      89           0 :     debug_printf("finger up %d %d %d; state %d\n", id, x, y, m_State);
      90           0 :     m_Down[id] = false;
      91           0 :     m_Pos[id] = CVector2D(x, y);
      92             : 
      93           0 :     if (m_State == STATE_FIRST_TOUCH && id == 0 && timer_Time() < m_FirstTouchTime + 0.5)
      94             :     {
      95           0 :         m_State = STATE_INACTIVE;
      96             : 
      97           0 :         SDL_Event ev;
      98           0 :         ev.button.button = SDL_BUTTON_LEFT;
      99           0 :         ev.button.x = m_Pos[0].X;
     100           0 :         ev.button.y = m_Pos[0].Y;
     101             : 
     102           0 :         ev.type = SDL_MOUSEBUTTONDOWN;
     103           0 :         ev.button.state = SDL_PRESSED;
     104           0 :         SDL_PushEvent(&ev);
     105             : 
     106           0 :         ev.type = SDL_MOUSEBUTTONUP;
     107           0 :         ev.button.state = SDL_RELEASED;
     108           0 :         SDL_PushEvent(&ev);
     109             :     }
     110           0 :     else if (m_State == STATE_ZOOMING && id == 1)
     111             :     {
     112           0 :         m_State = STATE_PANNING;
     113             :     }
     114             :     else
     115             :     {
     116           0 :         m_State = STATE_INACTIVE;
     117             :     }
     118           0 : }
     119             : 
     120           0 : void CTouchInput::OnFingerMotion(int id, int x, int y)
     121             : {
     122           0 :     debug_printf("finger motion %d %d %d; state %d\n", id, x, y, m_State);
     123             : 
     124           0 :     CVector2D pos(x, y);
     125             : 
     126           0 :     m_Prev[id] = m_Pos[id];
     127           0 :     m_Pos[id] = pos;
     128             : 
     129           0 :     if (m_State == STATE_FIRST_TOUCH && id == 0)
     130             :     {
     131           0 :         if ((pos - m_FirstTouchPos).Length() > 16)
     132             :         {
     133           0 :             m_State = STATE_PANNING;
     134             : 
     135           0 :             const CCamera& camera{g_Game->GetView()->GetCamera()};
     136           0 :             m_PanFocus = camera.GetWorldCoordinates(m_FirstTouchPos.X, m_FirstTouchPos.Y, true);
     137           0 :             m_PanDist = (m_PanFocus - camera.GetOrientation().GetTranslation()).Y;
     138             :         }
     139             :     }
     140             : 
     141           0 :     if (m_State == STATE_PANNING && id == 0)
     142             :     {
     143           0 :         CCamera camera{g_Game->GetView()->GetCamera()};
     144           0 :         auto [origin, dir] = camera.BuildCameraRay(x, y);
     145           0 :         dir *= m_PanDist / dir.Y;
     146           0 :         camera.GetOrientation().Translate(m_PanFocus - dir - origin);
     147           0 :         camera.UpdateFrustum();
     148           0 :         g_Game->GetView()->SetCamera(camera);
     149           0 :     }
     150             : 
     151           0 :     if (m_State == STATE_ZOOMING && id == 1)
     152             :     {
     153           0 :         float oldDist = (m_Prev[id] - m_Pos[1 - id]).Length();
     154           0 :         float newDist = (m_Pos[id] - m_Pos[1 - id]).Length();
     155           0 :         float zoomDist = (newDist - oldDist) * -0.005f * m_PanDist;
     156             : 
     157           0 :         CCamera camera{g_Game->GetView()->GetCamera()};
     158           0 :         CVector3D dir{camera.BuildCameraRay(m_Pos[0].X, m_Pos[0].Y).direction};
     159           0 :         dir *= zoomDist;
     160           0 :         camera.GetOrientation().Translate(dir);
     161           0 :         camera.UpdateFrustum();
     162           0 :         g_Game->GetView()->SetCamera(camera);
     163             : 
     164           0 :         m_PanFocus = camera.GetWorldCoordinates(m_Pos[0].X, m_Pos[0].Y, true);
     165           0 :         m_PanDist = (m_PanFocus - camera.GetOrientation().GetTranslation()).Y;
     166           0 :     }
     167           0 : }
     168             : 
     169           0 : void CTouchInput::Frame()
     170             : {
     171           0 :     double t = timer_Time();
     172           0 :     if (m_State == STATE_FIRST_TOUCH && t > m_FirstTouchTime + 1.0)
     173             :     {
     174           0 :         m_State = STATE_INACTIVE;
     175             : 
     176           0 :         SDL_Event ev;
     177           0 :         ev.button.button = SDL_BUTTON_RIGHT;
     178           0 :         ev.button.x = m_Pos[0].X;
     179           0 :         ev.button.y = m_Pos[0].Y;
     180             : 
     181           0 :         ev.type = SDL_MOUSEBUTTONDOWN;
     182           0 :         ev.button.state = SDL_PRESSED;
     183           0 :         SDL_PushEvent(&ev);
     184             : 
     185           0 :         ev.type = SDL_MOUSEBUTTONUP;
     186           0 :         ev.button.state = SDL_RELEASED;
     187           0 :         SDL_PushEvent(&ev);
     188             :     }
     189           0 : }
     190             : 
     191           0 : Input::Reaction CTouchInput::HandleEvent(const SDL_Event& ev)
     192             : {
     193           0 :     if (!IsEnabled())
     194           0 :         return Input::Reaction::PASS;
     195             : 
     196             : #if EMULATE_FINGERS_WITH_MOUSE
     197             :     switch(ev.type)
     198             :     {
     199             :     case SDL_MOUSEBUTTONDOWN:
     200             :     {
     201             :         int button;
     202             :         if (ev.button.button == SDL_BUTTON_LEFT)
     203             :             button = 0;
     204             :         else if (ev.button.button == SDL_BUTTON_RIGHT)
     205             :             button = 1;
     206             :         else
     207             :             return IN_PASS;
     208             : 
     209             :         m_MouseEmulateDownPos[button] = CVector2D(ev.button.x, ev.button.y);
     210             :         if (m_MouseEmulateState[button] == MOUSE_INACTIVE)
     211             :         {
     212             :             m_MouseEmulateState[button] = MOUSE_ACTIVATING;
     213             :             OnFingerDown(button, ev.button.x, ev.button.y);
     214             :         }
     215             :         else if (m_MouseEmulateState[button] == MOUSE_ACTIVE_UP)
     216             :         {
     217             :             m_MouseEmulateState[button] = MOUSE_ACTIVE_DOWN;
     218             :         }
     219             :         return IN_HANDLED;
     220             :     }
     221             : 
     222             :     case SDL_MOUSEBUTTONUP:
     223             :     {
     224             :         int button;
     225             :         if (ev.button.button == SDL_BUTTON_LEFT)
     226             :             button = 0;
     227             :         else if (ev.button.button == SDL_BUTTON_RIGHT)
     228             :             button = 1;
     229             :         else
     230             :             return IN_PASS;
     231             : 
     232             :         if (m_MouseEmulateState[button] == MOUSE_ACTIVATING)
     233             :         {
     234             :             m_MouseEmulateState[button] = MOUSE_ACTIVE_UP;
     235             :         }
     236             :         else if (m_MouseEmulateState[button] == MOUSE_ACTIVE_DOWN)
     237             :         {
     238             :             float dist = (m_MouseEmulateDownPos[button] - CVector2D(ev.button.x, ev.button.y)).Length();
     239             :             if (dist <= 2)
     240             :             {
     241             :                 m_MouseEmulateState[button] = MOUSE_INACTIVE;
     242             :                 OnFingerUp(button, ev.button.x, ev.button.y);
     243             :             }
     244             :             else
     245             :             {
     246             :                 m_MouseEmulateState[button] = MOUSE_ACTIVE_UP;
     247             :             }
     248             :         }
     249             :         return IN_HANDLED;
     250             :     }
     251             : 
     252             :     case SDL_MOUSEMOTION:
     253             :     {
     254             :         for (size_t i = 0; i < MAX_MOUSE; ++i)
     255             :         {
     256             :             if (m_MouseEmulateState[i] == MOUSE_ACTIVE_DOWN)
     257             :             {
     258             :                 OnFingerMotion(i, ev.motion.x, ev.motion.y);
     259             :             }
     260             :         }
     261             :         return IN_HANDLED;
     262             :     }
     263             :     }
     264             : #endif
     265             : 
     266             :     switch(ev.type)
     267             :     {
     268             :     case SDL_FINGERDOWN:
     269             :     case SDL_FINGERUP:
     270             :     case SDL_FINGERMOTION:
     271             :     {
     272             :         // Map finger events onto the mouse, for basic testing
     273             :         debug_printf("finger %s tid=%" PRId64 " fid=%" PRId64 " x=%f y=%f dx=%f dy=%f p=%f\n",
     274             :             ev.type == SDL_FINGERDOWN ? "down" :
     275             :             ev.type == SDL_FINGERUP ? "up" :
     276             :             ev.type == SDL_FINGERMOTION ? "motion" : "?",
     277             :             ev.tfinger.touchId, ev.tfinger.fingerId,
     278             :             ev.tfinger.x, ev.tfinger.y, ev.tfinger.dx, ev.tfinger.dy, ev.tfinger.pressure);
     279             : 
     280             :         if (ev.type == SDL_FINGERDOWN)
     281             :             OnFingerDown(ev.tfinger.fingerId, g_VideoMode.GetWindowWidth() * ev.tfinger.x, g_VideoMode.GetWindowHeight() * ev.tfinger.y);
     282             :         else if (ev.type == SDL_FINGERUP)
     283             :             OnFingerUp(ev.tfinger.fingerId, g_VideoMode.GetWindowWidth() * ev.tfinger.x, g_VideoMode.GetWindowHeight() * ev.tfinger.y);
     284             :         else if (ev.type == SDL_FINGERMOTION)
     285             :             OnFingerMotion(ev.tfinger.fingerId, g_VideoMode.GetWindowWidth() * ev.tfinger.x, g_VideoMode.GetWindowHeight() * ev.tfinger.y);
     286             :         return Input::Reaction::HANDLED;
     287             :     }
     288             :     }
     289             : 
     290             :     return Input::Reaction::PASS;
     291             : }
     292             : 
     293             : CTouchInput g_TouchInput;
     294             : 
     295           7 : Input::Reaction touch_input_handler(const SDL_Event& ev)
     296             : {
     297           7 :     return g_TouchInput.HandleEvent(ev);
     298             : }

Generated by: LCOV version 1.16