From 4fb43c73b5dc017d156ff1052b0243d103b87507 Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Tue, 23 Jun 2026 08:33:40 +0800 Subject: [PATCH] refactor(UXAssist): split tab management out of MyConfigWindow --- UXAssist/UI/ConfigTabGroup.cs | 81 +++++++++++++++++++++++++++++++++++ UXAssist/UI/MyWindow.cs | 25 +++-------- 2 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 UXAssist/UI/ConfigTabGroup.cs diff --git a/UXAssist/UI/ConfigTabGroup.cs b/UXAssist/UI/ConfigTabGroup.cs new file mode 100644 index 0000000..b685066 --- /dev/null +++ b/UXAssist/UI/ConfigTabGroup.cs @@ -0,0 +1,81 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace UXAssist.UI; + +public class ConfigTabGroup +{ + public class Tab + { + public RectTransform RectTransform { get; set; } + public UIButton Button { get; set; } + } + + public class Group + { + public string Label { get; set; } + public List Tabs { get; } = []; + } + + private readonly List _groups = []; + private int _currentTabIndex = -1; + + public IReadOnlyList Groups => _groups; + + public int CurrentTabIndex => _currentTabIndex; + + public int TabCount + { + get + { + var count = 0; + foreach (var group in _groups) + { + count += group.Tabs.Count; + } + + return count; + } + } + + public void AddGroup(string label) + { + _groups.Add(new Group { Label = label }); + } + + public int AddTab(RectTransform rectTransform, UIButton button) + { + if (_groups.Count == 0) + { + AddGroup(string.Empty); + } + + var group = _groups[_groups.Count - 1]; + group.Tabs.Add(new Tab { RectTransform = rectTransform, Button = button }); + return TabCount - 1; + } + + public void SetCurrentTab(int index) + { + _currentTabIndex = index; + var current = 0; + foreach (var group in _groups) + { + foreach (var tab in group.Tabs) + { + if (current != index) + { + tab.Button.highlighted = false; + tab.RectTransform.gameObject.SetActive(false); + } + else + { + tab.Button.highlighted = true; + tab.RectTransform.gameObject.SetActive(true); + } + + current++; + } + } + } +} diff --git a/UXAssist/UI/MyWindow.cs b/UXAssist/UI/MyWindow.cs index fbae705..b821c5b 100644 --- a/UXAssist/UI/MyWindow.cs +++ b/UXAssist/UI/MyWindow.cs @@ -425,7 +425,7 @@ public class MyWindow : ManualBehaviour public class MyWindowWithTabs : MyWindow { - private readonly List> _tabs = []; + private readonly ConfigTabGroup _tabGroup = new(); private float _tabY = 54f; public override void TryClose() @@ -471,7 +471,7 @@ public class MyWindowWithTabs : MyWindow btnText.fontSize = 16; btn.data = index; - _tabs.Add(Tuple.Create(tabRect, btn)); + _tabGroup.AddTab(tabRect, btn); btn.onClick += OnTabButtonClick; MaxY = Math.Max(MaxY, y + TabHeight); @@ -480,7 +480,7 @@ public class MyWindowWithTabs : MyWindow public RectTransform AddTab(RectTransform parent, string label) { - var result = AddTabInternal(_tabY, _tabs.Count, parent, label); + var result = AddTabInternal(_tabY, _tabGroup.TabCount, parent, label); _tabY += 28f; return result; } @@ -497,27 +497,14 @@ public class MyWindowWithTabs : MyWindow public void AddTabGroup(RectTransform parent, string label, string objName = "tabl-group-label") { + _tabGroup.AddGroup(label); LayoutHelper.AddText(28, _tabY - 2, parent, label, 16, objName); _tabY += 28f; } - protected void SetCurrentTab(int index) => OnTabButtonClick(index); + protected void SetCurrentTab(int index) => _tabGroup.SetCurrentTab(index); - private void OnTabButtonClick(int index) - { - foreach (var (rectTransform, btn) in _tabs) - { - if (btn.data != index) - { - btn.highlighted = false; - rectTransform.gameObject.SetActive(false); - continue; - } - - btn.highlighted = true; - rectTransform.gameObject.SetActive(true); - } - } + private void OnTabButtonClick(int index) => _tabGroup.SetCurrentTab(index); } public abstract class MyWindowManager