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:
co-authored by
Claude Opus 5.5
parent
c132891aef
commit
ae713a0772
@@ -725,6 +725,7 @@ ava2::app::AppLoop::Frame RunClient(ava2::app::AppLoop* loop, int argc, char** a
|
||||
void UiMouseUp(float x, float y) override { if (wm) ava2::TouchUiMouseUp(*wm, long(x), long(y)); }
|
||||
void UiDoubleClick(float x, float y) override { if (wm) wm->RunMouseLeftButtonDoubleClick(long(x), long(y)); }
|
||||
bool UiLongPress(float x, float y) override { return wm && ava2::TouchUiLongPress(*wm, long(x), long(y)); }
|
||||
void UiPointerIdle() override { if (wm) ava2::TouchUiPointerIdle(*wm); }
|
||||
void WorldTap(float x, float y) override {
|
||||
if (!wm) return;
|
||||
// GameWindow.OnMouseLeftButtonDown sets player.SetMouseState; holding it for one
|
||||
@@ -740,6 +741,8 @@ ava2::app::AppLoop::Frame RunClient(ava2::app::AppLoop* loop, int argc, char** a
|
||||
if (!release_pending || !wm) return;
|
||||
release_pending = false;
|
||||
wm->RunMouseLeftButtonUp(long(release_x), long(release_y));
|
||||
// The finger is gone: no hover target / tooltip stays under the tap point.
|
||||
ava2::TouchUiPointerIdle(*wm);
|
||||
}
|
||||
void Joystick(bool active, float x, float y) override { ava2::SetPlayerJoystick(active, x, y); }
|
||||
void Attack(bool pressed) override { ava2::SetPlayerAttackKey(pressed); }
|
||||
@@ -1382,7 +1385,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
|
||||
touch_router.FingerUp(event.tfinger.fingerID, tx, ty, tms);
|
||||
break;
|
||||
case SDL_EVENT_FINGER_CANCELED:
|
||||
touch_router.CancelAll();
|
||||
touch_router.FingerCancel(event.tfinger.fingerID, tms);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -35,6 +35,7 @@ void TouchRouter::SetWorldEnabled(bool enabled) {
|
||||
if (enabled == m_world) return;
|
||||
// Phase change: world fingers would otherwise keep a stale joystick or attack state.
|
||||
if (!enabled) {
|
||||
ReleaseDeferred();
|
||||
for (auto it = m_fingers.begin(); it != m_fingers.end();) {
|
||||
if (it->second.owner != Owner::Ui) {
|
||||
Release(it->first, it->second, true);
|
||||
@@ -58,6 +59,33 @@ bool TouchRouter::InJoystickArea(float x, float y) const {
|
||||
y >= m_layout.joy_y && y < m_layout.joy_y + m_layout.joy_h;
|
||||
}
|
||||
|
||||
bool TouchRouter::InSystemEdge(float, float y) const {
|
||||
return m_layout.safe_bottom > 0.0f && y >= m_layout.screen_h - m_layout.safe_bottom;
|
||||
}
|
||||
|
||||
bool TouchRouter::CanReacquireJoystick(float x, float y, std::uint64_t ms) const {
|
||||
if (!m_joy_orphan || (ms > m_joy_orphan_ms && ms - m_joy_orphan_ms > m_layout.joy_reacquire_ms)) return false;
|
||||
// The thumb comes back roughly where it was: anywhere within one and a half stick radii.
|
||||
const float reach = m_layout.joy_radius * 1.5f;
|
||||
return std::hypot(x - m_joy.base_x, y - m_joy.base_y) <= reach;
|
||||
}
|
||||
|
||||
void TouchRouter::ReleaseJoystick() {
|
||||
m_joy_finger = 0;
|
||||
m_joy_orphan = false;
|
||||
m_joy = JoystickView{};
|
||||
if (m_sink) m_sink->Joystick(false, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
void TouchRouter::ReleaseDeferred() {
|
||||
if (m_joy_orphan) ReleaseJoystick();
|
||||
if (m_attack_release_pending) {
|
||||
m_attack_release_pending = false;
|
||||
if (m_sink) m_sink->Attack(false);
|
||||
}
|
||||
m_attack_fresh = false;
|
||||
}
|
||||
|
||||
void TouchRouter::UpdateJoystick(float x, float y) {
|
||||
const float r = m_layout.joy_radius;
|
||||
float dx = x - m_joy.base_x, dy = y - m_joy.base_y;
|
||||
@@ -104,11 +132,26 @@ void TouchRouter::FingerDown(std::uint64_t raw_id, float x, float y, std::uint64
|
||||
if (!m_attack_finger && InAttack(x, y)) {
|
||||
finger.owner = Owner::Attack;
|
||||
m_attack_finger = id;
|
||||
m_attack_fresh = true;
|
||||
m_fingers[id] = finger;
|
||||
if (m_sink) m_sink->Attack(true);
|
||||
// A release still waiting for its frame: the key simply stays down.
|
||||
if (m_attack_release_pending)
|
||||
m_attack_release_pending = false;
|
||||
else if (m_sink)
|
||||
m_sink->Attack(true);
|
||||
return;
|
||||
}
|
||||
if (!m_joy_finger && CanReacquireJoystick(x, y, ms)) {
|
||||
// Same base as before the system cancel, so the direction does not jump.
|
||||
finger.owner = Owner::Joystick;
|
||||
m_joy_finger = id;
|
||||
m_joy_orphan = false;
|
||||
m_fingers[id] = finger;
|
||||
UpdateJoystick(x, y);
|
||||
return;
|
||||
}
|
||||
if (!m_joy_finger && InJoystickArea(x, y)) {
|
||||
m_joy_orphan = false;
|
||||
finger.owner = Owner::Joystick;
|
||||
m_joy_finger = id;
|
||||
m_fingers[id] = finger;
|
||||
@@ -119,6 +162,12 @@ void TouchRouter::FingerDown(std::uint64_t raw_id, float x, float y, std::uint64
|
||||
UpdateJoystick(x, y);
|
||||
return;
|
||||
}
|
||||
if (InSystemEdge(x, y)) {
|
||||
// Home-gesture strip: a swipe up here belongs to the system, not to the camera.
|
||||
finger.owner = Owner::Ignored;
|
||||
m_fingers[id] = finger;
|
||||
return;
|
||||
}
|
||||
finger.owner = Owner::Camera;
|
||||
if (Finger* other = OtherCameraFinger(id)) {
|
||||
// Second camera finger: a pinch, neither finger can still be a tap.
|
||||
@@ -149,6 +198,7 @@ void TouchRouter::FingerMove(std::uint64_t raw_id, float x, float y, std::uint64
|
||||
m_sink->UiMouseMove(x, y);
|
||||
break;
|
||||
case Owner::Attack:
|
||||
case Owner::Ignored:
|
||||
break;
|
||||
case Owner::Joystick:
|
||||
UpdateJoystick(x, y);
|
||||
@@ -179,19 +229,25 @@ void TouchRouter::Release(std::uint64_t id, Finger& finger, bool cancelled) {
|
||||
m_ui_finger = 0;
|
||||
// A cancelled UI finger still releases a pressed button so no window keeps the capture.
|
||||
if (finger.ui == Finger::Ui::Pressed && m_sink) m_sink->UiMouseUp(finger.x, finger.y);
|
||||
// A cancelled pending touch never clicked: its hover tooltip would otherwise stay. A long
|
||||
// press the host took keeps the pointer (the item tooltip stays next to the action sheet).
|
||||
else if (finger.ui == Finger::Ui::Pending && m_sink) m_sink->UiPointerIdle();
|
||||
break;
|
||||
case Owner::Attack:
|
||||
m_attack_finger = 0;
|
||||
if (m_sink) m_sink->Attack(false);
|
||||
if (!cancelled && m_attack_fresh)
|
||||
m_attack_release_pending = true;
|
||||
else if (m_sink)
|
||||
m_sink->Attack(false);
|
||||
break;
|
||||
case Owner::Joystick:
|
||||
m_joy_finger = 0;
|
||||
m_joy = JoystickView{};
|
||||
if (m_sink) m_sink->Joystick(false, 0.0f, 0.0f);
|
||||
ReleaseJoystick();
|
||||
break;
|
||||
case Owner::Camera:
|
||||
if (!cancelled && finger.tap && m_sink) m_sink->WorldTap(finger.x, finger.y);
|
||||
break;
|
||||
case Owner::Ignored:
|
||||
break;
|
||||
}
|
||||
(void)id;
|
||||
}
|
||||
@@ -217,6 +273,22 @@ void TouchRouter::FingerUp(std::uint64_t raw_id, float x, float y, std::uint64_t
|
||||
Release(id, finger, false);
|
||||
}
|
||||
|
||||
void TouchRouter::FingerCancel(std::uint64_t raw_id, std::uint64_t ms) {
|
||||
const std::uint64_t id = raw_id + 1;
|
||||
auto it = m_fingers.find(id);
|
||||
if (it == m_fingers.end()) return;
|
||||
Finger finger = it->second;
|
||||
m_fingers.erase(it);
|
||||
if (finger.owner == Owner::Joystick) {
|
||||
// Keep moving for a moment: the finger is usually still on the glass and comes back.
|
||||
m_joy_finger = 0;
|
||||
m_joy_orphan = true;
|
||||
m_joy_orphan_ms = ms;
|
||||
return;
|
||||
}
|
||||
Release(id, finger, true);
|
||||
}
|
||||
|
||||
void TouchRouter::UiTap(const Finger& finger, std::uint64_t ms) {
|
||||
if (!m_sink) return;
|
||||
// The click lands where the finger landed; a tap moves less than tap_slop anyway.
|
||||
@@ -236,6 +308,12 @@ void TouchRouter::UiTap(const Finger& finger, std::uint64_t ms) {
|
||||
}
|
||||
|
||||
void TouchRouter::Update(std::uint64_t ms) {
|
||||
m_attack_fresh = false;
|
||||
if (m_attack_release_pending) {
|
||||
m_attack_release_pending = false;
|
||||
if (m_sink) m_sink->Attack(false);
|
||||
}
|
||||
if (m_joy_orphan && ms > m_joy_orphan_ms && ms - m_joy_orphan_ms > m_layout.joy_reacquire_ms) ReleaseJoystick();
|
||||
if (!m_ui_finger || !m_sink) return;
|
||||
auto it = m_fingers.find(m_ui_finger);
|
||||
if (it == m_fingers.end()) return;
|
||||
@@ -252,6 +330,7 @@ void TouchRouter::Update(std::uint64_t ms) {
|
||||
}
|
||||
|
||||
void TouchRouter::CancelAll() {
|
||||
ReleaseDeferred();
|
||||
for (auto& [id, finger] : m_fingers) Release(id, finger, true);
|
||||
m_fingers.clear();
|
||||
}
|
||||
|
||||
@@ -20,7 +20,19 @@
|
||||
// rest of the touch is ignored, otherwise the button goes down and the
|
||||
// touch continues as a press
|
||||
//
|
||||
// The pointer follows the finger from the moment it lands, so hover tooltips show while holding.
|
||||
// The pointer follows the finger from the moment it lands, so hover tooltips show while holding;
|
||||
// when a touch ends without a click (cancelled, or a world tap) the host parks it off screen
|
||||
// (UiPointerIdle) so no hover tooltip stays behind.
|
||||
//
|
||||
// System edges (interaction ideas from jhaizhou-ops/d2r-mobile, MIT; no code taken):
|
||||
//
|
||||
// - a world finger that lands in the bottom safe inset (the home-gesture strip) is ignored:
|
||||
// no camera drag, no world tap;
|
||||
// - when the system cancels the joystick finger (FingerCancel, e.g. an edge gesture starting
|
||||
// under it) the stick keeps its vector for joy_reacquire_ms, and a finger landing near the
|
||||
// old base in that time takes it over with the same base;
|
||||
// - an attack tap shorter than one frame is held until the next Update so the player module
|
||||
// sees the key at least once.
|
||||
//
|
||||
// Outside the game phase only the UI owner exists. Coordinates are UI pixels (the same space as
|
||||
// wndMgr). The router has no SDL or game dependencies; the host implements TouchSink.
|
||||
@@ -41,6 +53,9 @@ struct TouchSink {
|
||||
virtual void UiDoubleClick(float, float) {}
|
||||
// Finger held still on UI; true when the host handled it and wants no button press.
|
||||
virtual bool UiLongPress(float, float) { return false; }
|
||||
// A UI or world touch ended without the pointer being parked by UiMouseUp: the host moves it
|
||||
// off screen unless an item hangs on the cursor.
|
||||
virtual void UiPointerIdle() {}
|
||||
// Short tap on the game world: the host clicks there as the original left button would.
|
||||
virtual void WorldTap(float x, float y) = 0;
|
||||
// Joystick vector in screen axes (x right, y down), length 0..1; active=false releases it.
|
||||
@@ -65,6 +80,7 @@ struct TouchLayout {
|
||||
std::uint64_t double_tap_ms = 300; // first tap's lift to second tap's landing
|
||||
float double_tap_slop = 40.0f; // distance between the two taps
|
||||
std::uint64_t long_press_ms = 450;
|
||||
std::uint64_t joy_reacquire_ms = 300; // a cancelled joystick waits this long for a new finger
|
||||
|
||||
// Unobstructed-area insets (platform::SafeInsets); the HUD reads them through `touch`.
|
||||
float safe_left = 0.0f, safe_top = 0.0f, safe_right = 0.0f, safe_bottom = 0.0f;
|
||||
@@ -83,7 +99,8 @@ struct JoystickView {
|
||||
|
||||
class TouchRouter {
|
||||
public:
|
||||
enum class Owner { Ui, Attack, Joystick, Camera };
|
||||
// Ignored: a world finger that landed in the bottom system-gesture strip.
|
||||
enum class Owner { Ui, Attack, Joystick, Camera, Ignored };
|
||||
|
||||
void SetSink(TouchSink* sink) { m_sink = sink; }
|
||||
void SetLayout(const TouchLayout& layout) { m_layout = layout; }
|
||||
@@ -95,13 +112,15 @@ public:
|
||||
void FingerDown(std::uint64_t id, float x, float y, std::uint64_t ms);
|
||||
void FingerMove(std::uint64_t id, float x, float y, std::uint64_t ms);
|
||||
void FingerUp(std::uint64_t id, float x, float y, std::uint64_t ms);
|
||||
// The system took this finger (SDL_EVENT_FINGER_CANCELED): released without a tap or click.
|
||||
void FingerCancel(std::uint64_t id, std::uint64_t ms);
|
||||
// Per-frame tick in the same milliseconds as the finger events (long press detection).
|
||||
void Update(std::uint64_t ms);
|
||||
// Lost focus / app paused: release everything without taps or clicks.
|
||||
void CancelAll();
|
||||
|
||||
JoystickView GetJoystick() const { return m_joy; }
|
||||
bool IsAttackPressed() const { return m_attack_finger != 0; }
|
||||
bool IsAttackPressed() const { return m_attack_finger != 0 || m_attack_release_pending; }
|
||||
std::size_t FingerCount() const { return m_fingers.size(); }
|
||||
|
||||
private:
|
||||
@@ -118,6 +137,11 @@ private:
|
||||
|
||||
bool InAttack(float x, float y) const;
|
||||
bool InJoystickArea(float x, float y) const;
|
||||
bool InSystemEdge(float x, float y) const;
|
||||
bool CanReacquireJoystick(float x, float y, std::uint64_t ms) const;
|
||||
void ReleaseJoystick();
|
||||
// Drops the orphaned joystick and a deferred attack release (phase change, focus loss).
|
||||
void ReleaseDeferred();
|
||||
void UpdateJoystick(float x, float y);
|
||||
Finger* OtherCameraFinger(std::uint64_t id);
|
||||
void Release(std::uint64_t id, Finger& finger, bool cancelled);
|
||||
@@ -129,6 +153,11 @@ private:
|
||||
std::map<std::uint64_t, Finger> m_fingers; // ids are offset by one so 0 means "none"
|
||||
std::uint64_t m_ui_finger = 0, m_joy_finger = 0, m_attack_finger = 0;
|
||||
JoystickView m_joy;
|
||||
// Joystick whose finger the system cancelled; still active until m_joy_orphan_ms + reacquire.
|
||||
bool m_joy_orphan = false;
|
||||
std::uint64_t m_joy_orphan_ms = 0;
|
||||
// Attack finger landed since the last Update; a lift before it defers the release to Update.
|
||||
bool m_attack_fresh = false, m_attack_release_pending = false;
|
||||
// Last UI tap for double tap detection; m_last_tap_valid is cleared once it pairs.
|
||||
bool m_last_tap_valid = false;
|
||||
std::uint64_t m_last_tap_ms = 0;
|
||||
|
||||
@@ -65,8 +65,9 @@ PyObject* touch_get_safe_area(PyObject*, PyObject*) {
|
||||
int(l.safe_right + 0.5f), int(l.safe_bottom + 0.5f));
|
||||
}
|
||||
|
||||
// SetLongPressEvent(func): func(slotWindow, slotNumber, itemCount, x, y) runs when a finger rests on an item
|
||||
// slot; a true result takes the touch (no click follows). None clears it.
|
||||
// SetLongPressEvent(func): func(slotWindow, slotNumber, itemCount, x, y, isEmpty) runs when a finger rests on
|
||||
// a slot (isEmpty 1: no item there, e.g. an empty inventory cell); a true result takes the touch (no click
|
||||
// follows). None clears it.
|
||||
PyObject* s_long_press_event = nullptr;
|
||||
PyObject* touch_set_long_press_event(PyObject*, PyObject* args) {
|
||||
PyObject* event = nullptr;
|
||||
@@ -89,10 +90,11 @@ PyObject* touch_set_virtual_key(PyObject*, PyObject* args) {
|
||||
|
||||
} // namespace
|
||||
|
||||
bool RunTouchLongPress(PyObject* slot_window, unsigned long slot_number, unsigned long item_count, long x, long y) {
|
||||
bool RunTouchLongPress(PyObject* slot_window, unsigned long slot_number, unsigned long item_count, long x, long y,
|
||||
bool empty) {
|
||||
if (!s_long_press_event || !slot_window) return false;
|
||||
PyObject* result = PyObject_CallFunction(s_long_press_event, const_cast<char*>("Okkll"), slot_window, slot_number,
|
||||
item_count, x, y);
|
||||
PyObject* result = PyObject_CallFunction(s_long_press_event, const_cast<char*>("Okklli"), slot_window, slot_number,
|
||||
item_count, x, y, empty ? 1 : 0);
|
||||
if (!result) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
@@ -102,9 +104,14 @@ bool RunTouchLongPress(PyObject* slot_window, unsigned long slot_number, unsigne
|
||||
return taken;
|
||||
}
|
||||
|
||||
void TouchUiPointerIdle(UI::CWindowManager& wm) {
|
||||
// An item on the cursor follows the last finger position until it is dropped.
|
||||
if (!wm.IsAttaching()) wm.RunMouseMove(-10000, -10000);
|
||||
}
|
||||
|
||||
void TouchUiMouseUp(UI::CWindowManager& wm, long x, long y) {
|
||||
wm.RunMouseLeftButtonUp(x, y);
|
||||
if (!wm.IsAttaching()) wm.RunMouseMove(-10000, -10000);
|
||||
TouchUiPointerIdle(wm);
|
||||
}
|
||||
|
||||
bool TouchUiLongPress(UI::CWindowManager& wm, long x, long y) {
|
||||
@@ -112,8 +119,11 @@ bool TouchUiLongPress(UI::CWindowManager& wm, long x, long y) {
|
||||
wm.RunMouseMove(x, y);
|
||||
auto* slots = dynamic_cast<UI::CSlotWindow*>(wm.PickWindow(x, y));
|
||||
UI::CSlotWindow::SSlot* slot = nullptr;
|
||||
if (!slots || !slots->GetPickedSlotPointer(&slot) || !slot || !slot->isItem) return false;
|
||||
return RunTouchLongPress(slots->GetHandler(), slot->dwSlotNumber, slot->dwItemCount, x, y);
|
||||
if (!slots || !slots->GetPickedSlotPointer(&slot) || !slot) return false;
|
||||
if (slot->isItem) return RunTouchLongPress(slots->GetHandler(), slot->dwSlotNumber, slot->dwItemCount, x, y, false);
|
||||
// A cell under a larger item (grid windows point it at the item's slot) is not empty.
|
||||
if (slot->dwCenterSlotNumber != slot->dwSlotNumber && slot->dwCenterSlotNumber != 0xffffffff) return false;
|
||||
return RunTouchLongPress(slots->GetHandler(), slot->dwSlotNumber, 0, x, y, true);
|
||||
}
|
||||
|
||||
void inittouch() {
|
||||
|
||||
@@ -7,15 +7,18 @@ namespace ava2 {
|
||||
// `touch` module: lets the mobile HUD place the attack button / joystick area of the native
|
||||
// touch router (src/app/input/touch_router.h) and draw the joystick it drives.
|
||||
void inittouch();
|
||||
// Long press on an item slot: calls the touch.SetLongPressEvent function with the slot window's
|
||||
// Python object; true when it took the touch.
|
||||
bool RunTouchLongPress(PyObject* slot_window, unsigned long slot_number, unsigned long item_count, long x, long y);
|
||||
// Long press on a slot: calls the touch.SetLongPressEvent function with the slot window's Python
|
||||
// object; true when it took the touch.
|
||||
bool RunTouchLongPress(PyObject* slot_window, unsigned long slot_number, unsigned long item_count, long x, long y,
|
||||
bool empty);
|
||||
|
||||
// Touch router sink helpers shared by the host loop and tests:
|
||||
// left button up for a finger; afterwards the pointer leaves the screen (a lifted finger does not
|
||||
// hover, so tooltips close) unless an icon is attached and must stay where it was picked up.
|
||||
void TouchUiMouseUp(UI::CWindowManager& wm, long x, long y);
|
||||
// Long press: on an item slot (nothing attached) the pointer rests there, so the original item
|
||||
// tooltip shows, and RunTouchLongPress decides; true when the touch was taken.
|
||||
// A touch ended without a button release: same pointer parking as TouchUiMouseUp.
|
||||
void TouchUiPointerIdle(UI::CWindowManager& wm);
|
||||
// Long press: on a slot (nothing attached) the pointer rests there, so the original item tooltip
|
||||
// shows, and RunTouchLongPress decides (empty cells too); true when the touch was taken.
|
||||
bool TouchUiLongPress(UI::CWindowManager& wm, long x, long y);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user