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
+13 -2
View File
@@ -141,10 +141,21 @@ Python 更新(原版脚本 + mobile HUD)
| 拖动(超过 tap_slop) | 在起点按下左键,随后跟随移动,抬起即松开 | 原版拖拽:换格、拖到装备栏、拖出窗口丢弃、拖到商店出售 |
| 双击(300 ms、40 px 内) | 原版左键双击 | `OnUseSlot`:使用药水、穿戴/卸下装备 |
| 长按物品格(450 ms) | `touch.SetLongPressEvent` 回调 | 提示保持显示,弹出 `ui_mobile_actions.ItemActionSheet`:使用、拆分、加入快捷栏、关闭 |
| 长按背包空格子(450 ms) | 同一回调,`isEmpty=1` | 面板只有“整理”和“关闭”;整理调用原版整理按钮 `separateButton` 的事件(确认框 → `/click_sort_items`) |
- 长按面板的按钮调用原版槽位事件。“使用”调用 `OnUseSlot`;“拆分”和“加入快捷栏”通过 `touch.SetVirtualKey` 临时按住 Shift / Ctrl,再调用 `OnSelectItemSlot`,走原版的修饰键分支。
- 点面板外任意处关闭面板。长按空格子没有操作,按普通按下处理。
- 手指抬起后指针移到屏幕外(拿着物品时除外),以免提示一直停留。
- 点面板外任意处关闭面板。长按其他窗口的空格子、或被大件物品占住的格子,仍按普通按下处理。
- 手指抬起后指针移到屏幕外(拿着物品时除外),以免提示一直停留。被取消的触摸、点地面(世界点击)结束后也一样。
- 快捷栏和药水栏:每次点按的左键按下都会触发一次使用;第二下被识别成双击时只多发一个双击,快捷格没有双击事件,所以连点 N 次就是用 N 次。
## 系统边缘与取消
以下交互思路参考 [jhaizhou-ops/d2r-mobile](https://github.com/jhaizhou-ops/d2r-mobile)(MIT),只借鉴想法,没有拷贝代码:
- 底部安全区(手势导航条)里落下的世界手指不拖镜头,也不算点地面。
- 系统取消某一根手指(`SDL_EVENT_FINGER_CANCELED`,例如边缘手势从它下面开始)时只释放这一根,其他手指不受影响。摇杆手指被取消后,摇杆保持原方向 `joy_reacquire_ms`(300 ms);这段时间内在原基点 1.5 倍半径内落下的手指接管摇杆,基点不变。
- 攻击键点得比一帧还快时,松开推迟到下一帧,保证原版 player 模块至少看到一次按下。
- 没有采用的:技能拖动瞄准(Metin2 技能按目标/朝向释放)、点面板外关闭所有窗口(点世界要移动角色)、手柄自动切换(以后再说)。
- 技能:技能圈和底部栏是原版快捷栏格,点按即使用(第 4 步)。从技能窗口把技能拖到快捷栏用的是同一套槽位拖拽,但还没有单独测试。
## 目录变化
+37 -1
View File
@@ -1588,4 +1588,40 @@ Verified on the device: minimap inside its frame, portrait/status bars and the e
at the top edge. Desktop ctest 59/59.
Outstanding: after a tap the cursor stays where the finger lifted, so hover tooltips
(e.g. "[F6] Battle pass") remain until the next touch.
(e.g. "[F6] Battle pass") remain until the next touch. (Addressed below: world taps and
cancelled touches now park the pointer too.)
### 2026-10-13 Mobile touch: system edges, cancel, long-press sort
Interaction ideas from jhaizhou-ops/d2r-mobile (MIT); only the ideas were taken, no code.
docs/MOBILE-ARCHITECTURE.md has the details ("系统边缘与取消").
- `TouchRouter`:
- world fingers that land in the bottom safe inset (the gesture bar) are `Owner::Ignored`;
- new `FingerCancel(id, ms)`: `SDL_EVENT_FINGER_CANCELED` now cancels only that finger
instead of `CancelAll`;
- a cancelled joystick finger keeps the stick vector for `joy_reacquire_ms` (300). A finger
landing within 1.5 radii of the old base takes it over, keeping the same base;
- an attack tap shorter than a frame releases on the next `Update`;
- new sink hook `UiPointerIdle`: a cancelled pending UI touch parks the pointer.
- Host: a world tap's deferred button-up also parks the pointer (`TouchUiPointerIdle`), so no
hover tooltip or target stays under the tap point.
- Long press on an empty inventory cell: `touch.SetLongPressEvent` callbacks get a sixth
argument, `isEmpty`. `ItemActionSheet` offers Sort / Close. Sort fires the original
`separateButton` event, which opens the question and then sends `/click_sort_items`.
Cells covered by a larger item and empty cells in other windows still press normally.
- Checked, no change needed: every quick-slot tap uses the slot once (select on button
down; the double click goes to `OnUseSlot`, and quick-slot discs have no use event).
Dropping an item on any shop slot, empty or occupied, already sells it (uishop).
- Tests:
- `touch_router_test` `system_edges`;
- `inventory_touch_test`: long-press empty cell → Sort → question → `/click_sort_items`;
- `touch_game_test` now keeps its assert()s in release builds.
- Desktop ctest 59/59.
- Verified on the device: long press on an empty bag cell → Sort / Close → the original
sort question.
Outstanding:
- the system-edge strip and joystick re-acquire are covered by unit tests only, not yet
exercised by hand on the phone;
- hold-to-repeat on quick slots (d2r's held potion) is not done.
+4 -1
View File
@@ -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;
+84 -5
View File
@@ -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();
}
+32 -3
View File
@@ -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;
+18 -8
View File
@@ -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() {
+8 -5
View File
@@ -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);
}
+12 -2
View File
@@ -211,12 +211,22 @@ int main(int argc, char** argv) {
runtime.execute("assert not sheet.IsShow() and not calls, ('outside', sheet.IsShow(), calls)\n"
"assert not mouseModule.mouseController.isAttached(), 'outside attached'\n");
// Long press on an empty cell is an ordinary press.
// Long press on an empty cell offers the original sort (separateButton -> question ->
// /click_sort_items); the sheet's Close button leaves the bag alone.
runtime.execute("net.SendChatPacket = lambda *a: calls.append(('chat',) + a)\n");
router.FingerDown(0, x5, y5, now);
router.Update(now + 500);
runtime.execute("assert not sheet.IsShow(), 'empty long'\n");
router.FingerUp(0, x5, y5, now + 600);
now += 1000;
runtime.execute("assert sheet.IsShow() and len(sheet.buttons) == 2, ('empty long', sheet.IsShow(), len(sheet.buttons))\n"
"assert not mouseModule.mouseController.isAttached() and not calls, calls\n");
button(0, bx, by);
Tap(router, bx, by); // Sort
runtime.execute("assert not sheet.IsShow(), 'sort closes'\n"
"assert inv.question and inv.question.IsShow(), 'sort question'\n"
"inv.question.acceptButton.CallEvent() if hasattr(inv.question, 'acceptButton') else inv.ClickSort()\n"
"assert ('chat', '/click_sort_items') in calls, calls\n"
"del calls[:]\n");
// Desktop keyboard state reaches app.IsPressed (Shift-split, Ctrl-quickslot on PC).
ava2::SetKeyPressed(42, true);
+5
View File
@@ -1,5 +1,9 @@
// Touch mode in the original GameWindow: the mobile HUD must leave the joystick / attack zones and
// open world to the touch router, while real UI (HUD buttons) still takes the finger.
// The checks are assert()s; keep them in release builds.
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "python_runtime.h"
#include "python_gameplay_modules.h"
#include "app/input/touch_router.h"
@@ -117,6 +121,7 @@ int main(int argc, char** argv) {
runtime.execute("game.interface.wndMobileHUD.combat_wheel.OnUpdate()\n"
"assert game.interface.wndMobileHUD.combat_wheel.attack_pressed\n"
"game.interface.wndMobileHUD.joystick.OnUpdate()\n");
router.Update(100); // a frame has seen the held key
router.FingerUp(2, layout.attack_x, layout.attack_y, 200);
router.FingerUp(1, 160.0f + layout.joy_radius, H - 180.0f, 220);
assert(!sink.attack && !ava2::GetPlayerMovementYaw(0.0f, yaw));
+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';
+31 -2
View File
@@ -8,7 +8,9 @@
# 使用/装备 -> slotWindow.OnUseSlot(原版右键/双击)
# 拆分 -> 按住 Shift 调 OnSelectItemSlot(uiinventory.SelectItemSlot 的拆分分支)
# 加入快捷栏 -> 按住 Ctrl 调 OnSelectItemSlot(交易/商店等窗口打开时是原版 Ctrl 放入)
# 点面板外任意处关闭。丢弃、出售沿用拖拽(拖出窗口 / 拖到商店)。
# 长按背包空格子(参考 jhaizhou-ops/d2r-mobile 的“长按空格整理”,MIT,未拷贝代码):
# 整理 -> 原版整理按钮 separateButton 的事件(确认框后 /click_sort_items)
# 点面板外任意处关闭。丢弃、出售沿用拖拽(拖出窗口 / 拖到商店任一格)。
import ui
import grp
import app
@@ -59,7 +61,9 @@ class ItemActionSheet(ui.Window):
return inventory is not None and slotWindow is getattr(inventory, "wndItem", None)
# touch.SetLongPressEvent: True = 已处理(手指松开不再点击)
def OnLongPress(self, slotWindow, slotNumber, itemCount, x, y):
def OnLongPress(self, slotWindow, slotNumber, itemCount, x, y, isEmpty=0):
if isEmpty:
return self.__OnLongPressEmpty(slotWindow, slotNumber, x, y)
actions = []
if getattr(slotWindow, "eventUseSlot", None):
actions.append((_Text("MOBILE_ITEM_USE", "Use"), self.__OnUse))
@@ -80,6 +84,31 @@ class ItemActionSheet(ui.Window):
self.SetTop()
return True
# 空格子:只有背包格有整理;其他窗口照常按下(False)
def __OnLongPressEmpty(self, slotWindow, slotNumber, x, y):
if not self.__IsInventoryGrid(slotWindow) or not self.__SortButton():
return False
self.slotWindow = slotWindow
self.slotNumber = slotNumber
self.__Build([(_Text("MOBILE_ITEM_SORT", "Sort"), self.__OnSort), (_Text("UI_CLOSE", "Close"), self.Close)])
self.__Place(x, y)
self.Show()
self.SetTop()
return True
def __SortButton(self):
inventory = getattr(self.interface, "wndInventory", None) if self.interface else None
button = getattr(inventory, "separateButton", None) if inventory else None
if button is None or not getattr(button, "eventFunc", None):
return None
return button
def __OnSort(self):
self.__Take()
button = self.__SortButton()
if button:
button.CallEvent()
def __Build(self, actions):
for button in self.buttons:
button.Hide()