修复表格中对样式的依赖与更新
This commit is contained in:
@@ -42,6 +42,12 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
/// <summary>池根节点(所有回收对象均挂在此节点下,节点本身处于 inactive)</summary>
|
||||
public Transform PoolRoot => m_PoolRoot;
|
||||
|
||||
/// <summary>运行时的默认样式命名空间(编辑器下为 null,不绑定)</summary>
|
||||
private string m_DefaultStyleNamespace;
|
||||
|
||||
/// <summary>所有已绑定的 StyleBoundElement 回调集合(用于样式变更时通知外部)</summary>
|
||||
public event System.Action OnStyleChangeDetected;
|
||||
|
||||
private Stack<PooledImage> m_ImagePool = new Stack<PooledImage>();
|
||||
private Stack<PooledText> m_TextPool = new Stack<PooledText>();
|
||||
private List<PooledImage> m_ActiveImages = new List<PooledImage>();
|
||||
@@ -54,9 +60,10 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
|
||||
#region 构造函数与析构
|
||||
|
||||
public CellAssembler(Transform parent)
|
||||
public CellAssembler(Transform parent, string defaultStyleNamespace = null)
|
||||
{
|
||||
m_Parent = parent;
|
||||
m_DefaultStyleNamespace = defaultStyleNamespace;
|
||||
|
||||
GameObject poolRoot = new GameObject("_CellPool");
|
||||
poolRoot.hideFlags = POOL_FLAGS;
|
||||
@@ -120,7 +127,7 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
|
||||
#region 池操作
|
||||
|
||||
public PooledImage GetImage(string name = "cell_img")
|
||||
public PooledImage GetImage(string name = "cell_img", string styleNamespace = null)
|
||||
{
|
||||
PooledImage item;
|
||||
if (m_ImagePool.Count > 0)
|
||||
@@ -134,11 +141,12 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
{
|
||||
item = CreateImage(name);
|
||||
}
|
||||
BindStyle(item.Style, styleNamespace);
|
||||
m_ActiveImages.Add(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public PooledText GetText(string name = "cell_text")
|
||||
public PooledText GetText(string name = "cell_text", string styleNamespace = null)
|
||||
{
|
||||
PooledText item;
|
||||
if (m_TextPool.Count > 0)
|
||||
@@ -152,10 +160,32 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
{
|
||||
item = CreateText(name);
|
||||
}
|
||||
BindStyle(item.Style, styleNamespace);
|
||||
m_ActiveTexts.Add(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
/// <summary>运行时绑定样式到池元素的 StyleBoundElement,实现被动更新(push)</summary>
|
||||
private void BindStyle(StyleBoundElement style, string cellNamespace)
|
||||
{
|
||||
if (!Application.isPlaying) return;
|
||||
|
||||
string ns = cellNamespace ?? m_DefaultStyleNamespace;
|
||||
if (string.IsNullOrEmpty(ns)) return;
|
||||
|
||||
if (style != null && style.BoundNamespace != ns)
|
||||
{
|
||||
style.OnStyleChangedCallback -= OnStyleChangedNotify;
|
||||
style.OnStyleChangedCallback += OnStyleChangedNotify;
|
||||
style.Bind(ns);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnStyleChangedNotify()
|
||||
{
|
||||
OnStyleChangeDetected?.Invoke();
|
||||
}
|
||||
|
||||
public void ReturnAll()
|
||||
{
|
||||
foreach (var img in m_ActiveImages) ReturnImage(img);
|
||||
|
||||
@@ -32,12 +32,19 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
/// <summary>是否已绑定</summary>
|
||||
private bool m_IsBound;
|
||||
|
||||
/// <summary>样式变更时触发(通知表格重绘)</summary>
|
||||
public event System.Action OnStyleChangedCallback;
|
||||
|
||||
/// <summary>当前绑定的命名空间(空字符串表示未绑定)</summary>
|
||||
public string BoundNamespace => m_IsBound ? (m_StyleNamespace ?? "") : "";
|
||||
|
||||
#endregion
|
||||
|
||||
#region 公开方法
|
||||
|
||||
/// <summary>
|
||||
/// 绑定到指定命名空间并开始监听样式。
|
||||
/// 运行时注册 StyleField 实现被动更新(push);编辑器下在渲染时直接 pull。
|
||||
/// </summary>
|
||||
public void Bind(string styleNamespace)
|
||||
{
|
||||
@@ -46,6 +53,11 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
|
||||
if (string.IsNullOrEmpty(styleNamespace)) return;
|
||||
|
||||
// 检查命名空间是否存在,避免 Register → GetValue 报错
|
||||
var namespaces = StyleManager.GetAvailableNamespaces();
|
||||
if (namespaces == null || !namespaces.Contains(styleNamespace))
|
||||
return;
|
||||
|
||||
// 预注册常用路径
|
||||
RegisterField("/cell/bg/color", OnStyleChanged);
|
||||
RegisterField("/cell/fg/color", OnStyleChanged);
|
||||
@@ -72,7 +84,7 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
{
|
||||
if (kvp.Value != null)
|
||||
{
|
||||
kvp.Value.OnValueChanged -= OnStyleChanged;
|
||||
kvp.Value.Member.OnValueChanged -= OnStyleChanged;
|
||||
kvp.Value.Unregister();
|
||||
}
|
||||
}
|
||||
@@ -125,7 +137,7 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
StylePath = path
|
||||
};
|
||||
field.Register();
|
||||
field.OnValueChanged += callback;
|
||||
field.Member.OnValueChanged += callback;
|
||||
m_Fields[path] = field;
|
||||
}
|
||||
|
||||
@@ -137,6 +149,7 @@ namespace XericUI.XTable.Rendering.Elements
|
||||
private void OnStyleChanged(StyleValue val)
|
||||
{
|
||||
RefreshAll();
|
||||
OnStyleChangedCallback?.Invoke();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user