1
This commit is contained in:
18
Packages/Runtime~/Unity/Scripts/UX/Library/CustomButton.cs
Normal file
18
Packages/Runtime~/Unity/Scripts/UX/Library/CustomButton.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
public class CustomButton : Button
|
||||
{
|
||||
public new class UxmlTraits : Button.UxmlTraits
|
||||
{
|
||||
}
|
||||
public CustomButton() : base()
|
||||
{
|
||||
}
|
||||
public new class UxmlFactory : UxmlFactory<CustomButton, UxmlTraits> { }
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ea5d1b4303697ea4f8597e0db992bb3b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
131
Packages/Runtime~/Unity/Scripts/UX/Library/TabBar.cs
Normal file
131
Packages/Runtime~/Unity/Scripts/UX/Library/TabBar.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
/// <summary>
|
||||
/// Tab容器,仅用于快速生成TabBar
|
||||
/// </summary>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public class TabBar : VisualElement,INotifyValueChanged<int>
|
||||
{
|
||||
public new class UxmlTraits:VisualElement.UxmlTraits
|
||||
{
|
||||
private readonly UxmlIntAttributeDescription m_TabBarAttribute = new ()
|
||||
{
|
||||
name = "CurrentTab",
|
||||
defaultValue = -1
|
||||
};
|
||||
private readonly UxmlStringAttributeDescription m_TabsAttribute = new ()
|
||||
{
|
||||
name = "tabs"
|
||||
};
|
||||
private readonly UxmlBoolAttributeDescription m_allowFocus = new ()
|
||||
{
|
||||
name = "allowFocus",
|
||||
defaultValue = true
|
||||
};
|
||||
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
|
||||
{
|
||||
base.Init(ve, bag, cc);
|
||||
var tabBar = (TabBar)ve;
|
||||
tabBar.CurrentTab = m_TabBarAttribute.GetValueFromBag(bag, cc);
|
||||
tabBar.Tabs = m_TabsAttribute.GetValueFromBag(bag, cc);
|
||||
tabBar.allowFocus = m_allowFocus.GetValueFromBag(bag, cc);
|
||||
}
|
||||
}
|
||||
public new class UxmlFactory : UxmlFactory<TabBar, UxmlTraits> { }
|
||||
// These are USS class names for the control overall and the label.
|
||||
|
||||
public event Action<int> OnTabChanged;
|
||||
|
||||
private Button[] _buttons = Array.Empty<Button>();
|
||||
private int _currentTab;
|
||||
public int CurrentTab
|
||||
{
|
||||
get=>_currentTab;
|
||||
set
|
||||
{
|
||||
_currentTab = value;
|
||||
SetTabs(value);
|
||||
}
|
||||
}
|
||||
|
||||
private string tabs;
|
||||
public string Tabs
|
||||
{
|
||||
get => tabs;
|
||||
set
|
||||
{
|
||||
tabs = value;
|
||||
SetTabs(value);
|
||||
}
|
||||
}
|
||||
|
||||
private bool allowFocus;
|
||||
public bool AllowFocus
|
||||
{
|
||||
get => allowFocus;
|
||||
set
|
||||
{
|
||||
allowFocus = value;
|
||||
foreach (var x in _buttons)
|
||||
{
|
||||
x.focusable = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void SetTabs(string value)
|
||||
{
|
||||
var split = value.Split(",");
|
||||
foreach (var x in _buttons)
|
||||
{
|
||||
x.RemoveFromHierarchy();
|
||||
}
|
||||
_buttons = new Button[split.Length];
|
||||
for (var i = 0; i < split.Length; i++)
|
||||
{
|
||||
var tabName = split[i];
|
||||
var index = i;
|
||||
var button = _buttons[i] = this.Create<Button>();
|
||||
button.text = tabName;
|
||||
button.focusable = allowFocus;
|
||||
button.clicked += () => CurrentTab = index;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetTabs(int index)
|
||||
{
|
||||
SetValueWithoutNotify(index);
|
||||
OnTabChanged?.Invoke(index);
|
||||
MarkDirtyRepaint();
|
||||
}
|
||||
|
||||
public void SetValueWithoutNotify(int newValue)
|
||||
{
|
||||
switch (newValue)
|
||||
{
|
||||
case var _ when newValue < 0 || newValue > _buttons.Length:
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < _buttons.Length; i++)
|
||||
{
|
||||
var button = _buttons[i];
|
||||
var entry = newValue == i;
|
||||
button.SetEnabled(!entry);
|
||||
}
|
||||
}
|
||||
int INotifyValueChanged<int>.value
|
||||
{
|
||||
get => CurrentTab;
|
||||
set=>CurrentTab = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
11
Packages/Runtime~/Unity/Scripts/UX/Library/TabBar.cs.meta
Normal file
11
Packages/Runtime~/Unity/Scripts/UX/Library/TabBar.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 22a1d2dab9cea2a4fa90bfc3439b11f1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62f0964438c4cda43a9ed00227e25ab1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user