From 6244b1d860e541305e0ac9ecee14a3a2d43c6314 Mon Sep 17 00:00:00 2001 From: shenlei Date: Tue, 13 Oct 2026 05:16:12 +0900 Subject: [PATCH] Mobile gestures: item tap/drag/double-tap/long-press on original windows Port the EterPythonLib CSlotWindow/CGridSlotWindow mouse handlers (select, drop, use, right click, tooltips, grid picking/CheckMoving) and CWindowManager IsDragging/right button, which were missing so item interaction did not work on desktop either. app.SetMouseHandler now renders the attached icon, app.IsPressed reads real key state, desktop double clicks reach the window manager. The touch router maps UI touches to the original mouse: tap = click at the touch-down point, drag = press-and-drag, double tap = double click, long press on an item slot = touch.SetLongPressEvent, which opens the ui_mobile_actions item sheet (use / split / quick slot via virtual Shift/Ctrl). New ava2.inventory_touch test drives uiinventory.py. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 6 + docs/MOBILE-ARCHITECTURE.md | 17 + docs/PORT-PLAN.md | 42 ++ src/app/app_loop.cpp | 19 +- src/app/input/touch_router.cpp | 58 ++- src/app/input/touch_router.h | 32 +- src/eter_ui/PythonWindow.cpp | 535 +++++++++++++++++++--- src/eter_ui/PythonWindow.h | 36 +- src/eter_ui/PythonWindowManager.cpp | 53 ++- src/scripting/python_app.cpp | 33 +- src/scripting/python_app.h | 6 + src/scripting/python_touch.cpp | 56 +++ src/scripting/python_touch.h | 14 + tests/inventory_touch_test.cpp | 231 ++++++++++ tests/touch_router_test.cpp | 94 +++- upstream/python-root/ui_mobile_actions.py | 146 ++++++ upstream/python-root/ui_mobile_hud.py | 4 + 17 files changed, 1301 insertions(+), 81 deletions(-) create mode 100644 tests/inventory_touch_test.cpp create mode 100644 upstream/python-root/ui_mobile_actions.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 8db31adc..e96c3425 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -227,6 +227,12 @@ if(BUILD_TESTING AND NOT ANDROID) add_test(NAME ava2.touch_game COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tests/check_completion.py" "Touch router drives joystick, attack and world taps around the mobile HUD" "$" "${MT_PYTHON_STDLIB_ZIP}" "${CMAKE_SOURCE_DIR}/upstream/python-root") set_tests_properties(ava2.touch_game PROPERTIES ENVIRONMENT "AVA2_TEST_RUNDIR=${AVA2_TEST_RUNDIR}" SKIP_RETURN_CODE 77) + add_executable(inventory_touch_test tests/inventory_touch_test.cpp) + target_link_libraries(inventory_touch_test PRIVATE ava2_python mtpython) + add_dependencies(inventory_touch_test mtpython_stdlib) + add_test(NAME ava2.inventory_touch COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tests/check_completion.py" + "Touch gestures drive the original inventory" "$" "${MT_PYTHON_STDLIB_ZIP}" "${CMAKE_SOURCE_DIR}/upstream/python-root") + set_tests_properties(ava2.inventory_touch PROPERTIES ENVIRONMENT "AVA2_TEST_RUNDIR=${AVA2_TEST_RUNDIR}" SKIP_RETURN_CODE 77) add_test(NAME ava2.pack_assets COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tests/pack_assets_test.py") endif() diff --git a/docs/MOBILE-ARCHITECTURE.md b/docs/MOBILE-ARCHITECTURE.md index 68f334fa..61767fea 100644 --- a/docs/MOBILE-ARCHITECTURE.md +++ b/docs/MOBILE-ARCHITECTURE.md @@ -130,6 +130,23 @@ Python 更新(原版脚本 + mobile HUD) - 20:9 等宽屏需按安全区域布局。 - 不使用其他服务器的界面美术;图标用 AVA2 自带技能/道具图标,按钮底框和摇杆另制。 +## 物品与窗口手势 + +原版窗口不改,触摸由 `src/app/input/touch_router` 翻译成原版鼠标事件,经窗口管理器和移植的 `CSlotWindow` 鼠标处理进入 `uiinventory.py` 等原版脚本: + +| 手势 | 翻译成 | 原版效果 | +|---|---|---| +| 手指按住不动 | 鼠标移到该处 | 物品格显示原版物品提示 | +| 点按 | 在按下点依次发移动、左键按下、左键抬起 | 拿起物品(图标跟随);再点空格/装备格/其他窗口即放下、穿戴、交易放入 | +| 拖动(超过 tap_slop) | 在起点按下左键,随后跟随移动,抬起即松开 | 原版拖拽:换格、拖到装备栏、拖出窗口丢弃、拖到商店出售 | +| 双击(300 ms、40 px 内) | 原版左键双击 | `OnUseSlot`:使用药水、穿戴/卸下装备 | +| 长按物品格(450 ms) | `touch.SetLongPressEvent` 回调 | 提示保持显示,弹出 `ui_mobile_actions.ItemActionSheet`:使用、拆分、加入快捷栏、关闭 | + +- 长按面板的按钮调用原版槽位事件。“使用”调用 `OnUseSlot`;“拆分”和“加入快捷栏”通过 `touch.SetVirtualKey` 临时按住 Shift / Ctrl,再调用 `OnSelectItemSlot`,走原版的修饰键分支。 +- 点面板外任意处关闭面板。长按空格子没有操作,按普通按下处理。 +- 手指抬起后指针移到屏幕外(拿着物品时除外),以免提示一直停留。 +- 技能:技能圈和底部栏是原版快捷栏格,点按即使用(第 4 步)。从技能窗口把技能拖到快捷栏用的是同一套槽位拖拽,但还没有单独测试。 + ## 目录变化 - `support/android` → 顶层 `android/`(补全)。 diff --git a/docs/PORT-PLAN.md b/docs/PORT-PLAN.md index 7ed4ecd7..20ea399d 100644 --- a/docs/PORT-PLAN.md +++ b/docs/PORT-PLAN.md @@ -1453,3 +1453,45 @@ the app is in the background (iOS suspends SDL_AppIterate). Android still uses the classic loop. Paths the client opens without lower-casing them would fail on iOS; none showed up in the login screen or the game phase, but character select and a real-server login have not been run on iOS yet. + +### 2026-10-13 Mobile gestures — item and window interaction + +- Before this step the port's `CSlotWindow` had no mouse handlers, so item + pick-up, drop, equip, double-click use, right-click and hover tooltips did + not work on desktop either. Ported from EterPythonLib, 1:1: + - `PythonSlotWindow.cpp` handlers: left down → OnSelectItemSlot/OnSelectEmptySlot, + the drop on left up, right down, double click → OnUseSlot, over/over-out + tooltips, RefreshSlot. + - `PythonGridSlotWindow.cpp`: the slot vector, GetPickedSlotList, + OnRefreshSlot, CheckMoving, the grid GetPickedSlotPointer. + - `CWindowManager`: IsDragging (the 10 px rule), mouse over on every move, + right button down/up with DeattachIcon. +- `app.SetMouseHandler` now stores the handler. app_loop calls its + Update/Render after the phase window, so the attached icon follows the + pointer. `app.IsPressed` reads the real keyboard state, so desktop + Shift-split and Ctrl-quickslot work. Desktop double clicks + (`clicks >= 2`) reach RunMouseLeftButtonDoubleClick. Keys are released on + focus loss and when the app goes to the background. +- Touch router UI gestures: + - A tap sends move, down and up at the touch-down point. + - A drag past tap_slop presses at the start point. + - A double tap (within 300 ms and 40 px) becomes a double click. + - A long press (450 ms) on an item slot calls `touch.SetLongPressEvent`. +- The callback opens `ui_mobile_actions.ItemActionSheet` with Use, Split and + Quick slot. Split and Quick slot run the original Shift/Ctrl branches via + `touch.SetVirtualKey`. +- Mapping table: docs/MOBILE-ARCHITECTURE.md, "物品与窗口手势". + +Verified: the new `ava2.inventory_touch` test drives the original +uiinventory.py through the router and the window manager. It covers the +hover tooltip, tap pick-up and tap drop (move packet 0→5), drag 0→6, +double-tap use, the long-press sheet (Use, Split opens dlgPickMoney, +Quick slot), tap-outside close, long press on an empty cell, and +app.IsPressed. `touch_router_test` covers the gesture timing. The phone +simulation (`--phone --touch`) draws no software cursor. Desktop 55/55. +Outstanding: +- Gestures have not been tried on an Android or iOS device yet. +- Drop and sell are drag-only; the sheet has no buttons for them. +- A button's zero-size TextLine sits at the button's exact centre pixel, + and IsIn is inclusive, as in the original. Only that one pixel misses + the button. diff --git a/src/app/app_loop.cpp b/src/app/app_loop.cpp index a3fcb6bc..e701d510 100644 --- a/src/app/app_loop.cpp +++ b/src/app/app_loop.cpp @@ -37,6 +37,7 @@ #include "player_settings.h" #include "eter_render/RaceData.h" #include "app/input/touch_router.h" +#include "python_touch.h" #endif #include "app/platform/platform.h" #include @@ -712,7 +713,9 @@ ava2::app::AppLoop::Frame RunClient(ava2::app::AppLoop* loop, int argc, char** a } void UiMouseMove(float x, float y) override { if (wm) wm->RunMouseMove(long(x), long(y)); } void UiMouseDown(float x, float y) override { if (wm) wm->RunMouseLeftButtonDown(long(x), long(y)); } - void UiMouseUp(float x, float y) override { if (wm) wm->RunMouseLeftButtonUp(long(x), long(y)); } + 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 WorldTap(float x, float y) override { if (!wm) return; // GameWindow.OnMouseLeftButtonDown sets player.SetMouseState; holding it for one @@ -1326,6 +1329,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); if (touch_mode) { touch_sink.FlushRelease(); touch_router.SetWorldEnabled(last_phase == "game"); + touch_router.Update(SDL_GetTicks()); } #endif SDL_Event event; @@ -1348,6 +1352,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); if (event.type == SDL_EVENT_WINDOW_FOCUS_LOST) { ava2::CancelPlayerMovement(); orbit_camera.EndDrag(); key_move_fwd = key_move_back = key_turn_left = key_turn_right = false; + ava2::ReleaseAllKeys(); if (touch_mode) touch_router.CancelAll(); } if (touch_mode) { @@ -1391,6 +1396,9 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); SDL_RenderCoordinatesFromWindow(renderer.get(), event.button.x, event.button.y, &bx, &by); if (event.button.button == SDL_BUTTON_LEFT) { wm->RunMouseLeftButtonDown(static_cast(bx), static_cast(by)); + // PythonApplicationEvent.cpp OnMouseLeftButtonDoubleClick: down, then double click. + if (event.button.clicks >= 2) + wm->RunMouseLeftButtonDoubleClick(static_cast(bx), static_cast(by)); if (last_phase == "game" && renderer3d) { // Ignore clicks on the minimap radar at top-right bool is_minimap_area = (bx >= static_cast(screen_width) - 135.0f && by <= 140.0f); @@ -1722,6 +1730,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); SDL_Log("3D Rasterizer toggled: %s", use_gpu_raster ? "GPU Hardware Accelerated (Metal)" : "CPU Software Rasterizer"); } int dik = SDLKeyToDIK(event.key.key); + if (dik) ava2::SetKeyPressed(dik, true); if (dik) wm->RunKeyDown(dik); } else if (event.type == SDL_EVENT_KEY_UP) { if (original_phase_mode && last_phase == "game") { @@ -1731,6 +1740,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); else if (event.key.key == SDLK_D || event.key.key == SDLK_RIGHT) key_turn_right = false; } int dik = SDLKeyToDIK(event.key.key); + if (dik) ava2::SetKeyPressed(dik, false); if (dik) wm->RunKeyUp(dik); } else if (event.type == SDL_EVENT_TEXT_INPUT && original_phase_mode && python) { if (ava2::IME_IsCaptureInput()) { @@ -1786,6 +1796,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); #ifdef AVA2_HAS_PYTHON ava2::CancelPlayerMovement(); orbit_camera.EndDrag(); key_move_fwd = key_move_back = key_turn_left = key_turn_right = false; + ava2::ReleaseAllKeys(); if (touch_mode) touch_router.CancelAll(); // The OS may kill a background app without another event. if (python) { @@ -2502,6 +2513,12 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); " if hasattr(stream.curPhaseWindow, 'OnRender'): stream.curPhaseWindow.OnRender()\n" ); } + if (python && wm) { + // CPythonApplication OnMouseUpdate/OnMouseRender: the attached icon over the UI. + long mouse_x = 0, mouse_y = 0; + wm->GetMousePosition(mouse_x, mouse_y); + ava2::RunMouseHandler(mouse_x, mouse_y); + } graphicDevice.EndScene(false); if (last_phase == "game" && renderer3d && preview_data) { diff --git a/src/app/input/touch_router.cpp b/src/app/input/touch_router.cpp index eb2bdfc2..d9c503e4 100644 --- a/src/app/input/touch_router.cpp +++ b/src/app/input/touch_router.cpp @@ -27,6 +27,7 @@ TouchLayout TouchLayout::ForScreen(float w, float h, float safeLeft, float safeT layout.attack_x = w - safeRight - layout.attack_r - unit * 0.06f; layout.attack_y = h - safeBottom - layout.attack_r - unit * 0.06f; layout.tap_slop = unit * 0.02f; + layout.double_tap_slop = unit * 0.06f; return layout; } @@ -95,9 +96,9 @@ void TouchRouter::FingerDown(std::uint64_t raw_id, float x, float y, std::uint64 if (m_ui_finger || !m_sink) return; finger.owner = Owner::Ui; m_ui_finger = id; + finger.ui = Finger::Ui::Pending; m_fingers[id] = finger; m_sink->UiMouseMove(x, y); - m_sink->UiMouseDown(x, y); return; } if (!m_attack_finger && InAttack(x, y)) { @@ -137,7 +138,15 @@ void TouchRouter::FingerMove(std::uint64_t raw_id, float x, float y, std::uint64 finger.y = y; switch (finger.owner) { case Owner::Ui: - if (m_sink) m_sink->UiMouseMove(x, y); + if (!m_sink || finger.ui == Finger::Ui::Consumed) break; + if (finger.ui == Finger::Ui::Pending) { + if (std::hypot(x - finger.start_x, y - finger.start_y) <= m_layout.tap_slop) break; + // A drag: the button goes down where the finger landed, as a mouse drag would start. + finger.ui = Finger::Ui::Pressed; + m_sink->UiMouseMove(finger.start_x, finger.start_y); + m_sink->UiMouseDown(finger.start_x, finger.start_y); + } + m_sink->UiMouseMove(x, y); break; case Owner::Attack: break; @@ -168,8 +177,8 @@ void TouchRouter::Release(std::uint64_t id, Finger& finger, bool cancelled) { switch (finger.owner) { case Owner::Ui: m_ui_finger = 0; - // A cancelled UI finger still releases the button so no window keeps the capture. - if (m_sink) m_sink->UiMouseUp(finger.x, finger.y); + // 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); break; case Owner::Attack: m_attack_finger = 0; @@ -195,7 +204,12 @@ void TouchRouter::FingerUp(std::uint64_t raw_id, float x, float y, std::uint64_t m_fingers.erase(it); finger.x = x; finger.y = y; - if (finger.owner == Owner::Ui && m_sink) m_sink->UiMouseMove(x, y); + if (finger.owner == Owner::Ui && finger.ui == Finger::Ui::Pending) { + m_ui_finger = 0; + UiTap(finger, ms); + return; + } + if (finger.owner == Owner::Ui && finger.ui == Finger::Ui::Pressed && m_sink) m_sink->UiMouseMove(x, y); if (finger.owner == Owner::Camera && finger.tap && (ms - finger.start_ms > m_layout.tap_ms || std::hypot(x - finger.start_x, y - finger.start_y) > m_layout.tap_slop)) @@ -203,6 +217,40 @@ void TouchRouter::FingerUp(std::uint64_t raw_id, float x, float y, std::uint64_t Release(id, finger, false); } +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. + const float x = finger.start_x, y = finger.start_y; + const bool second = m_last_tap_valid && finger.start_ms >= m_last_tap_ms && + finger.start_ms - m_last_tap_ms <= m_layout.double_tap_ms && + std::hypot(x - m_last_tap_x, y - m_last_tap_y) <= m_layout.double_tap_slop; + m_sink->UiMouseMove(x, y); + m_sink->UiMouseDown(x, y); + if (second) m_sink->UiDoubleClick(x, y); + m_sink->UiMouseUp(x, y); + // A third tap starts a new pair. + m_last_tap_valid = !second; + m_last_tap_ms = ms; + m_last_tap_x = x; + m_last_tap_y = y; +} + +void TouchRouter::Update(std::uint64_t ms) { + if (!m_ui_finger || !m_sink) return; + auto it = m_fingers.find(m_ui_finger); + if (it == m_fingers.end()) return; + Finger& finger = it->second; + if (finger.ui != Finger::Ui::Pending || ms < finger.start_ms || ms - finger.start_ms < m_layout.long_press_ms) return; + m_last_tap_valid = false; + if (m_sink->UiLongPress(finger.x, finger.y)) { + finger.ui = Finger::Ui::Consumed; + return; + } + finger.ui = Finger::Ui::Pressed; + m_sink->UiMouseMove(finger.x, finger.y); + m_sink->UiMouseDown(finger.x, finger.y); +} + void TouchRouter::CancelAll() { for (auto& [id, finger] : m_fingers) Release(id, finger, true); m_fingers.clear(); diff --git a/src/app/input/touch_router.h b/src/app/input/touch_router.h index 1a7b0e46..1183fe02 100644 --- a/src/app/input/touch_router.h +++ b/src/app/input/touch_router.h @@ -3,12 +3,25 @@ // // Every finger is assigned an owner when it goes down and keeps it until it lifts: // -// UI window under the finger -> mouse events for the window manager (one finger at a time) +// UI window under the finger -> mouse events for the window manager (one finger at a time), +// see "UI gestures" below // attack button circle -> attack key held while the finger is down // joystick area (lower left) -> floating virtual joystick, camera-relative movement // anything else -> one finger drags the camera, two fingers pinch-zoom, // a short tap clicks the game world (target / ground) // +// UI gestures. The button press is held back until the finger shows what it is doing: +// +// tap -> down + up where the finger lifts (a click: pick up / drop an item) +// second tap nearby -> down + double click + up, as PythonApplicationEvent.cpp sends a mouse +// double click (use / equip an item) +// move past tap_slop -> down at the start point, then moves (drag an item, scroll, move a window) +// hold long_press_ms -> UiLongPress; when the host takes it (item details / action sheet) the +// 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. +// // 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. @@ -24,6 +37,10 @@ struct TouchSink { virtual void UiMouseMove(float x, float y) = 0; virtual void UiMouseDown(float x, float y) = 0; virtual void UiMouseUp(float x, float y) = 0; + // Second tap of a double tap, between its UiMouseDown and UiMouseUp. + 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; } // 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. @@ -45,6 +62,9 @@ struct TouchLayout { float attack_x = 0.0f, attack_y = 0.0f, attack_r = 0.0f; float tap_slop = 14.0f; // movement that turns a tap into a camera drag std::uint64_t tap_ms = 300; // longer presses are not taps + 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; // 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; @@ -75,6 +95,8 @@ 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); + // 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(); @@ -89,6 +111,9 @@ private: float start_x = 0.0f, start_y = 0.0f; std::uint64_t start_ms = 0; bool tap = true; // still a tap candidate (camera fingers) + // UI fingers: Pending holds the button press back, Pressed sent it, Consumed was a long + // press the host took. + enum class Ui { Pending, Pressed, Consumed } ui = Ui::Pending; }; bool InAttack(float x, float y) const; @@ -96,6 +121,7 @@ private: void UpdateJoystick(float x, float y); Finger* OtherCameraFinger(std::uint64_t id); void Release(std::uint64_t id, Finger& finger, bool cancelled); + void UiTap(const Finger& finger, std::uint64_t ms); TouchSink* m_sink = nullptr; TouchLayout m_layout; @@ -103,6 +129,10 @@ private: std::map 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; + // 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; + float m_last_tap_x = 0.0f, m_last_tap_y = 0.0f; }; // The client's router, shared by the host loop and the Python `touch` module. diff --git a/src/eter_ui/PythonWindow.cpp b/src/eter_ui/PythonWindow.cpp index 3d6aefd7..3ebbf65c 100644 --- a/src/eter_ui/PythonWindow.cpp +++ b/src/eter_ui/PythonWindow.cpp @@ -2255,6 +2255,12 @@ namespace UI SSlot& slot = it->second; slot.dwItemIndex = 0; slot.isItem = false; + // EterPythonLib/PythonSlotWindow.cpp ClearSlot(TSlot*). + slot.dwState = 0; + slot.byxPlacedItemSize = 1; + slot.byyPlacedItemSize = 1; + slot.dwCenterSlotNumber = 0xffffffff; + slot.bRenderBaseSlotImage = true; slot.dwItemCount = 0; slot.dwGrade = 0; slot.fCoolTime = 0.0f; @@ -2341,7 +2347,10 @@ namespace UI slot.byHeight = byHeight; slot.byxPlacedItemSize = byWidth ? byWidth : 1; slot.byyPlacedItemSize = byHeight ? byHeight : 1; - slot.isItem = (pImage != nullptr); + // EterPythonLib/PythonSlotWindow.cpp SetSlot: a slot holds an item when it has a virtual + // number (the mouse events and tooltips key on this), and setting it resets the state. + slot.dwState = 0; + slot.isItem = dwItemIndex > 0; if (pImage) { if (!slot.pInstance) slot.pInstance = CGraphicImageInstance::New(); @@ -2647,6 +2656,266 @@ namespace UI return FALSE; } + // EterPythonLib/PythonSlotWindow.cpp GetPickedSlotPointer. Slots are searched in index order + // (the original list order for the top-left-first layouts), so a multi-cell item is found by + // its own slot before the cells it covers; its size counts even if the cell size is smaller. + BOOL CSlotWindow::GetPickedSlotPointer(SSlot** ppSlot) + { + long lx, ly; + CWindowManager::Instance().GetMousePosition(lx, ly); + + const int ixLocal = lx - m_rect.left; + const int iyLocal = ly - m_rect.top; + + for (auto& pair : m_slotMap) + { + SSlot& rSlot = pair.second; + + int ixCellSize = rSlot.ixCellSize; + int iyCellSize = rSlot.iyCellSize; + + if (rSlot.isItem) + { + ixCellSize = std::max(rSlot.ixCellSize, int(rSlot.byxPlacedItemSize * 32)); + iyCellSize = std::max(rSlot.iyCellSize, int(rSlot.byyPlacedItemSize * 32)); + } + + if (ixLocal >= rSlot.ixPosition && iyLocal >= rSlot.iyPosition && + ixLocal <= rSlot.ixPosition + ixCellSize && iyLocal <= rSlot.iyPosition + iyCellSize) + { + *ppSlot = &rSlot; + return TRUE; + } + } + + return FALSE; + } + + // EterPythonLib/PythonSlotWindow.cpp RefreshSlot: re-issue the tooltip of the slot under the + // mouse, since its item may have changed. + void CSlotWindow::RefreshSlot() + { + OnRefreshSlot(); + + if (IsRendering()) + { + SSlot* pSlot; + if (GetPickedSlotPointer(&pSlot)) + { + if (pSlot->isItem) + { + OnOverOutItem(); + OnOverInItem(pSlot->dwSlotNumber); + } + else + { + OnOverOut(); + OnOverIn(pSlot->dwSlotNumber); + } + } + } + } + + // Mouse events: EterPythonLib/PythonSlotWindow.cpp (WJ_ENABLE_TRADABLE_ICON branches, which + // AVA2 builds with). + BOOL CSlotWindow::OnMouseLeftButtonDown() + { + SSlot* pSlot; + if (!GetPickedSlotPointer(&pSlot)) + { + UI::CWindowManager::Instance().DeattachIcon(); + return TRUE; + } + + if (!(pSlot->dwState & SLOT_STATE_CANT_MOUSE_EVENT)) + { + if (pSlot->isItem && !(pSlot->dwState & SLOT_STATE_LOCK)) + OnSelectItemSlot(pSlot->dwSlotNumber); + else + OnSelectEmptySlot(pSlot->dwSlotNumber); + } + + return TRUE; + } + + BOOL CSlotWindow::OnMouseLeftButtonUp() + { + auto& wm = UI::CWindowManager::Instance(); + if (wm.IsAttaching() && wm.IsDragging() && IsIn()) + { + UI::CWindow* pWin = wm.GetPointWindow(); + + if (pWin && pWin->IsType(UI::CSlotWindow::Type())) + { + auto* pSlotWin = static_cast(pWin); + + SSlot* pSlot; + if (!pSlotWin->GetPickedSlotPointer(&pSlot)) + { + wm.DeattachIcon(); + return TRUE; + } + + if (!(pSlot->dwState & SLOT_STATE_CANT_MOUSE_EVENT)) + { + if (pSlot->isItem) + pSlotWin->OnSelectItemSlot(pSlot->dwSlotNumber); + else + pSlotWin->OnSelectEmptySlot(pSlot->dwSlotNumber); + } + + return TRUE; + } + + return FALSE; + } + + return FALSE; + } + + BOOL CSlotWindow::OnMouseRightButtonDown() + { + SSlot* pSlot; + if (!GetPickedSlotPointer(&pSlot)) + return TRUE; + + if (!(pSlot->dwState & SLOT_STATE_CANT_MOUSE_EVENT)) + { + if (pSlot->isItem) + OnUnselectItemSlot(pSlot->dwSlotNumber); + else + OnUnselectEmptySlot(pSlot->dwSlotNumber); + } + + return TRUE; + } + + BOOL CSlotWindow::OnMouseLeftButtonDoubleClick() + { + SSlot* pSlot; + if (GetPickedSlotPointer(&pSlot)) + if (!(pSlot->dwState & SLOT_STATE_CANT_MOUSE_EVENT)) + OnUseSlot(); + + return TRUE; + } + + void CSlotWindow::OnMouseOverOut() + { + OnOverOutItem(); + OnOverOut(); + } + + void CSlotWindow::OnMouseOver() + { + SSlot* pSlot; + CWindow* pPointWindow = UI::CWindowManager::Instance().GetPointWindow(); + if (this == pPointWindow) + { + if (GetPickedSlotPointer(&pSlot)) + { + if (OnOverInItem(pSlot->dwSlotNumber)) + return; + else + OnOverOutItem(); + + if (OnOverIn(pSlot->dwSlotNumber)) + return; + else + OnOverOut(); + + return; + } + } + + OnOverOutItem(); + OnOverOut(); + } + + void CSlotWindow::OnSelectEmptySlot(int iSlotNumber) + { + PyCallClassMemberFunc(m_poHandler, "OnSelectEmptySlot", Py_BuildValue("(i)", iSlotNumber)); + } + + void CSlotWindow::OnSelectItemSlot(int iSlotNumber) + { + PyCallClassMemberFunc(m_poHandler, "OnSelectItemSlot", Py_BuildValue("(i)", iSlotNumber)); + + if (UI::CWindowManager::Instance().IsAttaching()) + OnOverOutItem(); + } + + void CSlotWindow::OnUnselectEmptySlot(int iSlotNumber) + { + PyCallClassMemberFunc(m_poHandler, "OnUnselectEmptySlot", Py_BuildValue("(i)", iSlotNumber)); + } + + void CSlotWindow::OnUnselectItemSlot(int iSlotNumber) + { + PyCallClassMemberFunc(m_poHandler, "OnUnselectItemSlot", Py_BuildValue("(i)", iSlotNumber)); + } + + void CSlotWindow::OnUseSlot() + { + SSlot* pSlot; + if (GetPickedSlotPointer(&pSlot)) + if (pSlot->isItem) + PyCallClassMemberFunc(m_poHandler, "OnUseSlot", Py_BuildValue("(i)", pSlot->dwSlotNumber)); + } + + BOOL CSlotWindow::OnOverIn(DWORD dwSlotNumber) + { + SSlot* pSlot; + if (!GetSlotPointer(dwSlotNumber, &pSlot)) + return FALSE; + + if (pSlot->isItem) + return FALSE; + + if (pSlot->dwSlotNumber == m_dwOverInSlotNumber) + return TRUE; + + m_dwOverInSlotNumber = dwSlotNumber; + PyCallClassMemberFunc(m_poHandler, "OnOverIn", Py_BuildValue("(i)", dwSlotNumber)); + return TRUE; + } + + void CSlotWindow::OnOverOut() + { + if (SLOT_NUMBER_NONE == m_dwOverInSlotNumber) + return; + + m_dwOverInSlotNumber = SLOT_NUMBER_NONE; + PyCallClassMemberFunc(m_poHandler, "OnOverOut", Py_BuildValue("()")); + } + + BOOL CSlotWindow::OnOverInItem(DWORD dwSlotNumber) + { + SSlot* pSlot; + if (!GetSlotPointer(dwSlotNumber, &pSlot)) + return FALSE; + + if (!pSlot->isItem) + return FALSE; + + if (pSlot->dwSlotNumber == m_dwToolTipSlotNumber) + return TRUE; + + m_dwToolTipSlotNumber = dwSlotNumber; + PyCallClassMemberFunc(m_poHandler, "OnOverInItem", + Py_BuildValue("(iii)", dwSlotNumber, pSlot->dwItemIndex, pSlot->dwItemID)); + return TRUE; + } + + void CSlotWindow::OnOverOutItem() + { + if (SLOT_NUMBER_NONE == m_dwToolTipSlotNumber) + return; + + m_dwToolTipSlotNumber = SLOT_NUMBER_NONE; + PyCallClassMemberFunc(m_poHandler, "OnOverOutItem", Py_BuildValue("()")); + } + void CSlotWindow::RenderSlotBaseImage() { if (!m_pBaseImageInstance) return; @@ -2963,105 +3232,259 @@ namespace UI CGridSlotWindow::CGridSlotWindow(PyObject * ppyObject) : CSlotWindow(ppyObject) {} CGridSlotWindow::~CGridSlotWindow() {} + // EterPythonLib/PythonGridSlotWindow.cpp ArrangeGridSlot (slot numbers stand in for the + // original's slot pointers, so ClearAllSlot cannot leave them dangling). void CGridSlotWindow::ArrangeGridSlot(DWORD dwStartIndex, DWORD dwxCount, DWORD dwyCount, int ixSlotSize, int iySlotSize, int ixTemporarySize, int iyTemporarySize) { ClearAllSlot(); + + m_dwxCount = dwxCount; + m_dwyCount = dwyCount; + m_SlotVector.assign(dwxCount * dwyCount, SLOT_NUMBER_NONE); + for (DWORD y = 0; y < dwyCount; ++y) { for (DWORD x = 0; x < dwxCount; ++x) { DWORD dwIndex = dwStartIndex + x + y * dwxCount; int ixPos = x * (ixSlotSize + ixTemporarySize); int iyPos = y * (iySlotSize + iyTemporarySize); AppendSlot(dwIndex, ixPos, iyPos, ixSlotSize, iySlotSize); + m_SlotVector[x + y * dwxCount] = dwIndex; } } SetSize(dwxCount * (ixSlotSize + ixTemporarySize), dwyCount * (iySlotSize + iyTemporarySize)); } - BOOL CGridSlotWindow::GetPickedSlotList(BYTE byWidth, BYTE byHeight, std::vector* pSlotList) + BOOL CGridSlotWindow::GetGridSlotPointer(int ix, int iy, SSlot** ppSlot) { - if (!pSlotList) return FALSE; - pSlotList->clear(); + if (ix < 0 || iy < 0 || DWORD(ix) >= m_dwxCount) + return FALSE; + DWORD dwSlotIndex = ix + iy * m_dwxCount; + if (dwSlotIndex >= m_SlotVector.size()) + return FALSE; + return GetSlotPointer(m_SlotVector[dwSlotIndex], ppSlot); + } - long lx = 0, ly = 0; - GetMouseLocalPosition(lx, ly); - - if (lx < 0 || ly < 0) return FALSE; - - for (auto& pair : m_slotMap) + BOOL CGridSlotWindow::GetPickedGridSlotPosition(int ixLocal, int iyLocal, int* pix, int* piy) + { + for (DWORD x = 0; x < m_dwxCount; ++x) + for (DWORD y = 0; y < m_dwyCount; ++y) { - SSlot& s = pair.second; - if (lx >= s.ixPosition && lx < s.ixPosition + s.ixCellSize && - ly >= s.iyPosition && ly < s.iyPosition + s.iyCellSize) + SSlot* pSlot; + if (!GetGridSlotPointer(x, y, &pSlot)) + continue; + + if (ixLocal >= pSlot->ixPosition && iyLocal >= pSlot->iyPosition && + ixLocal <= pSlot->ixPosition + pSlot->ixCellSize && iyLocal <= pSlot->iyPosition + pSlot->iyCellSize) { - for (BYTE y = 0; y < byHeight; ++y) - { - for (BYTE x = 0; x < byWidth; ++x) - { - int targetX = s.ixPosition + x * s.ixCellSize; - int targetY = s.iyPosition + y * s.iyCellSize; - bool found = false; - for (auto& p2 : m_slotMap) - { - if (std::abs(p2.second.ixPosition - targetX) < 4 && - std::abs(p2.second.iyPosition - targetY) < 4) - { - pSlotList->push_back(&p2.second); - found = true; - break; - } - } - if (!found) return FALSE; - } - } + *pix = x; + *piy = y; return TRUE; } } + return FALSE; } + // EterPythonLib/PythonGridSlotWindow.cpp GetPickedSlotList: the cells an attached item of + // iWidth x iHeight would cover around the mouse, shifted to stay inside the grid; in use mode + // (refine scrolls) a single picked cell expands to the whole item under it. + BOOL CGridSlotWindow::GetPickedSlotList(BYTE iWidth, BYTE iHeight, std::vector* pSlotPointerList) + { + if (!pSlotPointerList) return FALSE; + + long lx, ly; + GetMouseLocalPosition(lx, ly); + + if (lx < 0 || ly < 0 || lx >= GetWidth() || ly >= GetHeight()) + return FALSE; + + pSlotPointerList->clear(); + + int ix, iy; + if (GetPickedGridSlotPosition(lx, ly, &ix, &iy)) + { + int ixHalfStep = (iWidth / 2); + + int ixStart = int(ix) - int(ixHalfStep - (ixHalfStep % 2)); + int ixEnd = int(ix) + int(ixHalfStep); + + int iyStart = 0, iyEnd = 0; + + if (1 == iHeight) + { + iyStart = iy; + iyEnd = iy; + } + else if (2 == iHeight) + { + iyStart = iy; + iyEnd = iy + 1; + } + else if (3 == iHeight) + { + iyStart = iy - 1; + iyEnd = iy + 1; + } + + if (ixStart < 0) + { + ixEnd += -ixStart; + ixStart = 0; + } + + if (iyStart < 0) + { + iyEnd += -iyStart; + iyStart = 0; + } + + if (DWORD(ixEnd) >= m_dwxCount) + { + int ixTemporary = DWORD(ixEnd) - m_dwxCount + 1; + ixStart -= ixTemporary; + ixEnd -= ixTemporary; + } + + if (DWORD(iyEnd) >= m_dwyCount) + { + int iyTemporary = DWORD(iyEnd) - m_dwyCount + 1; + iyStart -= iyTemporary; + iyEnd -= iyTemporary; + } + + for (int i = ixStart; i <= ixEnd; ++i) + for (int j = iyStart; j <= iyEnd; ++j) + { + SSlot* pSlot; + if (GetGridSlotPointer(i, j, &pSlot)) + pSlotPointerList->push_back(pSlot); + } + + if (m_isUseMode && 1 == pSlotPointerList->size()) + { + SSlot* pMainSlot = pSlotPointerList->front(); + + for (DWORD dwSlotNumber : m_SlotVector) + { + SSlot* pSlot; + if (!GetSlotPointer(dwSlotNumber, &pSlot)) + continue; + if (pSlot->dwCenterSlotNumber == pMainSlot->dwCenterSlotNumber) + if (pSlotPointerList->end() == std::find(pSlotPointerList->begin(), pSlotPointerList->end(), pSlot)) + pSlotPointerList->push_back(pSlot); + } + } + + if (!pSlotPointerList->empty()) + return TRUE; + } + + return FALSE; + } + + // EterPythonLib/PythonGridSlotWindow.cpp OnRefreshSlot: every cell an item covers points at + // the item's slot (dwCenterSlotNumber) and carries its virtual number. + void CGridSlotWindow::OnRefreshSlot() + { + DWORD x, y; + + for (x = 0; x < m_dwxCount; ++x) + for (y = 0; y < m_dwyCount; ++y) + { + SSlot* pSlot; + if (!GetGridSlotPointer(x, y, &pSlot)) + continue; + + pSlot->dwCenterSlotNumber = pSlot->dwSlotNumber; + } + + for (x = 0; x < m_dwxCount; ++x) + for (y = 0; y < m_dwyCount; ++y) + { + SSlot* pSlot; + if (!GetGridSlotPointer(x, y, &pSlot)) + continue; + + if (pSlot->isItem) + { + for (DWORD xSub = 0; xSub < pSlot->byxPlacedItemSize; ++xSub) + for (DWORD ySub = 0; ySub < pSlot->byyPlacedItemSize; ++ySub) + { + SSlot* pSubSlot; + if (!GetGridSlotPointer(x + xSub, y + ySub, &pSubSlot)) + continue; + + pSubSlot->dwCenterSlotNumber = pSlot->dwSlotNumber; + pSubSlot->dwItemIndex = pSlot->dwItemIndex; + } + } + else + { + pSlot->dwItemIndex = 0; + } + } + } + + // EterPythonLib/PythonGridSlotWindow.cpp CheckMoving: in a pick-up grid the attached item may + // only land on its own cells, empty cells, or a stack of the same item. BOOL CGridSlotWindow::CheckMoving(DWORD dwSlotNumber, DWORD dwItemIndex, const std::vector& slotList) { + if (m_dwSlotStyle != SLOT_STYLE_PICK_UP) + return TRUE; + for (auto* pSlot : slotList) { - if (pSlot->isItem && pSlot->dwSlotNumber != dwSlotNumber && pSlot->dwCenterSlotNumber != dwSlotNumber) - return FALSE; + if (dwSlotNumber != pSlot->dwCenterSlotNumber) + { + if (0 != pSlot->dwItemIndex || pSlot->dwCenterSlotNumber != pSlot->dwSlotNumber) + { + if (dwItemIndex != pSlot->dwItemIndex) + return FALSE; + } + } } + return TRUE; } + // EterPythonLib/PythonGridSlotWindow.cpp GetPickedSlotPointer: with an icon attached, the + // lowest-numbered covered cell (preferring one with an item) resolves to its item's slot, + // except that the attached item's own slot resolves to the cell itself. BOOL CGridSlotWindow::GetPickedSlotPointer(SSlot** ppSlot) { if (!UI::CWindowManager::Instance().IsAttaching()) - { - long lx = 0, ly = 0; - GetMouseLocalPosition(lx, ly); - for (auto& pair : m_slotMap) - { - SSlot& s = pair.second; - if (lx >= s.ixPosition && lx < s.ixPosition + s.ixCellSize && - ly >= s.iyPosition && ly < s.iyPosition + s.iyCellSize) - { - if (ppSlot) *ppSlot = &s; - return TRUE; - } - } - return FALSE; - } + return CSlotWindow::GetPickedSlotPointer(ppSlot); BYTE byWidth = 1, byHeight = 1; UI::CWindowManager::Instance().GetAttachingIconSize(&byWidth, &byHeight); std::vector slotList; - if (!GetPickedSlotList(byWidth, byHeight, &slotList) || slotList.empty()) + if (!GetPickedSlotList(byWidth, byHeight, &slotList)) return FALSE; - SSlot* pMin = slotList.front(); - for (auto* s : slotList) + SSlot* pMinSlot = nullptr; + for (auto* pSlot : slotList) { - if (s->dwSlotNumber < pMin->dwSlotNumber) - pMin = s; + if (!pMinSlot) + pMinSlot = pSlot; + else if (pSlot->dwSlotNumber < pMinSlot->dwSlotNumber) + pMinSlot = pSlot; + else if (!pMinSlot->isItem && pSlot->isItem) + pMinSlot = pSlot; } - if (ppSlot) *ppSlot = pMin; + + if (!pMinSlot) + return FALSE; + + SSlot* pCenterSlot; + if (!GetSlotPointer(pMinSlot->dwCenterSlotNumber, &pCenterSlot)) + return FALSE; + + *ppSlot = pCenterSlot; + + if (UI::CWindowManager::Instance().GetAttachingSlotNumber() == pCenterSlot->dwSlotNumber) + *ppSlot = pMinSlot; + return TRUE; } diff --git a/src/eter_ui/PythonWindow.h b/src/eter_ui/PythonWindow.h index a7f4383b..9407c8d6 100644 --- a/src/eter_ui/PythonWindow.h +++ b/src/eter_ui/PythonWindow.h @@ -772,7 +772,7 @@ namespace UI void RestoreSlotCoolTime(DWORD dwKey); void ActivateSlot(DWORD dwIndex); void DeactivateSlot(DWORD dwIndex); - void RefreshSlot() {} + void RefreshSlot(); DWORD GetSlotCount() { return (DWORD)m_slotMap.size(); } void LockSlot(DWORD dwIndex); void UnlockSlot(DWORD dwIndex); @@ -803,10 +803,33 @@ namespace UI BOOL GetSlotPointer(DWORD dwIndex, SSlot** ppSlot); BOOL GetSelectedSlotPointer(SSlot** ppSlot); + // The slot under the mouse (EterPythonLib/PythonSlotWindow.cpp GetPickedSlotPointer). + virtual BOOL GetPickedSlotPointer(SSlot** ppSlot); + + // Mouse events and their Python callbacks (EterPythonLib/PythonSlotWindow.cpp). + BOOL OnMouseLeftButtonDown() override; + BOOL OnMouseLeftButtonUp() override; + BOOL OnMouseRightButtonDown() override; + BOOL OnMouseLeftButtonDoubleClick() override; + void OnMouseOverOut() override; + void OnMouseOver() override; + + void OnSelectEmptySlot(int iSlotNumber); + void OnSelectItemSlot(int iSlotNumber); + void OnUnselectEmptySlot(int iSlotNumber); + void OnUnselectItemSlot(int iSlotNumber); + void OnUseSlot(); + BOOL OnOverIn(DWORD dwSlotNumber); + void OnOverOut(); + BOOL OnOverInItem(DWORD dwSlotNumber); + void OnOverOutItem(); + + static constexpr DWORD SLOT_NUMBER_NONE = 0xffffffff; protected: void OnRender() override; BOOL OnIsType(DWORD dwType) override; + virtual void OnRefreshSlot() {} virtual void OnRenderPickingSlot(); virtual void OnRenderSelectedSlot(); void RenderSlotBaseImage(); @@ -817,6 +840,8 @@ namespace UI DWORD m_dwSlotStyle; BOOL m_isUseMode; BOOL m_isUsableItem; + DWORD m_dwToolTipSlotNumber = SLOT_NUMBER_NONE; + DWORD m_dwOverInSlotNumber = SLOT_NUMBER_NONE; std::vector m_slots; std::vector m_dwSelectedSlotIndexList; std::map m_slotMap; @@ -833,10 +858,17 @@ namespace UI virtual ~CGridSlotWindow(); void ArrangeGridSlot(DWORD dwStartIndex, DWORD dwxCount, DWORD dwyCount, int ixSlotSize, int iySlotSize, int ixTemporarySize, int iyTemporarySize); BOOL GetPickedSlotList(BYTE byWidth, BYTE byHeight, std::vector* pSlotList); - BOOL GetPickedSlotPointer(SSlot** ppSlot); + BOOL GetPickedSlotPointer(SSlot** ppSlot) override; BOOL CheckMoving(DWORD dwSlotNumber, DWORD dwItemIndex, const std::vector& slotList); protected: void OnRenderPickingSlot() override; BOOL OnIsType(DWORD dwType) override; + void OnRefreshSlot() override; + BOOL GetGridSlotPointer(int ix, int iy, SSlot** ppSlot); + BOOL GetPickedGridSlotPosition(int ixLocal, int iyLocal, int* pix, int* piy); + + DWORD m_dwxCount = 0, m_dwyCount = 0; + // Slot numbers by grid cell x + y * m_dwxCount (the original keeps slot pointers). + std::vector m_SlotVector; }; } diff --git a/src/eter_ui/PythonWindowManager.cpp b/src/eter_ui/PythonWindowManager.cpp index e19f23c3..97cfe2db 100644 --- a/src/eter_ui/PythonWindowManager.cpp +++ b/src/eter_ui/PythonWindowManager.cpp @@ -1,5 +1,6 @@ #include "PythonWindowManager.h" #include +#include namespace UI { @@ -119,9 +120,16 @@ namespace UI ry = m_lMouseY; } + // EterPythonLib/PythonWindowManager.cpp: dragging means the mouse moved 10 px (Manhattan) + // since the left button went down; a click on a slot picks the item up instead of dropping it. BOOL CWindowManager::IsDragging() { - return (m_pLeftCaptureWindow != nullptr); + long ldx = std::labs(m_lMouseX - m_lPickedX); + long ldy = std::labs(m_lMouseY - m_lPickedY); + if (ldx + ldy < 10) + return FALSE; + + return TRUE; } void CWindowManager::SetParent(CWindow * pWindow, CWindow * pParentWindow) @@ -361,10 +369,11 @@ namespace UI m_pPointWindow = pPointWindow; if (m_pPointWindow) m_pPointWindow->OnMouseOverIn(); } - else if (m_pPointWindow) - { + + // The original calls OnMouseOver on every move, also right after OnMouseOverIn (slot + // windows show the tooltip of the slot under the mouse from it). + if (m_pPointWindow) m_pPointWindow->OnMouseOver(); - } } void CWindowManager::RunMouseLeftButtonDown(long x, long y) @@ -381,6 +390,8 @@ namespace UI m_lDragX = m_lMouseX - pWin->GetRect().left; m_lDragY = m_lMouseY - pWin->GetRect().top; + m_lPickedX = m_lMouseX; + m_lPickedY = m_lMouseY; pWin->OnMouseLeftButtonDown(); } @@ -394,11 +405,11 @@ namespace UI m_pLeftCaptureWindow = nullptr; return; } - m_pLeftCaptureWindow = nullptr; } + // The capture stays set while the window under the mouse gets the event: a slot window + // there drops the dragged icon only while wndMgr.IsDragging()/capture hold (original). CWindow * pWin = __PickWindow(m_lMouseX, m_lMouseY); - printf("DEBUG RunMouseLeftButtonUp: pos=(%ld, %ld) m_lMouse=(%ld, %ld) picked=%s\n", x, y, m_lMouseX, m_lMouseY, pWin ? pWin->GetName() : "null"); if (!pWin) pWin = GetPointWindow(); if (pWin) { @@ -416,16 +427,42 @@ namespace UI if (pWin) pWin->OnMouseLeftButtonDoubleClick(); } + // EterPythonLib/PythonWindowManager.cpp: the right button captures like the left one, and + // releasing it drops any attached icon. void CWindowManager::RunMouseRightButtonDown(long x, long y) { SetMousePosition(x, y); - if (m_pPointWindow) m_pPointWindow->OnMouseRightButtonDown(); + CWindow * pWin = GetPointWindow(); + if (!pWin) + return; + + if (pWin->IsFlag(CWindow::FLAG_ATTACH)) + pWin = pWin->GetRoot(); + + if (!pWin->IsFlag(CWindow::FLAG_NOT_CAPTURE)) + m_pRightCaptureWindow = pWin; + + pWin->OnMouseRightButtonDown(); } void CWindowManager::RunMouseRightButtonUp(long x, long y) { SetMousePosition(x, y); - if (m_pPointWindow) m_pPointWindow->OnMouseRightButtonUp(); + if (m_pRightCaptureWindow) + { + if (m_pRightCaptureWindow->OnMouseRightButtonUp()) + { + m_pRightCaptureWindow = nullptr; + return; + } + } + + CWindow * pWin = GetPointWindow(); + if (pWin) + pWin->OnMouseRightButtonUp(); + + m_pRightCaptureWindow = nullptr; + DeattachIcon(); } void CWindowManager::RunMouseRightButtonDoubleClick(long x, long y) diff --git a/src/scripting/python_app.cpp b/src/scripting/python_app.cpp index 0f2760e8..b99f2928 100644 --- a/src/scripting/python_app.cpp +++ b/src/scripting/python_app.cpp @@ -363,7 +363,17 @@ PyObject* app_set_hair_color_enable(PyObject*, PyObject* args) { int f = 0; if(! PyObject* app_set_armor_specular_enable(PyObject*, PyObject* args) { int f = 0; if(!PyArg_ParseTuple(args, "i", &f)) return nullptr; s_armor_specular = (f != 0); NetworkActorManager::SetArmorSpecularEnable(f != 0); Py_RETURN_NONE; } PyObject* app_set_weapon_specular_enable(PyObject*, PyObject* args) { int f = 0; if(!PyArg_ParseTuple(args, "i", &f)) return nullptr; s_weapon_specular = (f != 0); Py_RETURN_NONE; } -PyObject* app_set_mouse_handler(PyObject*, PyObject* args) { Py_RETURN_NONE; } +// app.SetMouseHandler (CPythonApplication::SetMouseHandler): mouseModule.mouseController, which +// draws the icon attached to the mouse; RunMouseHandler is OnMouseUpdate + OnMouseRender. +PyObject* s_mouse_handler = nullptr; +PyObject* app_set_mouse_handler(PyObject*, PyObject* args) { + PyObject* handler = nullptr; + if (!PyArg_ParseTuple(args, "O", &handler)) return nullptr; + Py_XINCREF(handler); + Py_XDECREF(s_mouse_handler); + s_mouse_handler = handler; + Py_RETURN_NONE; +} // appCreate: after the window, LoadPythonPlayerSettingsModuleLibrary registers the // PythonPlayerSettingsModule tables and CPythonPlayerSettingsModule::Load runs // introLoading.__main__ (name/title colours, title names, emotion icons, dungeon names), @@ -394,7 +404,16 @@ PyObject* app_abort(PyObject*, PyObject*) { SDL_PushEvent(&ev); Py_RETURN_NONE; } -PyObject* app_is_pressed(PyObject*, PyObject* args) { return PyInt_FromLong(0); } +// app.IsPressed(DIK_*) (CPythonApplication::IsPressed, the DirectInput keyboard state): the host +// reports key changes as DIK codes; touch actions can hold a key virtually (SetVirtualKey). +bool s_dik_pressed[256] = {}; +bool s_dik_virtual[256] = {}; +PyObject* app_is_pressed(PyObject*, PyObject* args) { + int key = 0; + if (!PyArg_ParseTuple(args, "i", &key)) return nullptr; + const bool pressed = key >= 0 && key < 256 && (s_dik_pressed[key] || s_dik_virtual[key]); + return PyInt_FromLong(pressed ? 1 : 0); +} PyObject* app_get_locale_service_name(PyObject*, PyObject*) { return PyString_FromString(s_locale_service_name.c_str()); @@ -493,7 +512,15 @@ std::uint32_t GetLocalMSec() { return local_milliseconds(); } std::int32_t GetApplicationSeconds() { return static_cast(std::chrono::duration_cast(std::chrono::steady_clock::now()-app_start).count()); } void SetServerMSec(std::uint32_t time) { if(time==0) return;elt_server_time=time;elt_client_time=local_milliseconds(); ELTimer_SetServerMSec(time); } std::uint32_t GetServerMSec() { return local_milliseconds()-elt_client_time+elt_server_time; } -void ResetPythonAppState() { s_cameraSettingPending=false; elt_server_time=elt_client_time=0;ELTimer_Init();server_clock=ServerClock(local_milliseconds());app_start=std::chrono::steady_clock::now();s_test_server_flag=false;twoHandedAttackSpeedDecrease=0; } +void SetKeyPressed(int dik, bool pressed) { if (dik >= 0 && dik < 256) s_dik_pressed[dik] = pressed; } +void SetVirtualKey(int dik, bool pressed) { if (dik >= 0 && dik < 256) s_dik_virtual[dik] = pressed; } +void ReleaseAllKeys() { for (int i = 0; i < 256; ++i) s_dik_pressed[i] = s_dik_virtual[i] = false; } +void RunMouseHandler(long x, long y) { + if (!s_mouse_handler) return; + PyCallClassMemberFunc(s_mouse_handler, "Update", Py_BuildValue("(ii)", int(x), int(y))); + PyCallClassMemberFunc(s_mouse_handler, "Render", Py_BuildValue("()")); +} +void ResetPythonAppState() { s_mouse_handler=nullptr;ReleaseAllKeys();s_cameraSettingPending=false; elt_server_time=elt_client_time=0;ELTimer_Init();server_clock=ServerClock(local_milliseconds());app_start=std::chrono::steady_clock::now();s_test_server_flag=false;twoHandedAttackSpeedDecrease=0; } std::uint8_t GetEnterGameLanguage() { const char* locales[]={"ymir","en","de","tr","pt","es","fr","ro","pl","it","cz","hu"}; for(std::uint8_t i=1;i<12;++i) if(s_locale_name==locales[i]) return i; diff --git a/src/scripting/python_app.h b/src/scripting/python_app.h index 7e34896b..ed49b558 100644 --- a/src/scripting/python_app.h +++ b/src/scripting/python_app.h @@ -9,6 +9,12 @@ bool IsYmirLocale(); std::int32_t GetServerTimeStamp(); void SetServerTime(std::int32_t time); void ResetPythonAppState(); +// app.IsPressed key state in DIK codes: host keyboard, and keys held virtually by touch actions. +void SetKeyPressed(int dik, bool pressed); +void SetVirtualKey(int dik, bool pressed); +void ReleaseAllKeys(); +// Calls the app.SetMouseHandler object's Update(x, y) and Render() (attached icon, cursor). +void RunMouseHandler(long x, long y); std::uint32_t GetLocalMSec(); std::int32_t GetApplicationSeconds(); void SetServerMSec(std::uint32_t time); diff --git a/src/scripting/python_touch.cpp b/src/scripting/python_touch.cpp index 12db986f..3d4a8d25 100644 --- a/src/scripting/python_touch.cpp +++ b/src/scripting/python_touch.cpp @@ -1,5 +1,8 @@ #include "python_touch.h" #include "app/input/touch_router.h" +#include "python_app.h" +#include "eter_ui/PythonWindow.h" +#include "eter_ui/PythonWindowManager.h" #include namespace ava2 { @@ -62,9 +65,60 @@ 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. +PyObject* s_long_press_event = nullptr; +PyObject* touch_set_long_press_event(PyObject*, PyObject* args) { + PyObject* event = nullptr; + if (!PyArg_ParseTuple(args, "O", &event)) return nullptr; + if (event == Py_None) event = nullptr; + Py_XINCREF(event); + Py_XDECREF(s_long_press_event); + s_long_press_event = event; + Py_RETURN_NONE; +} + +// SetVirtualKey(dik, pressed): holds a key for app.IsPressed, e.g. DIK_LSHIFT while an action +// runs the original Shift+click item split. +PyObject* touch_set_virtual_key(PyObject*, PyObject* args) { + int dik = 0, pressed = 0; + if (!PyArg_ParseTuple(args, "ii", &dik, &pressed)) return nullptr; + SetVirtualKey(dik, pressed != 0); + Py_RETURN_NONE; +} + } // namespace +bool RunTouchLongPress(PyObject* slot_window, unsigned long slot_number, unsigned long item_count, long x, long y) { + if (!s_long_press_event || !slot_window) return false; + PyObject* result = PyObject_CallFunction(s_long_press_event, const_cast("Okkll"), slot_window, slot_number, + item_count, x, y); + if (!result) { + PyErr_Print(); + return false; + } + const bool taken = PyObject_IsTrue(result) == 1; + Py_DECREF(result); + return taken; +} + +void TouchUiMouseUp(UI::CWindowManager& wm, long x, long y) { + wm.RunMouseLeftButtonUp(x, y); + if (!wm.IsAttaching()) wm.RunMouseMove(-10000, -10000); +} + +bool TouchUiLongPress(UI::CWindowManager& wm, long x, long y) { + if (wm.IsAttaching()) return false; + wm.RunMouseMove(x, y); + auto* slots = dynamic_cast(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); +} + void inittouch() { + // A new interpreter: the old callback object belonged to the previous one. + s_long_press_event = nullptr; static PyMethodDef methods[] = { {"IsEnabled", touch_is_enabled, METH_NOARGS, nullptr}, {"GetJoystick", touch_get_joystick, METH_NOARGS, nullptr}, @@ -73,6 +127,8 @@ void inittouch() { {"SetJoystickArea", touch_set_joystick_area, METH_VARARGS, nullptr}, {"GetLayout", touch_get_layout, METH_NOARGS, nullptr}, {"GetSafeArea", touch_get_safe_area, METH_NOARGS, nullptr}, + {"SetLongPressEvent", touch_set_long_press_event, METH_VARARGS, nullptr}, + {"SetVirtualKey", touch_set_virtual_key, METH_VARARGS, nullptr}, {nullptr, nullptr, 0, nullptr} }; Py_InitModule("touch", methods); diff --git a/src/scripting/python_touch.h b/src/scripting/python_touch.h index e4602cb4..ccded0e3 100644 --- a/src/scripting/python_touch.h +++ b/src/scripting/python_touch.h @@ -1,7 +1,21 @@ #pragma once +typedef struct _object PyObject; +namespace UI { class CWindowManager; } + 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); + +// 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. +bool TouchUiLongPress(UI::CWindowManager& wm, long x, long y); } diff --git a/tests/inventory_touch_test.cpp b/tests/inventory_touch_test.cpp new file mode 100644 index 00000000..af46ced4 --- /dev/null +++ b/tests/inventory_touch_test.cpp @@ -0,0 +1,231 @@ +// Item gestures in the original inventory (docs/MOBILE-ARCHITECTURE.md, gestures): the touch router's +// taps, drags, double taps and long presses reach uiinventory.py through the window manager and the +// ported CSlotWindow mouse handlers, as the original mouse would. +#include "python_runtime.h" +#include "python_app.h" +#include "python_touch.h" +#include "app/input/touch_router.h" +#include "eter_ui/PythonWindowManager.h" +#include "eter_image/GrpDevice.h" +#include "test_rundir.h" +#include +#include +#include +#include +#include + +namespace { + +// The UI half of app_loop's HostTouchSink. +struct UiSink final : ava2::input::TouchSink { + UI::CWindowManager* wm = nullptr; + bool IsUiAt(float x, float y) override { return wm->PickWindow(long(x), long(y)) != nullptr; } + void UiMouseMove(float x, float y) override { wm->RunMouseMove(long(x), long(y)); } + void UiMouseDown(float x, float y) override { wm->RunMouseLeftButtonDown(long(x), long(y)); } + void UiMouseUp(float x, float y) override { ava2::TouchUiMouseUp(*wm, long(x), long(y)); } + void UiDoubleClick(float x, float y) override { wm->RunMouseLeftButtonDoubleClick(long(x), long(y)); } + bool UiLongPress(float x, float y) override { return ava2::TouchUiLongPress(*wm, long(x), long(y)); } + void WorldTap(float, float) override {} + void Joystick(bool, float, float) override {} + void Attack(bool) override {} + void CameraRotate(float, float) override {} + void CameraZoom(float) override {} +}; + +PyObject* MainDict() { return PyModule_GetDict(PyImport_AddModule("__main__")); } + +// Centre of an inventory cell in UI pixels (computed by the Python side). +void Cell(ava2::PythonRuntime& runtime, int n, float& x, float& y) { + runtime.execute("cell_xy = cell(" + std::to_string(n) + ")\n"); + PyObject* xy = PyDict_GetItemString(MainDict(), "cell_xy"); + assert(xy && PyTuple_Check(xy)); + x = float(PyInt_AsLong(PyTuple_GetItem(xy, 0))); + y = float(PyInt_AsLong(PyTuple_GetItem(xy, 1))); +} + +std::uint64_t now = 10000; + +void Tap(ava2::input::TouchRouter& router, float x, float y) { + router.FingerDown(0, x, y, now); + router.FingerUp(0, x, y, now + 60); + now += 1000; // never pairs with the next tap +} + +} // namespace + +int main(int argc, char** argv) { + if (argc < 3) return 2; + if (!ava2_test::EnterTestRunDir()) return ava2_test::kSkip; + constexpr int W = 1024, H = 768; + ava2::input::SetTouchMode(true); + auto& router = ava2::input::GetTouchRouter(); + router.SetLayout(ava2::input::TouchLayout::ForScreen(W, H)); + + ava2::PythonRuntime runtime(argv[1], argv[2]); + auto* wm = runtime.window_manager(); + wm->SetScreenSize(W, H); wm->SetResolution(W, H); + SDL_Surface* surface = SDL_CreateSurface(W, H, SDL_PIXELFORMAT_ARGB8888); + SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(surface); + assert(surface && renderer); + CGraphicDevice device; + device.Init(renderer); + runtime.execute( + "import app, touch, localeInfo, networkModule, introLoading, net, player, mouseModule, wndMgr\n" + "net.SendSelectCharacterPacket=lambda slot: None\n" + "mouseModule.mouseController.Create()\n" // interfacemodule.py __main__ + "introLoading.__main__()\n" + "localeInfo.LoadLocaleData()\n" + "stream = networkModule.MainStream()\nstream.Create()\n" + "stream.SetLoadingPhase()\n" + "stream.SetGamePhase()\n" + "game = stream.curPhaseWindow\n" + "hud = game.interface.wndMobileHUD\n" + "assert hud and hud.item_actions\n" + "calls = []\n" + "net.SendItemMovePacket = lambda *a: calls.append(('move',) + a)\n" + "net.SendItemUsePacket = lambda *a: calls.append(('use',) + a)\n" + "player.RequestAddToEmptyLocalQuickSlot = lambda *a: calls.append(('quick',) + a)\n" + "inv = game.interface.wndInventory\n" + "inv.Show()\ninv.SetTop()\n" + // Red potions x3 in cell 0, one more in cell 3. + "player.SetItemData(0, 27001, 3)\n" + "player.SetItemData(3, 27001, 1)\n" + "inv.RefreshItemSlot()\n" + // Page 0 rows are unlocked: OnUpdate hides the unlock buttons over them. + "inv.OnUpdate()\n" + "g = inv.wndItem\n" + "def cell(n):\n" + " x, y = g.GetGlobalPosition()\n" + " cols = g.GetWidth() / 32\n" + " return (x + (n % cols) * 32 + 16, y + (n / cols) * 32 + 16)\n" + "def attached():\n" + " mc = mouseModule.mouseController\n" + " return mc.isAttached() and mc.GetAttachedSlotNumber()\n"); + + UiSink sink; + sink.wm = wm; + router.SetSink(&sink); + router.SetWorldEnabled(true); + + float x0, y0, x3, y3, x5, y5, x6, y6; + Cell(runtime, 0, x0, y0); + Cell(runtime, 3, x3, y3); + Cell(runtime, 5, x5, y5); + Cell(runtime, 6, x6, y6); + assert(sink.IsUiAt(x0, y0)); + assert(wm->PickWindow(long(x0), long(y0))->GetHandler() == PyDict_GetItemString(MainDict(), "g")); + + // Hover (finger resting before it lifts) shows the original item tooltip. + router.FingerDown(0, x0, y0, now); + runtime.execute("assert inv.tooltipItem.IsShow(), ('hover', attached(), mouseModule.mouseController.isAttached())\n"); + router.FingerUp(0, x0, y0, now + 60); + now += 1000; + // Tap: SelectItemSlot picks the potion up; it stays attached at the finger. + runtime.execute("assert mouseModule.mouseController.isAttached(), calls\n" + "assert mouseModule.mouseController.GetAttachedSlotNumber() == 0, 'pick slot'\n"); + assert(wm->IsAttaching()); + // Tap an empty cell: dropped there with the original move packet. + Tap(router, x5, y5); + runtime.execute("assert not mouseModule.mouseController.isAttached(), ('drop', attached(), calls)\n" + "assert calls and calls[-1][0] == 'move' and calls[-1][2] == 0 and calls[-1][4] == 5, calls\n" + "assert not inv.tooltipItem.IsShow(), 'drop tooltip'\n" + "del calls[:]\n"); + + // Drag cell 0 onto cell 6: the button goes down at the start, the release drops it. + router.FingerDown(0, x0, y0, now); + router.FingerMove(0, x0 + 20, y0 + 5, now + 30); + router.FingerMove(0, x6, y6, now + 60); + runtime.execute("assert wndMgr.IsDragging()\n"); + router.FingerUp(0, x6, y6, now + 90); + now += 1000; + runtime.execute("assert calls and calls[-1][0] == 'move' and calls[-1][2] == 0 and calls[-1][4] == 6, calls\n" + "assert not mouseModule.mouseController.isAttached(), 'drag attached'\n" + "del calls[:]\n"); + + // Double tap: the original double click uses the item (and leaves nothing attached). + router.FingerDown(0, x3, y3, now); + router.FingerUp(0, x3, y3, now + 50); + router.FingerDown(0, x3, y3, now + 150); + router.FingerUp(0, x3, y3, now + 200); + now += 1000; + runtime.execute("assert ('use', player.INVENTORY, 3) in calls or ('use', 3) in calls or " + "any(c[0] == 'use' and c[-1] == 3 for c in calls), calls\n" + "assert not mouseModule.mouseController.isAttached(), 'dbl attached'\n" + "del calls[:]\n"); + + // Long press: tooltip stays, the action sheet offers use / split (3 potions) / quick slot. + router.FingerDown(0, x0, y0, now); + router.Update(now + 500); + router.FingerUp(0, x0, y0, now + 600); + now += 1000; + runtime.execute("sheet = hud.item_actions\n" + "assert sheet.IsShow() and len(sheet.buttons) == 4, len(sheet.buttons)\n" + "assert inv.tooltipItem.IsShow(), 'long tooltip'\n" + "assert not mouseModule.mouseController.isAttached() and not calls, ('long', attached(), calls)\n" + // Off the exact centre: the zero-size button TextLine sits there and IsIn is inclusive. + "def btn_xy(i):\n" + " b = sheet.buttons[i]\n" + " x, y = b.GetGlobalPosition()\n" + " return (x + b.GetWidth() / 4, y + b.GetHeight() / 2)\n" + "cell_xy = btn_xy(0)\n"); + auto button = [&](int i, float& bx, float& by) { + runtime.execute("cell_xy = btn_xy(" + std::to_string(i) + ")\n"); + PyObject* xy = PyDict_GetItemString(MainDict(), "cell_xy"); + bx = float(PyInt_AsLong(PyTuple_GetItem(xy, 0))); + by = float(PyInt_AsLong(PyTuple_GetItem(xy, 1))); + }; + float bx, by; + button(0, bx, by); + Tap(router, bx, by); // Use + runtime.execute("assert not sheet.IsShow(), 'use closes'\n" + "assert any(c[0] == 'use' and c[-1] == 0 for c in calls), calls\n" + "del calls[:]\n"); + + // Split: the original Shift+click opens the count dialog. + router.FingerDown(0, x0, y0, now); + router.Update(now + 500); + router.FingerUp(0, x0, y0, now + 600); + now += 1000; + button(1, bx, by); + Tap(router, bx, by); + runtime.execute("assert inv.dlgPickMoney.IsShow(), ('split', attached(), calls)\n" + "assert not app.IsPressed(app.DIK_LSHIFT), 'shift left down'\n" + "inv.dlgPickMoney.Close()\n"); + + // Quick slot: the original Ctrl+click registers the item in an empty quickslot. + router.FingerDown(0, x0, y0, now); + router.Update(now + 500); + router.FingerUp(0, x0, y0, now + 600); + now += 1000; + button(2, bx, by); + Tap(router, bx, by); + runtime.execute("assert any(c[0] == 'quick' for c in calls), calls\n" + "del calls[:]\n"); + + // Close, and a tap outside the sheet both close it without touching the item. + router.FingerDown(0, x0, y0, now); + router.Update(now + 500); + router.FingerUp(0, x0, y0, now + 600); + now += 1000; + Tap(router, 5.0f, float(H) / 2.0f); + 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. + 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; + + // Desktop keyboard state reaches app.IsPressed (Shift-split, Ctrl-quickslot on PC). + ava2::SetKeyPressed(42, true); + runtime.execute("assert app.IsPressed(app.DIK_LSHIFT)\n"); + ava2::ReleaseAllKeys(); + runtime.execute("assert not app.IsPressed(app.DIK_LSHIFT)\n"); + + std::cout << "Touch gestures drive the original inventory\n"; + SDL_DestroyRenderer(renderer); + SDL_DestroySurface(surface); + return 0; +} diff --git a/tests/touch_router_test.cpp b/tests/touch_router_test.cpp index 4a630927..06820e3f 100644 --- a/tests/touch_router_test.cpp +++ b/tests/touch_router_test.cpp @@ -1,6 +1,8 @@ -// TouchRouter finger ownership: UI, joystick, attack, camera drag / pinch / tap, phases, cancel. +// 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 #include #include #include @@ -17,6 +19,8 @@ struct RecordingSink : TouchSink { 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 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; } @@ -27,7 +31,7 @@ struct RecordingSink : TouchSink { events.push_back(std::string(what) + " " + std::to_string(int(x)) + "," + std::to_string(int(y))); } std::vector events; - bool joy_active = false, attack = false; + 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; }; @@ -47,6 +51,9 @@ TouchLayout layout() { 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; } @@ -61,7 +68,9 @@ void ui_outside_game() { r.FingerMove(0, 110, 610, 10); r.FingerUp(1, 900, 300, 20); r.FingerUp(0, 120, 620, 30); - const std::vector want{"move 100,600", "down 100,600", "move 110,610", "move 120,620", "up 120,620"}; + // The pointer hovers at once; moving past the slop presses the button where the finger landed. + const std::vector 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"); @@ -131,7 +140,7 @@ void tap_pinch_and_ui_in_game() { // 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 want{"move 600,100", "down 600,100", "move 600,100", "up 600,100"}; + const std::vector want{"move 600,100", "move 600,100", "down 600,100", "up 600,100"}; expect(sink.events == want, "ui click in game"); } @@ -144,13 +153,14 @@ void phase_change_and_cancel() { 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 600,100", "cancel releases the UI button"); + 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); @@ -158,6 +168,79 @@ void phase_change_and_cancel() { 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.empty(), "cancelled pending UI finger sends nothing"); +} + +void ui_gestures() { + RecordingSink sink; + TouchRouter r; + r.SetSink(&sink); + r.SetLayout(layout()); + r.SetWorldEnabled(true); + using V = std::vector; + + // 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 default_layout() { @@ -175,6 +258,7 @@ int main() { joystick_attack_camera_together(); tap_pinch_and_ui_in_game(); phase_change_and_cancel(); + ui_gestures(); default_layout(); } catch (const std::exception& e) { std::cerr << "touch_router_test: " << e.what() << '\n'; diff --git a/upstream/python-root/ui_mobile_actions.py b/upstream/python-root/ui_mobile_actions.py new file mode 100644 index 00000000..5f91d85d --- /dev/null +++ b/upstream/python-root/ui_mobile_actions.py @@ -0,0 +1,146 @@ +# -*- coding: utf-8 -*- +# 移动端长按物品的操作面板(docs/MOBILE-ARCHITECTURE.md,手势交互)。 +# +# 触摸手势由原生 touch router 翻译成原版鼠标事件:点按 = 单击(拿起/放下物品), +# 拖动 = 鼠标拖拽,双击 = 鼠标双击(使用/装备)。长按物品格时 router 调用 +# touch.SetLongPressEvent 注册的函数:指针停在格子上,原版 OverInItem 的物品提示保持显示, +# 同时弹出本面板,把 PC 上靠修饰键完成的操作做成按钮,按钮调用的仍是原版槽位事件: +# 使用/装备 -> slotWindow.OnUseSlot(原版右键/双击) +# 拆分 -> 按住 Shift 调 OnSelectItemSlot(uiinventory.SelectItemSlot 的拆分分支) +# 加入快捷栏 -> 按住 Ctrl 调 OnSelectItemSlot(交易/商店等窗口打开时是原版 Ctrl 放入) +# 点面板外任意处关闭。丢弃、出售沿用拖拽(拖出窗口 / 拖到商店)。 +import ui +import grp +import app +import wndMgr +import touch +import localeInfo + + +def _Text(name, default): + return getattr(localeInfo, name, default) + + +class ItemActionSheet(ui.Window): + WIDTH = 180 + ROW = 40 + PAD = 12 + # 物品提示框以手指为中心显示,面板放在它旁边 + TOOLTIP_HALF_WIDTH = 150 + + def __init__(self, interface=None): + ui.Window.__init__(self, "TOP_MOST") + self.SetWindowName("MobileItemActions") + self.interface = interface + self.SetPosition(0, 0) + self.SetSize(wndMgr.GetScreenWidth(), wndMgr.GetScreenHeight()) + + self.panel = ui.Bar("TOP_MOST") + self.panel.SetParent(self) + self.panel.SetColor(grp.GenerateColor(0.08, 0.07, 0.06, 0.92)) + self.panel.Show() + self.buttons = [] + + self.slotWindow = None + self.slotNumber = 0 + self.Hide() + + def __del__(self): + ui.Window.__del__(self) + + def Destroy(self): + self.Close() + self.buttons = [] + self.slotWindow = None + self.interface = None + + def __IsInventoryGrid(self, slotWindow): + inventory = getattr(self.interface, "wndInventory", None) if self.interface else None + return inventory is not None and slotWindow is getattr(inventory, "wndItem", None) + + # touch.SetLongPressEvent: True = 已处理(手指松开不再点击) + def OnLongPress(self, slotWindow, slotNumber, itemCount, x, y): + actions = [] + if getattr(slotWindow, "eventUseSlot", None): + actions.append((_Text("MOBILE_ITEM_USE", "Use"), self.__OnUse)) + if self.__IsInventoryGrid(slotWindow): + if itemCount > 1: + actions.append((_Text("MOBILE_ITEM_SPLIT", "Split"), self.__OnSplit)) + actions.append((_Text("MOBILE_ITEM_QUICKSLOT", "Quick slot"), self.__OnQuickSlot)) + # 没有可做的操作时也吃掉这次触摸:只看物品提示 + if not actions: + return True + actions.append((_Text("UI_CLOSE", "Close"), self.Close)) + + self.slotWindow = slotWindow + self.slotNumber = slotNumber + self.__Build(actions) + self.__Place(x, y) + self.Show() + self.SetTop() + return True + + def __Build(self, actions): + for button in self.buttons: + button.Hide() + self.buttons = [] + for i, (text, event) in enumerate(actions): + button = ui.Button() + button.SetParent(self.panel) + button.SetPosition(self.PAD, self.PAD + i * self.ROW) + button.SetUpVisual("d:/ymir work/ui/public/large_button_01.sub") + button.SetOverVisual("d:/ymir work/ui/public/large_button_02.sub") + button.SetDownVisual("d:/ymir work/ui/public/large_button_03.sub") + button.SetText(text) + button.SetEvent(ui.__mem_func__(event)) + button.Show() + self.buttons.append(button) + self.panel.SetSize(self.WIDTH, self.PAD * 2 + len(actions) * self.ROW - (self.ROW - 25)) + + def __Place(self, x, y): + sw = wndMgr.GetScreenWidth() + sh = wndMgr.GetScreenHeight() + safe_l, safe_t, safe_r, safe_b = touch.GetSafeArea() + w = self.WIDTH + h = self.panel.GetHeight() + if x - self.TOOLTIP_HALF_WIDTH - w >= safe_l: + px = x - self.TOOLTIP_HALF_WIDTH - w + else: + px = min(x + self.TOOLTIP_HALF_WIDTH, sw - safe_r - w) + py = max(safe_t, min(y - h / 2, sh - safe_b - h)) + self.panel.SetPosition(px, py) + + def Close(self): + self.Hide() + self.slotWindow = None + + # 面板外的点按(全屏透明底)关闭面板 + def OnMouseLeftButtonUp(self): + self.Close() + return True + + def __Take(self): + slotWindow, slotNumber = self.slotWindow, self.slotNumber + self.Close() + return slotWindow, slotNumber + + def __OnUse(self): + slotWindow, slotNumber = self.__Take() + if slotWindow: + slotWindow.OnUseSlot(slotNumber) + + def __SelectWithKey(self, dik): + slotWindow, slotNumber = self.__Take() + if not slotWindow: + return + touch.SetVirtualKey(dik, 1) + try: + slotWindow.OnSelectItemSlot(slotNumber) + finally: + touch.SetVirtualKey(dik, 0) + + def __OnSplit(self): + self.__SelectWithKey(app.DIK_LSHIFT) + + def __OnQuickSlot(self): + self.__SelectWithKey(app.DIK_LCONTROL) diff --git a/upstream/python-root/ui_mobile_hud.py b/upstream/python-root/ui_mobile_hud.py index b1d8852f..d363223b 100644 --- a/upstream/python-root/ui_mobile_hud.py +++ b/upstream/python-root/ui_mobile_hud.py @@ -8,6 +8,7 @@ import wndMgr import ui_mobile_joystick import ui_mobile_combat import ui_mobile_drawer +import ui_mobile_actions import ui_mobile_widgets as widgets import touch import uiCharacter @@ -53,6 +54,9 @@ class MobileHUD(ui.Window): self.safe_area = touch.GetSafeArea() self.root = None self.__BuildUI(sw, sh) + # 长按物品格:物品提示 + 操作面板(ui_mobile_actions) + self.item_actions = ui_mobile_actions.ItemActionSheet(interface) + touch.SetLongPressEvent(ui.__mem_func__(self.item_actions.OnLongPress)) def __del__(self): ui.Window.__del__(self)