BITKit/Src/Unity/Scripts/UX/Library/TabContainer.cs

87 lines
1.8 KiB
C#
Raw Normal View History

2023-10-06 23:43:19 +08:00
using System;
2023-09-01 14:35:05 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class TabContainer : VisualElement
{
2024-08-13 18:42:51 +08:00
public new class UxmlTraits:VisualElement.UxmlTraits
2023-09-01 14:35:05 +08:00
{
2024-08-13 18:42:51 +08:00
private readonly UxmlStringAttributeDescription _mTabPath = new ()
2023-09-01 14:35:05 +08:00
{
2024-08-13 18:42:51 +08:00
name = "TabPath",
2023-09-01 14:35:05 +08:00
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
2024-08-13 18:42:51 +08:00
var container = (TabContainer)ve;
container.TabPath = _mTabPath.GetValueFromBag(bag, cc);
2023-09-01 14:35:05 +08:00
}
}
public new class UxmlFactory : UxmlFactory<TabContainer, UxmlTraits> { }
public TabContainer()
{
2025-01-12 11:13:19 +08:00
RegisterCallback<AttachToPanelEvent>(RebuildOnEvent);
RegisterCallback<DetachFromPanelEvent>(RebuildOnEvent);
RegisterCallback<GeometryChangedEvent>(RebuildOnEvent);
RegisterCallback<BlurEvent>(RebuildOnEvent);
RegisterCallback<FocusEvent>(RebuildOnEvent);
2024-12-09 16:40:42 +08:00
}
2024-08-13 18:42:51 +08:00
public string TabPath
2024-07-06 16:14:29 +08:00
{
2024-08-13 18:42:51 +08:00
get=>_tabPath;
2023-09-01 14:35:05 +08:00
set
{
2024-08-13 18:42:51 +08:00
_tabPath = value;
2025-01-12 11:13:19 +08:00
Rebuild();
2023-09-01 14:35:05 +08:00
}
}
2024-08-13 18:42:51 +08:00
private string _tabPath;
private TabBar _tabBar;
private int _index;
2025-01-12 11:13:19 +08:00
private void RebuildOnEvent<T>(T evt)
2023-09-01 14:35:05 +08:00
{
2024-08-13 18:42:51 +08:00
Rebuild();
2023-09-01 14:35:05 +08:00
}
2024-08-13 18:42:51 +08:00
private void Rebuild()
2023-09-01 14:35:05 +08:00
{
2024-08-13 18:42:51 +08:00
if (_tabBar is not null)
2023-09-01 14:35:05 +08:00
{
2024-08-13 18:42:51 +08:00
_tabBar.OnTabChanged -= OnTabChanged;
2023-09-01 14:35:05 +08:00
}
2025-01-12 11:13:19 +08:00
var p = parent;
while (p is not null)
2024-08-08 23:07:50 +08:00
{
2025-01-12 11:13:19 +08:00
_tabBar = p.Q<TabBar>(TabPath);
if (_tabBar is not null)
{
break;
}
p = p.parent;
2024-08-08 23:07:50 +08:00
}
2025-01-12 11:13:19 +08:00
if (_tabBar is not null)
2024-12-13 16:14:20 +08:00
{
2025-01-12 11:13:19 +08:00
_tabBar.OnTabChanged += OnTabChanged;
_index = _tabBar.CurrentTab;
2024-12-13 16:14:20 +08:00
}
2024-08-13 18:42:51 +08:00
OnTabChanged(_index);
}
2024-08-08 23:07:50 +08:00
2024-08-13 18:42:51 +08:00
private void OnTabChanged(int obj)
{
_index = obj;
2025-01-12 11:13:19 +08:00
if (childCount < 0) return;
2024-08-13 18:42:51 +08:00
for (var i = 0; i < childCount; i++)
2024-08-08 23:07:50 +08:00
{
2024-08-13 18:42:51 +08:00
var visualElement = this[i];
visualElement.SetActive(i == _index);
2024-08-08 23:07:50 +08:00
}
2023-09-01 14:35:05 +08:00
}
}
}