mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-08-05 22:40:16 +08:00
refactor(UXAssist): split tab management out of MyConfigWindow
This commit is contained in:
@@ -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<Tab> Tabs { get; } = [];
|
||||
}
|
||||
|
||||
private readonly List<Group> _groups = [];
|
||||
private int _currentTabIndex = -1;
|
||||
|
||||
public IReadOnlyList<Group> 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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-19
@@ -425,7 +425,7 @@ public class MyWindow : ManualBehaviour
|
||||
|
||||
public class MyWindowWithTabs : MyWindow
|
||||
{
|
||||
private readonly List<Tuple<RectTransform, UIButton>> _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
|
||||
|
||||
Reference in New Issue
Block a user