Add getPreferredTextSize to calculate unconstrained text width

Introduces (CButton|CText).getPreferredTextSize, a new method for estimating
the natural width of a caption if the object had no width constraints.

Unlike .getTextSize, which reports the size after applying current
layout constraints (e.g., fixed width or anchors), getPreferredTextSize
answers the question: "How wide would this object need to be to display
the caption on a single line?"

This is particularly useful for modders and layout logic that wants to
dynamically size elements *before* assigning a fixed width or anchoring.
This commit is contained in:
trompetin17
2025-06-06 17:38:09 -05:00
parent 7eaea3d181
commit 19e82ebceb
5 changed files with 21 additions and 5 deletions
@@ -1,4 +1,4 @@
/* Copyright (C) 2024 Wildfire Games.
/* Copyright (C) 2025 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -44,6 +44,7 @@ DECLARE_GUIPROXY(IGUIObject);
template<> void JSI_GUIProxy<CButton>::CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache)
{
CreateFunction<&CButton::GetTextSize>(rq, cache, "getTextSize");
CreateFunction<&CButton::GetPreferredTextSize>(rq, cache, "getPreferredTextSize");
}
DECLARE_GUIPROXY(CButton);
@@ -51,6 +52,7 @@ DECLARE_GUIPROXY(CButton);
template<> void JSI_GUIProxy<CText>::CreateFunctions(const ScriptRequest& rq, GUIProxyProps* cache)
{
CreateFunction<&CText::GetTextSize>(rq, cache, "getTextSize");
CreateFunction<&CText::GetPreferredTextSize>(rq, cache, "getPreferredTextSize");
}
DECLARE_GUIPROXY(CText);