using TMPro; using UnityEngine; using UnityEngine.UI; namespace Jovian.PopupSystem { /// /// Fluent API struct for building popup content. Operates on the generic element cache /// inside a . Received in the build callback passed to /// . Use for fully custom elements, /// or convenience methods for common types. /// public readonly struct PopupContentBuilder { private readonly PopupView view; /// /// Creates a builder targeting the given popup view. /// public PopupContentBuilder(PopupView view) { this.view = view; } /// /// Returns a cached or newly instantiated element by its registered type. /// Use this for fully custom or game-specific element types. /// public GameObject Add(PopupElementType elementType) { return view.GetElement(elementType); } /// /// Adds a text element using the given element type and sets its TMP_Text content. /// public PopupContentBuilder AddText(string text, PopupElementType elementType) { var go = view.GetElement(elementType); if(!go) { return this; } var tmp = go.GetComponentInChildren(); if(tmp) { tmp.text = text; } return this; } /// /// Adds a colored text element using the given element type. /// Hex color can be with or without the # prefix. /// public PopupContentBuilder AddText(string text, string hexColor, PopupElementType elementType) { var go = view.GetElement(elementType); if(!go) { return this; } var tmp = go.GetComponentInChildren(); if(!tmp) { return this; } var prefix = hexColor.Length > 0 && hexColor[0] == '#' ? "" : "#"; tmp.text = $"{text}"; return this; } /// /// Adds a name/value row using the given element type. The prefab must have /// exactly two TMP_Text children (label first, value second). /// public PopupContentBuilder AddNameValue(string label, int value, PopupElementType elementType) { return AddNameValueInternal(elementType, label, value.ToString()); } /// /// Adds a name/value row using the given element type. The prefab must have /// exactly two TMP_Text children (label first, value second). /// public PopupContentBuilder AddNameValue(string label, string value, PopupElementType elementType) { return AddNameValueInternal(elementType, label, value); } private PopupContentBuilder AddNameValueInternal(PopupElementType elementType, string label, string value) { var go = view.GetElement(elementType); if(go != null) { var children = go.GetComponentsInChildren(true); if(children.Length >= 2) { children[0].text = label; children[1].text = value; } } return this; } /// /// Adds an image element with the given sprite and optional height. /// public PopupContentBuilder AddImage(Sprite sprite, PopupElementType elementType, float height = 64f) { var go = view.GetElement(elementType); if(go != null) { var image = go.GetComponentInChildren(); if(image != null) { image.sprite = sprite; var rt = (RectTransform)image.transform; rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); } } return this; } /// /// Adds a separator element using the given element type. /// public PopupContentBuilder AddSeparator(PopupElementType elementType) { view.GetElement(elementType); return this; } } }