Files
trail-into-darkness/Packages/com.jovian.popup-system/Runtime/PopupContentBuilder.cs
2026-04-06 17:30:09 +02:00

88 lines
2.9 KiB
C#

using Jovian.PopupSystem.UI;
using UnityEngine;
namespace Jovian.PopupSystem {
/// <summary>
/// Fluent API struct for building popup content. Operates directly on cached elements
/// inside a <see cref="PopupReference"/> — no intermediate allocations. Received in
/// the build callback passed to <see cref="IPopupSystem.Show"/>.
/// </summary>
public struct PopupContentBuilder {
readonly PopupReference view;
/// <summary>
/// Creates a builder targeting the given popup reference.
/// </summary>
public PopupContentBuilder(PopupReference view) {
this.view = view;
}
/// <summary>
/// Adds a header text element (bold, larger font).
/// </summary>
public PopupContentBuilder AddHeader(string text) {
var header = view.GetHeader();
header.text = text;
return this;
}
/// <summary>
/// Adds a body text element.
/// </summary>
public PopupContentBuilder AddText(string text) {
var label = view.GetText();
label.text = text;
return this;
}
/// <summary>
/// Adds a colored body text element. Hex color can be with or without the # prefix.
/// </summary>
public PopupContentBuilder AddText(string text, string hexColor) {
var label = view.GetText();
var prefix = hexColor.Length > 0 && hexColor[0] == '#' ? "" : "#";
label.text = $"<color={prefix}{hexColor}>{text}</color>";
return this;
}
/// <summary>
/// Adds a stat row with a label and integer value.
/// </summary>
public PopupContentBuilder AddStat(string label, int value) {
var (labelText, valueText) = view.GetStat();
labelText.text = label;
valueText.text = value.ToString();
return this;
}
/// <summary>
/// Adds a stat row with a label and string value.
/// </summary>
public PopupContentBuilder AddStat(string label, string value) {
var (labelText, valueText) = view.GetStat();
labelText.text = label;
valueText.text = value;
return this;
}
/// <summary>
/// Adds an image element with the given sprite and optional height.
/// </summary>
public PopupContentBuilder AddImage(Sprite sprite, float height = 64f) {
var image = view.GetImage();
image.sprite = sprite;
var rt = (RectTransform)image.transform;
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
return this;
}
/// <summary>
/// Adds a horizontal separator line.
/// </summary>
public PopupContentBuilder AddSeparator() {
view.GetSeparator();
return this;
}
}
}