This commit is contained in:
CortexCore
2023-08-11 23:57:37 +08:00
parent 936a94c84b
commit 75889ec34f
149 changed files with 6524 additions and 1043 deletions

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit.UX;
using UnityEngine;
using UnityEngine.UIElements;
public interface IVisualElementProvider
{
VisualElement GetVisualElement();
T GetVisualElement<T>() where T : VisualElement;
}
[Serializable]
public class GetVisualElementFromUXElement:IVisualElementProvider
{
[SerializeField] private UXElement _uxElement;
public VisualElement GetVisualElement() => _uxElement.GetVisualElement();
public T GetVisualElement<T>() where T : VisualElement => GetVisualElement() as T;
}
[Serializable]
public class GetVisualElementFromUIDocument:IVisualElementProvider
{
[SerializeField] private UIDocument document;
[SerializeField] private string path;
public VisualElement GetVisualElement()
{
return document.rootVisualElement.Q(path);
}
public T GetVisualElement<T>() where T : VisualElement=> GetVisualElement() as T;
}

View File

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

View File

@@ -0,0 +1,35 @@
using System.Collections;
using System.Collections.Generic;
using BITKit.UX;
using UnityEngine;
using UnityEngine.UIElements;
public class UXBuilder : MonoBehaviour
{
[SerializeField, SerializeReference, SubclassSelector]
private IVisualElementProvider visualElementProvider;
[SerializeField] private VisualTreeAsset visualTreeAsset;
private readonly List<VisualElement> instances = new();
public T Build<T>() where T : VisualElement
{
var clone = visualTreeAsset.CloneTree()[0];
visualElementProvider.GetVisualElement().Add(clone);
instances.Add(clone);
return clone as T;
}
public UXContainer BuildAsContainer() => new(Build<VisualElement>());
public void Clear()
{
foreach (var x in instances)
{
visualElementProvider.GetVisualElement().Remove(x);
}
instances.Clear();
}
}

View File

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

View File

@@ -19,6 +19,16 @@ namespace BITKit.UX
public const string Icon = "icon-image";
public const string ContextListView = "context-listview";
public const string Inspector = "inspector-container";
public const string ProcessBarFill = "fill-bar";
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

@@ -7,24 +7,31 @@ namespace BITKit.UX
public class UXContainer
{
public static implicit operator VisualElement(UXContainer self) => self.visualElement;
public Label contextlabel;
public Label titleLabel;
public Label descriptionLabel;
public Label numberLabel;
public Button button;
public Button secButton;
public VisualElement visualElement;
public VisualElement icon;
public readonly Label contextLabel;
public readonly Label titleLabel;
public readonly Label descriptionLabel;
public readonly Label numberLabel;
public readonly Button button;
public readonly Button secButton;
public readonly VisualElement visualElement;
public readonly VisualElement container;
public readonly VisualElement icon;
public UXContainer(VisualElement visualElement)
{
this.visualElement = visualElement;
contextlabel = visualElement.Q<Label>(UXConstant.ContextLabel);
contextLabel = visualElement.Q<Label>(UXConstant.ContextLabel);
titleLabel = visualElement.Q<Label>(UXConstant.TitleLabel);
numberLabel = visualElement.Q<Label>(UXConstant.TitleLabel);
descriptionLabel = visualElement.Q<Label>(UXConstant.DescriptionLabel);
button = visualElement.Q<Button>(UXConstant.MainButton);
secButton = visualElement.Q<Button>(UXConstant.SecButton);
icon = visualElement.Q(UXConstant.Icon);
container = this.visualElement.Q(UXConstant.ContextContainer);
}
public T Get<T>(int index) where T : VisualElement => visualElement.Q<T>($"{typeof(T).Name}--{index}");
public void SetProcess(float process)
{
visualElement.Q(UXConstant.ProcessBarFill).style.width =Length.Percent(Mathf.Clamp(process * 100,0,100)) ;
}
}
}

View File

