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:
shenlei
2026-10-12 21:39:58 +09:00
co-authored by Claude Opus 5.5
parent 11bd94d1ce
commit ed2c185135
10 changed files with 232 additions and 23 deletions
+39
View File
@@ -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{};