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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
908838c8f9
commit
6244b1d860
@@ -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 <Python.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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 <algorithm>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
@@ -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<std::string> 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<std::string> 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<std::string> want{"move 100,600", "move 100,600", "down 100,600", "move 110,610",
|
||||
"move 120,620", "up 120,620"};
|
||||
expect(sink.events == want, "ui_outside_game events");
|
||||
expect(!sink.joy_active && sink.attack_calls == 0, "ui_outside_game world controls");
|
||||
expect(r.FingerCount() == 0, "ui_outside_game fingers");
|
||||
@@ -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<std::string> want{"move 600,100", "down 600,100", "move 600,100", "up 600,100"};
|
||||
const std::vector<std::string> want{"move 600,100", "move 600,100", "down 600,100", "up 600,100"};
|
||||
expect(sink.events == want, "ui click in game");
|
||||
}
|
||||
|
||||
@@ -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<std::string>;
|
||||
|
||||
// Double tap: the second tap is down + double click + up (the original mouse sequence).
|
||||
r.FingerDown(0, 600, 100, 1000);
|
||||
r.Update(1050);
|
||||
r.FingerUp(0, 602, 101, 1080);
|
||||
r.FingerDown(0, 610, 105, 1250);
|
||||
r.FingerUp(0, 610, 105, 1300);
|
||||
expect(sink.events == V{"move 600,100", "move 600,100", "down 600,100", "up 600,100",
|
||||
"move 610,105", "move 610,105", "down 610,105", "dbl 610,105", "up 610,105"},
|
||||
"double tap");
|
||||
|
||||
// A third tap starts a new pair.
|
||||
sink.events.clear();
|
||||
r.FingerDown(0, 610, 105, 1400);
|
||||
r.FingerUp(0, 610, 105, 1450);
|
||||
expect(sink.events == V{"move 610,105", "move 610,105", "down 610,105", "up 610,105"}, "third tap is single");
|
||||
|
||||
// Too late or too far: two single clicks.
|
||||
sink.events.clear();
|
||||
r.FingerDown(0, 620, 100, 1900); // 450 ms after the last tap
|
||||
r.FingerUp(0, 620, 100, 1950);
|
||||
r.FingerDown(0, 680, 100, 2000); // 60 px away
|
||||
r.FingerUp(0, 680, 100, 2050);
|
||||
expect(std::count(sink.events.begin(), sink.events.end(), std::string("dbl 620,100")) == 0 &&
|
||||
std::count(sink.events.begin(), sink.events.end(), std::string("dbl 680,100")) == 0 && sink.events.size() == 8,
|
||||
"late or distant taps do not pair");
|
||||
|
||||
// Long press taken by the host: nothing else happens, not even a button release.
|
||||
sink.events.clear();
|
||||
r.FingerDown(0, 600, 100, 3000);
|
||||
r.Update(3400);
|
||||
expect(sink.events == V{"move 600,100"}, "no long press before long_press_ms");
|
||||
r.FingerMove(0, 603, 100, 3420);
|
||||
r.Update(3460);
|
||||
r.FingerMove(0, 660, 100, 3500);
|
||||
r.FingerUp(0, 660, 100, 3600);
|
||||
expect(sink.events == V{"move 600,100", "long 603,100"}, "taken long press swallows the touch");
|
||||
|
||||
// Long press the host declines: the button goes down and the touch continues as a press.
|
||||
sink.events.clear();
|
||||
sink.take_long_press = false;
|
||||
r.FingerDown(0, 600, 100, 4000);
|
||||
r.Update(4500);
|
||||
r.FingerMove(0, 640, 100, 4600);
|
||||
r.FingerUp(0, 640, 100, 4700);
|
||||
expect(sink.events == V{"move 600,100", "long 600,100", "move 600,100", "down 600,100", "move 640,100",
|
||||
"move 640,100", "up 640,100"},
|
||||
"declined long press becomes a press");
|
||||
|
||||
// A drag never becomes a long press.
|
||||
sink.events.clear();
|
||||
r.FingerDown(0, 600, 100, 5000);
|
||||
r.FingerMove(0, 650, 100, 5100);
|
||||
r.Update(5600);
|
||||
r.FingerUp(0, 650, 100, 5700);
|
||||
expect(sink.events == V{"move 600,100", "move 600,100", "down 600,100", "move 650,100", "move 650,100",
|
||||
"up 650,100"},
|
||||
"drag");
|
||||
}
|
||||
|
||||
void 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';
|
||||
|
||||
Reference in New Issue
Block a user