refactor(coord): push script coordinates through one engine chain

Every script coordinate the engine pushes (GetLeft, GetStringWidth,
GetFont's height, GetCursorPosition, OnCursorChanged) is
DivideByScreenScaleX(GetScaledScreenAspect() * [0x007FFD68] * value):
0x0041AD70, then the 1024.0f read from .rdata, then 0x0041AE40, with
the quotient stored from ST0 as a double.

Game::InternalToPixel did the same arithmetic from the globals in double
precision, while the edit-box caret, the font height getters and texture
tiling each kept a private copy of the engine chain. InternalToPixel now
makes the engine's calls in the engine's order, rounding the float
product once as the engine's fstp dword does, and the three modules use
it. PixelToInternal reads the 1024 from the same .rdata float, so the
UI_COORD_SCALE_UNIT constant is gone.
This commit is contained in:
Brues
2026-11-09 11:51:25 -06:00
parent be21ff50f6
commit 4bb31eb98d
6 changed files with 22 additions and 35 deletions
+13 -5
View File
@@ -30,15 +30,23 @@ float PixelToInternal(float px) {
const float div = Read<float>(Offsets::VAR_COORD_ASPECT_COMPENSATION);
if (div == 0.0f)
return 0.0f;
return px * mul / (div * static_cast<float>(Offsets::UI_COORD_SCALE_UNIT));
return px * mul / (div * Read<float>(Offsets::FLOAT_SCRIPT_COORD_SCALE));
}
double InternalToPixel(float internal) {
const float mul = Read<float>(Offsets::VAR_COORD_DDC_WIDTH_SCALE);
const float div = Read<float>(Offsets::VAR_COORD_ASPECT_COMPENSATION);
if (mul == 0.0f)
using GetScaledScreenAspect_t = float(__cdecl *)();
// Declared double so the quotient is read from ST0, as the engine's
// pushes store it with `fstp qword`, not narrowed to float.
using DivideByScreenScaleX_t = double(__stdcall *)(float);
if (Read<float>(Offsets::VAR_COORD_DDC_WIDTH_SCALE) == 0.0f)
return 0.0;
return static_cast<double>(internal) * div * Offsets::UI_COORD_SCALE_UNIT / mul;
// `aspect * 1024` is exact, so the float product rounds once, as the
// engine's `fstp dword` does.
const float aspect =
reinterpret_cast<GetScaledScreenAspect_t>(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT)();
const float scaled = aspect * Read<float>(Offsets::FLOAT_SCRIPT_COORD_SCALE) * internal;
return reinterpret_cast<DivideByScreenScaleX_t>(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X)(
scaled);
}
bool StringToFlag(const char *text) {
+4 -2
View File
@@ -89,8 +89,10 @@ void *ResolveUnitToken(const char *token);
// UI pixels <-> internal layout (anchor) units. Script_SetPoint converts its
// pixel offsets with `px * [VAR_COORD_DDC_WIDTH_SCALE] / ([VAR_COORD_ASPECT_COMPENSATION]
// * UI_COORD_SCALE_UNIT)`; Script_GetLeft and Script_GetStringWidth push the
// inverse. A region's LOCAL pixels must be multiplied by its effective scale
// * [FLOAT_SCRIPT_COORD_SCALE])`; every script coordinate push (GetLeft,
// GetStringWidth, GetFont's height, GetCursorPosition, OnCursorChanged) is the
// inverse, `DivideByScreenScaleX(GetScaledScreenAspect() * 1024 * value)`,
// which InternalToPixel calls in that order. A region's LOCAL pixels must be multiplied by its effective scale
// (OFF_REGION_EFFECTIVE_SCALE) first. Both return 0 while the scale globals
// are unset, so an early caller gets a zero offset rather than a wild one.
float PixelToInternal(float px);
-1
View File
@@ -750,7 +750,6 @@ enum Offsets {
VAR_COORD_DDC_WIDTH_SCALE = 0x00832A44,
VAR_COORD_DDC_HEIGHT_SCALE = 0x00832A48,
VAR_COORD_ASPECT_COMPENSATION = 0x00832A4C,
UI_COORD_SCALE_UNIT = 1024, // DAT_007ffd68
// `SetUiParentScale` — `void __stdcall(float scale)`, `ret 4`. The one
// setter of UIParent's scale: raises the value to 0.64, calls SetScale
// with force (0x0076AC10), drains the layout queue (0x00768ED0), and does
+2 -7
View File
@@ -65,8 +65,6 @@ using SetPoint_t = void(__thiscall *)(void *anchor, int point, void *relAnchor,
int flag);
using Resize_t = void(__fastcall *)(void *anchor, void *edx, int force);
using Extent_t = float(__fastcall *)(void *anchor, void *edx);
using ScreenAspect_t = float(__cdecl *)();
using DivideByScreenScaleX_t = float(__stdcall *)(float value);
using FireVariadic_t = void(__cdecl *)(void *frame, uint32_t *slotPtr, const char *fmt, ...);
PlaceCaret_t g_origPlaceCaret = nullptr;
@@ -280,11 +278,8 @@ void __fastcall PlaceCaret_h(void *editBox) {
void **vtable = *static_cast<void ***>(anchor);
const float width = reinterpret_cast<Extent_t>(vtable[7])(anchor, nullptr); // +0x1C GetWidth
const float height = reinterpret_cast<Extent_t>(vtable[8])(anchor, nullptr); // +0x20 GetHeight
const auto aspect = reinterpret_cast<ScreenAspect_t>(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT);
const auto divide = reinterpret_cast<DivideByScreenScaleX_t>(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X);
const float factor = Game::Read<float>(Offsets::FLOAT_SCRIPT_COORD_SCALE);
const auto push = [&](float value) { return static_cast<double>(divide(aspect() * factor * value)); };
const double args[4] = {push(x), push(y), push(width), push(height)};
const double args[4] = {Game::InternalToPixel(x), Game::InternalToPixel(y), Game::InternalToPixel(width),
Game::InternalToPixel(height)};
void *L = Game::Lua::State();
const int savedTop = (L != nullptr) ? Game::Lua::GetTop(L) : 0;
reinterpret_cast<FireVariadic_t>(Offsets::FUN_FRAME_RUN_SCRIPT_VARIADIC)(
+2 -13
View File
@@ -39,19 +39,8 @@ namespace {
// FUN_FONTSTRING_FONT_HEIGHT — __thiscall(fs, scaled-on-stack); dummy-EDX form.
using FsFontHeight_t = float(__fastcall *)(void *fs, void *edx, int scaled);
using GetScaledScreenAspect_t = float(__cdecl *)();
// Declared double so the quotient is read from ST0 with `fstp qword`, as
// GetFont stores it, not narrowed to float.
using DivideByScreenScaleX_t = double(__stdcall *)(float);
// GetFont's push (0x0079D4A5..0x0079D4C4). `aspect * 1024` is exact, so the
// float product rounds once, as GetFont's `fstp dword` does.
double ToUiHeight(float internal) {
const float aspect =
reinterpret_cast<GetScaledScreenAspect_t>(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT)();
const float scaled = aspect * static_cast<float>(Offsets::UI_COORD_SCALE_UNIT) * internal;
return reinterpret_cast<DivideByScreenScaleX_t>(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X)(scaled);
}
// GetFont's push (0x0079D4A5..0x0079D4C4).
double ToUiHeight(float internal) { return Game::InternalToPixel(internal); }
void *FontStringSelf(void *L) { return Game::Lua::ResolveFontString(L); }
void *FontSelf(void *L) { return Game::Lua::ResolveTypedObject(L, 1, Offsets::VAR_FONT_LUA_TYPE_ID); }
+1 -7
View File
@@ -65,8 +65,6 @@ namespace Texture::Tile {
namespace {
using GetRect_t = int(__fastcall *)(void *anchor, void *edx, float *outRect);
using ScreenAspect_t = float(__cdecl *)();
using DivideByScreenScaleX_t = float(__stdcall *)(float value);
using SetTexCoord_t = void(__fastcall *)(void *texture, void *edx, const float *values);
using Emit_t = void(__fastcall *)(void *texture, void *edx, void *batch);
@@ -74,11 +72,7 @@ using Emit_t = void(__fastcall *)(void *texture, void *edx, void *batch);
constexpr float kWholeImage[8] = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f};
// A length in rect units as script units.
float ScriptUnits(float length) {
const auto aspect = reinterpret_cast<ScreenAspect_t>(Offsets::FUN_COORD_GET_SCALED_SCREEN_ASPECT);
const auto divide = reinterpret_cast<DivideByScreenScaleX_t>(Offsets::FUN_COORD_DIVIDE_BY_SCREEN_SCALE_X);
return divide(aspect() * Game::Read<float>(Offsets::FLOAT_SCRIPT_COORD_SCALE) * length);
}
float ScriptUnits(float length) { return static_cast<float>(Game::InternalToPixel(length)); }
// 4.3.4's rule on one axis (0 x, 1 y) of the four corner pairs.
void Count(float *coords, int axis, float length, uint32_t texels) {