Files
AVA2/tests/touch_game_test.cpp
T
shenleiandClaude Opus 5.5 ed2c185135 Mobile UI scaling and live safe-area relayout (mobile step 5)
Phones and tablets pick the logical UI size from the screen (height
600..768 from the dp height, width from the aspect ratio) instead of
letterboxing 1024x768; metin2.cfg is left untouched via a display-size
override. --phone/--safe-insets/--ui-height simulate on desktop. The
mobile HUD rebuilds and re-adopts the original windows when the safe
area changes. Keeps HUD gauge backgrounds alive and fixes the step-4
disc pixel check threshold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 21:39:58 +09:00

171 lines
8.5 KiB
C++

// Touch mode in the original GameWindow: the mobile HUD must leave the joystick / attack zones and
// open world to the touch router, while real UI (HUD buttons) still takes the finger.
#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 <array>
#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, player\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));
// Mobile mode hides the PC taskbar; the HUD buttons (here the combat wheel's page button)
// are real UI. The quick belt is quickslot page 4 and the minimap sits under the top-right row.
runtime.execute("assert not game.interface.wndTaskBar.IsShow()\n"
"assert [s.globalIndex for s in hud.quick_slots] == range(24, 32)\n"
"mm = game.interface.wndMiniMap\n"
"assert mm.GetGlobalPosition()[1] >= hud.drawer.GetGlobalPosition()[1] + hud.drawer.GetRowHeight()\n"
"pb = hud.combat_wheel.page_button\n"
"touch_tb = pb.GetGlobalPosition()\n"
"touch_tb = (touch_tb[0] + pb.GetWidth() / 2, touch_tb[1] + pb.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));
// HUD button 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);
// Safe area (platform/): desktop has none; a HUD built with insets keeps the attack button inside.
runtime.execute("assert touch.GetSafeArea() == (0, 0, 0, 0)\n");
router.SetLayout(ava2::input::TouchLayout::ForScreen(W, H, 40.0f, 0.0f, 30.0f, 20.0f));
runtime.execute("assert touch.GetSafeArea() == (40, 0, 30, 20)\n"
"import ui_mobile_hud\n"
"safe_hud = ui_mobile_hud.MobileHUD(game.interface)\n"
"(ax, ay, ar), joy = touch.GetLayout()\n"
"assert (ax, ay, ar) == (1024 - 30 - 120, 768 - 20 - 120, 60), (ax, ay, ar)\n"
"assert safe_hud.player_hud.GetGlobalPosition()[0] == 40 + 12\n"
"safe_hud.Hide()\nsafe_hud = None\n"
// Live: the running HUD notices the new insets, rebuilds and moves the original windows.
"hud.OnUpdate()\n"
"assert hud.safe_area == (40, 0, 30, 20)\n"
"assert hud.player_hud.GetGlobalPosition()[0] == 40 + 12\n"
"assert touch.GetLayout()[0] == (1024 - 30 - 120, 768 - 20 - 120, 60)\n"
"assert mm.GetGlobalPosition()[0] + mm.GetWidth() == 1024 - 30\n"
"assert game.affectShower.GetGlobalPosition()[0] == 40 + 12 + 64 + 8\n"
"assert not game.interface.wndTaskBar.IsShow()\n");
router.SetLayout(layout);
runtime.execute("hud.OnUpdate()\n"
"assert hud.safe_area == (0, 0, 0, 0) and hud.player_hud.GetGlobalPosition()[0] == 12\n"
"assert mm.GetGlobalPosition()[0] + mm.GetWidth() == 1024\n"
"game.OnUpdate()\n");
SDL_FillSurfaceRect(surface, nullptr, 0xff10141e);
wm->Render(); SDL_RenderPresent(renderer);
// CDisc geometry: the attack disc is dark red between the icon and the rim; empty screen is not.
auto pixel = [&](int x, int y) {
Uint8 r = 0, g = 0, b = 0, a = 0;
SDL_ReadSurfacePixel(surface, x, y, &r, &g, &b, &a);
return std::array<int, 3>{r, g, b};
};
const auto disc = pixel(int(layout.attack_x), int(layout.attack_y) + 45);
assert(disc[0] > 40 && disc[0] > disc[1] * 3 && disc[0] > disc[2] * 3);
const auto empty = pixel(int(W * 0.6f), int(H * 0.4f));
assert(empty[0] < 40);
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";
}