This commit is contained in:
CortexCore
2023-09-01 14:35:05 +08:00
parent a71288cf2d
commit 5561f5c3cc
136 changed files with 69284 additions and 66121 deletions

View File

@@ -15,7 +15,7 @@ public class UXBuilder : MonoBehaviour
[SerializeField] private bool clearOnStart;
private readonly List<VisualElement> instances = new();
public VisualElement VisualElement => visualElementProvider.GetVisualElement();
private IList ItemSource
{
get => _itemSource;

View File

@@ -115,8 +115,8 @@ namespace BITKit.UX
{
Singleton = this;
root = document.rootVisualElement;
container = document.rootVisualElement[0];
root.RegisterCallback<MouseDownEvent>(x =>
container = document.rootVisualElement[1];
root.Q("background-image").RegisterCallback<MouseDownEvent>(x =>
{
Close();
});

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4cac3f1115a23fd45b7feb40a6e8065e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class UXFlexEvent : UXElement<VisualElement>
{
[SerializeField] private UnityEvent<bool> OnDisplay;
[SerializeField] private UnityEvent onFlex;
[SerializeField] private UnityEvent onNone;
public override void OnStart()
{
base.OnStart();
visualElement.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
}
private void OnGeometryChanged(GeometryChangedEvent evt)
{
switch (visualElement.style.display.value)
{
case DisplayStyle.None:
OnDisplay?.Invoke(false);
onNone?.Invoke();
break;
case DisplayStyle.Flex:
OnDisplay?.Invoke(true);
onFlex?.Invoke();
break;
}
}
}
}

View File

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

View File

@@ -76,16 +76,23 @@ namespace BITKit.UX
}
var split = bindName.Split(@".");
var visualElement = document.rootVisualElement;
foreach (var name in split)
if (string.IsNullOrEmpty(bindName))
{
try
}
else
{
foreach (var name in split)
{
visualElement = visualElement.Q(name);
}
catch (System.Exception)
{
Debug.LogWarning(name);
throw;
try
{
visualElement = visualElement.Q(name);
}
catch (System.Exception)
{
Debug.LogWarning(name);
throw;
}
}
}
m_visualElement = visualElement as UX;

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@@ -6,6 +7,7 @@ using UnityEngine.UIElements;
namespace BITKit.UX
{
[Obsolete("Use UXBuilder instead")]
public class UXFactory : MonoBehaviour
{
[Header(Constant.Header.Components)]

View File

@@ -13,6 +13,7 @@ using UnityEditor;
#endif
namespace BITKit.UX
{
[Obsolete]
public interface IPanelComponent : IActivable
{
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@@ -10,6 +11,7 @@ using System.Threading.Tasks;
using System.Linq;
namespace BITKit.UX
{
[Obsolete]
public interface IWindowElement
{
string GetName();
@@ -19,7 +21,8 @@ namespace BITKit.UX
Task Open(CancellationToken cancellationToken = default);
Task Close(CancellationToken cancellationToken = default);
}
[System.Serializable]
[Obsolete]
[Serializable]
public class UXWindowElement : IWindowElement
{
[SerializeField]
@@ -102,16 +105,13 @@ namespace BITKit.UX
List<IWindowElement> activeWindows = new();
IWindowElement activeWindow;
public int index { get; private set; }
CancellationTokenSource cts;
protected CancellationToken _cancellationToken;
public async void Select(UXElement element)
{
cts.Cancel();
cts = new();
foreach (var activeWindow in activeWindows)
foreach (var _activeWindow in activeWindows.ToArray())
{
await activeWindow.Close(cts.Token);
OnExit.Invoke(activeWindow.GetName());
await _activeWindow.Close(_cancellationToken);
OnExit.Invoke(_activeWindow.GetName());
}
activeWindows.Clear();
@@ -180,7 +180,8 @@ namespace BITKit.UX
get => hidden;
set
{
if (hidden)
hidden = value;
if (value)
{
Exit();
}
@@ -200,18 +201,20 @@ namespace BITKit.UX
hidden = false;
activeWindow?.Open();
}
void Awake()
private void Awake()
{
cts = new();
var ct = gameObject.GetCancellationTokenOnDestroy();
ct.Register(cts.Cancel);
_cancellationToken = gameObject.GetCancellationTokenOnDestroy();
}
async void Start()
private async void Start()
{
await UniTask.Yield();
foreach (var window in windows)
{
await window.Close(cts.Token);
if (window.Navigation is UXButton uxButton)
{
uxButton.onClick.AddListener(() => Select(window.Window));
}
await window.Close(_cancellationToken);
}
Select(startElement);
}

View 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> { }
}
}

View File

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

View 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;
}
}
}

View File

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

View 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);
}
}
}

View File

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

View File

@@ -6,6 +6,8 @@ using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UIElements;
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
// ReSharper disable UnusedMember.Global
namespace BITKit.UX
{
@@ -48,6 +50,7 @@ namespace BITKit.UX
{
OnEntryOrExit(true);
document.rootVisualElement.SetActive(true);
OnEntry?.Invoke();
}
void IUXPanel.Exit()
{
@@ -62,8 +65,11 @@ namespace BITKit.UX
BIT4Log.Warning<UIToolKitPanel>(name);
BIT4Log.LogException(e);
}
OnExit?.Invoke();
}
public event Action OnEntry;
public event Action OnExit;
protected virtual void OnEntryOrExit(bool isEntry)
{
}

View File

@@ -76,6 +76,8 @@ namespace BITKit.UX
[SerializeReference, SubclassSelector] private IUXPanel initialPanel;
[SerializeField] private TextAsset validTexts;
private bool initialized;
private void Awake()
{
@@ -150,8 +152,10 @@ namespace BITKit.UX
protected override void OnUpdate()
{
if (panelsLabel is null || currentPanelLabel is null) return;
panelsLabel.text=string.Join("\n",UXService.Panels);
currentPanelLabel.text = string.Join("\n",UXService.EntryCompletedPanels);
currentPanelLabel.text = string.Join("\n",UXService.EntryCompletedPanels);
}
}
#endif

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@@ -5,28 +6,29 @@ using BITKit;
using UnityEngine.Events;
namespace BITKit.UX
{
public class UXPanelEvent : MonoBehaviour, IPanelComponent
/// <summary>
/// 基于<see cref="IUXPanel"/>的面板事件
/// </summary>
public class UXPanelEvent : MonoBehaviour
{
public UnityEvent<bool> onSetActive = new();
public UnityEvent onActive = new();
public UnityEvent onInactive = new();
public void SetActive(bool active)
public UnityEvent<bool> onEntryOrExit = new();
public UnityEvent onEntry = new();
public UnityEvent onExit = new();
private void Awake()
{
onSetActive.Invoke(active);
if (active)
{
onActive.Invoke();
}
else
{
onInactive.Invoke();
}
var panel = GetComponent<IUXPanel>();
panel.OnEntry += OnEntry;
panel.OnExit += OnExit;
}
public bool Enabled
private void OnExit()
{
get => enabled;
set => enabled = value;
onEntryOrExit.Invoke(false);
onExit.Invoke();
}
private void OnEntry()
{
onEntryOrExit.Invoke(true);
onEntry.Invoke();
}
}
}