This commit is contained in:
CortexCore
2023-10-06 23:43:19 +08:00
parent ebf9c1f526
commit 2c4710bc5d
186 changed files with 111802 additions and 764 deletions

View File

@@ -22,5 +22,10 @@ namespace BITKit.UX
{
Alert.Print(message);
}
public void Execute(string message)
{
Alert.Print("提示", message);
}
}
}

View File

@@ -20,6 +20,15 @@ namespace BITKit.UX
{
UXAlert.Singleton.PrintAlertMessage(message);
}
public static void Print(string title, string content)
{
Print(new AlertMessage()
{
title = title,
message = content
});
}
}
public class UXAlert : MonoBehaviour
{

View File

@@ -14,7 +14,8 @@
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:9400d40641bab5b4a9702f65bf5c6eb5",
"GUID:7d3ace4c6aad3684abe11aa38b6cdf99"
"GUID:7d3ace4c6aad3684abe11aa38b6cdf99",
"GUID:517785bb4600a5140b47eac5fa49b8fc"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -7,7 +7,7 @@ using UnityEngine.UIElements;
// ReSharper disable MemberCanBePrivate.Global
public class UXBuilder : MonoBehaviour
{
[SerializeField, SerializeReference, SubclassSelector]
[SerializeReference, SubclassSelector]
private IVisualElementProvider visualElementProvider;
[SerializeField] private VisualTreeAsset visualTreeAsset;
@@ -24,9 +24,9 @@ public class UXBuilder : MonoBehaviour
private IList _itemSource;
public T Build<T>() where T : VisualElement
public T Build<T>() where T : VisualElement,new()
{
var clone = visualTreeAsset.CloneTree()[0];
var clone =visualTreeAsset is not null ? visualTreeAsset.CloneTree()[0] : new T();
visualElementProvider.GetVisualElement().Add(clone);
instances.Add(clone);
return clone as T;
@@ -45,9 +45,9 @@ public class UXBuilder : MonoBehaviour
{
foreach (var x in instances)
{
visualElementProvider.GetVisualElement().Remove(x);
x.RemoveFromHierarchy();
//visualElementProvider.GetVisualElement().Remove(x);
}
instances.Clear();
}

View File

@@ -54,15 +54,23 @@ namespace BITKit.UX
}
}
public async void SetDirect(float progess,string label)
public async void SetDirect(float progess, string label)
{
await UniTask.SwitchToMainThread(cancellationToken);
Set(progess);
if (labelElement is not null)
try
{
await UniTask.SwitchToMainThread(cancellationToken);
Set(progess);
if (labelElement is not null)
{
labelElement.text = label;
}
}
catch (OperationCanceledException)
{
labelElement.text = label;
}
}
float IProvider<float>.Get()
{
throw new System.NotImplementedException();

View File

@@ -12,16 +12,12 @@ namespace BITKit.UX
[Header(Constant.Header.Events)]
public UnityEvent onClick = new();
public UnityEvent onRightClick = new();
[SerializeField] private BITKit.Events.UnityEvent clicked=new();
public override void OnStart()
{
try
{
visualElement.clicked += Click;
}
catch (System.Exception)
{
throw;
}
if (allowRightClick)
{
foreach (var x in visualElement.Children())
@@ -38,18 +34,19 @@ namespace BITKit.UX
base.Set(active);
visualElement.SetEnabled(active);
}
void Click()
private void Click()
{
onClick.Invoke();
clicked.Invoke();
}
void OnMouseDown(MouseDownEvent mouseEvent)
private void OnMouseDown(MouseDownEvent mouseEvent)
{
if (mouseEvent.button is 1)
{
onRightClick.Invoke();
}
}
void OnMouseUp(MouseUpEvent mouseEvent)
private void OnMouseUp(MouseUpEvent mouseEvent)
{
}

View File

@@ -21,15 +21,6 @@ namespace BITKit.UX
public const string Inspector = "inspector-container";
public const string ProcessBarFill = "fill-bar";
public const string Toggle = "context-toggle";
public class Buttons
{
public const string Button_0 = "button--0";
public const string Button_1 = "button--0";
public const string Button_2 = "button--0";
public const string Button_3 = "button--0";
public const string Button_4 = "button--0";
public const string Button_5 = "button--0";
}
}

View File

@@ -59,7 +59,7 @@ namespace BITKit.UX
[Header(Constant.Header.Settings)]
public string bindName;
[SerializeField, SerializeReference, SubclassSelector]
IReference bindNameProvider = new GetNameFromGameobject();
protected IReference bindNameProvider = new GetNameFromGameobject();
[Header(Constant.Header.InternalVariables)]
private UX m_visualElement;
protected IStyle style => visualElement.style;

View File

@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit.UX
{
public class UXJsonBasedTable : UXElement<JsonBasedTable>
{
}
}

View File

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

View File

@@ -16,7 +16,15 @@ namespace BITKit.UX
public override void OnStart()
{
base.OnStart();
visualElement.RegisterValueChangedCallback(OnValueChanged);
switch (visualElement)
{
case CustomTextField customTextField:
customTextField.OnValueChanged += OnValueChanged;
break;
default:
visualElement.RegisterValueChangedCallback(OnValueChanged);
break;
}
}
public async void Set(string t)
{
@@ -32,9 +40,13 @@ namespace BITKit.UX
return visualElement.value;
}
void OnValueChanged(ChangeEvent<string> changeEvent)
private void OnValueChanged(string value)
{
onValueChanged.Invoke(changeEvent.newValue);
onValueChanged.Invoke(value);
}
private void OnValueChanged(ChangeEvent<string> changeEvent)
{
OnValueChanged(changeEvent.newValue);
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class CustomTextField : TextField
{
public const string errorUssName = "error";
public new class UxmlTraits : TextField.UxmlTraits
{
private readonly UxmlStringAttributeDescription m_RegexAttribute = new ()
{
name = "Regex"
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
var textField = (CustomTextField)ve;
textField.Regex = m_RegexAttribute.GetValueFromBag(bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<CustomTextField, UxmlTraits> { }
public CustomTextField():base()
{
this.RegisterValueChangedCallback(OnValueChangedInternal);
}
public string Regex { get; set; }
public event Action<string> OnValueChanged;
private void OnValueChangedInternal(ChangeEvent<string> evt)
{
var isMatch = string.IsNullOrEmpty(Regex) || System.Text.RegularExpressions.Regex.IsMatch(evt.newValue, Regex);
this.Q("unity-text-input").EnableInClassList(errorUssName, !isMatch);
if (isMatch)
{
OnValueChanged?.Invoke(evt.newValue);
}
}
}
}

View File

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

View File

@@ -0,0 +1,96 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
/// <summary>
/// 基于Json的Table生成元素
/// </summary>
public class JsonBasedTable : VisualElement
{
public new class UxmlTraits : VisualElement.UxmlTraits
{
private readonly UxmlStringAttributeDescription m_JsonAttribute = new ()
{
name = "Json"
};
private readonly UxmlBoolAttributeDescription m_allowWarningsAttribute = new ()
{
name = "allowWarnings",
defaultValue = false
};
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
var table = (JsonBasedTable)ve;
table.Json = m_JsonAttribute.GetValueFromBag(bag, cc);
table.AllowWarning = m_allowWarningsAttribute.GetValueFromBag(bag, cc);
}
}
public new class UxmlFactory : UxmlFactory<JsonBasedTable, UxmlTraits> { }
private string _json;
public string Json
{
get => _json;
set
{
_json = value;
ApplyJson();
}
}
public bool AllowWarning { get; set; }
private readonly List<VisualElement> instanceColumns = new();
private void ApplyJson()
{
try
{
Clear();
style.flexDirection = FlexDirection.Row;
style.justifyContent = Justify.SpaceAround;
instanceColumns.Clear();
if (string.IsNullOrEmpty(Json)) return;
var jArray = JArray.Parse(Json);
var colLength = jArray.Max(x => x.Count());
var rowLength = jArray.Count;
for (var i = 0; i < colLength; i++)
{
instanceColumns.Add(this.Create<VisualElement>());
}
for (var y = 0; y < rowLength; y++)
{
var array = jArray[y] as JArray;
for (var x = 0; x < colLength; x++)
{
var instance = instanceColumns[x];
if (x >= array!.Count)
{
instance.Create<VisualElement>();
}
else
{
var label = instance.Create<Label>();
label.text = array[x].ToString();
}
}
}
}
catch (Exception)
{
if (AllowWarning)
throw;
}
}
}
}

View File

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

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
@@ -50,6 +51,7 @@ namespace BITKit.UX
_container.pickingMode = PickingMode.Ignore;
}
public override VisualElement contentContainer => _container;
public event Action<int> OnTabChanged;
private int currentTab=-1;
public int CurrentTab
@@ -116,6 +118,7 @@ namespace BITKit.UX
}
if(_container.Children().TryGet(index,out var element))
element.SetActive(true);
OnTabChanged?.Invoke(index);
}
}
}

