1
This commit is contained in:
122
Packages/Runtime~/Unity/Scripts/UX/Library/TabContainer.cs
Normal file
122
Packages/Runtime~/Unity/Scripts/UX/Library/TabContainer.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
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 UxmlIntAttributeDescription m_currentTabAttribute = new ()
|
||||
{
|
||||
name = "CurrentTab",
|
||||
defaultValue = -1
|
||||
};
|
||||
private readonly UxmlStringAttributeDescription m_customTabPathAttribute = new ()
|
||||
{
|
||||
name = "CustomTabPath",
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
||||
{
|
||||
base.Init(ve, bag, cc);
|
||||
var tabContainer = (TabContainer)ve;
|
||||
tabContainer.CurrentTab = m_currentTabAttribute.GetValueFromBag(bag, cc);
|
||||
tabContainer.CustomTabPath = m_customTabPathAttribute.GetValueFromBag(bag, cc);
|
||||
tabContainer.pickingMode = PickingMode.Ignore;
|
||||
tabContainer._container.pickingMode = PickingMode.Ignore;
|
||||
}
|
||||
}
|
||||
public new class UxmlFactory : UxmlFactory<TabContainer, UxmlTraits> { }
|
||||
public TabContainer()
|
||||
{
|
||||
_internalTabBar = new TabBar();
|
||||
_container = new VisualElement
|
||||
{
|
||||
name = UXConstant.ContextContainer,
|
||||
style =
|
||||
{
|
||||
flexGrow = 1
|
||||
}
|
||||
};
|
||||
hierarchy.Add(_internalTabBar);
|
||||
hierarchy.Add(_container);
|
||||
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
||||
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
|
||||
_internalTabBar.OnTabChanged += EntryTab;
|
||||
pickingMode = PickingMode.Ignore;
|
||||
_container.pickingMode = PickingMode.Ignore;
|
||||
}
|
||||
public override VisualElement contentContainer => _container;
|
||||
private int currentTab=-1;
|
||||
|
||||
public int CurrentTab
|
||||
{
|
||||
get => currentTab;
|
||||
set
|
||||
{
|
||||
currentTab = value;
|
||||
EntryTab(currentTab);
|
||||
}
|
||||
}
|
||||
|
||||
private string _customTabPath;
|
||||
public string CustomTabPath
|
||||
{
|
||||
get => _customTabPath;
|
||||
set
|
||||
{
|
||||
_customTabPath = value;
|
||||
if (_externalTabBar is not null)
|
||||
{
|
||||
_externalTabBar.OnTabChanged -= EntryTab;
|
||||
}
|
||||
if (panel?.visualTree != null)
|
||||
{
|
||||
_externalTabBar = panel.visualTree.Q<TabBar>(value);
|
||||
if (_externalTabBar is not null)
|
||||
{
|
||||
_externalTabBar.OnTabChanged += EntryTab;
|
||||
}
|
||||
}
|
||||
_internalTabBar.SetActive(_externalTabBar is null);
|
||||
}
|
||||
}
|
||||
private readonly VisualElement _container;
|
||||
private readonly TabBar _internalTabBar;
|
||||
private TabBar _externalTabBar;
|
||||
|
||||
private void OnDetachFromPanel(DetachFromPanelEvent evt)
|
||||
{
|
||||
UpdateTabBar();
|
||||
}
|
||||
private void OnAttachToPanel(AttachToPanelEvent evt)
|
||||
{
|
||||
UpdateTabBar();
|
||||
}
|
||||
private void UpdateTabBar()
|
||||
{
|
||||
CustomTabPath = CustomTabPath;
|
||||
CurrentTab = CurrentTab;
|
||||
_externalTabBar?.SetValueWithoutNotify(CurrentTab);
|
||||
_internalTabBar.SetValueWithoutNotify(CurrentTab);
|
||||
_internalTabBar.Tabs = string.Join(",", _container.Children().Select(x => x.name));
|
||||
}
|
||||
private void OnShowTab(bool value)
|
||||
{
|
||||
_internalTabBar.style.display = value ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
public void EntryTab(int index)
|
||||
{
|
||||
foreach (var x in _container.Children())
|
||||
{
|
||||
x.SetActive(false);
|
||||
}
|
||||
if(_container.Children().TryGet(index,out var element))
|
||||
element.SetActive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user