@@ -12,6 +12,7 @@ namespace BITKit.UX
protected bool isActive = true;
public virtual bool Get() => isActive;
public virtual void Set(bool active) { }
public virtual void OnAwake(){}
public virtual void OnStart() { }
public override void Set<T>(T obj)
{
@@ -30,25 +31,26 @@ namespace BITKit.UX
public virtual void SetPosition(Vector3 worldPosition)
{
if (Camera.main != null)
{
var cameraTrans = Camera.main.transform;
var visualElement = GetVisualElement();
var pos = RuntimePanelUtils
.CameraTransformWorldToPanel(visualElement.panel, worldPosition, Camera.main);
SetPosition(ref pos);
pos.x = (pos.x - visualElement.layout.width / 2);
visualElement.style.left = 0;
visualElement.style.top = 0;
visualElement.style.right = new StyleLength(StyleKeyword.Auto);
visualElement.style.bottom = new StyleLength(StyleKeyword.Auto);
visualElement.style.position = Position.Absolute;
visualElement.transform.position = pos;
visualElement.SetOpacity(Vector3.Dot(cameraTrans.forward, worldPosition - cameraTrans.position) > 0 ? 1 : 0);
}
GetVisualElement().SetPosition(worldPosition);
// if (Camera.main != null)
// {
// var cameraTrans = Camera.main.transform;
// var visualElement = GetVisualElement();
// var pos = RuntimePanelUtils
// .CameraTransformWorldToPanel(visualElement.panel, worldPosition, Camera.main);
//
// SetPosition(ref pos);
//
// pos.x = (pos.x - visualElement.layout.width / 2);
//
// visualElement.style.left = 0;
// visualElement.style.top = 0;
// visualElement.style.right = new StyleLength(StyleKeyword.Auto);
// visualElement.style.bottom = new StyleLength(StyleKeyword.Auto);
// visualElement.style.position = Position.Absolute;
// visualElement.transform.position = pos;
// visualElement.SetOpacity(Vector3.Dot(cameraTrans.forward, worldPosition - cameraTrans.position) > 0 ? 1 : 0);
// }
}
protected virtual void SetPosition(ref Vector2 position)
{
@@ -129,12 +131,13 @@ namespace BITKit.UX
visualElement.SetEnabled(active);
}
}
void Awake()
private void Awake()
{
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
OnAwake();
OnStart();
}
void OnValidate()
private void OnValidate()
{
if (bindNameProvider is GetNameFromGameobject x)
{

View File

@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
public class UXListView : UXElement<ListView>
{
}
}

View File

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

View File

@@ -2,13 +2,13 @@
"name": "BITKit.UX.OnScreen",
"rootNamespace": "",
"references": [
"GUID:a209c53514018594f9f482516f2a6781",
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:2bafac87e7f4b9b418d9448d219b01ab",
"GUID:be17a8778dbfe454890ed8279279e153",
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:9400d40641bab5b4a9702f65bf5c6eb5"
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:517785bb4600a5140b47eac5fa49b8fc"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -0,0 +1,124 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;
using UnityEngine.UIElements;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace BITKit.UX
{
[Serializable]
public class AllowTouchWhenPointerNotOverUI : ICondition
{
public bool OnCheck() => UXAllowTouch.Singleton.AllowTouch;
}
/*
* 当指针进入区域时启用输入
* 当指针离开区域时禁用输入,直到所有设备都没有按下,才会再次启用输入
*/
/// <summary>
/// Allow touch when pointer not over UI
/// </summary>
public class UXAllowTouch : UXElement<VisualElement>, ICondition
{
internal static UXAllowTouch Singleton { get; private set; }
public bool AllowTouch;
internal bool IsHoveringUI { get; private set; }
public bool IsTouching { get; private set; }
private int updateRequest;
private CancellationTokenSource cancellationTokenSource;
public override void OnAwake()
{
Singleton = this;
EnhancedTouchSupport.Enable();
Touch.onFingerUp += OnFingerUp;
Touch.onFingerDown += OnFingerDown;
cancellationTokenSource = new CancellationTokenSource();
}
private void OnDestroy()
{
cancellationTokenSource.Cancel();
}
private async void OnFingerDown(Finger obj)
{
IsTouching = true;
try
{
await Task.Delay(TimeSpan.FromSeconds(Time.deltaTime*2));
AllowTouch = !IsHoveringUI;
}
catch (OperationCanceledException)
{
}
}
private void OnFingerUp(Finger obj)
{
IsTouching = false;
}
public override void OnStart()
{
base.OnStart();
visualElement.RegisterCallback<PointerEnterEvent>(OnPointerEnter);
visualElement.RegisterCallback<PointerLeaveEvent>(OnPointerLevel);
}
private void OnPointerLevel(PointerLeaveEvent evt)
{
IsHoveringUI = true;
if (isMouseInput) AllowTouch = false;
}
private void OnPointerEnter(PointerEnterEvent evt)
{
IsHoveringUI = false;
if (isMouseInput) AllowTouch = true;
}
public bool OnCheck() => AllowTouch;
private bool isMouseInput => Mouse.current.delta.ReadValue().sqrMagnitude > 0;
}
#if UNITY_EDITOR
[CustomEditor(typeof(UXAllowTouch))]
public class UXAllowTouchInspector:BITInspector<UXAllowTouch>
{
private Label allowTouchLabel;
private Label isHoveringUILabel;
private Label isTouching;
public override VisualElement CreateInspectorGUI()
{
FillDefaultInspector();
CreateSubTitle("Editor");
allowTouchLabel = root.Create<Label>();
isHoveringUILabel = root.Create<Label>();
isTouching = root.Create<Label>();
return root;
}
protected override void OnUpdate()
{
if (allowTouchLabel is null) return;
allowTouchLabel.text = agent.AllowTouch?"Allow Touch":"Block Touch";
isHoveringUILabel.text = agent.IsHoveringUI ? "Is Hovering UI" : "Not Hovering UI";
isTouching.text = agent.IsTouching ? "Is Touching" : "Not Touching";
allowTouchLabel.style.color = agent.AllowTouch?Color.green:Color.red;
isHoveringUILabel.style.color = agent.IsHoveringUI?Color.red:Color.green;
isTouching.style.color = agent.IsTouching?Color.green:Color.red;
}
}
#endif
}

View File

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