371 lines
16 KiB
C++
371 lines
16 KiB
C++
// TouchRouter finger ownership: UI, joystick, attack, camera drag / pinch / tap, phases, cancel,
|
|
// and the UI gestures (tap, double tap, drag, long press).
|
|
#include "app/input/touch_router.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace ava2::input;
|
|
|
|
namespace {
|
|
|
|
struct RecordingSink : TouchSink {
|
|
// One UI window: a 200x100 box at (500, 50).
|
|
bool skills=false;
|
|
struct Cast {int slot;float x,y;bool repeat;};std::vector<Cast> casts;
|
|
int SkillAt(float x,float y) override {return skills && x>=500 && x<700 && y>=50 && y<150?7:-1;}
|
|
void Skill(int slot,float x,float y,bool repeat) override {casts.push_back({slot,x,y,repeat});}
|
|
bool IsUiAt(float x, float y) override { return x >= 500 && x < 700 && y >= 50 && y < 150; }
|
|
void UiMouseMove(float x, float y) override { log("move", x, y); }
|
|
void UiMouseDown(float x, float y) override { log("down", x, y); }
|
|
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; }
|
|
void CameraRotate(float dx, float dy) override { rot_x += dx; rot_y += dy; }
|
|
void CameraZoom(float ratio) override { zoom *= ratio; }
|
|
|
|
void log(const char* what, float x, float y) {
|
|
events.push_back(std::string(what) + " " + std::to_string(int(x)) + "," + std::to_string(int(y)));
|
|
}
|
|
std::vector<std::string> events;
|
|
bool joy_active = false, attack = false, take_long_press = true;
|
|
int attack_calls = 0;
|
|
float joy_x = 0, joy_y = 0, rot_x = 0, rot_y = 0, zoom = 1;
|
|
};
|
|
|
|
void expect(bool ok, const char* what) {
|
|
if (!ok) throw std::runtime_error(what);
|
|
}
|
|
bool near(float a, float b) { return std::fabs(a - b) < 0.01f; }
|
|
|
|
TouchLayout layout() {
|
|
TouchLayout l;
|
|
l.screen_w = 1280;
|
|
l.screen_h = 720;
|
|
l.joy_x = 0; l.joy_y = 360; l.joy_w = 500; l.joy_h = 360;
|
|
l.joy_radius = 100;
|
|
l.joy_dead_zone = 0.2f;
|
|
l.attack_x = 1180; l.attack_y = 620; l.attack_r = 80;
|
|
l.tap_slop = 10;
|
|
l.tap_ms = 300;
|
|
l.double_tap_ms = 300;
|
|
l.double_tap_slop = 30;
|
|
l.long_press_ms = 450;
|
|
return l;
|
|
}
|
|
|
|
void ui_outside_game() {
|
|
RecordingSink sink;
|
|
TouchRouter r;
|
|
r.SetSink(&sink);
|
|
r.SetLayout(layout());
|
|
// Not in game: a finger anywhere, even on the joystick area, is the UI pointer.
|
|
r.FingerDown(0, 100, 600, 0);
|
|
r.FingerDown(1, 900, 300, 0); // second finger ignored while the first holds the pointer
|
|
r.FingerMove(0, 110, 610, 10);
|
|
r.FingerUp(1, 900, 300, 20);
|
|
r.FingerUp(0, 120, 620, 30);
|
|
// The pointer hovers at once; moving past the slop presses the button where the finger landed.
|
|
const std::vector<std::string> want{"move 100,600", "move 100,600", "down 100,600", "move 110,610",
|
|
"move 120,620", "up 120,620"};
|
|
expect(sink.events == want, "ui_outside_game events");
|
|
expect(!sink.joy_active && sink.attack_calls == 0, "ui_outside_game world controls");
|
|
expect(r.FingerCount() == 0, "ui_outside_game fingers");
|
|
}
|
|
|
|
void joystick_attack_camera_together() {
|
|
RecordingSink sink;
|
|
TouchRouter r;
|
|
r.SetSink(&sink);
|
|
r.SetLayout(layout());
|
|
r.SetWorldEnabled(true);
|
|
|
|
r.FingerDown(0, 200, 500, 0); // joystick, base at the touch point
|
|
expect(sink.joy_active && near(sink.joy_x, 0) && near(sink.joy_y, 0), "joystick centred");
|
|
r.FingerMove(0, 210, 500, 5); // inside the dead zone
|
|
expect(near(sink.joy_x, 0), "joystick dead zone");
|
|
r.FingerMove(0, 400, 500, 10); // beyond the radius: clamped to full right
|
|
expect(near(sink.joy_x, 1) && near(sink.joy_y, 0), "joystick full right");
|
|
expect(near(r.GetJoystick().knob_x, 300), "joystick knob clamped");
|
|
|
|
r.FingerDown(1, 1180, 620, 12); // attack
|
|
expect(sink.attack && r.IsAttackPressed(), "attack pressed");
|
|
|
|
r.FingerDown(2, 900, 300, 15); // camera
|
|
r.FingerMove(2, 905, 300, 16); // within slop: nothing yet
|
|
expect(near(sink.rot_x, 0), "camera slop");
|
|
r.FingerMove(2, 930, 290, 17);
|
|
expect(near(sink.rot_x, 30) && near(sink.rot_y, -10), "camera drag includes slop");
|
|
r.FingerMove(2, 940, 290, 18);
|
|
expect(near(sink.rot_x, 40), "camera drag delta");
|
|
r.FingerUp(2, 940, 290, 19);
|
|
expect(sink.events.empty(), "dragged camera finger is not a tap");
|
|
|
|
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);
|
|
expect(!sink.joy_active && !r.GetJoystick().active, "joystick released");
|
|
}
|
|
|
|
void tap_pinch_and_ui_in_game() {
|
|
RecordingSink sink;
|
|
TouchRouter r;
|
|
r.SetSink(&sink);
|
|
r.SetLayout(layout());
|
|
r.SetWorldEnabled(true);
|
|
|
|
r.FingerDown(0, 800, 300, 0);
|
|
r.FingerMove(0, 804, 303, 50);
|
|
r.FingerUp(0, 804, 303, 100);
|
|
expect(sink.events.size() == 1 && sink.events[0] == "tap 804,303", "short tap clicks the world");
|
|
|
|
sink.events.clear();
|
|
r.FingerDown(0, 800, 300, 200);
|
|
r.FingerUp(0, 800, 300, 600); // held too long
|
|
expect(sink.events.empty(), "long press is not a tap");
|
|
|
|
r.FingerDown(0, 800, 300, 700);
|
|
r.FingerDown(1, 900, 300, 700); // pinch: distance 100 -> 200
|
|
r.FingerMove(1, 1000, 300, 710);
|
|
expect(near(sink.zoom, 2), "pinch zoom ratio");
|
|
r.FingerUp(1, 1000, 300, 720);
|
|
r.FingerUp(0, 800, 300, 730);
|
|
expect(sink.events.empty() && near(sink.rot_x, 0), "pinch fingers neither tap nor rotate");
|
|
|
|
// UI windows win over world controls, also inside the game.
|
|
r.FingerDown(3, 600, 100, 800);
|
|
r.FingerUp(3, 600, 100, 810);
|
|
const std::vector<std::string> want{"move 600,100", "move 600,100", "down 600,100", "up 600,100"};
|
|
expect(sink.events == want, "ui click in game");
|
|
}
|
|
|
|
void phase_change_and_cancel() {
|
|
RecordingSink sink;
|
|
TouchRouter r;
|
|
r.SetSink(&sink);
|
|
r.SetLayout(layout());
|
|
r.SetWorldEnabled(true);
|
|
r.FingerDown(0, 200, 500, 0);
|
|
r.FingerDown(1, 1180, 620, 0);
|
|
r.FingerDown(2, 600, 100, 0); // UI
|
|
r.FingerMove(2, 620, 100, 1); // dragging: the button is down
|
|
r.SetWorldEnabled(false); // leaving the game drops world fingers, keeps the UI finger
|
|
expect(!sink.joy_active && !sink.attack && r.FingerCount() == 1, "phase change releases world fingers");
|
|
r.FingerMove(0, 300, 500, 5); // a dropped finger is ignored
|
|
expect(!sink.joy_active, "dropped finger ignored");
|
|
sink.events.clear();
|
|
r.CancelAll();
|
|
expect(sink.events.size() == 1 && sink.events[0] == "up 620,100", "cancel releases the UI button");
|
|
expect(r.FingerCount() == 0, "cancel clears fingers");
|
|
|
|
r.SetWorldEnabled(true);
|
|
sink.events.clear();
|
|
r.FingerDown(5, 800, 300, 0);
|
|
r.CancelAll();
|
|
expect(sink.events.empty(), "cancelled camera finger does not tap");
|
|
|
|
// A UI finger that never pressed the button releases nothing.
|
|
r.FingerDown(6, 600, 100, 0);
|
|
sink.events.clear();
|
|
r.CancelAll();
|
|
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() {
|
|
RecordingSink sink;
|
|
TouchRouter r;
|
|
r.SetSink(&sink);
|
|
r.SetLayout(layout());
|
|
r.SetWorldEnabled(true);
|
|
using V = std::vector<std::string>;
|
|
|
|
// Double tap: the second tap is down + double click + up (the original mouse sequence).
|
|
r.FingerDown(0, 600, 100, 1000);
|
|
r.Update(1050);
|
|
r.FingerUp(0, 602, 101, 1080);
|
|
r.FingerDown(0, 610, 105, 1250);
|
|
r.FingerUp(0, 610, 105, 1300);
|
|
expect(sink.events == V{"move 600,100", "move 600,100", "down 600,100", "up 600,100",
|
|
"move 610,105", "move 610,105", "down 610,105", "dbl 610,105", "up 610,105"},
|
|
"double tap");
|
|
|
|
// A third tap starts a new pair.
|
|
sink.events.clear();
|
|
r.FingerDown(0, 610, 105, 1400);
|
|
r.FingerUp(0, 610, 105, 1450);
|
|
expect(sink.events == V{"move 610,105", "move 610,105", "down 610,105", "up 610,105"}, "third tap is single");
|
|
|
|
// Too late or too far: two single clicks.
|
|
sink.events.clear();
|
|
r.FingerDown(0, 620, 100, 1900); // 450 ms after the last tap
|
|
r.FingerUp(0, 620, 100, 1950);
|
|
r.FingerDown(0, 680, 100, 2000); // 60 px away
|
|
r.FingerUp(0, 680, 100, 2050);
|
|
expect(std::count(sink.events.begin(), sink.events.end(), std::string("dbl 620,100")) == 0 &&
|
|
std::count(sink.events.begin(), sink.events.end(), std::string("dbl 680,100")) == 0 && sink.events.size() == 8,
|
|
"late or distant taps do not pair");
|
|
|
|
// Long press taken by the host: nothing else happens, not even a button release.
|
|
sink.events.clear();
|
|
r.FingerDown(0, 600, 100, 3000);
|
|
r.Update(3400);
|
|
expect(sink.events == V{"move 600,100"}, "no long press before long_press_ms");
|
|
r.FingerMove(0, 603, 100, 3420);
|
|
r.Update(3460);
|
|
r.FingerMove(0, 660, 100, 3500);
|
|
r.FingerUp(0, 660, 100, 3600);
|
|
expect(sink.events == V{"move 600,100", "long 603,100"}, "taken long press swallows the touch");
|
|
|
|
// Long press the host declines: the button goes down and the touch continues as a press.
|
|
sink.events.clear();
|
|
sink.take_long_press = false;
|
|
r.FingerDown(0, 600, 100, 4000);
|
|
r.Update(4500);
|
|
r.FingerMove(0, 640, 100, 4600);
|
|
r.FingerUp(0, 640, 100, 4700);
|
|
expect(sink.events == V{"move 600,100", "long 600,100", "move 600,100", "down 600,100", "move 640,100",
|
|
"move 640,100", "up 640,100"},
|
|
"declined long press becomes a press");
|
|
|
|
// A drag never becomes a long press.
|
|
sink.events.clear();
|
|
r.FingerDown(0, 600, 100, 5000);
|
|
r.FingerMove(0, 650, 100, 5100);
|
|
r.Update(5600);
|
|
r.FingerUp(0, 650, 100, 5700);
|
|
expect(sink.events == V{"move 600,100", "move 600,100", "down 600,100", "move 650,100", "move 650,100",
|
|
"up 650,100"},
|
|
"drag");
|
|
}
|
|
|
|
void floating_edges_and_skills() {
|
|
RecordingSink sink;TouchRouter r;r.SetSink(&sink);r.SetLayout(layout());r.SetWorldEnabled(true);
|
|
r.FingerDown(0,1,719,1000);
|
|
expect(near(sink.joy_x,0) && near(sink.joy_y,0),"edge landing moved before dragging");
|
|
expect(near(r.GetJoystick().base_x,100) && near(r.GetJoystick().base_y,620),"visual base not clamped");
|
|
r.FingerMove(0,101,719,1010);expect(near(sink.joy_x,1) && near(sink.joy_y,0),"edge joystick uses visual origin");
|
|
r.FingerCancel(0,1020);r.FingerDown(1,101,719,1040);
|
|
expect(near(sink.joy_x,1) && near(sink.joy_y,0),"reacquire lost true touch origin");r.CancelAll();
|
|
sink.skills=true;r.FingerDown(0,600,100,2000);r.FingerUp(0,600,100,2050);
|
|
expect(sink.casts.size()==1 && !sink.casts[0].repeat && sink.casts[0].x==0,"skill tap not exactly once");
|
|
r.FingerDown(0,600,100,3000);r.FingerDown(1,600,100,3001);expect(r.FingerCount()==1,"duplicate skill fingers");
|
|
r.FingerDown(2,200,500,3002);r.FingerMove(2,300,500,3003);
|
|
r.FingerDown(3,1180,620,3004);r.FingerDown(4,900,300,3005);r.FingerMove(4,940,300,3006);
|
|
r.FingerMove(0,740,100,3010);r.Update(3011);
|
|
expect(sink.casts.size()==2 && near(sink.casts.back().x,140) && sink.attack && near(sink.joy_x,1) && sink.rot_x>0,"skill/attack/joystick/camera ownership");
|
|
r.Update(3100);expect(sink.casts.size()==2,"repeat ignores cadence");r.Update(3211);
|
|
expect(sink.casts.size()==3 && sink.casts.back().repeat,"held skill did not repeat");
|
|
r.FingerUp(0,740,100,3220);r.Update(3500);expect(sink.casts.size()==3,"skill lift recast");r.CancelAll();
|
|
r.FingerDown(0,600,100,4000);r.FingerCancel(0,4050);r.Update(4500);expect(sink.casts.size()==3,"cancelled pending skill cast");
|
|
r.FingerDown(0,600,100,5000);r.SetWorldEnabled(false);r.Update(5500);expect(sink.casts.size()==3,"phase change kept skill held");
|
|
}
|
|
void default_layout() {
|
|
const TouchLayout l = TouchLayout::ForScreen(1600, 720);
|
|
expect(l.joy_x == 0 && l.joy_w > 0 && l.joy_y + l.joy_h <= 720.5f, "default joystick area");
|
|
expect(l.attack_x + l.attack_r <= 1600 && l.attack_y + l.attack_r <= 720, "default attack on screen");
|
|
expect(l.attack_x - l.attack_r > l.joy_x + l.joy_w, "attack right of the joystick area");
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
ui_outside_game();
|
|
joystick_attack_camera_together();
|
|
tap_pinch_and_ui_in_game();
|
|
phase_change_and_cancel();
|
|
ui_gestures();
|
|
system_edges();
|
|
default_layout();
|
|
floating_edges_and_skills();
|
|
} catch (const std::exception& e) {
|
|
std::cerr << "touch_router_test: " << e.what() << '\n';
|
|
return 1;
|
|
}
|
|
std::cout << "touch_router_test: ok\n";
|
|
return 0;
|
|
}
|