Fix touchscreen mode detection

* Touchscreen mode used to be detected based on SDL2 GetNumTouchDevices
  but on a Macbook, the trackpad registers as a touch device - worse,
  GetNumTouchDevices will only start returning 1 the first time some
  devices are touched.
* The result was that on macOS the custom mouse cursor was drawn by
  default, but on the first trackpad touch, would disappear in favor of
  assuming the game is running on a touch screen device (which is not
  the case).
* New method: the render engine has an IsFingerDown boolean which will
  be true as long as at least one finger has registered a FingerDown
  event, but not yet a FingerUp event.
* So as long as one finger is down, the mouse cursor can disappear and
  then it comes back on release. This isn't perfectly ideal for pure
  touch devices (ideally the cursor remains hidden until a mouse
  movement without touch occurs).
master
Noah 2024-04-19 22:01:33 -07:00
parent f2a20808ea
commit 5b3121171e
6 changed files with 32 additions and 5 deletions

View File

@ -215,9 +215,7 @@ func main() {
usercfg.Load()
// Hide the mouse cursor over the window, we draw our own sprite image for it.
if !native.HasTouchscreen(engine) {
engine.ShowCursor(false)
}
engine.ShowCursor(false)
// Set the app window icon.
if engine, ok := game.Engine.(*sdl.Renderer); ok {

View File

@ -25,7 +25,7 @@ var NoCursor = &Cursor{}
// Draw the cursor on screen. NOTE: Does not draw on touchscreen devices.
func Draw(e render.Engine) {
if native.HasTouchscreen(e) {
if native.IsTouchScreenMode {
return
}

View File

@ -141,6 +141,20 @@ func (d *Doodle) Run() error {
}
d.event = ev
// If they have touched a touch screen, set the touchscreen mode boolean. This
// will hide the mouse cursor until they let up off the screen.
if !native.IsTouchScreenMode {
if ev.IsFingerDown {
log.Debug("TouchScreenMode ON!")
native.IsTouchScreenMode = true
}
} else {
if !ev.IsFingerDown {
log.Debug("TouchScreenMode OFF")
native.IsTouchScreenMode = false
}
}
// Always have an accurate idea of the window size.
if ev.WindowResized {
d.width, d.height = d.Engine.WindowSize()

View File

@ -20,6 +20,9 @@ import (
// not for JavaScript/WASM yet.
// HasTouchscreen checks if the device has at least one SDL_GetNumTouchDevices.
//
// Note: SDL2 GetNumTouchDevices will sometimes return 0 until a touch device is actually touched. On Macbooks,
// the trackpad counts as a touch device and on first touch, HasTouchscreen may begin returning true.
func HasTouchscreen(e render.Engine) bool {
if _, ok := e.(*sdl.Renderer); ok {
return sdl2.GetNumTouchDevices() > 0

View File

@ -0,0 +1,12 @@
package native
// Common code to handle basic touch screen detection.
/*
IsTouchScreenMode is set to true when the user has touched the screen, and false when the mouse is moved.
This informs the game's control scheme: if the user has a touch screen and they've touched it,
the custom mouse cursor is hidden (as it becomes distracting) and if they then move a real mouse
over the window, the custom cursor appears again and we assume non-touchscreen mode once more.
*/
var IsTouchScreenMode bool

View File

@ -56,7 +56,7 @@ func (w *Canvas) loopEditorScroll(ev *event.State) error {
}
// Multitouch events to pan the level, like middle click on desktop.
if ev.Touching {
if ev.Touching && ev.TouchNumFingers > 1 {
// Intention: user drags with 2 fingers to scroll the canvas.
// SDL2 will register one finger also as a Button1 mouse click.
// We need to record the "mouse cursor" as start point but then