135 lines
3.0 KiB
C#
135 lines
3.0 KiB
C#
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 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.CustomTabPath = m_customTabPathAttribute.GetValueFromBag(bag, cc);
|
|
tabContainer.pickingMode = PickingMode.Ignore;
|
|
tabContainer._container.pickingMode = PickingMode.Ignore;
|
|
}
|
|
}
|
|
public new class UxmlFactory : UxmlFactory<TabContainer, UxmlTraits> { }
|
|
public TabContainer()
|
|
{
|
|
_container = new VisualElement
|
|
{
|
|
name = UXConstant.ContextContainer,
|
|
style =
|
|
{
|
|
flexGrow = 1
|
|
}
|
|
};
|
|
hierarchy.Add(_container);
|
|
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
|
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
|
|
RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
|
|
pickingMode = PickingMode.Ignore;
|
|
_container.pickingMode = PickingMode.Ignore;
|
|
}
|
|
|
|
private void OnGeometryChanged(GeometryChangedEvent evt)
|
|
{
|
|
UpdateTabBar();
|
|
}
|
|
|
|
public override VisualElement contentContainer => _container;
|
|
public event Action<int> OnTabChanged;
|
|
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 (parent is not null)
|
|
{
|
|
_externalTabBar = parent.Q<TabBar>(value);
|
|
if (_externalTabBar is not null)
|
|
{
|
|
_externalTabBar.OnTabChanged += EntryTab;
|
|
}
|
|
viewDataKey=_externalTabBar is null ? $"找到了:{value}" :$"未找到:{value}";
|
|
}
|
|
else
|
|
{
|
|
viewDataKey = $"{value} is null";
|
|
}
|
|
|
|
}
|
|
}
|
|
private readonly VisualElement _container;
|
|
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);
|
|
CurrentTab = CurrentTab;
|
|
}
|
|
public void EntryTab(int index)
|
|
{
|
|
if(index == -1)return;
|
|
var isCatched = false;
|
|
foreach (var x in _container.Children())
|
|
{
|
|
x.SetActive(false);
|
|
}
|
|
|
|
if (_container.Children().TryGetElementAt(index, out var element))
|
|
{
|
|
isCatched = true;
|
|
element.SetActive(true);
|
|
}
|
|
|
|
if (index is -1 && isCatched is false && this.childCount>0)
|
|
{
|
|
this.Children().First().SetActive(true);
|
|
}
|
|
|
|
OnTabChanged?.Invoke(index);
|
|
}
|
|
}
|
|
}
|
|
|