using System;
using UnityEngine;
using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.XTable.Core
{
/// 单元格内容类型——决定渲染时使用哪种内容
public enum CellContentType
{
/// 文本内容
Text,
/// 图片纹理
Image,
/// 预制体实例
Prefab,
}
///
/// 单元格数据模型——仅保存数据内容,不保存自身尺寸。
/// 尺寸由渲染阶段的行列标题(行高/列宽)决定。
/// 样式通过 StyleNamespace 字符串引用 StyleManager 中的命名空间。
///
[Serializable]
public class XTableCellData
{
/// 内容类型——决定渲染时使用 Text / Image / Prefab 中的哪一个
public CellContentType ContentType;
/// 文本内容(ContentType == Text 时生效)
public string Text;
/// 图片纹理(ContentType == Image 时生效)
public Texture2D Image;
/// 预制体对象(ContentType == Prefab 时生效,用于嵌入复杂 UI 元素)
public GameObject Prefab;
///
/// 样式命名空间——对应 StyleManager 中的命名空间。
/// 为空或 "xeric_table_default" 时使用默认表格样式。
/// 渲染时从此命名空间读取 /cell/bg/color、/cell/font/size 等路径。
///
public string StyleNamespace;
///
/// 单元格级别样式覆盖——通过命名空间 + 路径引用样式表中的值。
/// 渲染时优先使用此样式路径派生颜色/尺寸(如 {StylePath}/bg/color),未设置时回退到 StyleNamespace。
///
public StyleMember CellStyle;
/// 预制体实例从对象池取出时回调——外部可在此初始化实例内容
[NonSerialized] public System.Action OnPrefabAcquire;
/// 预制体实例归还对象池前回调——外部可在此清理引用
[NonSerialized] public System.Action OnPrefabRelease;
public XTableCellData()
{ }
public XTableCellData(string text) => Text = text;
public XTableCellData(string text, string styleNamespace = null) : this(text) =>
StyleNamespace = styleNamespace;
public XTableCellData(string text, Texture2D image, string styleNamespace = null) :
this(text, styleNamespace) => Image = image;
/// 获取该单元格渲染时使用的样式命名空间(CellStyle 优先,否则 fallback 到 StyleNamespace)
public string GetEffectiveStyleNamespace(string fallbackNamespace)
{
if (CellStyle != null && !string.IsNullOrEmpty(CellStyle.CurrentNamespace))
return CellStyle.CurrentNamespace;
if (!string.IsNullOrEmpty(StyleNamespace))
return StyleNamespace;
return fallbackNamespace;
}
public void ClearEvent()
{
OnPrefabAcquire = null;
OnPrefabRelease = null;
}
}
}