[webgpu] Align touch handoff with WebGL fixes

The WebGPU touch path was still using the older gesture state machine.
It handled 3->2 reseeding, but it did not track whether a pinch was
active and it had no explicit 2->1 single-finger pan handoff.

Carry over the proven transition rules from the WebGL path without
changing desktop input handling: track pinch-active and synthetic
single-pan state, preserve the 3->2 reseed, and continue pan after a
pinch when one finger remains. touchcancel now also clears those touch
states consistently.

Testing:
- source /home/behdad/emsdk/emsdk_env.sh >/dev/null &&
  EM_CACHE=/tmp/emscripten-cache ./util/gpu/web/build.sh
- source /home/behdad/emsdk/emsdk_env.sh >/dev/null &&
  EM_CACHE=/tmp/emscripten-cache ./util/gpu/web/build-webgpu.sh

Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod committed 2026-03-30 18:36:54 -06:00
1 parent 2c15cb9123
commit 00a3475ce0
1 file changed
+54
+54
View File
@@ -522,6 +522,8 @@ static double pinch_dist;
static double pinch_angle; static double pinch_angle;
static double pinch_cx, pinch_cy; static double pinch_cx, pinch_cy;
static double gesture_x, gesture_y; static double gesture_x, gesture_y;
static bool pinch_active;
static bool single_pan_active;
static bool three_finger_active; static bool three_finger_active;
static bool skip_next_pinch_move; static bool skip_next_pinch_move;
@@ -530,11 +532,14 @@ on_touchstart (int type, const EmscriptenTouchEvent *e, void *ud)
{ {
if (e->numTouches == 1) if (e->numTouches == 1)
{ {
single_pan_active = false;
demo_view_motion_func (vu, e->touches[0].targetX, e->touches[0].targetY); demo_view_motion_func (vu, e->touches[0].targetX, e->touches[0].targetY);
demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_PRESS, 0); demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_PRESS, 0);
} }
else if (e->numTouches == 2) else if (e->numTouches == 2)
{ {
pinch_active = true;
single_pan_active = false;
demo_view_cancel_gesture (vu); demo_view_cancel_gesture (vu);
demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0); demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0);
double dx = e->touches[1].targetX - e->touches[0].targetX; double dx = e->touches[1].targetX - e->touches[0].targetX;
@@ -549,6 +554,8 @@ on_touchstart (int type, const EmscriptenTouchEvent *e, void *ud)
} }
else if (e->numTouches >= 3) else if (e->numTouches >= 3)
{ {
pinch_active = false;
single_pan_active = false;
demo_view_cancel_gesture (vu); demo_view_cancel_gesture (vu);
demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0); demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0);
three_finger_active = true; three_finger_active = true;
@@ -568,18 +575,26 @@ on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud)
{ {
if (e->numTouches >= 3 && three_finger_active) if (e->numTouches >= 3 && three_finger_active)
{ {
pinch_active = false;
double cx = (e->touches[0].targetX + e->touches[1].targetX + e->touches[2].targetX) / 3.0; double cx = (e->touches[0].targetX + e->touches[1].targetX + e->touches[2].targetX) / 3.0;
double cy = (e->touches[0].targetY + e->touches[1].targetY + e->touches[2].targetY) / 3.0; double cy = (e->touches[0].targetY + e->touches[1].targetY + e->touches[2].targetY) / 3.0;
gesture_x = cx; gesture_x = cx;
gesture_y = cy; gesture_y = cy;
demo_view_motion_func (vu, cx, cy); demo_view_motion_func (vu, cx, cy);
} }
else if (e->numTouches == 1 && single_pan_active)
{
gesture_x = e->touches[0].targetX;
gesture_y = e->touches[0].targetY;
demo_view_motion_func (vu, gesture_x, gesture_y);
}
else if (e->numTouches == 1) else if (e->numTouches == 1)
{ {
demo_view_motion_func (vu, e->touches[0].targetX, e->touches[0].targetY); demo_view_motion_func (vu, e->touches[0].targetX, e->touches[0].targetY);
} }
else if (e->numTouches == 2) else if (e->numTouches == 2)
{ {
pinch_active = true;
double dx = e->touches[1].targetX - e->touches[0].targetX; double dx = e->touches[1].targetX - e->touches[0].targetX;
double dy = e->touches[1].targetY - e->touches[0].targetY; double dy = e->touches[1].targetY - e->touches[0].targetY;
double dist = sqrt (dx * dx + dy * dy); double dist = sqrt (dx * dx + dy * dy);
@@ -616,6 +631,9 @@ on_touchmove (int type, const EmscriptenTouchEvent *e, void *ud)
static EM_BOOL static EM_BOOL
on_touchend (int type, const EmscriptenTouchEvent *e, void *ud) on_touchend (int type, const EmscriptenTouchEvent *e, void *ud)
{ {
bool pinch_ending = pinch_active && e->numTouches < 2;
bool single_pan_ending = single_pan_active && e->numTouches == 0;
if (three_finger_active && e->numTouches < 3) if (three_finger_active && e->numTouches < 3)
{ {
three_finger_active = false; three_finger_active = false;
@@ -639,10 +657,34 @@ on_touchend (int type, const EmscriptenTouchEvent *e, void *ud)
pinch_cy = release_y; pinch_cy = release_y;
gesture_x = release_x; gesture_x = release_x;
gesture_y = release_y; gesture_y = release_y;
pinch_active = true;
skip_next_pinch_move = true; skip_next_pinch_move = true;
} }
else else
{
pinch_active = false;
skip_next_pinch_move = false; skip_next_pinch_move = false;
}
}
else if (pinch_ending)
{
pinch_active = false;
skip_next_pinch_move = false;
if (e->numTouches == 1)
{
single_pan_active = true;
gesture_x = e->touches[0].targetX;
gesture_y = e->touches[0].targetY;
demo_view_motion_func (vu, gesture_x, gesture_y);
demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_PRESS, 0);
}
}
else if (single_pan_ending)
{
single_pan_active = false;
demo_view_motion_func (vu, gesture_x, gesture_y);
demo_view_cancel_gesture (vu);
demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0);
} }
else else
{ {
@@ -662,12 +704,24 @@ on_touchcancel (int type, const EmscriptenTouchEvent *e, void *ud)
demo_view_cancel_gesture (vu); demo_view_cancel_gesture (vu);
demo_view_mouse_func (vu, BUTTON_RIGHT, ACTION_RELEASE, 0); demo_view_mouse_func (vu, BUTTON_RIGHT, ACTION_RELEASE, 0);
} }
else if (pinch_active)
{
pinch_active = false;
}
else if (single_pan_active)
{
single_pan_active = false;
demo_view_motion_func (vu, gesture_x, gesture_y);
demo_view_cancel_gesture (vu);
demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0);
}
else else
{ {
demo_view_cancel_gesture (vu); demo_view_cancel_gesture (vu);
demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0); demo_view_mouse_func (vu, BUTTON_LEFT, ACTION_RELEASE, 0);
} }
pinch_active = false;
skip_next_pinch_move = false; skip_next_pinch_move = false;
return EM_TRUE; return EM_TRUE;
} }