Merge remote-tracking branch 'gitea/master' into thr_dev/dev2

This commit is contained in:
2026-06-30 09:10:41 +08:00
5 changed files with 274 additions and 169 deletions
+2 -3
View File
@@ -80,14 +80,13 @@ namespace XericUIEditor.XTable
if (GUILayout.Button("重新生成表格", GUILayout.Height(26))) if (GUILayout.Button("重新生成表格", GUILayout.Height(26)))
{ {
m_Table.FullRebuild(); m_Table.FullRebuildPreservingData();
m_Table.SetChildrenVisible(m_ShowChildren); m_Table.SetChildrenVisible(m_ShowChildren);
} }
if (GUILayout.Button("强制刷新样式", GUILayout.Height(26))) if (GUILayout.Button("强制刷新样式", GUILayout.Height(26)))
{ {
XericLibrary.Runtime.SuperStyleSheet.StyleManager.ForceRefresh(); m_Table.ForceStyleRefresh();
m_Table.FullRebuild();
m_Table.SetChildrenVisible(m_ShowChildren); m_Table.SetChildrenVisible(m_ShowChildren);
} }
@@ -217,6 +217,25 @@ namespace XericUIEditor.XTable
// ★ 状态机:必须最先执行,后续所有逻辑依赖 s_State // ★ 状态机:必须最先执行,后续所有逻辑依赖 s_State
UpdateState(); UpdateState();
// Unity 2021 兼容:阻止鼠标事件穿透 overlay 到背后 UI 对象
// 原理:在 overlay 区域注册默认控件,Scene View 拣选系统检测到控件后
// 不再选中背后 GameObject(Canvas、ScrollRect 等)
if (s_State == EditorState.TableNavigation)
{
// 覆盖区域:行标签 + 列表头 + 表格主体 + 底部工具栏
Rect overlayArea = new Rect(
tableRect.x - COL_W - MARGIN, // 包含行标签
tableRect.y - ROW_H - MARGIN, // 包含列表头
tableRect.width + COL_W + MARGIN + 4, // 包含行标签
tableRect.height + ROW_H + MARGIN + 56); // 包含列表头+工具栏
if (overlayArea.Contains(Event.current.mousePosition))
{
int blockId = GUIUtility.GetControlID(FocusType.Passive);
HandleUtility.AddDefaultControl(blockId);
}
}
DrawRowLabels(rh, tableRect, xScale, yScale, table); DrawRowLabels(rh, tableRect, xScale, yScale, table);
DrawColumnLabels(cw, tableRect, xScale, yScale, table); DrawColumnLabels(cw, tableRect, xScale, yScale, table);
DrawGridLines(rh, cw, tableRect, xScale, yScale); DrawGridLines(rh, cw, tableRect, xScale, yScale);
@@ -9,6 +9,9 @@ using XericUI.XTable.Core;
using XericUI.XTable.Mapping; using XericUI.XTable.Mapping;
using XericUI.XTable.Rendering.Elements; using XericUI.XTable.Rendering.Elements;
using XericLibrary.Runtime.SuperStyleSheet; using XericLibrary.Runtime.SuperStyleSheet;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace XericUI.XTable.Rendering.Component namespace XericUI.XTable.Rendering.Component
{ {
@@ -723,6 +726,48 @@ namespace XericUI.XTable.Rendering.Component
SetChildrenVisible(m_ChildrenVisible); SetChildrenVisible(m_ChildrenVisible);
} }
/// <summary>重新生成表格但保留已有文本数据——先保存,重建后再回填</summary>
public void FullRebuildPreservingData()
{
// 保存当前单元格文本
var savedTexts = new Dictionary<(int, int), string>();
if (m_TableData != null)
{
int rows = m_RowHeights?.Length ?? 0;
int cols = m_ColWidths?.Length ?? 0;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
var cell = m_TableData.GetCell(r, c);
if (cell != null && !string.IsNullOrEmpty(cell.Text))
savedTexts[(r, c)] = cell.Text;
}
}
}
FullRebuild();
// 恢复文本数据
foreach (var kv in savedTexts)
{
int r = kv.Key.Item1, c = kv.Key.Item2;
if (r < (m_RowHeights?.Length ?? 0) && c < (m_ColWidths?.Length ?? 0))
m_TableData.GetOrCreateCell(r, c).Text = kv.Value;
}
RefreshView();
SetChildrenVisible(m_ChildrenVisible);
}
/// <summary>仅强制刷新样式,不重建数据结构</summary>
public void ForceStyleRefresh()
{
StyleManager.ForceRefresh();
MarkDirty(TableDirtyType.StyleChanged);
RefreshView();
}
#endregion #endregion
#region 单元格渲染 #region 单元格渲染
@@ -808,6 +853,7 @@ namespace XericUI.XTable.Rendering.Component
txt.Text.color = GetStyleColor(cellNS, "/cell/fg/color", fgFallback); txt.Text.color = GetStyleColor(cellNS, "/cell/fg/color", fgFallback);
txt.Text.fontSize = GetStyleInt(cellNS, "/cell/font/size", 14); txt.Text.fontSize = GetStyleInt(cellNS, "/cell/font/size", 14);
txt.Text.alignment = TextAlignmentOptions.Midline; txt.Text.alignment = TextAlignmentOptions.Midline;
txt.Text.font = GetStyleFontAsset(cellNS, "/cell/font/fontAsset") ?? txt.Text.font;
} }
} }
} }
@@ -886,6 +932,7 @@ namespace XericUI.XTable.Rendering.Component
txt.Text.color = GetStyleColor(cellNS, "/cell/fg/color", fgFallback); txt.Text.color = GetStyleColor(cellNS, "/cell/fg/color", fgFallback);
txt.Text.fontSize = GetStyleInt(cellNS, "/cell/font/size", 14); txt.Text.fontSize = GetStyleInt(cellNS, "/cell/font/size", 14);
txt.Text.alignment = TextAlignmentOptions.Midline; txt.Text.alignment = TextAlignmentOptions.Midline;
txt.Text.font = GetStyleFontAsset(cellNS, "/cell/font/fontAsset") ?? txt.Text.font;
} }
#endif #endif
} }
@@ -1007,6 +1054,14 @@ namespace XericUI.XTable.Rendering.Component
return val != null ? (float)val : fallback; return val != null ? (float)val : fallback;
} }
private TMP_FontAsset GetStyleFontAsset(string ns, string path, TMP_FontAsset fallback = null)
{
StyleValue val = StyleManager.GetValue(ns, path);
if (val == null) return fallback;
try { return val.GetValueAs<TMP_FontAsset>(); }
catch { return fallback; }
}
private void EnsureDefaultStyleNamespace() private void EnsureDefaultStyleNamespace()
{ {
// 避免在样式表尚未加载时触发 HasStyle → GetValue 的命名空间不存在错误 // 避免在样式表尚未加载时触发 HasStyle → GetValue 的命名空间不存在错误
@@ -1037,6 +1092,13 @@ namespace XericUI.XTable.Rendering.Component
StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/size", 14); StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/size", 14);
StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/alignment", "MiddleCenter"); StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/alignment", "MiddleCenter");
StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/richText", true); StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/richText", true);
#if UNITY_EDITOR
// 编辑器下尝试加载默认字体资源
var defaultFont = UnityEditor.AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(
"Packages/com.lrss3.xericuiactionvessel/Runtime/Font/SourceHanSansSC-Regular-2 SDF.asset");
if (defaultFont != null)
StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/font/fontAsset", defaultFont);
#endif
StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/default/height", 40f); StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/default/height", 40f);
StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/default/width", 100f); StyleManager.AddDynamicStyle(m_DefaultStyleNamespace, "/cell/default/width", 100f);
StyleManager.ForceRefresh(); StyleManager.ForceRefresh();
@@ -1,8 +1,6 @@
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using XericLibrary.Runtime.SuperStyleSheet; using XericLibrary.Runtime.SuperStyleSheet;
namespace XericUI.XTable.Rendering.Elements namespace XericUI.XTable.Rendering.Elements
@@ -68,6 +66,7 @@ namespace XericUI.XTable.Rendering.Elements
RegisterField("/cell/font/size", OnStyleChanged); RegisterField("/cell/font/size", OnStyleChanged);
RegisterField("/cell/font/alignment", OnStyleChanged); RegisterField("/cell/font/alignment", OnStyleChanged);
RegisterField("/cell/font/richText", OnStyleChanged); RegisterField("/cell/font/richText", OnStyleChanged);
RegisterField("/cell/font/fontAsset", OnStyleChanged);
m_IsBound = true; m_IsBound = true;
RefreshAll(); RefreshAll();
@@ -88,6 +87,7 @@ namespace XericUI.XTable.Rendering.Elements
kvp.Value.Unregister(); kvp.Value.Unregister();
} }
} }
m_Fields.Clear(); m_Fields.Clear();
m_IsBound = false; m_IsBound = false;
} }
@@ -123,6 +123,20 @@ namespace XericUI.XTable.Rendering.Elements
StyleValue richVal = GetStyleValue("/cell/font/richText"); StyleValue richVal = GetStyleValue("/cell/font/richText");
if (richVal != null && TextComponent != null) if (richVal != null && TextComponent != null)
TextComponent.richText = (bool)richVal; TextComponent.richText = (bool)richVal;
// 字体资源
StyleValue fontVal = GetStyleValue("/cell/font/fontAsset");
if (fontVal != null && TextComponent != null)
{
try
{
TextComponent.font = fontVal.GetValueAs<TMP_FontAsset>();
}
catch
{
/* 类型不匹配时忽略 */
}
}
} }
#endregion #endregion
+31 -20
View File
@@ -3,36 +3,47 @@
// 表格默认样式定义 // 表格默认样式定义
// ========================================== // ==========================================
namespace: xeric_table_default namespace: xeric_table_default
// ---- 单元格背景 ----
cell/bg/color = (1,1,1,1):UnityEngine.Color @Desc:单元格背景颜色 cell/bg/color = (1,1,1,1):UnityEngine.Color @Desc:单元格背景颜色
// ---- 单元格前景 ----
cell/fg/color = (0.1,0.1,0.1,1):UnityEngine.Color @Desc:单元格前景文字颜色 cell/fg/color = (0.1,0.1,0.1,1):UnityEngine.Color @Desc:单元格前景文字颜色
// ---- 边框 ---- cell/border/top/ {
cell/border/top/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:上边框颜色 color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:上边框颜色
cell/border/top/width = 1:System.Single @Desc:上边框宽度 width = 1:System.Single @Desc:上边框宽度
cell/border/bottom/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:下边框颜色 }
cell/border/bottom/width = 1:System.Single @Desc:下边框宽度
cell/border/left/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:左边框颜色 cell/border/bottom/ {
cell/border/left/width = 1:System.Single @Desc:左边框宽度 color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:下边框颜色
cell/border/right/color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:右边框颜色 width = 1:System.Single @Desc:下边框宽度
cell/border/right/width = 1:System.Single @Desc:右边框宽度 }
cell/border/left/ {
color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:左边框颜色
width = 1:System.Single @Desc:左边框宽度
}
cell/border/right/ {
color = (0.8,0.8,0.8,1):UnityEngine.Color @Desc:右边框颜色
width = 1:System.Single @Desc:右边框宽度
}
// ---- 选中高亮 ----
cell/selection/bg/color = (0.2,0.5,1,0.3):UnityEngine.Color @Desc:选中单元格高亮颜色 cell/selection/bg/color = (0.2,0.5,1,0.3):UnityEngine.Color @Desc:选中单元格高亮颜色
// ---- 表头样式 ----
header/bg/color = (0.9,0.9,0.9,1):UnityEngine.Color @Desc:表头背景颜色 header/bg/color = (0.9,0.9,0.9,1):UnityEngine.Color @Desc:表头背景颜色
header/fg/color = (0,0,0,1):UnityEngine.Color @Desc:表头文字颜色 header/fg/color = (0,0,0,1):UnityEngine.Color @Desc:表头文字颜色
// ---- 字体 ---- cell/font/ {
cell/font/size = 14:System.Int32 @Desc:默认字体大小 size = 14:System.Int32 @Desc:默认字体大小
cell/font/alignment = MiddleCenter:UnityEngine.TextAnchor @Desc:默认文本对齐 alignment = MiddleCenter:UnityEngine.TextAnchor @Desc:默认文本对齐
cell/font/richText = true:System.Boolean @Desc:是否启用富文本 richText = true:System.Boolean @Desc:是否启用富文本
fontAsset = guid:68dace674a028544187e7ffe55cdae8b:TMPro.TMP_FontAsset:TMPro.TMP_FontAsset @Desc:字体资源 @ResourceSoftReference:Packages/com.lrss3.xericuiactionvessel/Runtime/Font/SourceHanSansSC-Regular-2 SDF.asset
}
// ---- 行高 / 列宽(缺省值) ---- cell/default/ {
cell/default/height = 40:System.Single @Desc:默认行高 height = 40:System.Single @Desc:默认行高
cell/default/width = 100:System.Single @Desc:默认列宽 width = 100:System.Single @Desc:默认列宽
}