using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UIElements; namespace BITKit.UX { public class TabContainer : VisualElement { public new class UxmlTraits:VisualElement.UxmlTraits { private readonly UxmlStringAttributeDescription _mTabPath = new () { name = "TabPath", }; public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc) { base.Init(ve, bag, cc); var container = (TabContainer)ve; container.TabPath = _mTabPath.GetValueFromBag(bag, cc); } } public new class UxmlFactory : UxmlFactory { } public TabContainer() { RegisterCallback(RebuildOnEvent); RegisterCallback(RebuildOnEvent); RegisterCallback(RebuildOnEvent); RegisterCallback(RebuildOnEvent); RegisterCallback(RebuildOnEvent); } public string TabPath { get=>_tabPath; set { _tabPath = value; Rebuild(); } } private string _tabPath; private TabBar _tabBar; private int _index; private void RebuildOnEvent(T evt) { Rebuild(); } private void Rebuild() { if (_tabBar is not null) { _tabBar.OnTabChanged -= OnTabChanged; } var p = parent; while (p is not null) { _tabBar = p.Q(TabPath); if (_tabBar is not null) { break; } p = p.parent; } if (_tabBar is not null) { _tabBar.OnTabChanged += OnTabChanged; _index = _tabBar.CurrentTab; } OnTabChanged(_index); } private void OnTabChanged(int obj) { _index = obj; if (childCount < 0) return; for (var i = 0; i < childCount; i++) { var visualElement = this[i]; visualElement.SetActive(i == _index); } } } }