View File

@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BITKit.UX
{
[SerializeField]
public class UXServiceBasedAllowTouch : ICondition
{
public bool OnCheck() => true;
}
}

View File

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

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
@@ -14,7 +15,10 @@ namespace BITKit.UX
/// </summary>
public class UXService : MonoBehaviour, IUXService
{
[RuntimeInitializeOnLoadMethod]
/// <summary>
/// 重新初始化,使用<see cref="RuntimeInitializeLoadType.SubsystemRegistration"/>确保在所有子系统注册后执行
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Initialized()
{
RegistryQueue.Clear();
@@ -76,8 +80,6 @@ namespace BITKit.UX
[SerializeReference, SubclassSelector] private IUXPanel initialPanel;
[SerializeField] private TextAsset validTexts;
private bool initialized;
private void Awake()
{
@@ -105,14 +107,24 @@ namespace BITKit.UX
Entry(initialPanel);
}
if (!EntryQueue.TryPop(out var next)) return;
if (!EntryQueue.TryPop(out var next) || next is null) return;
if (Panels.ContainsKey(next.Index) is false) return;
while (EntryCompletedPanels.TryPop(out var entryCompletedPanel))
{
entryCompletedPanel?.Exit();
}
next.Entry();
try
{
next.Entry();
}
catch (Exception e)
{
Debug.LogWarning(next.Index);
Debug.LogException(e);
}
BITAppForUnity.AllowCursor.SetElements(this, next.AllowCursor);
BITInputSystem.AllowInput.SetElements(this, next.AllowInput);
@@ -154,7 +166,7 @@ namespace BITKit.UX
{
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.Select(x=>x.Index));
}
}

