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
+15 -1
View File
@@ -28,6 +28,13 @@ if(BUILD_TESTING)
target_link_libraries(skill_poly_test PRIVATE ava2_poly)
add_test(NAME ava2.skill_poly COMMAND skill_poly_test)
endif()
add_library(ava2_input src/app/input/touch_router.cpp)
target_include_directories(ava2_input PUBLIC src)
if(BUILD_TESTING)
add_executable(touch_router_test tests/touch_router_test.cpp)
target_link_libraries(touch_router_test PRIVATE ava2_input)
add_test(NAME ava2.touch_router COMMAND touch_router_test)
endif()
add_library(ava2_protocol src/network/protocol.cpp src/network/legacy_cipher.cpp src/network/login_session.cpp)
target_include_directories(ava2_protocol PUBLIC src src/network src/game)
find_package(Threads REQUIRED)
@@ -63,6 +70,7 @@ if(AVA2_EMBED_PYTHON)
src/scripting/python_runtime.cpp
src/scripting/python_net.cpp
src/scripting/python_dbg.cpp
src/scripting/python_touch.cpp
src/scripting/python_system.cpp
src/scripting/python_minimap.cpp
src/scripting/python_chr.cpp
@@ -87,7 +95,7 @@ if(AVA2_EMBED_PYTHON)
src/eter_net/PythonExchange.cpp
src/eter_net/PythonSafeBox.cpp)
target_include_directories(ava2_python PUBLIC src src/network src/game src/scripting ${CMAKE_CURRENT_SOURCE_DIR} third_party/extern_include)
target_link_libraries(ava2_python PUBLIC SDL3::SDL3 mtpython ava2_render PRIVATE mt_codepage ava2_transport ava2_lzo ava2_poly)
target_link_libraries(ava2_python PUBLIC SDL3::SDL3 mtpython ava2_render ava2_input PRIVATE mt_codepage ava2_transport ava2_lzo ava2_poly)
target_sources(ava2_python PRIVATE src/eter_ui/Font.cpp)
find_package(Freetype QUIET)
if(Freetype_FOUND)
@@ -212,6 +220,12 @@ if(BUILD_TESTING AND NOT ANDROID)
add_test(NAME ava2.game_entry COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tests/check_completion.py"
"Original GameWindow, inventory, taskbar and minimap created successfully" "$<TARGET_FILE:game_entry_test>" "${MT_PYTHON_STDLIB_ZIP}" "${CMAKE_SOURCE_DIR}/upstream/python-root")
set_tests_properties(ava2.game_entry PROPERTIES ENVIRONMENT "AVA2_TEST_RUNDIR=${AVA2_TEST_RUNDIR}" SKIP_RETURN_CODE 77)
add_executable(touch_game_test tests/touch_game_test.cpp)
target_link_libraries(touch_game_test PRIVATE ava2_python mtpython)
add_dependencies(touch_game_test mtpython_stdlib)
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" "$<TARGET_FILE:touch_game_test>" "${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_test(NAME ava2.pack_assets COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tests/pack_assets_test.py")
endif()
+3 -1
View File
@@ -8,7 +8,9 @@
- `android/push_assets.py` 把资源(`assets/loose` + python-root + `python27.zip`)推送到应用外部存储目录,客户端启动时切换到该目录。
- 游戏资源为 2.8 GB、约 5 万个散文件(`assets/loose`),17 个源文件中 120 多处通过 `std::filesystem` / `ifstream` 直接读文件,无法读取 APK 内置资源。
- 没有处理前后台切换(`SDL_EVENT_WILL_ENTER_BACKGROUND` / `DID_ENTER_FOREGROUND`)和低内存事件。
- 没有触摸事件处理;SDL 默认把单指触摸模拟成鼠标,只能点按钮。
- 触摸分流已实现(`src/app/input/touch_router.*`,见下文 `input/`):移动端或 `--touch` / `AVA2_TOUCH=1` 时关闭 SDL 触摸转鼠标,每个手指分到界面、攻击键、左下浮动摇杆或镜头(单指拖动转向、双指缩放、轻点=点地移动)。Python 模块 `touch` 供 HUD 设置攻击键位置并读取摇杆状态;`ui_mobile_joystick` / `ui_mobile_combat` 改为只做显示(`not_pick`)。多指同时操作尚未在真机验证。
- 实际出招尚未接通:触摸攻击键和键盘一样只设置 `player` 的攻击键状态,真服路径里 `player_ComboAttack` 仍是空实现。
- 原型 HUD 用到的 `taskbar/attack_button_0x.sub`、`quickslot_0x.sub` 不在资源包里:攻击键暂用色块,技能圈槽位不可见。
- 软键盘已改为仅在输入框获得焦点(`ime.EnableCaptureInput`)或聊天打开时弹出。
- `src/app/main.cpp` 共 2418 行,启动、主循环、登录和调试参数都在其中。
- Android 默认渲染缩放 0.75。
+34
View File
@@ -1144,3 +1144,37 @@ Verified: debug APK builds; the desktop client reaches the login screen inside
`build/android-data/root` (the phone layout) with no missing-effect errors.
Not verified: any run on a device (no phone attached), Vulkan rendering, touch,
lifecycle, frame rate.
### 2026-10-12 Mobile step 3: touch input router
- `src/app/input/touch_router.{h,cpp}` (`ava2_input`): per-finger routing in
this order: UI under the finger (or any finger outside the game phase) -> one
UI pointer; attack circle; lower-left joystick area (floating base, dead zone
0.18); otherwise camera (one-finger drag rotates after a slop, two-finger
pinch zooms, a short tap is a world click). `CancelAll` on
`SDL_EVENT_FINGER_CANCELED` / focus loss.
- Touch mode is on by default on Android/iOS, or with `--touch` /
`AVA2_TOUCH=1` on desktop; it sets `SDL_HINT_TOUCH_MOUSE_EVENTS=0` and makes
`app.ENABLE_MOBILE_HUD` follow it. `main.cpp` hosts the sink: UI = a
picked window that is not the player GameWindow; world tap = mouse move +
left down for one frame (GameWindow keeps the move target after release);
joystick -> `SetPlayerJoystick` (replaces the WASD axes while active, so
movement uses the original camera-relative yaw path); attack ->
`SetPlayerAttackKey` (same state and fishing check as
`player.SetAttackKeyState`).
- Python module `touch`: `IsEnabled`, `GetJoystick`, `IsAttackPressed`,
`SetAttackButton`, `SetJoystickArea`, `GetLayout`. The mobile HUD prototype's
joystick and combat wheel are now display-only (`not_pick`); the old joystick
sent raw move packets and the attack button was a one-pointer `ui.Button`.
Verified: `ava2.touch_router` (routing unit test) and `ava2.touch_game` (the
original GameWindow with the mobile HUD in touch mode: open world, joystick
zone and attack button are not UI, the taskbar is; joystick right gives
movement yaw 90 at camera yaw 0; attack held while walking; taskbar tap reaches
the window manager, open-world tap becomes a world click); 51/51 desktop tests;
`ava2_client --touch` reaches the login screen.
Outstanding: no device run (real multi-touch, finger/pixel scale); the attack
key state is still not consumed on the real-server path (`player_ComboAttack`
stub), so touch attack does not swing yet; the prototype's attack/quickslot
`.sub` art is missing from the packs (attack drawn as a coloured block, skill
slots invisible).
+222
View File
@@ -0,0 +1,222 @@
#include "touch_router.h"
#include <algorithm>
#include <cmath>
#if defined(__APPLE__)
#include <TargetConditionals.h>
#endif
namespace ava2::input {
TouchLayout TouchLayout::ForScreen(float w, float h) {
TouchLayout layout;
layout.screen_w = w;
layout.screen_h = h;
const float unit = std::min(w, h);
layout.joy_x = 0.0f;
layout.joy_y = h * 0.45f;
layout.joy_w = w * 0.40f;
layout.joy_h = h * 0.55f;
layout.joy_radius = unit * 0.12f;
layout.attack_r = unit * 0.11f;
layout.attack_x = w - layout.attack_r - unit * 0.06f;
layout.attack_y = h - layout.attack_r - unit * 0.06f;
layout.tap_slop = unit * 0.02f;
return layout;
}
void TouchRouter::SetWorldEnabled(bool enabled) {
if (enabled == m_world) return;
// Phase change: world fingers would otherwise keep a stale joystick or attack state.
if (!enabled) {
for (auto it = m_fingers.begin(); it != m_fingers.end();) {
if (it->second.owner != Owner::Ui) {
Release(it->first, it->second, true);
it = m_fingers.erase(it);
} else {
++it;
}
}
}
m_world = enabled;
}
bool TouchRouter::InAttack(float x, float y) const {
if (m_layout.attack_r <= 0.0f) return false;
const float dx = x - m_layout.attack_x, dy = y - m_layout.attack_y;
return dx * dx + dy * dy <= m_layout.attack_r * m_layout.attack_r;
}
bool TouchRouter::InJoystickArea(float x, float y) const {
return x >= m_layout.joy_x && x < m_layout.joy_x + m_layout.joy_w &&
y >= m_layout.joy_y && y < m_layout.joy_y + m_layout.joy_h;
}
void TouchRouter::UpdateJoystick(float x, float y) {
const float r = m_layout.joy_radius;
float dx = x - m_joy.base_x, dy = y - m_joy.base_y;
const float dist = std::sqrt(dx * dx + dy * dy);
if (dist > r && dist > 0.0f) {
dx *= r / dist;
dy *= r / dist;
}
m_joy.knob_x = m_joy.base_x + dx;
m_joy.knob_y = m_joy.base_y + dy;
if (!m_sink) return;
const float len = std::min(dist, r) / r;
if (len < m_layout.joy_dead_zone)
m_sink->Joystick(true, 0.0f, 0.0f);
else
m_sink->Joystick(true, dx / r, dy / r);
}
TouchRouter::Finger* TouchRouter::OtherCameraFinger(std::uint64_t id) {
for (auto& [other, finger] : m_fingers)
if (other != id && finger.owner == Owner::Camera) return &finger;
return nullptr;
}
void TouchRouter::FingerDown(std::uint64_t raw_id, float x, float y, std::uint64_t ms) {
const std::uint64_t id = raw_id + 1;
if (m_fingers.count(id)) return;
Finger finger;
finger.x = finger.start_x = x;
finger.y = finger.start_y = y;
finger.start_ms = ms;
const bool ui = m_sink && m_sink->IsUiAt(x, y);
if (!m_world || ui) {
// wndMgr tracks a single pointer; further fingers on UI are ignored.
if (m_ui_finger || !m_sink) return;
finger.owner = Owner::Ui;
m_ui_finger = id;
m_fingers[id] = finger;
m_sink->UiMouseMove(x, y);
m_sink->UiMouseDown(x, y);
return;
}
if (!m_attack_finger && InAttack(x, y)) {
finger.owner = Owner::Attack;
m_attack_finger = id;
m_fingers[id] = finger;
if (m_sink) m_sink->Attack(true);
return;
}
if (!m_joy_finger && InJoystickArea(x, y)) {
finger.owner = Owner::Joystick;
m_joy_finger = id;
m_fingers[id] = finger;
const float r = m_layout.joy_radius;
m_joy.active = true;
m_joy.base_x = std::clamp(x, r, std::max(r, m_layout.screen_w - r));
m_joy.base_y = std::clamp(y, r, std::max(r, m_layout.screen_h - r));
UpdateJoystick(x, y);
return;
}
finger.owner = Owner::Camera;
if (Finger* other = OtherCameraFinger(id)) {
// Second camera finger: a pinch, neither finger can still be a tap.
other->tap = false;
finger.tap = false;
}
m_fingers[id] = finger;
}
void TouchRouter::FingerMove(std::uint64_t raw_id, float x, float y, std::uint64_t) {
const std::uint64_t id = raw_id + 1;
auto it = m_fingers.find(id);
if (it == m_fingers.end()) return;
Finger& finger = it->second;
const float prev_x = finger.x, prev_y = finger.y;
finger.x = x;
finger.y = y;
switch (finger.owner) {
case Owner::Ui:
if (m_sink) m_sink->UiMouseMove(x, y);
break;
case Owner::Attack:
break;
case Owner::Joystick:
UpdateJoystick(x, y);
break;
case Owner::Camera: {
if (Finger* other = OtherCameraFinger(id)) {
const float before = std::hypot(prev_x - other->x, prev_y - other->y);
const float after = std::hypot(x - other->x, y - other->y);
if (before > 1.0f && after > 1.0f && m_sink) m_sink->CameraZoom(after / before);
break;
}
if (finger.tap) {
if (std::hypot(x - finger.start_x, y - finger.start_y) <= m_layout.tap_slop) break;
finger.tap = false;
// The slop distance is part of the drag so the camera does not jump.
if (m_sink) m_sink->CameraRotate(x - finger.start_x, y - finger.start_y);
break;
}
if (m_sink) m_sink->CameraRotate(x - prev_x, y - prev_y);
break;
}
}
}
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);
break;
case Owner::Attack:
m_attack_finger = 0;
if (m_sink) m_sink->Attack(false);
break;
case Owner::Joystick:
m_joy_finger = 0;
m_joy = JoystickView{};
if (m_sink) m_sink->Joystick(false, 0.0f, 0.0f);
break;
case Owner::Camera:
if (!cancelled && finger.tap && m_sink) m_sink->WorldTap(finger.x, finger.y);
break;
}
(void)id;
}
void TouchRouter::FingerUp(std::uint64_t raw_id, float x, float y, std::uint64_t ms) {
const std::uint64_t id = raw_id + 1;
auto it = m_fingers.find(id);
if (it == m_fingers.end()) return;
Finger finger = it->second;
m_fingers.erase(it);
finger.x = x;
finger.y = y;
if (finger.owner == Owner::Ui && 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))
finger.tap = false;
Release(id, finger, false);
}
void TouchRouter::CancelAll() {
for (auto& [id, finger] : m_fingers) Release(id, finger, true);
m_fingers.clear();
}
TouchRouter& GetTouchRouter() {
static TouchRouter router;
return router;
}
namespace {
#if defined(__ANDROID__) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
bool s_touch_mode = true;
#else
bool s_touch_mode = false;
#endif
} // namespace
bool IsTouchMode() { return s_touch_mode; }
void SetTouchMode(bool enabled) { s_touch_mode = enabled; }
} // namespace ava2::input
+109
View File
@@ -0,0 +1,109 @@
#pragma once
// Touch input router for the mobile client (docs/MOBILE-ARCHITECTURE.md, input/).
//
// 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)
// 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)
//
// 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.
#include <cstdint>
#include <map>
namespace ava2::input {
struct TouchSink {
virtual ~TouchSink() = default;
// True when a UI window other than the full-screen game window is under the point.
virtual bool IsUiAt(float x, float y) = 0;
virtual void UiMouseMove(float x, float y) = 0;
virtual void UiMouseDown(float x, float y) = 0;
virtual void UiMouseUp(float x, float y) = 0;
// 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.
virtual void Joystick(bool active, float x, float y) = 0;
virtual void Attack(bool pressed) = 0;
// Camera drag in UI pixels since the last call.
virtual void CameraRotate(float dx, float dy) = 0;
// Pinch ratio since the last call: > 1 fingers moved apart (zoom in).
virtual void CameraZoom(float ratio) = 0;
};
struct TouchLayout {
float screen_w = 1280.0f, screen_h = 720.0f;
// Joystick area; the stick centres where the finger lands inside it.
float joy_x = 0.0f, joy_y = 0.0f, joy_w = 0.0f, joy_h = 0.0f;
float joy_radius = 80.0f; // full deflection distance
float joy_dead_zone = 0.18f; // fraction of joy_radius treated as centred
// Attack button circle; radius 0 disables it.
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
// Default layout for a screen: joystick in the lower-left 40% x 55%, attack bottom right.
static TouchLayout ForScreen(float w, float h);
};
struct JoystickView {
bool active = false;
float base_x = 0.0f, base_y = 0.0f; // where the finger landed (clamped on screen)
float knob_x = 0.0f, knob_y = 0.0f; // knob centre, at most joy_radius from the base
};
class TouchRouter {
public:
enum class Owner { Ui, Attack, Joystick, Camera };
void SetSink(TouchSink* sink) { m_sink = sink; }
void SetLayout(const TouchLayout& layout) { m_layout = layout; }
const TouchLayout& GetLayout() const { return m_layout; }
// World controls (joystick, attack, camera, world taps) exist only in the game phase.
void SetWorldEnabled(bool enabled);
bool IsWorldEnabled() const { return m_world; }
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);
// Lost focus / app paused: release everything without taps or clicks.
void CancelAll();
JoystickView GetJoystick() const { return m_joy; }
bool IsAttackPressed() const { return m_attack_finger != 0; }
std::size_t FingerCount() const { return m_fingers.size(); }
private:
struct Finger {
Owner owner = Owner::Ui;
float x = 0.0f, y = 0.0f; // last position
float start_x = 0.0f, start_y = 0.0f;
std::uint64_t start_ms = 0;
bool tap = true; // still a tap candidate (camera fingers)
};
bool InAttack(float x, float y) const;
bool InJoystickArea(float x, float y) const;
void UpdateJoystick(float x, float y);
Finger* OtherCameraFinger(std::uint64_t id);
void Release(std::uint64_t id, Finger& finger, bool cancelled);
TouchSink* m_sink = nullptr;
TouchLayout m_layout;
bool m_world = false;
std::map<std::uint64_t, Finger> m_fingers; // ids are offset by one so 0 means "none"
std::uint64_t m_ui_finger = 0, m_joy_finger = 0, m_attack_finger = 0;
JoystickView m_joy;
};
// The client's router, shared by the host loop and the Python `touch` module.
TouchRouter& GetTouchRouter();
// Touch mode: on for Android/iOS, or desktop with --touch (mouse emulates one finger).
bool IsTouchMode();
void SetTouchMode(bool enabled);
} // namespace ava2::input
+112 -3
View File
@@ -36,6 +36,7 @@
#include "python_ime.h"
#include "player_settings.h"
#include "eter_render/RaceData.h"
#include "app/input/touch_router.h"
#endif
#include <algorithm>
#include <array>
@@ -235,7 +236,7 @@ int main(int argc, char** argv) {
float render_scale = 1.0f;
#endif
bool auto_connect = false, until_loading = false, until_select = false, until_game = false;
bool use_gpu_raster = true, preview_data = false;
bool use_gpu_raster = true, preview_data = false, touch_flag = false;
std::string stdlib, script_root, view_script, screenshot, game_script, frame_profile;
bool game_script_executed = false;
std::string start_phase = "login";
@@ -270,9 +271,17 @@ int main(int argc, char** argv) {
else if (arg == "--game-frames") game_frames = std::max(1, std::stoi(next()));
else if (arg == "--preview-data") preview_data = true;
else if (arg == "--software-raster") use_gpu_raster = false;
else throw std::invalid_argument("Usage: ava2_client [--frames N] [--fps-cap N] [--render-scale 0.25..1] [--stdlib ZIP --python-root DIR] [--login-ui SCRIPT | --phase login|select|create|loading|game] [--auth-host HOST --auth-port PORT --game-host HOST --game-port PORT --account ACCOUNT] [--auto-connect] [--auto-select 0..4] [--until-select|--until-loading|--until-game] [--game-script FILE.py] [--software-raster] [--screenshot FILE.bmp]");
else if (arg == "--touch") touch_flag = true;
else throw std::invalid_argument("Usage: ava2_client [--frames N] [--fps-cap N] [--render-scale 0.25..1] [--stdlib ZIP --python-root DIR] [--login-ui SCRIPT | --phase login|select|create|loading|game] [--auth-host HOST --auth-port PORT --game-host HOST --game-port PORT --account ACCOUNT] [--auto-connect] [--auto-select 0..4] [--until-select|--until-loading|--until-game] [--game-script FILE.py] [--software-raster] [--touch] [--screenshot FILE.bmp]");
}
#ifdef AVA2_HAS_PYTHON
// Before the Python runtime starts: app.ENABLE_MOBILE_HUD follows the touch mode.
if (const char* env = std::getenv("AVA2_TOUCH"); touch_flag || (env && std::strcmp(env, "0") != 0))
ava2::input::SetTouchMode(true);
#else
(void)touch_flag;
#endif
if (fps_cap < 0) {
if (const char* env = std::getenv("MT_FPS_CAP")) fps_cap = std::max(0, std::atoi(env));
else if (const char* env = std::getenv("AVA2_FPS_CAP")) fps_cap = std::max(0, std::atoi(env));
@@ -353,6 +362,15 @@ int main(int argc, char** argv) {
#ifdef AVA2_HAS_PYTHON
// WinMain: srandom(time(NULL) + DWORD(GetCurrentProcess())) for EterBase random_range.
eter::srandom(uint32_t(std::time(nullptr)) + std::random_device{}());
#endif
#ifdef AVA2_HAS_PYTHON
// Touch mode: fingers go through the touch router instead of SDL's touch-to-mouse emulation.
// On desktop (--touch) the mouse emulates one finger.
const bool touch_mode = ava2::input::IsTouchMode();
if (touch_mode) {
SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
}
#endif
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) throw std::runtime_error(SDL_GetError());
struct Quit { ~Quit(){ SDL_Quit(); } } quit;
@@ -669,6 +687,62 @@ int main(int argc, char** argv) {
orbit_camera.SetYaw(0.0f);
orbit_camera.SetTargetHeight(120.0f);
#ifdef AVA2_HAS_PYTHON
// Touch router sink: fingers become wndMgr mouse events, the joystick and attack key of the
// original player module, and orbit camera drags / pinches.
struct HostTouchSink final : ava2::input::TouchSink {
UI::CWindowManager* wm = nullptr;
ava2::OrbitCamera* camera = nullptr;
bool release_pending = false; // world tap: button goes up on the next frame
float release_x = 0.0f, release_y = 0.0f;
bool IsUiAt(float x, float y) override {
if (!wm) return false;
auto* window = wm->PickWindow(long(x), long(y));
return window && !ava2::IsPlayerGameWindow(window->GetHandler());
}
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 WorldTap(float x, float y) override {
if (!wm) return;
// GameWindow.OnMouseLeftButtonDown sets player.SetMouseState; holding it for one
// game update picks the target or ground under the finger like a mouse click.
FlushRelease();
wm->RunMouseMove(long(x), long(y));
wm->RunMouseLeftButtonDown(long(x), long(y));
release_pending = true;
release_x = x;
release_y = y;
}
void FlushRelease() {
if (!release_pending || !wm) return;
release_pending = false;
wm->RunMouseLeftButtonUp(long(release_x), long(release_y));
}
void Joystick(bool active, float x, float y) override { ava2::SetPlayerJoystick(active, x, y); }
void Attack(bool pressed) override { ava2::SetPlayerAttackKey(pressed); }
void CameraRotate(float dx, float dy) override {
camera->SetYaw(camera->GetYaw() + dx * 0.25f);
camera->SetPitch(camera->GetPitch() - dy * 0.25f);
}
void CameraZoom(float ratio) override { camera->SetDistance(camera->GetDistance() / ratio); }
} touch_sink;
touch_sink.wm = wm;
touch_sink.camera = &orbit_camera;
auto& touch_router = ava2::input::GetTouchRouter();
if (touch_mode) {
touch_router.SetSink(&touch_sink);
touch_router.SetLayout(ava2::input::TouchLayout::ForScreen(float(screen_width), float(screen_height)));
}
// Finger position in UI pixels (SDL gives it normalized to the window).
auto finger_point = [&](const SDL_TouchFingerEvent& finger, float& x, float& y) {
int ww = 0, wh = 0;
SDL_GetWindowSize(window.get(), &ww, &wh);
SDL_RenderCoordinatesFromWindow(renderer.get(), finger.x * float(ww), finger.y * float(wh), &x, &y);
};
#endif
// Initialize Minimap Radar System
ava2::MinimapManager minimap_mgr;
minimap_mgr.SetWorldBounds(outdoor_terrain.GetWorldMinX(), outdoor_terrain.GetWorldMaxX(),
@@ -1224,6 +1298,12 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
if (want != SDL_TextInputActive(window.get()))
want ? SDL_StartTextInput(window.get()) : SDL_StopTextInput(window.get());
}
#endif
#ifdef AVA2_HAS_PYTHON
if (touch_mode) {
touch_sink.FlushRelease();
touch_router.SetWorldEnabled(last_phase == "game");
}
#endif
SDL_Event event;
while (SDL_PollEvent(&event)) {
@@ -1232,8 +1312,37 @@ 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;
if (touch_mode) touch_router.CancelAll();
}
if (wm) {
if (touch_mode) {
float tx = 0.0f, ty = 0.0f;
const std::uint64_t tms = event.common.timestamp / 1000000;
switch (event.type) {
case SDL_EVENT_FINGER_DOWN:
finger_point(event.tfinger, tx, ty);
touch_router.FingerDown(event.tfinger.fingerID, tx, ty, tms);
break;
case SDL_EVENT_FINGER_MOTION:
finger_point(event.tfinger, tx, ty);
touch_router.FingerMove(event.tfinger.fingerID, tx, ty, tms);
break;
case SDL_EVENT_FINGER_UP:
finger_point(event.tfinger, tx, ty);
touch_router.FingerUp(event.tfinger.fingerID, tx, ty, tms);
break;
case SDL_EVENT_FINGER_CANCELED:
touch_router.CancelAll();
break;
default:
break;
}
}
// In touch mode the window manager only sees fingers; desktop mouse input arrives
// a second time as the emulated finger.
const bool touch_owns_mouse = touch_mode &&
(event.type == SDL_EVENT_MOUSE_MOTION || event.type == SDL_EVENT_MOUSE_BUTTON_DOWN ||
event.type == SDL_EVENT_MOUSE_BUTTON_UP);
if (wm && !touch_owns_mouse) {
if (event.type == SDL_EVENT_MOUSE_MOTION) {
float mx, my;
SDL_RenderCoordinatesFromWindow(renderer.get(), event.motion.x, event.motion.y, &mx, &my);
+1
View File
@@ -153,6 +153,7 @@ namespace UI
void SetName(const char * c_szName);
const char * GetName() { return m_strName.c_str(); }
PyObject * GetHandler() { return m_poHandler; }
void SetSize(long width, long height);
long GetWidth() { return m_lWidth; }
long GetHeight() { return m_lHeight; }
+2
View File
@@ -34,6 +34,8 @@ namespace UI
CWindow * GetLockWindow() { return m_pLockWindow; }
CWindow * GetPointWindow() { return m_pPointWindow; }
// The window a click at (x, y) would reach, without moving the mouse (touch routing).
CWindow * PickWindow(long x, long y) { return __PickWindow(x, y); }
bool IsFocus() { return (m_pActiveWindow || m_pLockWindow); }
bool IsFocusWindow(CWindow * pWindow) { return pWindow == m_pActiveWindow; }
+3 -5
View File
@@ -1,5 +1,6 @@
#include "player_settings.h"
#include "python_app.h"
#include "app/input/touch_router.h"
#include "server_clock.h"
#include "generated_app_constants.h"
#include "../upstream/ava2-client/UserInterface/Locale_inc.h"
@@ -1018,11 +1019,8 @@ void initapp() {
{nullptr, nullptr, 0, nullptr}
};
auto* module = Py_InitModule("app", methods);
#ifdef __ANDROID__
PyModule_AddIntConstant(module, "ENABLE_MOBILE_HUD", 1);
#else
PyModule_AddIntConstant(module, "ENABLE_MOBILE_HUD", 0);
#endif
// On for Android/iOS and for desktop --touch, which tests the mobile HUD with the mouse.
PyModule_AddIntConstant(module, "ENABLE_MOBILE_HUD", input::IsTouchMode() ? 1 : 0);
register_app_constants(module);
#ifdef ENABLE_MANAGER_BANK_SYSTEM
+20 -12
View File
@@ -972,11 +972,15 @@ void UseGuildSkill(int skillSlot) {
static bool s_isAtkKey = false;
static std::array<bool, 256> s_movementKeys{};
static bool s_joystickActive = false;
static float s_joystickX = 0.0f, s_joystickY = 0.0f;
bool GetPlayerMovementYaw(float cameraYaw, float& movementYaw) {
const int x = int(s_movementKeys[32] || s_movementKeys[205]) - int(s_movementKeys[30] || s_movementKeys[203]);
const int z = int(s_movementKeys[31] || s_movementKeys[208]) - int(s_movementKeys[17] || s_movementKeys[200]);
if (!x && !z) return false;
movementYaw = std::atan2(float(x), float(z)) * 180.0f / 3.14159265f - cameraYaw;
float x = float(int(s_movementKeys[32] || s_movementKeys[205]) - int(s_movementKeys[30] || s_movementKeys[203]));
float z = float(int(s_movementKeys[31] || s_movementKeys[208]) - int(s_movementKeys[17] || s_movementKeys[200]));
// Joystick screen-down is "back" like DIK_S, so it maps onto the same camera-relative axes.
if (s_joystickActive) { x = s_joystickX; z = s_joystickY; }
if (x == 0.0f && z == 0.0f) return false;
movementYaw = std::atan2(x, z) * 180.0f / 3.14159265f - cameraYaw;
movementYaw = std::fmod(movementYaw + 720.0f, 360.0f);
return true;
}
@@ -2513,7 +2517,9 @@ static int s_mouseFunc[8] = {0};
static int s_mouseState[8] = {0};
bool IsPlayerMouseMoving() { return s_mouseState[0] == 1 && (s_mouseFunc[0] == 0 || s_mouseFunc[0] == 1); }
bool IsPlayerCameraDragging() { return s_mouseState[1] == 1 && s_mouseFunc[1] == 2; }
void CancelPlayerMovement() { s_movementKeys.fill(false); std::fill(std::begin(s_mouseState),std::end(s_mouseState),0); }
void CancelPlayerMovement() { s_movementKeys.fill(false); s_joystickActive=false; std::fill(std::begin(s_mouseState),std::end(s_mouseState),0); }
void SetPlayerJoystick(bool active, float x, float y) { s_joystickActive=active; s_joystickX=active?x:0.0f; s_joystickY=active?y:0.0f; }
bool IsPlayerGameWindow(const void* handler) { return handler && handler==s_playerGameWindow; }
static int s_mouseMiddleButtonState = 0;
static bool s_playerVisible = true;
static bool s_levelLimit = false;
@@ -3182,16 +3188,18 @@ static PyObject* player_SendDragonSoulRefine(PyObject*, PyObject* args) {
return Py_BuildNone();
}
void SetPlayerAttackKey(bool pressed) {
if (pressed && IsMainActorFishingMode()) {
NEW_Fishing();
return;
}
s_isAtkKey = pressed;
}
static PyObject* player_SetAttackKeyState(PyObject*, PyObject* args) {
int pressed = 0;
if (!PyTuple_GetInteger(args, 0, &pressed)) return Py_BuildException();
if (pressed) {
if (IsMainActorFishingMode()) {
NEW_Fishing();
return Py_BuildNone();
}
}
s_isAtkKey = (pressed != 0);
SetPlayerAttackKey(pressed != 0);
return Py_BuildNone();
}
+6
View File
@@ -89,6 +89,12 @@ bool GetPlayerMovementYaw(float cameraYaw, float& movementYaw);
bool IsPlayerMouseMoving();
bool IsPlayerCameraDragging();
void CancelPlayerMovement();
// Touch joystick (screen axes, x right / y down, length 0..1); replaces the movement keys while active.
void SetPlayerJoystick(bool active, float x, float y);
// player.SetAttackKeyState for native input (touch attack button).
void SetPlayerAttackKey(bool pressed);
// True when handler is the window passed to player.SetGameWindow (game.GameWindow).
bool IsPlayerGameWindow(const void* handler);
void SetWeaponPower(uint32_t dwMinPower, uint32_t dwMaxPower, uint32_t dwMinMagicPower, uint32_t dwMaxMagicPower, uint32_t dwAddPower);
void UpdateEquippedWeaponPower();
bool HasEnoughArrow();
+2
View File
@@ -17,6 +17,7 @@
#include "python_net.h"
#include "python_app.h"
#include "python_dbg.h"
#include "python_touch.h"
#include "python_system.h"
#include "python_wndmgr.h"
#include "python_gameplay_modules.h"
@@ -113,6 +114,7 @@ PythonRuntime::PythonRuntime(const std::filesystem::path& stdlib,const std::file
if(PyImport_AppendInittab("pack",initpack)!=0 ||
PyImport_AppendInittab("app",initapp)!=0 ||
PyImport_AppendInittab("dbg",initdbg)!=0 ||
PyImport_AppendInittab("touch",inittouch)!=0 ||
PyImport_AppendInittab("systemSetting",initsystemSetting)!=0 ||
PyImport_AppendInittab("wndMgr",initwndMgr)!=0 ||
PyImport_AppendInittab("item",initItem)!=0 ||
+72
View File
@@ -0,0 +1,72 @@
#include "python_touch.h"
#include "app/input/touch_router.h"
#include <Python.h>
namespace ava2 {
namespace {
using input::GetTouchRouter;
PyObject* touch_is_enabled(PyObject*, PyObject*) {
return PyInt_FromLong(input::IsTouchMode() ? 1 : 0);
}
// (active, baseX, baseY, knobX, knobY) in UI pixels.
PyObject* touch_get_joystick(PyObject*, PyObject*) {
const auto joy = GetTouchRouter().GetJoystick();
return Py_BuildValue("(iffff)", joy.active ? 1 : 0, joy.base_x, joy.base_y, joy.knob_x, joy.knob_y);
}
PyObject* touch_is_attack_pressed(PyObject*, PyObject*) {
return PyInt_FromLong(GetTouchRouter().IsAttackPressed() ? 1 : 0);
}
// SetAttackButton(centerX, centerY, radius); radius 0 disables the button.
PyObject* touch_set_attack_button(PyObject*, PyObject* args) {
float x = 0, y = 0, r = 0;
if (!PyArg_ParseTuple(args, "fff", &x, &y, &r)) return nullptr;
auto layout = GetTouchRouter().GetLayout();
layout.attack_x = x;
layout.attack_y = y;
layout.attack_r = r;
GetTouchRouter().SetLayout(layout);
Py_RETURN_NONE;
}
// SetJoystickArea(x, y, width, height[, radius])
PyObject* touch_set_joystick_area(PyObject*, PyObject* args) {
auto layout = GetTouchRouter().GetLayout();
float x = 0, y = 0, w = 0, h = 0, r = layout.joy_radius;
if (!PyArg_ParseTuple(args, "ffff|f", &x, &y, &w, &h, &r)) return nullptr;
layout.joy_x = x;
layout.joy_y = y;
layout.joy_w = w;
layout.joy_h = h;
layout.joy_radius = r > 1.0f ? r : 1.0f;
GetTouchRouter().SetLayout(layout);
Py_RETURN_NONE;
}
// ((attackX, attackY, attackR), (joyX, joyY, joyW, joyH, joyRadius))
PyObject* touch_get_layout(PyObject*, PyObject*) {
const auto& l = GetTouchRouter().GetLayout();
return Py_BuildValue("((fff)(fffff))", l.attack_x, l.attack_y, l.attack_r,
l.joy_x, l.joy_y, l.joy_w, l.joy_h, l.joy_radius);
}
} // namespace
void inittouch() {
static PyMethodDef methods[] = {
{"IsEnabled", touch_is_enabled, METH_NOARGS, nullptr},
{"GetJoystick", touch_get_joystick, METH_NOARGS, nullptr},
{"IsAttackPressed", touch_is_attack_pressed, METH_NOARGS, nullptr},
{"SetAttackButton", touch_set_attack_button, METH_VARARGS, nullptr},
{"SetJoystickArea", touch_set_joystick_area, METH_VARARGS, nullptr},
{"GetLayout", touch_get_layout, METH_NOARGS, nullptr},
{nullptr, nullptr, 0, nullptr}
};
Py_InitModule("touch", methods);
}
} // namespace ava2
+7
View File
@@ -0,0 +1,7 @@
#pragma once
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();
}
+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;
}
+24 -11
View File
@@ -5,18 +5,25 @@ import math
import player
import app
import wndMgr
import touch
class MobileCombatWheel(ui.Window):
"""
移动端核心战斗轮盘 (Combat Radial Controller)
- 位于屏幕右下角
- 超大普攻按钮 (长按/轻点)
- 超大普攻按钮 (长按/轻点):按下由原生触摸分流器判定(touch.SetAttackButton),
按住期间等同键盘攻击键 player.SetAttackKeyState,本按钮只做按下显示
- 环绕式扇形技能槽位 (带 CD 倒计时遮罩与等阶显示)
- 辅助功能按键 (自动锁定、上下马等)
"""
ATTACK_IDLE_COLOR = grp.GenerateColor(0.6, 0.15, 0.1, 0.45)
ATTACK_DOWN_COLOR = grp.GenerateColor(0.95, 0.35, 0.2, 0.75)
def __init__(self, origin_x=None, origin_y=None):
ui.Window.__init__(self)
self.SetWindowName("MobileCombatWheel")
# 轮盘本身不拦截触摸,否则会盖住攻击键热区和世界点击;技能槽按钮仍可点
self.AddFlag("not_pick")
sw = wndMgr.GetScreenWidth()
sh = wndMgr.GetScreenHeight()
@@ -32,14 +39,16 @@ class MobileCombatWheel(ui.Window):
self.SetSize(380, 380)
# 1. 普通攻击主键 (超大圆形热区)
self.attack_btn = ui.Button()
# 包内没有 taskbar/attack_button_0x.sub,先用半透明色块标出热区(与摇杆同风格)
self.attack_btn = ui.Bar()
self.attack_btn.SetParent(self)
self.attack_btn.SetPosition(260 - 55, 260 - 55)
self.attack_btn.SetUpVisual("d:/ymir work/ui/game/taskbar/attack_button_01.sub")
self.attack_btn.SetOverVisual("d:/ymir work/ui/game/taskbar/attack_button_02.sub")
self.attack_btn.SetDownVisual("d:/ymir work/ui/game/taskbar/attack_button_03.sub")
self.attack_btn.SetEvent(ui.__mem_func__(self.__OnAttackClick))
self.attack_btn.AddFlag("not_pick")
self.attack_btn.SetPosition(260 - 60, 260 - 60)
self.attack_btn.SetSize(120, 120)
self.attack_btn.SetColor(self.ATTACK_IDLE_COLOR)
self.attack_btn.Show()
self.attack_pressed = False
touch.SetAttackButton(origin_x, origin_y, 60)
# 2. 扇形技能槽位群
self.skills = []
@@ -71,15 +80,19 @@ class MobileCombatWheel(ui.Window):
def __del__(self):
ui.Window.__del__(self)
def __OnAttackClick(self):
player.SetAttackKeyState(True)
app.RegisterDelayedCall(0.15, lambda: player.SetAttackKeyState(False))
def __OnSkillCast(self, index):
# 释放技能 (对应 Metin2 技能槽 1~4)
player.ClickSkillSlot(index + 1)
def OnUpdate(self):
pressed = touch.IsAttackPressed()
if pressed != self.attack_pressed:
self.attack_pressed = pressed
if pressed:
self.attack_btn.SetColor(self.ATTACK_DOWN_COLOR)
else:
self.attack_btn.SetColor(self.ATTACK_IDLE_COLOR)
# 刷新技能冷却状态
for item in self.skills:
slot_idx = item['slot'] + 1
+31 -70
View File
@@ -1,101 +1,62 @@
# -*- coding: utf-8 -*-
import ui
import grp
import math
import net
import chr
import player
import app
import wndMgr
import touch
class MobileJoystick(ui.Window):
"""
移动端专用虚拟摇杆 (Virtual Joystick)
- 位于屏幕左下角
- 支持手指按下、滑动拖拽、松开自动回中
- 动态计算移动向量与航向角,同步触发移动网络包
移动端虚拟摇杆的显示层。
触摸由原生触摸分流器处理(src/app/input/touch_router.h):手指落在左下摇杆区时,
摇杆以落点为中心出现,方向经 player 模块的移动接口驱动主角,与键盘 WASD 走同一路径。
本窗口只读取 touch.GetJoystick() 绘制底盘和摇杆头,不接收点击。
"""
def __init__(self, base_x=160, base_y=None, radius=75, knob_radius=35):
def __init__(self, base_x=160, base_y=None, radius=None, knob_radius=35):
ui.Window.__init__(self)
self.SetWindowName("MobileJoystick")
self.AddFlag("not_pick")
sw = wndMgr.GetScreenWidth()
sh = wndMgr.GetScreenHeight()
if base_y is None:
base_y = sh - 180
self.base_x = base_x
self.base_y = base_y
if radius is None:
radius = int(touch.GetLayout()[1][4])
self.radius = radius
self.knob_radius = knob_radius
self.knob_x = base_x
self.knob_y = base_y
self.is_dragging = False
self.SetPosition(base_x - radius - 20, base_y - radius - 20)
self.SetSize((radius + 20) * 2, (radius + 20) * 2)
self.SetSize(radius * 2, radius * 2)
# 外圈底盘
self.base_bar = ui.Bar()
self.base_bar.SetParent(self)
self.base_bar.SetPosition(20, 20)
self.base_bar.AddFlag("not_pick")
self.base_bar.SetPosition(0, 0)
self.base_bar.SetSize(radius * 2, radius * 2)
self.base_bar.SetColor(grp.GenerateColor(0.1, 0.1, 0.15, 0.4))
self.base_bar.Show()
# 内部活动摇杆
# 摇杆头
self.knob = ui.Bar()
self.knob.SetParent(self)
offset_k = radius - knob_radius + 20
self.knob.SetPosition(offset_k, offset_k)
self.knob.AddFlag("not_pick")
self.knob.SetSize(knob_radius * 2, knob_radius * 2)
self.knob.SetColor(grp.GenerateColor(0.8, 0.8, 0.9, 0.6))
self.knob.Show()
# 未按下时停在默认位置,提示摇杆区
if base_y is None:
import wndMgr
base_y = wndMgr.GetScreenHeight() - 180
self.rest_x = base_x
self.rest_y = base_y
self.__Place(base_x, base_y, base_x, base_y)
def __del__(self):
ui.Window.__del__(self)
def OnMouseLeftButtonDown(self):
self.is_dragging = True
return True
def OnMouseLeftButtonUp(self):
self.is_dragging = False
# 松手回中
offset_k = self.radius - self.knob_radius + 20
self.knob.SetPosition(offset_k, offset_k)
return True
def __Place(self, base_x, base_y, knob_x, knob_y):
self.SetPosition(int(base_x) - self.radius, int(base_y) - self.radius)
self.knob.SetPosition(int(knob_x - base_x) + self.radius - self.knob_radius,
int(knob_y - base_y) + self.radius - self.knob_radius)
def OnUpdate(self):
if not self.is_dragging:
return
# 采样当前鼠标/触控位置
mx, my = wndMgr.GetMousePosition()
dx = mx - self.base_x
dy = my - self.base_y
dist = math.hypot(dx, dy)
if dist > self.radius:
dx = (dx / dist) * self.radius
dy = (dy / dist) * self.radius
# 更新摇杆视觉位置
offset_k = self.radius - self.knob_radius + 20
self.knob.SetPosition(offset_k + int(dx), offset_k + int(dy))
# 若达到死区阈值,计算朝向并向服务器发包同步
if dist > 15:
angle_rad = math.atan2(dy, dx)
yaw_deg = math.degrees(angle_rad)
# 获取主角当前坐标
main_vid = net.GetMainActorVID() if hasattr(net, 'GetMainActorVID') else 0
if main_vid != 0:
cur_x, cur_y, cur_z = chr.GetPixelPosition(main_vid) if hasattr(chr, 'GetPixelPosition') else (0, 0, 0)
# 计算步进
step = 250
target_x = cur_x + int(step * math.cos(angle_rad))
target_y = cur_y + int(step * math.sin(angle_rad))
if hasattr(net, 'SendCharacterMovePacket'):
net.SendCharacterMovePacket(1, 0, int(yaw_deg), int(target_x), int(target_y))
active, base_x, base_y, knob_x, knob_y = touch.GetJoystick()
if active:
self.__Place(base_x, base_y, knob_x, knob_y)
else:
self.__Place(self.rest_x, self.rest_y, self.rest_x, self.rest_y)