Mobile devices (Android, iOS, Nintendo Switch) allow to use touch screen where user literally touches the screen to indicate clicking / dragging.
|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
TCastleUserInterface.Release with mouse button
buttonLeft. Again, you can override the view method
buttonLeft is in
TInputMotion.Pressed. Again, you can override the view method
If you want to additionally handle multi- touch on touch devices, you can also look at
TInputPressRelease.FingerIndex at each
Release event with
buttonLeft. Note that when using actual mouse on desktops, we always report
FingerIndex = 0.
To know currently pressed fingers, look at
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.
For easy testing, the
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
Remember that some things are not possible with a touch screen. Namely:
You will never have mouse press with button different than
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.