diff --git a/docs/MOBILE-ARCHITECTURE.md b/docs/MOBILE-ARCHITECTURE.md index 788ccf02..9ff1825a 100644 --- a/docs/MOBILE-ARCHITECTURE.md +++ b/docs/MOBILE-ARCHITECTURE.md @@ -7,13 +7,14 @@ - `android/build.sh` 可以编出 arm64 调试 APK(SDL3 + FreeType 自动编译,16 KB 页对齐),连接手机时自动安装启动;真机运行尚未验证。 - `android/push_assets.py` 把资源(`assets/loose` + python-root + `python27.zip`)推送到应用外部存储目录,客户端启动时切换到该目录。 - 游戏资源为 2.8 GB、约 5 万个散文件(`assets/loose`),17 个源文件中 120 多处通过 `std::filesystem` / `ifstream` 直接读文件,无法读取 APK 内置资源。 -- 平台层已实现(`src/app/platform/`):资源根目录与配置目录;前后台切换(进后台时保存配置、暂停声音、取消输入、停止渲染,网络照常收发;Android 设 `SDL_ANDROID_BLOCK_ON_PAUSE=0`);安全区域按逻辑分辨率换算后放进触摸布局,HUD 用 `touch.GetSafeArea()` 内缩。低内存事件目前只记日志;HUD 只在创建时读安全区;均未真机验证。 +- 平台层已实现(`src/app/platform/`):资源根目录与配置目录;前后台切换(进后台时保存配置、暂停声音、取消输入、停止渲染,网络照常收发;Android 设 `SDL_ANDROID_BLOCK_ON_PAUSE=0`);安全区域按逻辑分辨率换算后放进触摸布局,HUD 用 `touch.GetSafeArea()` 内缩。低内存事件目前只记日志;均未真机验证。 - 触摸分流已实现(`src/app/input/touch_router.*`,见下文 `input/`):移动端或 `--touch` / `AVA2_TOUCH=1` 时关闭 SDL 触摸转鼠标,每个手指分到界面、攻击键、左下浮动摇杆或镜头(单指拖动转向、双指缩放、轻点=点地移动)。Python 模块 `touch` 供 HUD 设置攻击键位置并读取摇杆状态;`ui_mobile_joystick` / `ui_mobile_combat` 改为只做显示(`not_pick`)。多指同时操作尚未在真机验证。 - 出招已接通(PORT-PLAN「Mobile step 3b」):触摸攻击键和键盘共用 `ProcessPlayerAttack`,按原版连招时序出招,发送 FUNC_COMBO 移动包,命中检测后发 `CG_ATTACK`;尚未在真服验证,弓箭飞行、技能、PvP 规则等仍缺。 - 手机 HUD 布局已实现(PORT-PLAN「Mobile step 4」):左上头像圆框 + 等级名字 + HP/SP/体力条,buff 图标在其下;顶部居中沿用原版目标栏;右上一排圆形入口(好友、角色、背包、菜单)+ 原版小地图 + 活动图标;左侧竖排按钮(任务、技能、表情、聊天)和原版提示按钮;右下攻击键 + 两圈 8 个快捷格(当前快捷页,可切第 1-3 页)+ 换页、上下马、拾取;底部药水栏(第 4 快捷页,全局格 24-31)和经验细条。移动模式下 PC 任务栏隐藏。圆框由原生 `CDisc`(`wndMgr.RegisterDisc`)几何绘制,图标只用 AVA2 自己的贴图,不用其他服的美术。小地图仍是原版圆形、按钮文字为英文、未真机验证。 - 软键盘已改为仅在输入框获得焦点(`ime.EnableCaptureInput`)或聊天打开时弹出。 - `src/app/main.cpp` 共 2418 行,启动、主循环、登录和调试参数都在其中。 - Android 默认渲染缩放 0.75。 +- 界面缩放已实现(PORT-PLAN「Mobile step 5」):手机/平板按屏幕宽高比和像素密度自动选逻辑分辨率(高 600–768,宽随比例,至少 1024),不再给 1024×768 加黑边,也不改写 metin2.cfg;`--ui-height` / `AVA2_UI_HEIGHT` 可改高度;桌面可用 `--phone 2400x1080@2.75 --safe-insets 50,0,0,24` 模拟。安全区变化(如旋转 180 度刘海换边)时 HUD 实时重建并重新摆放原版窗口。运行中窗口尺寸变化(分屏、折叠屏)仍沿用启动时的逻辑分辨率;尚无游戏内界面大小选项;未真机验证。 ## 分层结构 diff --git a/docs/PORT-PLAN.md b/docs/PORT-PLAN.md index cae6da39..d8468b70 100644 --- a/docs/PORT-PLAN.md +++ b/docs/PORT-PLAN.md @@ -1301,3 +1301,39 @@ Outstanding: no device run; minimap is still the original round one; labels are English; chat input line can overlap the wheel's page button while typing; no auto-hunt / target-lock buttons; Android keeps the 1024x768 letterboxed logical size (step 5 scaling); the HUD reads the safe area only when built. + +### 2026-10-12 Mobile step 5 — UI scaling and live safe area + +- Logical UI size on phones / tablets (`platform::ChooseMobileLogicalSize`): + instead of letterboxing metin2.cfg's 1024x768, height = + clamp(round(height in dp x 1.5), 600, 768) and width = height x screen + aspect (landscape, even, at least 1024). A 2400x1080 phone at density 2.75 + gets 1332x600, a 2400x1080 at 2.25 gets 1600x720, a 4:3 tablet 1024x768. + `--ui-height N` / `AVA2_UI_HEIGHT` override the height (600..1080). + `CPythonSystem::SetDisplaySizeOverride` makes `systemSetting.GetWidth/Height` + and the window manager report it without rewriting metin2.cfg. + Android / iOS read the primary display (`QueryDisplay`); desktop simulates + with `--phone WxH[@density]` (implies `--touch`) and `--safe-insets L,T,R,B`. +- Live safe area: on `SDL_EVENT_WINDOW_SAFE_AREA_CHANGED` / resize the host + resets the touch layout for the new insets; `MobileHUD.OnUpdate` sees + `touch.GetSafeArea()` change and rebuilds itself under a fresh root window + (old root hidden), re-registers the attack button and re-runs + `AdoptDefaultWindows` / `AdoptGameWindows` (minimap, chat, hint buttons, + buffs, event icon). Covers rotating 180 degrees with a side notch. +- HUD gauge backgrounds and frames are now kept referenced (they were + destroyed right after creation). +- Fixed the step-4 `touch_game` disc pixel check (threshold too high; the + earlier run used a stale test binary because the build stopped at the known + speedtree_forest_test errors without `-k`). + +Verified: `ava2.platform` (size choice for phones, portrait-reported +displays, tablets, override clamp, near-square foldable, `--phone` parsing), +`ava2.touch_game` (running HUD relayouts on new insets and back, minimap and +buffs follow, attack button re-registered), 53/53 desktop tests, +`ava2_client --phone 2400x1080@2.75 --safe-insets 50,0,0,24 --phase game +--preview-data` renders 1332x600 full-bleed with the HUD inset. +Outstanding: not run on a device (display mode vs. immersive window size, +real cutout insets); a window size change at runtime (split screen, +foldable) keeps the logical size chosen at start; no in-game UI size option +yet (only `--ui-height` / env); original PC windows (inventory etc.) are +not rearranged for 600 px height. diff --git a/src/app/main.cpp b/src/app/main.cpp index ff4433a9..bd76b543 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -191,6 +192,8 @@ int main(int argc, char** argv) { bool auto_connect = false, until_loading = false, until_select = false, until_game = false; bool use_gpu_raster = true, preview_data = false, touch_flag = false; std::string stdlib, script_root, view_script, screenshot, game_script, frame_profile; + std::string phone_spec, safe_spec; + int ui_height = 0; bool game_script_executed = false; std::string start_phase = "login"; std::array fields{{{"Auth host","127.0.0.1",255},{"Auth port","",5},{"Game host","127.0.0.1",255},{"Game port","",5},{"Account","",30},{"Password","",16,true}}}; @@ -225,7 +228,10 @@ int main(int argc, char** argv) { else if (arg == "--preview-data") preview_data = true; else if (arg == "--software-raster") use_gpu_raster = false; 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]"); + else if (arg == "--phone") { phone_spec = next(); touch_flag = true; } + else if (arg == "--ui-height") ui_height = std::stoi(next()); + else if (arg == "--safe-insets") safe_spec = next(); + 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] [--phone WxH[@density]] [--ui-height N] [--safe-insets L,T,R,B] [--screenshot FILE.bmp]"); } #ifdef AVA2_HAS_PYTHON @@ -336,8 +342,29 @@ int main(int argc, char** argv) { // CPythonApplication::Create sizes the device from metin2.cfg (ava2::CPythonSystem::GetWidth/GetHeight). int screen_width = 1280, screen_height = 720; + // Phones and tablets (or --phone on desktop): the logical UI size follows the screen instead + // of letterboxing metin2.cfg's size (platform::ChooseMobileLogicalSize). + bool mobile_ui = false; + float phone_w = 0.0f, phone_h = 0.0f, phone_density = 1.0f; +#if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_IOS) + mobile_ui = ava2::platform::QueryDisplay(&phone_w, &phone_h, &phone_density); +#endif + if (!phone_spec.empty()) { + if (!ava2::platform::ParsePhoneSpec(phone_spec, &phone_w, &phone_h, &phone_density)) + throw std::invalid_argument("--phone expects WxH or WxH@density"); + mobile_ui = true; + } + if (ui_height <= 0) { + if (const char* env = std::getenv("AVA2_UI_HEIGHT")) ui_height = std::atoi(env); + } #ifdef AVA2_HAS_PYTHON if (python) { + if (mobile_ui) { + const auto size = ava2::platform::ChooseMobileLogicalSize(phone_w, phone_h, phone_density, ui_height); + ava2::CPythonSystem::Instance().SetDisplaySizeOverride(uint32_t(size.width), uint32_t(size.height)); + SDL_Log("Mobile UI: screen %.0fx%.0f density %.2f -> logical %dx%d", + phone_w, phone_h, phone_density, size.width, size.height); + } screen_width = int(ava2::CPythonSystem::Instance().GetWidth()); screen_height = int(ava2::CPythonSystem::Instance().GetHeight()); } @@ -684,9 +711,17 @@ int main(int argc, char** argv) { touch_sink.wm = wm; touch_sink.camera = &orbit_camera; auto& touch_router = ava2::input::GetTouchRouter(); + // --safe-insets L,T,R,B simulates a notch / gesture bar on desktop. + auto query_safe = [&]() { + ava2::platform::SafeInsets safe; + if (!safe_spec.empty() && + std::sscanf(safe_spec.c_str(), "%f,%f,%f,%f", &safe.left, &safe.top, &safe.right, &safe.bottom) == 4) + return safe; + return ava2::platform::QuerySafeInsets(window.get(), renderer.get()); + }; if (touch_mode) { touch_router.SetSink(&touch_sink); - const auto safe = ava2::platform::QuerySafeInsets(window.get(), renderer.get()); + const auto safe = query_safe(); touch_router.SetLayout(ava2::input::TouchLayout::ForScreen(float(screen_width), float(screen_height), safe.left, safe.top, safe.right, safe.bottom)); SDL_Log("Safe area insets: %.0f %.0f %.0f %.0f", safe.left, safe.top, safe.right, safe.bottom); @@ -1266,12 +1301,17 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); if (event.type == SDL_EVENT_QUIT) running = false; #ifdef AVA2_HAS_PYTHON if (touch_mode && (event.type == SDL_EVENT_WINDOW_SAFE_AREA_CHANGED || event.type == SDL_EVENT_WINDOW_RESIZED)) { - // The HUD reads the insets when it is built; the router keeps its button positions. - const auto safe = ava2::platform::QuerySafeInsets(window.get(), renderer.get()); - auto layout = touch_router.GetLayout(); - layout.safe_left = safe.left; layout.safe_top = safe.top; - layout.safe_right = safe.right; layout.safe_bottom = safe.bottom; - touch_router.SetLayout(layout); + // Rotating 180 degrees moves the notch to the other side. The router gets the default + // layout for the new insets; MobileHUD sees touch.GetSafeArea() change, lays itself out + // again and re-registers its attack button. + const auto safe = query_safe(); + const auto old = touch_router.GetLayout(); + if (safe.left != old.safe_left || safe.top != old.safe_top || + safe.right != old.safe_right || safe.bottom != old.safe_bottom) { + touch_router.SetLayout(ava2::input::TouchLayout::ForScreen(float(screen_width), float(screen_height), + safe.left, safe.top, safe.right, safe.bottom)); + SDL_Log("Safe area insets: %.0f %.0f %.0f %.0f", safe.left, safe.top, safe.right, safe.bottom); + } } if (event.type == SDL_EVENT_WINDOW_FOCUS_LOST) { ava2::CancelPlayerMovement(); orbit_camera.EndDrag(); diff --git a/src/app/platform/platform.cpp b/src/app/platform/platform.cpp index 97c5ca2e..e6a09062 100644 --- a/src/app/platform/platform.cpp +++ b/src/app/platform/platform.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include #include @@ -135,6 +137,43 @@ SafeInsets MapSafeInsets(float windowW, float windowH, float safeX, float safeY, return out; } +LogicalSize ChooseMobileLogicalSize(float pixelW, float pixelH, float density, int uiHeight) { + if (pixelW <= 0.0f || pixelH <= 0.0f) return {1024, 768}; + if (pixelW < pixelH) std::swap(pixelW, pixelH); // the activity is landscape + if (density <= 0.0f) density = 1.0f; + const float aspect = pixelW / pixelH; + int height = uiHeight > 0 ? std::clamp(uiHeight, 600, 1080) + : std::clamp(int(std::lround(pixelH / density * 1.5f)), 600, 768); + int width = int(std::lround(height * aspect)) & ~1; + if (width < 1024) { + width = 1024; + height = int(std::lround(1024.0f / aspect)) & ~1; + } + return {width, height}; +} + +bool QueryDisplay(float* pixelW, float* pixelH, float* density) { + const SDL_DisplayID display = SDL_GetPrimaryDisplay(); + const SDL_DisplayMode* mode = display ? SDL_GetCurrentDisplayMode(display) : nullptr; + if (!mode || mode->w <= 0 || mode->h <= 0) return false; + const float pd = mode->pixel_density > 0.0f ? mode->pixel_density : 1.0f; + *pixelW = float(mode->w) * pd; + *pixelH = float(mode->h) * pd; + // Android: content scale = DisplayMetrics.density (dp -> px); desktop: the UI scale. + const float scale = SDL_GetDisplayContentScale(display); + *density = scale > 0.0f ? scale : pd; + return true; +} + +bool ParsePhoneSpec(const std::string& spec, float* pixelW, float* pixelH, float* density) { + float w = 0.0f, h = 0.0f, d = 1.0f; + char extra = 0; + const int n = std::sscanf(spec.c_str(), "%fx%f@%f%c", &w, &h, &d, &extra); + if ((n != 2 && n != 3) || w <= 0.0f || h <= 0.0f || d <= 0.0f) return false; + *pixelW = w; *pixelH = h; *density = d; + return true; +} + SafeInsets QuerySafeInsets(SDL_Window* window, SDL_Renderer* renderer) { int ww = 0, wh = 0, lw = 0, lh = 0; SDL_Rect safe{}; diff --git a/src/app/platform/platform.h b/src/app/platform/platform.h index fa68c383..dbe3673a 100644 --- a/src/app/platform/platform.h +++ b/src/app/platform/platform.h @@ -68,4 +68,23 @@ SafeInsets MapSafeInsets(float windowW, float windowH, float safeX, float safeY, // SDL_GetWindowSafeArea mapped through the renderer's logical presentation. SafeInsets QuerySafeInsets(SDL_Window* window, SDL_Renderer* renderer); +// Logical UI size on a phone or tablet (mobile step 5). The original windows are laid out in +// fixed UI pixels for 800x600 and up, so instead of letterboxing metin2.cfg's 1024x768 the UI +// height follows the screen's physical size and the width follows its aspect ratio: +// height = clamp(round(heightInDp * 1.5), 600, 768) — a 48 px HUD button is ~32 dp or more; +// width = height * aspect (landscape, even), at least 1024 (a 4:3 tablet gets 1024x768). +// uiHeight > 0 overrides the height (still clamped to 600..1080). +struct LogicalSize { + int width = 0, height = 0; + bool operator==(const LogicalSize& o) const { return width == o.width && height == o.height; } +}; +LogicalSize ChooseMobileLogicalSize(float pixelW, float pixelH, float density, int uiHeight = 0); + +// The primary display's pixel size and density (SDL_GetCurrentDisplayMode / content scale); +// false when SDL video is not up. +bool QueryDisplay(float* pixelW, float* pixelH, float* density); + +// Parses "WxH" or "WxH@density" (desktop --phone simulation, e.g. 2400x1080@2.75). +bool ParsePhoneSpec(const std::string& spec, float* pixelW, float* pixelH, float* density); + } // namespace ava2::platform diff --git a/src/scripting/python_system.cpp b/src/scripting/python_system.cpp index eb070e61..69008a14 100644 --- a/src/scripting/python_system.cpp +++ b/src/scripting/python_system.cpp @@ -229,12 +229,18 @@ int CPythonSystem::GetFrequencyIndex(int res_index, uint32_t frequency) uint32_t CPythonSystem::GetWidth() { - return m_Config.width; + return m_displayWidth ? m_displayWidth : m_Config.width; } uint32_t CPythonSystem::GetHeight() { - return m_Config.height; + return m_displayHeight ? m_displayHeight : m_Config.height; +} + +void CPythonSystem::SetDisplaySizeOverride(uint32_t width, uint32_t height) +{ + m_displayWidth = width; + m_displayHeight = height; } uint32_t CPythonSystem::GetBPP() diff --git a/src/scripting/python_system.h b/src/scripting/python_system.h index 122ef986..b37d10d1 100644 --- a/src/scripting/python_system.h +++ b/src/scripting/python_system.h @@ -145,6 +145,9 @@ public: uint32_t GetWidth(); uint32_t GetHeight(); + // Mobile: the logical UI size chosen from the screen (platform::ChooseMobileLogicalSize). + // GetWidth/GetHeight report it; metin2.cfg keeps its own WIDTH/HEIGHT. + void SetDisplaySizeOverride(uint32_t width, uint32_t height); uint32_t GetBPP(); uint32_t GetFrequency(); bool IsSoftwareCursor(); @@ -260,6 +263,7 @@ protected: int m_ResolutionCount = 0; TConfig m_Config; + uint32_t m_displayWidth = 0, m_displayHeight = 0; TConfig m_OldConfig; bool m_isInterfaceConfig = false; diff --git a/tests/platform_test.cpp b/tests/platform_test.cpp index 9048b0b5..12506679 100644 --- a/tests/platform_test.cpp +++ b/tests/platform_test.cpp @@ -77,12 +77,40 @@ void TestTouchLayout() { assert(safe.joy_y + safe.joy_h <= 720 - 30 + 0.01f); } +void TestMobileLogicalSize() { + using ava2::platform::ChooseMobileLogicalSize; + using ava2::platform::LogicalSize; + // 20:9 phone, 2400x1080 at density 2.75 (393 dp tall): height floor 600, width by aspect. + assert((ChooseMobileLogicalSize(2400, 1080, 2.75f) == LogicalSize{1332, 600})); + // Reported in portrait before the landscape activity rotates: same result. + assert((ChooseMobileLogicalSize(1080, 2400, 2.75f) == LogicalSize{1332, 600})); + // A larger 1.5x-density phone (480 dp tall) gets more UI pixels. + assert((ChooseMobileLogicalSize(2400, 1080, 2.25f) == LogicalSize{1600, 720})); + // 4:3 tablet: height cap 768 -> 1024x768, no letterbox. + assert((ChooseMobileLogicalSize(2048, 1536, 2.0f) == LogicalSize{1024, 768})); + // 16:10 tablet. + assert((ChooseMobileLogicalSize(1920, 1200, 1.5f) == LogicalSize{1228, 768})); + // Override (setting / --ui-height), clamped to 600..1080. + assert((ChooseMobileLogicalSize(2400, 1080, 2.75f, 720) == LogicalSize{1600, 720})); + assert((ChooseMobileLogicalSize(2400, 1080, 2.75f, 100) == LogicalSize{1332, 600})); + // Near-square foldable: the width floor 1024 wins. + assert((ChooseMobileLogicalSize(2208, 1768, 2.6f) == LogicalSize{1024, 820})); + assert((ChooseMobileLogicalSize(0, 0, 1.0f) == LogicalSize{1024, 768})); + + float w = 0, h = 0, d = 0; + assert(ava2::platform::ParsePhoneSpec("2400x1080@2.75", &w, &h, &d) && Near(w, 2400) && Near(h, 1080) && Near(d, 2.75f)); + assert(ava2::platform::ParsePhoneSpec("1600x720", &w, &h, &d) && Near(d, 1.0f)); + assert(!ava2::platform::ParsePhoneSpec("phone", &w, &h, &d)); + assert(!ava2::platform::ParsePhoneSpec("0x720", &w, &h, &d)); +} + } // namespace int main() { TestLifecycle(); TestSafeInsets(); TestTouchLayout(); + TestMobileLogicalSize(); std::cout << "Platform lifecycle and safe area insets behave\n"; return 0; } diff --git a/tests/touch_game_test.cpp b/tests/touch_game_test.cpp index 920bb584..48b91dc4 100644 --- a/tests/touch_game_test.cpp +++ b/tests/touch_game_test.cpp @@ -133,8 +133,20 @@ int main(int argc, char** argv) { "(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"); + "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); @@ -145,7 +157,7 @@ int main(int argc, char** argv) { return std::array{r, g, b}; }; const auto disc = pixel(int(layout.attack_x), int(layout.attack_y) + 45); - assert(disc[0] > 80 && disc[0] > disc[2] * 2); + 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]); diff --git a/upstream/python-root/ui_mobile_hud.py b/upstream/python-root/ui_mobile_hud.py index 5433cc08..b1d8852f 100644 --- a/upstream/python-root/ui_mobile_hud.py +++ b/upstream/python-root/ui_mobile_hud.py @@ -11,6 +11,7 @@ import ui_mobile_drawer import ui_mobile_widgets as widgets import touch import uiCharacter +import constInfo def unsigned32(n): return n & 0xFFFFFFFFL @@ -50,16 +51,15 @@ class MobileHUD(ui.Window): self.AddFlag("not_pick") self.safe_area = touch.GetSafeArea() - self.race = -1 - self.lastBeltRefresh = 0 + self.root = None self.__BuildUI(sw, sh) def __del__(self): ui.Window.__del__(self) - def __Container(self, x, y, w, h): + def __Container(self, x, y, w, h, parent=None): wnd = ui.Window() - wnd.SetParent(self) + wnd.SetParent(parent or self.root) wnd.AddFlag("not_pick") wnd.SetPosition(x, y) wnd.SetSize(w, h) @@ -88,10 +88,20 @@ class MobileHUD(ui.Window): frame.SetSize(w + 2, h + 2) frame.SetColor(grp.GenerateColor(0.55, 0.42, 0.25, 0.9)) frame.Show() + # 底条和边框不保留引用会被立即销毁 + self.gauge_parts += [bg, frame] return bar + # 所有控件挂在 root 下;安全区变化时整棵重建(旧 root 失去引用即销毁) def __BuildUI(self, w, h): safe_l, safe_t, safe_r, safe_b = self.safe_area + self.race = -1 + self.lastBeltRefresh = 0 + self.gauge_parts = [] + if self.root: + # 旧控件之间若有循环引用,Python 2 不会回收带 __del__ 的对象,先整体隐藏 + self.root.Hide() + self.root = self.__Container(0, 0, w, h, self) # 1. 左上:主角状态 self.player_hud = self.__Container(safe_l + 12, safe_t + 8, 300, self.FACE_DIAMETER + 4) @@ -118,9 +128,9 @@ class MobileHUD(ui.Window): # 2. 右上:入口按钮行 + 菜单面板 self.drawer = ui_mobile_drawer.MobileDrawer(self.interface) - self.drawer.SetParent(self) + self.drawer.SetParent(self.root) self.drawer.Show() - self.drawer.AttachPanel(self) + self.drawer.AttachPanel(self.root) # 3. 左侧竖排按钮 side = [ @@ -133,7 +143,7 @@ class MobileHUD(ui.Window): top = safe_t + 8 + self.FACE_DIAMETER + 48 for i, (label, icon, event) in enumerate(side): btn = widgets.RoundButton(self.SIDE_DIAMETER) - btn.SetParent(self) + btn.SetParent(self.root) btn.SetPosition(safe_l + 12, top + i * self.SIDE_STEP) if not icon or not btn.SetIcon(icon): btn.SetLabel(label) @@ -143,12 +153,12 @@ class MobileHUD(ui.Window): # 4. 左下:摇杆显示层 self.joystick = ui_mobile_joystick.MobileJoystick(base_x=safe_l + 160, base_y=h - safe_b - 180) - self.joystick.SetParent(self) + self.joystick.SetParent(self.root) self.joystick.Show() # 5. 右下:战斗轮盘 self.combat_wheel = ui_mobile_combat.MobileCombatWheel(origin_x=w - safe_r - 120, origin_y=h - safe_b - 120) - self.combat_wheel.SetParent(self) + self.combat_wheel.SetParent(self.root) self.combat_wheel.Show() # 6. 底部:药水栏(第 4 快捷页)与经验条 @@ -164,7 +174,7 @@ class MobileHUD(ui.Window): self.quick_slots.append(slot) self.exp_width = w - safe_l - safe_r - self.exp_bar = self.__Gauge(self, safe_l, h - safe_b - 4, self.exp_width, 3, + self.exp_bar = self.__Gauge(self.root, safe_l, h - safe_b - 4, self.exp_width, 3, grp.GenerateColor(0.05, 0.05, 0.05, 0.6), grp.GenerateColor(0.85, 0.70, 0.25, 0.9)) def __ToggleCharacter(self, state): @@ -261,7 +271,21 @@ class MobileHUD(ui.Window): pct = min(1.0, max(0.0, float(cur) / float(maximum))) bar.SetSize(int(width * pct), height) + # 刘海/手势条位置变了(例如手机旋转 180 度):按新安全区重建 HUD,并重新摆放原版窗口 + def Relayout(self, safeArea): + self.safe_area = safeArea + self.__BuildUI(wndMgr.GetScreenWidth(), wndMgr.GetScreenHeight()) + if self.IsShow(): + self.AdoptDefaultWindows() + game = constInfo.GetGameInstance() + if game and getattr(game, "interface", None) is self.interface: + self.AdoptGameWindows(game) + def OnUpdate(self): + safeArea = touch.GetSafeArea() + if safeArea != self.safe_area: + self.Relayout(safeArea) + # 1. 主角状态 self.__SetGauge(self.hp_bar, self.HP_WIDTH, 12, player.GetStatus(player.HP), player.GetStatus(player.MAX_HP)) self.__SetGauge(self.mp_bar, self.SP_WIDTH, 9, player.GetStatus(player.SP), player.GetStatus(player.MAX_SP))