This commit is contained in:
CortexCore 2024-07-01 17:54:06 +08:00
parent 828be493ff
commit 87eae34f4b
3 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,107 @@
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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e3da6c50edc6f75488b4e367c5b7eae1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms.VisualStyles;
using UnityEngine;
using UnityEngine.UIElements;