Mobile touch: system edges, per-finger cancel, long-press sort

Interaction ideas from jhaizhou-ops/d2r-mobile (MIT), no code taken:
- world fingers in the bottom safe inset (gesture bar) are ignored
- SDL_EVENT_FINGER_CANCELED cancels one finger; a cancelled joystick
  keeps its vector for 300 ms and a finger near the old base re-acquires it
- attack taps shorter than a frame release on the next Update
- cancelled UI touches and world taps park the pointer (no hover residue)
- long press on an empty inventory cell offers the original sort

Desktop ctest 59/59; sort sheet verified on the device.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-13 11:42:44 +09:00
co-authored by Claude Opus 5.5
parent c132891aef
commit ae713a0772
11 changed files with 320 additions and 30 deletions
+76 -1
View File
@@ -21,6 +21,7 @@ struct RecordingSink : TouchSink {
void UiMouseUp(float x, float y) override { log("up", x, y); }
void UiDoubleClick(float x, float y) override { log("dbl", x, y); }
bool UiLongPress(float x, float y) override { log("long", x, y); return take_long_press; }
void UiPointerIdle() override { events.push_back("idle"); }
void WorldTap(float x, float y) override { log("tap", x, y); }
void Joystick(bool active, float x, float y) override { joy_active = active; joy_x = x; joy_y = y; }
void Attack(bool pressed) override { attack = pressed; ++attack_calls; }
@@ -106,6 +107,7 @@ void joystick_attack_camera_together() {
r.FingerMove(0, 200, 400, 20); // joystick straight up
expect(near(sink.joy_x, 0) && near(sink.joy_y, -1), "joystick up");
r.Update(22); // the attack key has been seen by a frame
r.FingerUp(1, 1180, 620, 25);
expect(!sink.attack, "attack released");
r.FingerUp(0, 200, 400, 30);
@@ -173,7 +175,79 @@ void phase_change_and_cancel() {
r.FingerDown(6, 600, 100, 0);
sink.events.clear();
r.CancelAll();
expect(sink.events.empty(), "cancelled pending UI finger sends nothing");
expect(sink.events == std::vector<std::string>{"idle"}, "cancelled pending UI finger only parks the pointer");
}
// Ideas from jhaizhou-ops/d2r-mobile: home-gesture strip, joystick re-acquire after a system
// cancel, attack tap held for one frame.
void system_edges() {
RecordingSink sink;
TouchRouter r;
r.SetSink(&sink);
TouchLayout l = layout();
l.safe_bottom = 30;
l.joy_reacquire_ms = 300;
r.SetLayout(l);
r.SetWorldEnabled(true);
// Bottom strip: no world tap, no camera drag.
r.FingerDown(0, 800, 700, 0);
r.FingerMove(0, 900, 650, 10);
r.FingerUp(0, 900, 650, 20);
expect(sink.events.empty() && near(sink.rot_x, 0), "bottom strip ignored");
r.FingerDown(0, 800, 680, 30); // just above the strip: a world tap
r.FingerUp(0, 800, 680, 40);
expect(sink.events == std::vector<std::string>{"tap 800,680"}, "above the strip taps");
// System cancel of the joystick finger: the stick keeps going, a finger nearby takes over.
sink.events.clear();
r.FingerDown(1, 200, 500, 100);
r.FingerMove(1, 300, 500, 110);
expect(near(sink.joy_x, 1), "joystick right");
r.FingerCancel(1, 120);
r.Update(200);
expect(sink.joy_active && near(sink.joy_x, 1) && r.GetJoystick().active, "cancelled joystick keeps its vector");
r.FingerDown(2, 230, 420, 250); // near the old base (200, 500), not a new stick
expect(near(r.GetJoystick().base_x, 200) && near(r.GetJoystick().base_y, 500), "re-acquired with the old base");
r.FingerMove(2, 200, 400, 260);
expect(near(sink.joy_x, 0) && near(sink.joy_y, -1), "re-acquired stick steers");
r.FingerUp(2, 200, 400, 270);
expect(!sink.joy_active, "re-acquired stick releases");
// Nobody comes back: the stick lets go after joy_reacquire_ms.
r.FingerDown(1, 200, 500, 1000);
r.FingerMove(1, 300, 500, 1010);
r.FingerCancel(1, 1020);
r.Update(1300);
expect(sink.joy_active, "orphan stick within the window");
r.Update(1330);
expect(!sink.joy_active && !r.GetJoystick().active, "orphan stick released");
// A late finger far away starts a new stick (or camera) as usual.
r.FingerDown(3, 450, 650, 1400);
expect(near(r.GetJoystick().base_x, 450), "late finger is a new stick");
r.FingerUp(3, 450, 650, 1410);
// Other cancelled fingers do nothing else.
sink.events.clear();
r.FingerDown(4, 800, 300, 1500);
r.FingerCancel(4, 1510);
expect(sink.events.empty() && r.FingerCount() == 0, "cancelled camera finger");
// Attack tap faster than a frame: released on the next Update, not with the lift.
sink.attack_calls = 0;
r.FingerDown(5, 1180, 620, 2000);
r.FingerUp(5, 1180, 620, 2005);
expect(sink.attack && r.IsAttackPressed(), "quick attack tap still held");
r.Update(2016);
expect(!sink.attack && !r.IsAttackPressed() && sink.attack_calls == 2, "quick attack released next frame");
// A second tap before that frame keeps the key down without another press.
r.FingerDown(5, 1180, 620, 2100);
r.FingerUp(5, 1180, 620, 2102);
r.FingerDown(6, 1180, 620, 2104);
expect(sink.attack && sink.attack_calls == 3, "re-tap keeps the key down");
r.Update(2110);
r.FingerUp(6, 1180, 620, 2200);
expect(!sink.attack && sink.attack_calls == 4, "held attack releases with the lift");
}
void ui_gestures() {
@@ -259,6 +333,7 @@ int main() {
tap_pinch_and_ui_in_game();
phase_change_and_cancel();
ui_gestures();
system_edges();
default_layout();
} catch (const std::exception& e) {
std::cerr << "touch_router_test: " << e.what() << '\n';