From a24c94a161c84075295a55a90b0f0d778cc60081 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Wed, 6 Oct 2021 20:02:09 -0700 Subject: [PATCH] Multitouch Level Panning Add multi-touch gesture support so that the player can scroll the level in the editor (and title screen) by treating a two finger swipe to be equivalent to a middle click drag. Fun quirks found with SDL2's MultiGestureEvent: * They don't begin sending us the event until motion is detected after two fingers have touched the screen; not the moment the second finger touches it. * It spams us with events when it detects any tiny change and a lot of cool details like rotate/pinch deltas, but it never tells us when the multitouch STOPS! The game has to block left clicks while multitouch happens so the user doesn't draw all over their level, so it needs to know when touch has ended. * The workaround is to track the mouse cursor position at the first touch and each delta thereafter; if the deltas stop changing tick to tick, unset the "is touching" variable. --- pkg/uix/canvas.go | 9 +++++---- pkg/uix/canvas_scrolling.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/pkg/uix/canvas.go b/pkg/uix/canvas.go index 193cc25..2cede8c 100644 --- a/pkg/uix/canvas.go +++ b/pkg/uix/canvas.go @@ -95,10 +95,11 @@ type Canvas struct { lastPixel *level.Pixel // We inherit the ui.Widget which manages the width and height. - Scroll render.Point // Scroll offset for which parts of canvas are visible. - scrollDragging bool // Middle-click to pan scroll - scrollStartAt render.Point // Cursor point at beginning of pan - scrollWasAt render.Point // copy of Scroll at beginning of pan + Scroll render.Point // Scroll offset for which parts of canvas are visible. + scrollDragging bool // Middle-click to pan scroll + scrollStartAt render.Point // Cursor point at beginning of pan + scrollWasAt render.Point // copy of Scroll at beginning of pan + scrollLastDelta render.Point // multitouch spam } // NewCanvas initializes a Canvas widget. diff --git a/pkg/uix/canvas_scrolling.go b/pkg/uix/canvas_scrolling.go index 4499c8d..91c486c 100644 --- a/pkg/uix/canvas_scrolling.go +++ b/pkg/uix/canvas_scrolling.go @@ -54,8 +54,41 @@ func (w *Canvas) loopEditorScroll(ev *event.State) error { w.ScrollBy(scrollBy) } + // Multitouch events to pan the level, like middle click on desktop. + if ev.Touching { + // 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 + // fake that no click occurs so we don't nick the drawing. + if !w.scrollDragging { + w.scrollDragging = true + w.scrollStartAt = shmem.Cursor + w.scrollWasAt = w.Scroll + w.scrollLastDelta = render.Point{} + } else { + delta := shmem.Cursor.Compare(w.scrollStartAt) + w.Scroll = w.scrollWasAt + w.Scroll.Subtract(delta) + + // So, SDL2 spams us with events for every subtle movement of 2+ fingers + // on the screen, but we don't know when that STOPS. As a heuristic, it + // seems we can tell by if the delta stops updating. + if !w.scrollLastDelta.IsZero() { + if w.scrollLastDelta == delta { + ev.Touching = false + } + } + + w.scrollLastDelta = delta + } + + // Lift the mouse button. + ev.Button1 = false + + return nil + } + // Middle click of the mouse to pan the level. - // NOTE: returns are below! if keybind.MiddleClick(ev) { if !w.scrollDragging { w.scrollDragging = true