74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UIElements;
|
||
|
using Cysharp.Threading.Tasks;
|
||
|
using System;
|
||
|
namespace BITKit.UX
|
||
|
{
|
||
|
public class UXBar : UXElement<VisualElement>, IProvider<float>
|
||
|
{
|
||
|
[Header(Constant.Header.Settings)]
|
||
|
public string fillElementName;
|
||
|
public string labelName;
|
||
|
[Header(Constant.Header.Output)]
|
||
|
public Provider onValueChanged;
|
||
|
[Header(Constant.Header.InternalVariables)]
|
||
|
VisualElement fillElement;
|
||
|
Label labelElement;
|
||
|
public override void OnStart()
|
||
|
{
|
||
|
fillElement = visualElement.Q<VisualElement>(fillElementName);
|
||
|
labelElement = visualElement.Q<Label>(labelName);
|
||
|
if (visualElement is INotifyValueChanged<float> iNotify)
|
||
|
{
|
||
|
iNotify.RegisterValueChangedCallback(OnValueChanged);
|
||
|
}
|
||
|
}
|
||
|
public void Set(int value)
|
||
|
{
|
||
|
float w = Mathf.Clamp((float)value / 100 * 100, 0f, 100f);
|
||
|
Set(w);
|
||
|
}
|
||
|
public async void Set(float value)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
await UniTask.SwitchToMainThread(gameObject.GetCancellationTokenOnDestroy());
|
||
|
switch (visualElement)
|
||
|
{
|
||
|
case INotifyValueChanged<float> iNotify:
|
||
|
iNotify.SetValueWithoutNotify(value);
|
||
|
break;
|
||
|
default:
|
||
|
value *= 100;
|
||
|
fillElement.style.width = new StyleLength(Length.Percent(value));
|
||
|
break;
|
||
|
}
|
||
|
if (labelElement is not null)
|
||
|
labelElement.text = value.ToString();
|
||
|
}
|
||
|
catch (System.Exception e)
|
||
|
{
|
||
|
if (e is not OperationCanceledException)
|
||
|
throw;
|
||
|
}
|
||
|
}
|
||
|
public async void SetDirect(float progess,string label)
|
||
|
{
|
||
|
await UniTask.SwitchToMainThread(gameObject.GetCancellationTokenOnDestroy());
|
||
|
Set(progess);
|
||
|
labelElement.text = label;
|
||
|
}
|
||
|
float IProvider<float>.Get()
|
||
|
{
|
||
|
throw new System.NotImplementedException();
|
||
|
}
|
||
|
|
||
|
void OnValueChanged(ChangeEvent<float> changeEvent)
|
||
|
{
|
||
|
if(onValueChanged is not null)
|
||
|
onValueChanged.Set(changeEvent.newValue);
|
||
|
}
|
||
|
}
|
||
|
}
|