Add touch input router for mobile (step 3)

Route each finger to UI, the attack button, a floating lower-left joystick or
the camera (drag rotate, pinch zoom, tap = world click). Touch mode is default
on Android/iOS and available on desktop via --touch / AVA2_TOUCH=1; it disables
SDL touch-to-mouse and drives app.ENABLE_MOBILE_HUD. The joystick feeds the
original camera-relative movement path and the attack button sets the same
attack-key state as player.SetAttackKeyState. New `touch` Python module; the
mobile HUD joystick and combat wheel become display-only.

Tests: ava2.touch_router, ava2.touch_game (GameWindow + mobile HUD).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 19:46:24 +09:00
co-authored by Claude Opus 5.5
parent 1dfdcba739
commit 5597842d90
18 changed files with 978 additions and 103 deletions
+130
View File
@@ -0,0 +1,130 @@
// Touch mode in the original GameWindow: the mobile HUD must leave the joystick / attack zones and
// open world to the touch router, while real UI (taskbar) still takes the finger.
#include "python_runtime.h"
#include "python_gameplay_modules.h"
#include "app/input/touch_router.h"
#include "eter_ui/PythonWindowManager.h"
#include "eter_ui/PythonWindow.h"
#include "eter_image/GrpDevice.h"
#include "test_rundir.h"
#include "eter_net/PythonNetworkStream.h"
#include <Python.h>
#include <SDL3/SDL.h>
#include <cassert>
#include <cmath>
#include <iostream>
namespace {
struct GameSink final : ava2::input::TouchSink {
UI::CWindowManager* wm = nullptr;
int ui_downs = 0, taps = 0;
bool attack = false;
bool IsUiAt(float x, float y) override {
auto* win = wm->PickWindow(long(x), long(y));
return win && !ava2::IsPlayerGameWindow(win->GetHandler());
}
void UiMouseMove(float x, float y) override { wm->RunMouseMove(long(x), long(y)); }
void UiMouseDown(float x, float y) override { ++ui_downs; wm->RunMouseMove(long(x), long(y)); }
void UiMouseUp(float x, float y) override { wm->RunMouseMove(long(x), long(y)); }
void WorldTap(float, float) override { ++taps; }
void Joystick(bool active, float x, float y) override { ava2::SetPlayerJoystick(active, x, y); }
void Attack(bool pressed) override { attack = pressed; ava2::SetPlayerAttackKey(pressed); }
void CameraRotate(float, float) override {}
void CameraZoom(float) override {}
};
} // 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;
// Must be on before the runtime creates the app module (app.ENABLE_MOBILE_HUD).
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\n"
"assert app.ENABLE_MOBILE_HUD == 1 and touch.IsEnabled()\n"
"net.SendSelectCharacterPacket=lambda slot: None\n"
"introLoading.__main__()\n"
"localeInfo.LoadLocaleData()\n"
"stream = networkModule.MainStream()\nstream.Create()\n"
"stream.SetLoadingPhase()\n"
"stream.SetGamePhase()\n"
"game = stream.curPhaseWindow\n"
"assert game.__class__.__name__ == 'GameWindow'\n"
"hud = game.interface.wndMobileHUD\n"
"assert hud and hud.IsShow()\n"
"game.OnUpdate()\n");
GameSink sink;
sink.wm = wm;
router.SetSink(&sink);
router.SetWorldEnabled(true);
const auto layout = router.GetLayout();
// The combat wheel moved the attack button onto its own art (bottom-right, radius 60).
assert(layout.attack_r == 60.0f);
assert(layout.attack_x == W - 120.0f && layout.attack_y == H - 120.0f);
// Open world, joystick zone and attack button are not UI: the HUD visuals are not_pick.
assert(!sink.IsUiAt(W * 0.6f, H * 0.4f));
assert(!sink.IsUiAt(160.0f, H - 180.0f));
assert(!sink.IsUiAt(layout.attack_x, layout.attack_y));
// The taskbar along the bottom edge is real UI.
runtime.execute("tb = game.interface.wndTaskBar\n"
"touch_tb = tb.GetGlobalPosition()\n"
"touch_tb = (touch_tb[0] + tb.GetWidth() / 2, touch_tb[1] + tb.GetHeight() / 2)\n"
);
PyObject* main = PyImport_AddModule("__main__");
PyObject* tb = PyDict_GetItemString(PyModule_GetDict(main), "touch_tb");
assert(tb && PyTuple_Check(tb));
const float tb_x = float(PyInt_AsLong(PyTuple_GetItem(tb, 0)));
const float tb_y = float(PyInt_AsLong(PyTuple_GetItem(tb, 1)));
assert(sink.IsUiAt(tb_x, tb_y));
// Joystick pushed to the screen right walks the player camera-relative right (yaw 90 at camera 0).
float yaw = -1.0f;
assert(!ava2::GetPlayerMovementYaw(0.0f, yaw));
router.FingerDown(1, 160.0f, H - 180.0f, 0);
router.FingerMove(1, 160.0f + layout.joy_radius, H - 180.0f, 16);
assert(router.GetJoystick().active);
assert(ava2::GetPlayerMovementYaw(0.0f, yaw) && std::fabs(yaw - 90.0f) < 0.5f);
// A second finger holds attack while walking; the HUD shows the pressed button.
router.FingerDown(2, layout.attack_x, layout.attack_y, 20);
assert(sink.attack && router.IsAttackPressed());
runtime.execute("game.interface.wndMobileHUD.combat_wheel.OnUpdate()\n"
"assert game.interface.wndMobileHUD.combat_wheel.attack_pressed\n"
"game.interface.wndMobileHUD.joystick.OnUpdate()\n");
router.FingerUp(2, layout.attack_x, layout.attack_y, 200);
router.FingerUp(1, 160.0f + layout.joy_radius, H - 180.0f, 220);
assert(!sink.attack && !ava2::GetPlayerMovementYaw(0.0f, yaw));
// Taskbar taps go to the window manager, open world taps become world clicks.
router.FingerDown(3, tb_x, tb_y, 300);
router.FingerUp(3, tb_x, tb_y, 350);
assert(sink.ui_downs == 1);
router.FingerDown(4, W * 0.6f, H * 0.4f, 400);
router.FingerUp(4, W * 0.6f, H * 0.4f, 450);
assert(sink.taps == 1);
SDL_FillSurfaceRect(surface, nullptr, 0xff10141e);
wm->Render(); SDL_RenderPresent(renderer);
if (argc > 3) SDL_SaveBMP(surface, argv[3]);
router.SetSink(nullptr);
runtime.execute("stream.Destroy()\n");
wm->Update();
device.Init(nullptr);
SDL_DestroyRenderer(renderer); SDL_DestroySurface(surface);
std::cout << "Touch router drives joystick, attack and world taps around the mobile HUD\n";
}
+185
View File
@@ -0,0 +1,185 @@
// TouchRouter finger ownership: UI, joystick, attack, camera drag / pinch / tap, phases, cancel.
#include "app/input/touch_router.h"
#include <cmath>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace ava2::input;
namespace {
struct RecordingSink : TouchSink {
// One UI window: a 200x100 box at (500, 50).
bool IsUiAt(float x, float y) override { return x >= 500 && x < 700 && y >= 50 && y < 150; }
void UiMouseMove(float x, float y) override { log("move", x, y); }
void UiMouseDown(float x, float y) override { log("down", x, y); }
void UiMouseUp(float x, float y) override { log("up", x, y); }
void WorldTap(float x, float y) override { log("tap", x, y); }
void Joystick(bool active, float x, float y) override { joy_active = active; joy_x = x; joy_y = y; }
void Attack(bool pressed) override { attack = pressed; ++attack_calls; }
void CameraRotate(float dx, float dy) override { rot_x += dx; rot_y += dy; }
void CameraZoom(float ratio) override { zoom *= ratio; }
void log(const char* what, float x, float y) {
events.push_back(std::string(what) + " " + std::to_string(int(x)) + "," + std::to_string(int(y)));
}
std::vector<std::string> events;
bool joy_active = false, attack = false;
int attack_calls = 0;
float joy_x = 0, joy_y = 0, rot_x = 0, rot_y = 0, zoom = 1;
};
void expect(bool ok, const char* what) {
if (!ok) throw std::runtime_error(what);
}
bool near(float a, float b) { return std::fabs(a - b) < 0.01f; }
TouchLayout layout() {
TouchLayout l;
l.screen_w = 1280;
l.screen_h = 720;
l.joy_x = 0; l.joy_y = 360; l.joy_w = 500; l.joy_h = 360;
l.joy_radius = 100;
l.joy_dead_zone = 0.2f;
l.attack_x = 1180; l.attack_y = 620; l.attack_r = 80;
l.tap_slop = 10;
l.tap_ms = 300;
return l;
}
void ui_outside_game() {
RecordingSink sink;
TouchRouter r;
r.SetSink(&sink);
r.SetLayout(layout());
// Not in game: a finger anywhere, even on the joystick area, is the UI pointer.
r.FingerDown(0, 100, 600, 0);
r.FingerDown(1, 900, 300, 0); // second finger ignored while the first holds the pointer
r.FingerMove(0, 110, 610, 10);
r.FingerUp(1, 900, 300, 20);
r.FingerUp(0, 120, 620, 30);
const std::vector<std::string> want{"move 100,600", "down 100,600", "move 110,610", "move 120,620", "up 120,620"};
expect(sink.events == want, "ui_outside_game events");
expect(!sink.joy_active && sink.attack_calls == 0, "ui_outside_game world controls");
expect(r.FingerCount() == 0, "ui_outside_game fingers");
}
void joystick_attack_camera_together() {
RecordingSink sink;
TouchRouter r;
r.SetSink(&sink);
r.SetLayout(layout());
r.SetWorldEnabled(true);
r.FingerDown(0, 200, 500, 0); // joystick, base at the touch point
expect(sink.joy_active && near(sink.joy_x, 0) && near(sink.joy_y, 0), "joystick centred");
r.FingerMove(0, 210, 500, 5); // inside the dead zone
expect(near(sink.joy_x, 0), "joystick dead zone");
r.FingerMove(0, 400, 500, 10); // beyond the radius: clamped to full right
expect(near(sink.joy_x, 1) && near(sink.joy_y, 0), "joystick full right");
expect(near(r.GetJoystick().knob_x, 300), "joystick knob clamped");
r.FingerDown(1, 1180, 620, 12); // attack
expect(sink.attack && r.IsAttackPressed(), "attack pressed");
r.FingerDown(2, 900, 300, 15); // camera
r.FingerMove(2, 905, 300, 16); // within slop: nothing yet
expect(near(sink.rot_x, 0), "camera slop");
r.FingerMove(2, 930, 290, 17);
expect(near(sink.rot_x, 30) && near(sink.rot_y, -10), "camera drag includes slop");
r.FingerMove(2, 940, 290, 18);
expect(near(sink.rot_x, 40), "camera drag delta");
r.FingerUp(2, 940, 290, 19);
expect(sink.events.empty(), "dragged camera finger is not a tap");
r.FingerMove(0, 200, 400, 20); // joystick straight up
expect(near(sink.joy_x, 0) && near(sink.joy_y, -1), "joystick up");
r.FingerUp(1, 1180, 620, 25);
expect(!sink.attack, "attack released");
r.FingerUp(0, 200, 400, 30);
expect(!sink.joy_active && !r.GetJoystick().active, "joystick released");
}
void tap_pinch_and_ui_in_game() {
RecordingSink sink;
TouchRouter r;
r.SetSink(&sink);
r.SetLayout(layout());
r.SetWorldEnabled(true);
r.FingerDown(0, 800, 300, 0);
r.FingerMove(0, 804, 303, 50);
r.FingerUp(0, 804, 303, 100);
expect(sink.events.size() == 1 && sink.events[0] == "tap 804,303", "short tap clicks the world");
sink.events.clear();
r.FingerDown(0, 800, 300, 200);
r.FingerUp(0, 800, 300, 600); // held too long
expect(sink.events.empty(), "long press is not a tap");
r.FingerDown(0, 800, 300, 700);
r.FingerDown(1, 900, 300, 700); // pinch: distance 100 -> 200
r.FingerMove(1, 1000, 300, 710);
expect(near(sink.zoom, 2), "pinch zoom ratio");
r.FingerUp(1, 1000, 300, 720);
r.FingerUp(0, 800, 300, 730);
expect(sink.events.empty() && near(sink.rot_x, 0), "pinch fingers neither tap nor rotate");
// UI windows win over world controls, also inside the game.
r.FingerDown(3, 600, 100, 800);
r.FingerUp(3, 600, 100, 810);
const std::vector<std::string> want{"move 600,100", "down 600,100", "move 600,100", "up 600,100"};
expect(sink.events == want, "ui click in game");
}
void phase_change_and_cancel() {
RecordingSink sink;
TouchRouter r;
r.SetSink(&sink);
r.SetLayout(layout());
r.SetWorldEnabled(true);
r.FingerDown(0, 200, 500, 0);
r.FingerDown(1, 1180, 620, 0);
r.FingerDown(2, 600, 100, 0); // UI
r.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(r.FingerCount() == 0, "cancel clears fingers");
r.SetWorldEnabled(true);
sink.events.clear();
r.FingerDown(5, 800, 300, 0);
r.CancelAll();
expect(sink.events.empty(), "cancelled camera finger does not tap");
}
void default_layout() {
const TouchLayout l = TouchLayout::ForScreen(1600, 720);
expect(l.joy_x == 0 && l.joy_w > 0 && l.joy_y + l.joy_h <= 720.5f, "default joystick area");
expect(l.attack_x + l.attack_r <= 1600 && l.attack_y + l.attack_r <= 720, "default attack on screen");
expect(l.attack_x - l.attack_r > l.joy_x + l.joy_w, "attack right of the joystick area");
}
} // namespace
int main() {
try {
ui_outside_game();
joystick_attack_camera_together();
tap_pinch_and_ui_in_game();
phase_change_and_cancel();
default_layout();
} catch (const std::exception& e) {
std::cerr << "touch_router_test: " << e.what() << '\n';
return 1;
}
std::cout << "touch_router_test: ok\n";
return 0;
}