Mobile devices (Android, iOS, Nintendo Switch) allow to use touch screen where user literally touches the screen to indicate clicking / dragging.
Note
|
Desktop devices actually can have a touch screen too. Steam Deck allows to use a touch screen on a platform that is technically just Linux. Though right now we don’t have any special support for it, we just listen on mouse events from OS. |
The touches on the touch screen are reported exactly like using the left mouse button on desktops. This is exactly what you want in general to have cross-platform support for both mouse and touch.
So to write a cross-platform application that responds to both touches and mouse, just react to:
TCastleUserInterface.Press
with mouse button buttonLeft
. For example override the view method Press
.
TCastleUserInterface.Release
with mouse button buttonLeft
. Again, you can override the view method Release
.
TCastleUserInterface.Motion
when buttonLeft
is in TInputMotion.Pressed
. Again, you can override the view method Motion
.
If you want to additionally handle multi- touch on touch devices, you can also look at
TInputPressRelease.FingerIndex
at each Press
/ Release
event with buttonLeft
. Note that when using actual mouse on desktops, we always report FingerIndex = 0
.
To know currently pressed fingers, look at TCastleContainer.Touches
.
See example in examples/mobile/drawing_toy for a demo of using multi-touch.
If you want to conditionally behave differently on devices with touch screen, you can always check ApplicationProperties.TouchDevice
. But in usual cases, you shouldn’t really need it.
Note
|
For easy testing, the ApplicationProperties.TouchDevice property is settable, so you can set this property to true even on desktops, to test your "touch device mode" in a desktop application.
|
Note
|
Some devices with touch screen, like Android and Nintendo Switch, allow to plug external keyboard and mouse. We don’t support it now in any special way, but we will in the future, and then user will be able to use normal mouse behavior in your mobile game. It also means that inferring too much from ApplicationProperties.TouchDevice being true isn’t wise — the device may have a touch screen, but user may be using external keyboard / mouse.
|
Remember that some things are not possible with a touch screen. Namely:
You will never have mouse press with button different than buttonLeft
You will never observe motion when no mouse button is pressed, of course. Since we can only observe dragging when user is touching the screen. This means that e.g. mouse look cannot work the same — while it could observe motion when dragging, this changes user experience and ultimately you just need different controls for 3D navigation on mobile. We provide TCastleTouchNavigation
for this.
To improve this documentation just edit the source of this page in AsciiDoctor (simple wiki-like syntax) and create a pull request to Castle Game Engine WWW (cge-www) repository.
Copyright Michalis Kamburelis and Castle Game Engine Contributors.
This webpage is also open-source and we welcome pull requests to improve it.
We use cookies for analytics. See our privacy policy.