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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
11bd94d1ce
commit
ed2c185135
+48
-8
@@ -43,6 +43,7 @@
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
@@ -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<Field,6> 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();
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
@@ -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{};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user