108 lines
3.0 KiB
C#
108 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace BITKit.UX
|
|
{
|
|
public class CycleContainer : VisualElement
|
|
{
|
|
public new class UxmlTraits : VisualElement.UxmlTraits
|
|
{
|
|
private readonly UxmlIntAttributeDescription m_currentTabAttribute = new ()
|
|
{
|
|
name = "CurrentIndex",
|
|
defaultValue = -1
|
|
};
|
|
private readonly UxmlStringAttributeDescription m_customTabPathAttribute = new ()
|
|
{
|
|
name = "CurrentButtonPath",
|
|
};
|
|
|
|
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
|
{
|
|
base.Init(ve, bag, cc);
|
|
var self = (CycleContainer)ve;
|
|
self.Index = m_currentTabAttribute.GetValueFromBag(bag, cc);
|
|
self.CurrentButtonPath = m_customTabPathAttribute.GetValueFromBag(bag, cc);
|
|
}
|
|
}
|
|
public new class UxmlFactory : UxmlFactory<CycleContainer, UxmlTraits> { }
|
|
|
|
public CycleContainer()
|
|
{
|
|
RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
|
|
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
|
|
}
|
|
|
|
private void OnDetachFromPanel(DetachFromPanelEvent evt)
|
|
{
|
|
RebuildInternal();
|
|
}
|
|
|
|
private void OnAttachToPanel(AttachToPanelEvent evt)
|
|
{
|
|
RebuildInternal();
|
|
}
|
|
|
|
|
|
public int Index
|
|
{
|
|
get=>_index;
|
|
set
|
|
{
|
|
_index = value;
|
|
RebuildInternal();
|
|
}
|
|
}
|
|
private int _index;
|
|
public string CurrentButtonPath
|
|
{
|
|
get=>_currentButtonPath;
|
|
set
|
|
{
|
|
_currentButtonPath=value;
|
|
RebuildInternal();
|
|
}
|
|
}
|
|
private string _currentButtonPath;
|
|
|
|
|
|
|
|
private Button _currentButton;
|
|
public void Cycle()
|
|
{
|
|
_index++;
|
|
if (_index >= childCount)
|
|
{
|
|
_index = 0;
|
|
}
|
|
for (var i = 0; i < childCount; i++)
|
|
{
|
|
this[i].style.display = i == _index ? DisplayStyle.Flex : DisplayStyle.None;
|
|
}
|
|
}
|
|
|
|
private void RebuildInternal()
|
|
{
|
|
for (var i = 0; i < this.childCount; i++)
|
|
{
|
|
this[i].style.display = i == _index ? DisplayStyle.Flex : DisplayStyle.None;
|
|
}
|
|
if (panel is null) return;
|
|
if (_currentButton is not null)
|
|
{
|
|
_currentButton.clickable = null;
|
|
}
|
|
|
|
_currentButton = panel.visualTree.Q<Button>(_currentButtonPath);
|
|
|
|
if(_currentButton is null)return;
|
|
|
|
_currentButton.clickable = null;
|
|
_currentButton.clicked += Cycle;
|
|
}
|
|
}
|
|
|
|
}
|