Files
AVA2/tests/platform_test.cpp
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

117 lines
5.4 KiB
C++

// platform/ (mobile step 2): lifecycle transitions, safe-area mapping into the letterboxed UI and
// the touch layout kept inside the safe insets.
#include "app/platform/platform.h"
#include "app/input/touch_router.h"
#include <SDL3/SDL.h>
#include <cassert>
#include <cmath>
#include <iostream>
namespace {
using ava2::platform::Lifecycle;
using ava2::platform::MapSafeInsets;
using ava2::platform::SafeInsets;
bool Near(float a, float b) { return std::fabs(a - b) < 0.01f; }
void TestLifecycle() {
Lifecycle l;
assert(l.Poll() == Lifecycle::Change::None && !l.IsBackground());
// Android sends WILL and DID together; one transition.
l.OnEvent(SDL_EVENT_WILL_ENTER_BACKGROUND);
l.OnEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
assert(l.IsBackground());
assert(l.Poll() == Lifecycle::Change::EnterBackground);
assert(l.Poll() == Lifecycle::Change::None);
l.OnEvent(SDL_EVENT_WILL_ENTER_FOREGROUND);
l.OnEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
assert(!l.IsBackground());
assert(l.Poll() == Lifecycle::Change::EnterForeground);
assert(l.Poll() == Lifecycle::Change::None);
// A pause and resume between two frames still reports both, background first.
l.OnEvent(SDL_EVENT_DID_ENTER_BACKGROUND);
l.OnEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
assert(l.Poll() == Lifecycle::Change::EnterBackground);
assert(l.Poll() == Lifecycle::Change::EnterForeground);
assert(!l.IsBackground());
// Foreground without background is ignored.
l.OnEvent(SDL_EVENT_DID_ENTER_FOREGROUND);
assert(l.Poll() == Lifecycle::Change::None);
assert(!l.TakeLowMemory());
l.OnEvent(SDL_EVENT_LOW_MEMORY);
assert(l.TakeLowMemory() && !l.TakeLowMemory());
assert(!l.IsTerminating());
l.OnEvent(SDL_EVENT_TERMINATING);
assert(l.IsTerminating());
}
void TestSafeInsets() {
// Full safe area: no insets.
SafeInsets none = MapSafeInsets(1280, 720, 0, 0, 1280, 720, 1280, 720);
assert(none == SafeInsets{});
// 20:9 phone (2400x1080 window points) showing a 16:9 UI 1280x720 letterboxed: scale 1.5,
// bars of 240 px left/right. A 100 px notch on the left lies inside the bar -> no inset.
SafeInsets bar = MapSafeInsets(2400, 1080, 100, 0, 2300, 1080, 1280, 720);
assert(bar == SafeInsets{});
// Same screen with a UI as wide as the window (2400x1080 logical = 1:1): the notch shows.
SafeInsets wide = MapSafeInsets(2400, 1080, 100, 0, 2240, 1040, 2400, 1080);
assert(Near(wide.left, 100) && Near(wide.top, 0) && Near(wide.right, 60) && Near(wide.bottom, 40));
// Scaled: 1600x720 UI in a 2400x1080 window (scale 1.5); a 150 px cutout is 100 UI px.
SafeInsets scaled = MapSafeInsets(2400, 1080, 150, 0, 2250, 1035, 1600, 720);
assert(Near(scaled.left, 100) && Near(scaled.right, 0) && Near(scaled.bottom, 30));
// Degenerate input.
assert(MapSafeInsets(0, 0, 0, 0, 0, 0, 1280, 720) == SafeInsets{});
}
void TestTouchLayout() {
using ava2::input::TouchLayout;
const TouchLayout plain = TouchLayout::ForScreen(1600, 720);
const TouchLayout safe = TouchLayout::ForScreen(1600, 720, 100, 0, 60, 30);
assert(Near(safe.safe_left, 100) && Near(safe.safe_right, 60) && Near(safe.safe_bottom, 30));
assert(Near(safe.joy_x, plain.joy_x + 100));
assert(Near(safe.attack_x, plain.attack_x - 60) && Near(safe.attack_y, plain.attack_y - 30));
assert(Near(safe.attack_r, plain.attack_r));
// The attack circle stays inside the safe area.
assert(safe.attack_x + safe.attack_r <= 1600 - 60 && safe.attack_y + safe.attack_r <= 720 - 30);
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;
}