Skip to content

Touch vs Mouse

John Galvin edited this page Oct 27, 2024 · 11 revisions

A Touch Event and a Mouse Event are not same event.


What is the difference?

This is one of the hardest things to grasp when moving from a desktop development to mobile, is the understanding that there is no such thing as a "mouse" in the mobile development world. There are only touch events and unlike mouse events, touch events do not maintain state.

And that's it in a nutshell...

Take note:

  • There is no concept of a Left button events (i.e. Click, Down, Up etc)
  • There is no concept of a Right button events (i.e. Click, Down, Up etc)
  • There is no concept of Mouse Location Events (Hover etc)

So how do we use Touch Events?

The good news is, a single touch event, olc::GetTouch() , can be likened to a olc::GetMouse(0) which means your simple fire and forget code will work the very same.

For example:

  • olc::GetTouch().bPressed can be executed the same as olc::GetMouse(0).bPressed
  • olc::GetTouch().bHeld can be executed the same as olc::GetMouse(0).bHeld
  • olc::GetTouch().bReleased can be executed the same as olc::GetMouse(0).bReleased

However that is where the likeness ends.

To use Touch Events in the OLOC PGE 2.0 Mobile simply execute olc::GetTouch() or olc::GetTouchPos() commands. You can also select which touch by passing the index of that touch, i.e. olc::GetTouch(1) or olc::GetTouchPos(1) .

Always keep in mind that touch events do not maintain state.

For Example

  • If a user places 1 finger on the screen:
    • The OS (Android/iOS) will intrepid this as Touch Event 0 or olc::GetTouch() / olc::GetTouch(0)
  • If the user then places a second finger on the screen
    • The OS (Android/iOS) will intrepid this as Touch Event 1 or olc::GetTouch(1)
  • If the user then places a third finger on the screen
    • The OS (Android/iOS) will intrepid this as Touch Event 2 or olc::GetTouch(2)
  • And all is happy with the world until the user raises any finger
  • The OS will then re-intrepid the touch events, and work out what is the most likely the new Touch Event 0 and Touch Event 1
  • This means you can never rely on the state of your touch events and must code them without state
  • i.e. Touch Event 0, could now become Touch Event 1 or Touch Event 2 becomes 1, there is no state

How do you develop for Touch Events?

The best place to start is to download and play with the Pan and Zoom Demo

This demo will give you a good oversight of how touch differs from mouse events and the power touch can bring to your game.

Panning example:

// For panning, we need to capture the screen location when the user starts to pan...
if (GetTouch(1).bPressed && GetTouch().bHeld)
{
    fStartPanX = fMouseX;
    fStartPanY = fMouseY;

    // Let get out zoom params ready
    m_StartTime = std::chrono::system_clock::now();
    m_CurrentTime = m_StartTime;
    fStartDistance = GetDistanceBetweenPoints(GetTouchPos(), GetTouchPos(1));
    fCurrentDistance = fStartDistance;
    fChangeDistance = 0.0f;
    fLastDistance = 0.0f;
}
Clone this wiki locally