0ad/binaries/data/mods/public/gui/reference/structree/Boxes/EntityBox.js
trompetin17 b42a2b9adb
Apply correct use of TextSize APIs in GUI
Updates GUI code to apply the correct usage of `guiObject.getTextSize()`
and `guiObject.GetPreferedTextSize()`.

- `guiObject.getPreferedTextSize()` is now used when the goal is to compute the
  bounding box of a text string to dynamically resize or layout a GUI
  object accordingly. It simulates how the text would be rendered,
  accounting for markup and layout rules.

- `guiObject.getTextSize()` remains in use when text is being measured
  within a fixed-size GUI element (e.g., a button or label). It respects
  internal constraints like buffer zone and maxWidth, and is typically
  used to align other elements based on its visual appearance.

These changes ensure more accurate text sizing and consistent layout
behavior across GUI components.
2025-06-26 17:11:24 -05:00

83 lines
2.3 KiB
JavaScript

/**
* Class inherited by StructureBox and TrainerBox classes.
*/
class EntityBox
{
constructor(page)
{
this.page = page;
}
static setViewerOnPress(guiObject, templateName, civCode)
{
const viewerFunc = Engine.OpenChildPage.bind(null, "page_viewer.xml", {
"templateName": templateName,
"civ": civCode
});
guiObject.onPress = viewerFunc;
guiObject.onPressRight = viewerFunc;
}
draw(templateName, civCode)
{
this.template = this.page.TemplateParser.getEntity(templateName, civCode);
this.gui.hidden = false;
const caption = this.gui.children[0];
caption.caption = g_SpecificNamesPrimary ?
translate(this.template.name.specific) :
translate(this.template.name.generic);
const icon = this.gui.children[1];
icon.sprite = "stretched:" + this.page.IconPath + this.template.icon;
icon.tooltip = this.constructor.compileTooltip(this.template);
this.constructor.setViewerOnPress(icon, this.template.name.internal, civCode);
}
captionWidth()
{
// We make the assumption that the caption's padding is equal on both sides
const caption = this.gui.children[0];
return caption.getPreferredTextSize().width + caption.size.left;
}
static compileTooltip(template)
{
return ReferencePage.buildText(template, this.prototype.TooltipFunctions) + "\n" + getTemplateViewerOnClickTooltip();
}
/**
* Returns the height between the top of the EntityBox, and the top of the production rows.
*
* Used within the TreeSection class to position the production rows,
* and used with the PhaseIdent class to position grey bars under them.
*/
static IconAndCaptionHeight()
{
const height = Engine.GetGUIObjectByName("structure[0]_icon").size.bottom + this.prototype.IconPadding;
// Replace function so the above is only run once.
this.IconAndCaptionHeight = () => height;
return height;
}
}
/**
* Minimum width of the boxes, the margins between them (Horizontally and Vertically),
* and the padding between the main icon and the production row(s) beneath it.
*/
EntityBox.prototype.MinWidth = 96;
EntityBox.prototype.HMargin = 8;
EntityBox.prototype.VMargin = 12;
EntityBox.prototype.IconPadding = 8;
/**
* Functions used to collate the contents of a tooltip.
*/
EntityBox.prototype.TooltipFunctions = [
getEntityNamesFormatted,
getEntityCostTooltip,
getEntityTooltip,
getAurasTooltip
].concat(ReferencePage.prototype.StatsFunctions);