using UnityEngine;
namespace SplashEdit.RuntimeCode
{
///
/// A text UI element for PSX export.
/// Rendered via psyqo::Font::chainprintf on PS1 hardware.
/// Attach to a child of a PSXCanvas GameObject.
///
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
[AddComponentMenu("PSX/UI/PSX UI Text")]
[Icon("Packages/net.psxsplash.splashedit/Icons/PSXUIText.png")]
public class PSXUIText : MonoBehaviour
{
[Tooltip("Name used to reference this element from Lua (max 24 chars).")]
[SerializeField] private string elementName = "text";
[Tooltip("Default text content (max 63 chars). Can be changed at runtime via Lua UI.SetText().")]
[SerializeField] private string defaultText = "";
[Tooltip("Text color.")]
[SerializeField] private Color textColor = Color.white;
[Tooltip("Whether this element is visible when the scene first loads.")]
[SerializeField] private bool startVisible = true;
[Tooltip("Custom font override. If null, uses the canvas default font (or built-in system font).")]
[SerializeField] private PSXFontAsset fontOverride;
/// Element name for Lua access.
public string ElementName => elementName;
/// Default text content (truncated to 63 chars on export).
public string DefaultText => defaultText;
/// Text color (RGB, alpha ignored).
public Color TextColor => textColor;
/// Initial visibility flag.
public bool StartVisible => startVisible;
///
/// Custom font override. If null, inherits from parent PSXCanvas.DefaultFont.
/// If that is also null, uses the built-in system font.
///
public PSXFontAsset FontOverride => fontOverride;
///
/// Resolve the effective font for this text element.
/// Checks: fontOverride → parent PSXCanvas.DefaultFont → null (system font).
///
public PSXFontAsset GetEffectiveFont()
{
if (fontOverride != null) return fontOverride;
PSXCanvas canvas = GetComponentInParent();
if (canvas != null && canvas.DefaultFont != null) return canvas.DefaultFont;
return null; // system font
}
}
}