-
Notifications
You must be signed in to change notification settings - Fork 4
Touch vs Mouse
A Touch Event and a Mouse Event are not same event.
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...
- 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)
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.
-
olc::GetTouch().bPressed
can be executed the same asolc::GetMouse(0).bPressed
-
olc::GetTouch().bHeld
can be executed the same asolc::GetMouse(0).bHeld
-
olc::GetTouch().bReleased
can be executed the same asolc::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.
- 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)
- The OS (Android/iOS) will intrepid this as Touch Event 0 or
- 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)
- The OS (Android/iOS) will intrepid this as Touch Event 1 or
- 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)
- The OS (Android/iOS) will intrepid this as Touch Event 2 or
- 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
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.
// 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;
}
- For Visual Studio All In One Android and iOS (Windows) Project Template: OLC Pixel Game Engine Mobile 2.2.8 Visual Studio for Android and iOS
- For Visual Studio Android Only (Windows) Use this project: OLC Pixel Game Engine Mobile 2.2.8 for Android Visual Studio
- For Android Studio (Windows/Linux/MAC) Use this project: OLC Pixel Game Engine Mobile 2.2.8 for Android Studio
- For Xcode (MAC) Use this project: OLC Pixel Game Engine Mobile 2.2.8 for Xcode