View File

@@ -0,0 +1,65 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
namespace BITKit.UX
{
public class UXTabContainerEvent : UXElement<TabContainer>
{
[Tooltip("基于<see cref=\"TabBar\"/>的Tab切换事件")]
[SerializeField] private UnityEvent<bool>[] onEntryTab;
[SerializeField] private UnityEvent[] onEntry;
[SerializeField] private UnityEvent[] onExit;
private readonly DoubleBuffer<int> tabBuffer = new();
private CancellationToken _cancellationToken;
public override void OnAwake()
{
base.OnAwake();
_cancellationToken = gameObject.GetCancellationTokenOnDestroy();
}
public override async void OnStart()
{
base.OnStart();
visualElement.OnTabChanged += OnTabChanged;
foreach (var unityEvent in onEntryTab)
{
unityEvent.Invoke(false);
}
try
{
await UniTask.NextFrame(_cancellationToken);
OnTabChanged(visualElement.CurrentTab);
}
catch(OperationCanceledException){}
}
private void OnTabChanged(int obj)
{
if(onEntryTab.TryGetElementAt(tabBuffer.Current,out var unityEvent))
{
unityEvent.Invoke(false);
}
if(onEntryTab.TryGetElementAt(obj, out unityEvent))
{
unityEvent.Invoke(true);
}
if(onExit.TryGetElementAt(tabBuffer.Current,out var exitEvent))
{
exitEvent.Invoke();
}
if(onEntry.TryGetElementAt(obj, out var entryEvent))
{
entryEvent.Invoke();
}
tabBuffer.Release(obj);
}
}
}

View File

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

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
@@ -5,6 +6,7 @@ using BITKit;
using UnityEngine.Events;
namespace BITKit.UX
{
[Obsolete]
public class UXWindowEvent : MonoBehaviour, IWindowComponent
{
public UnityEvent<bool> onSetActive = new();