表格面板:多选统一设置样式、选择即重绘、单元格配置换成覆盖层入口

- 新增 SetCellStyleRect / SetCellStyleNamespaceRect:按选区矩形逐格写命名空间+样式路径,
  合并块按 ResolveStyleRect 展开,边框与脏标记各只写一次;面板改为作用于整个选区。
- Inspector 订阅 SelectionHandle.OnSelectionChanged / OnCellSelected / Undo.undoRedoPerformed,
  事件驱动 Repaint——点选单元格不再需要把鼠标移进面板才刷新。
- 「单元格配置」的 infobox 换成按钮,直接打开 Table Editor Override Layer;
  该开关状态收敛到 XTableEditorOverlay.Enabled(EditorPrefs + Menu.SetChecked),
  删掉 SceneEditor 里重复注册的同路径 MenuItem。
This commit is contained in:
2026-12-03 11:25:50 +08:00
parent c24c3e2fd6
commit 7db9d2f731
4 changed files with 110 additions and 43 deletions
+22 -4
View File
@@ -15,17 +15,35 @@ namespace XericUIEditor.XTable
/// </summary>
public static class XTableEditorOverlay
{
/// <summary>是否启用覆盖层(可通过菜单开关)</summary>
/// <summary>是否启用覆盖层(可通过菜单开关;Inspector 的按钮也走这里)</summary>
private static bool s_Enabled = true;
internal const string MENU_PATH = XericRegistrationPaths.TableBar + "/Table Editor Override Layer";
private const string ENABLED_KEY = "XericUI_TableSceneEditor_Enabled";
static XTableEditorOverlay()
{
s_Enabled = EditorPrefs.GetBool(ENABLED_KEY, true);
}
/// <summary>覆盖层开关(唯一状态源):Inspector 按钮与菜单共用。</summary>
public static bool Enabled
{
get => s_Enabled;
set
{
if (s_Enabled == value) return;
s_Enabled = value;
EditorPrefs.SetBool(ENABLED_KEY, s_Enabled);
Menu.SetChecked(MENU_PATH, s_Enabled);
SceneView.RepaintAll();
}
}
[MenuItem(MENU_PATH, false, 200)]
private static void ToggleOverlay()
{
s_Enabled = !s_Enabled;
Menu.SetChecked(MENU_PATH, s_Enabled);
SceneView.RepaintAll();
Enabled = !Enabled;
}
[MenuItem(MENU_PATH, true)]
+39 -19
View File
@@ -58,8 +58,27 @@ namespace XericUIEditor.XTable
// 同步表格的 ChildrenVisible 状态到编辑器 toggle
m_ShowChildren = m_Table.ChildrenVisible;
// 选择变化 / 撤销重做时主动重绘——否则要等鼠标移进面板触发 OnGUI 才刷新。
m_Table.SelectionHandle.OnSelectionChanged += OnTableSelectionChanged;
m_Table.OnCellSelected += OnTableCellSelected;
Undo.undoRedoPerformed += OnTableUndoRedo;
}
private void OnDisable()
{
if (m_Table != null)
{
m_Table.SelectionHandle.OnSelectionChanged -= OnTableSelectionChanged;
m_Table.OnCellSelected -= OnTableCellSelected;
}
Undo.undoRedoPerformed -= OnTableUndoRedo;
}
private void OnTableSelectionChanged(XTableSelectionHandle handle) => Repaint();
private void OnTableCellSelected(int row, int col) => Repaint();
private void OnTableUndoRedo() => Repaint();
public override void OnInspectorGUI()
{
serializedObject.Update();
@@ -182,16 +201,6 @@ namespace XericUIEditor.XTable
EditorGUILayout.Space(4);
bool autoTemplate = EditorGUILayout.Toggle(
new GUIContent("自动维护 ScrollRect 模板", "关掉后表格不再补/重置 Viewport、Content、滚动条,可以自己替换"),
m_Table.AutoScrollTemplate);
if (autoTemplate != m_Table.AutoScrollTemplate)
{
Undo.RecordObject(m_Table, "Toggle Auto Scroll Template");
m_Table.AutoScrollTemplate = autoTemplate;
EditorUtility.SetDirty(m_Table);
}
bool autoScrollbars = EditorGUILayout.Toggle(
new GUIContent("自动生成滚动条", "关掉后表格不再建横/竖滚动条(含手柄),也不接管引用"),
m_Table.AutoScrollbars);
@@ -234,8 +243,11 @@ namespace XericUIEditor.XTable
EditorGUILayout.IntField("块数量", m_Table.TableData.BlockCount);
EditorGUILayout.TextField("块尺寸",
$"{m_Table.TableData.BlockSizeX} × {m_Table.TableData.BlockSizeY}");
var handle = m_Table.SelectionHandle;
EditorGUILayout.TextField("选中单元格",
m_Table.SelectedRow >= 0 ? $"({m_Table.SelectedRow}, {m_Table.SelectedCol})" : "(未选中)");
handle.HasSelection
? $"({m_Table.SelectedRow}, {m_Table.SelectedCol}) 选区 ({handle.MinRow},{handle.MinCol})-({handle.MaxRow},{handle.MaxCol})"
: "(未选中)");
EditorGUI.EndDisabledGroup();
}
else
@@ -250,11 +262,21 @@ namespace XericUIEditor.XTable
// ── 单元格配置 ──
EditorGUILayout.Space(4);
EditorGUILayout.LabelField("单元格配置", EditorStyles.boldLabel);
if (GUILayout.Button("打开「Table Editor Override Layer」", GUILayout.Height(22)))
{
XTableEditorOverlay.Enabled = true;
SceneView.lastActiveSceneView?.Repaint();
}
int selRow = m_Table.SelectedRow;
int selCol = m_Table.SelectedCol;
int minRow = m_Table.SelectionHandle.MinRow;
int maxRow = m_Table.SelectionHandle.MaxRow;
int minCol = m_Table.SelectionHandle.MinCol;
int maxCol = m_Table.SelectionHandle.MaxCol;
if (selRow < 0 || selCol < 0)
{
EditorGUILayout.HelpBox("请先在 Scene View 中点击选中一个单元格", MessageType.Info);
EditorGUILayout.HelpBox("请先在 Scene View 中点击选中一个单元格(可框选多格)", MessageType.Info);
}
else
{
@@ -267,6 +289,8 @@ namespace XericUIEditor.XTable
{
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.TextField("目标单元格", $"({selRow}, {selCol})");
EditorGUILayout.TextField("选区",
$"({minRow},{minCol})-({maxRow},{maxCol}) 共 {(maxRow - minRow + 1) * (maxCol - minCol + 1)} 格");
EditorGUI.EndDisabledGroup();
EditorGUI.BeginChangeCheck();
@@ -318,19 +342,15 @@ namespace XericUIEditor.XTable
EditorGUILayout.Space(2);
EditorGUILayout.LabelField("单元格样式 (StyleMember)", EditorStyles.miniBoldLabel);
EditorGUI.BeginChangeCheck();
string ns = EditorGUILayout.TextField("命名空间",
string ns = EditorGUILayout.TextField("命名空间(整个选区)",
data.CellStyle?.CurrentNamespace ?? "");
string path = EditorGUILayout.TextField("样式路径",
string path = EditorGUILayout.TextField("样式路径(整个选区)",
data.CellStyle?.StylePath ?? "");
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(m_Table, "Edit Cell Style");
if (data.CellStyle == null)
data.CellStyle = new StyleMember();
data.CellStyle.CurrentNamespace = ns;
data.CellStyle.StylePath = path;
m_Table.SetCellStyleRect(minRow, minCol, maxRow, maxCol, ns, path);
EditorUtility.SetDirty(m_Table);
m_Table.MarkDirty(TableDirtyType.DataChanged);
Repaint();
}
}
+2 -20
View File
@@ -28,8 +28,6 @@ namespace XericUIEditor.XTable
private static EditorState s_State;
private static string s_CurrentControlName; // 当前聚焦的控件名(每帧由焦点决定)
private static bool s_Enabled;
private const string ENABLED_KEY = "XericUI_TableSceneEditor_Enabled";
private static string s_EditText = "";
private static string s_SizeEditText = ""; // 气泡输入框当前编辑文本
@@ -65,26 +63,10 @@ namespace XericUIEditor.XTable
static XericUIActionTableSceneEditor()
{
s_Enabled = EditorPrefs.GetBool(ENABLED_KEY, true);
// 开关状态收敛到 XTableEditorOverlay.Enabled(菜单与 Inspector 共用同一入口)。
SceneView.duringSceneGui += OnSceneGUI;
}
[MenuItem(XTableEditorOverlay.MENU_PATH, false, 200)]
private static void ToggleEnabled()
{
s_Enabled = !s_Enabled;
EditorPrefs.SetBool(ENABLED_KEY, s_Enabled);
Menu.SetChecked(XTableEditorOverlay.MENU_PATH, s_Enabled);
SceneView.RepaintAll();
}
[MenuItem(XTableEditorOverlay.MENU_PATH, true)]
private static bool ToggleEnabledValidate()
{
Menu.SetChecked(XTableEditorOverlay.MENU_PATH, s_Enabled);
return true;
}
// ═══════════════════════════════════════════════════════════════
// 状态机入口
// ═══════════════════════════════════════════════════════════════
@@ -164,7 +146,7 @@ namespace XericUIEditor.XTable
private static void OnSceneGUI(SceneView sceneView)
{
if (!s_Enabled) return;
if (!XTableEditorOverlay.Enabled) return;
GameObject sel = Selection.activeGameObject;
if (sel == null) return;
@@ -183,6 +183,53 @@ namespace XericUI.XTable.Rendering.Component
MarkDirty(TableDirtyType.StyleChanged);
}
/// <summary>
/// 按矩形区域设置命名空间 + 样式路径(Inspector 多选统一设置用)。
/// 每格仍走合并块展开;边框只写一次,脏标记只打一次。
/// </summary>
public void SetCellStyleRect(int minRow, int minCol, int maxRow, int maxCol,
string styleNamespace, string stylePath)
{
ClampCellRect(ref minRow, ref minCol, ref maxRow, ref maxCol);
for (int r = minRow; r <= maxRow; r++)
for (int c = minCol; c <= maxCol; c++)
{
var data = TableData.GetOrCreateCell(r, c);
data.CellStyle ??= new XericLibrary.Runtime.SuperStyleSheet.StyleMember();
data.CellStyle.CurrentNamespace = styleNamespace ?? "";
data.CellStyle.StylePath = stylePath ?? "";
data.StyleNamespace = styleNamespace ?? "";
}
TableData.SetBorderRect(minRow, maxRow + 1, minCol, maxCol + 1, styleNamespace);
MarkDirty(TableDirtyType.StyleChanged);
}
/// <summary>按矩形区域只设置命名空间(兼容旧版的批量版本)。</summary>
public void SetCellStyleNamespaceRect(int minRow, int minCol, int maxRow, int maxCol, string styleNamespace)
{
ClampCellRect(ref minRow, ref minCol, ref maxRow, ref maxCol);
for (int r = minRow; r <= maxRow; r++)
for (int c = minCol; c <= maxCol; c++)
TableData.GetOrCreateCell(r, c).StyleNamespace = styleNamespace ?? string.Empty;
TableData.SetBorderRect(minRow, maxRow + 1, minCol, maxCol + 1, styleNamespace);
MarkDirty(TableDirtyType.StyleChanged);
}
private void ClampCellRect(ref int minRow, ref int minCol, ref int maxRow, ref int maxCol)
{
int rowCount = m_RowHeights?.Length ?? 0;
int colCount = m_ColWidths?.Length ?? 0;
if (rowCount <= 0 || colCount <= 0)
{
minRow = 0; maxRow = -1; minCol = 0; maxCol = -1;
return;
}
minRow = Mathf.Clamp(minRow, 0, rowCount - 1);
maxRow = Mathf.Clamp(maxRow, minRow, rowCount - 1);
minCol = Mathf.Clamp(minCol, 0, colCount - 1);
maxCol = Mathf.Clamp(maxCol, minCol, colCount - 1);
}
private void ResolveStyleRect(int row, int col, out int rowStart, out int rowEnd, out int colStart, out int colEnd)
{
rowStart = row; colStart = col; rowEnd = row + 1; colEnd = col + 1;