1
This commit is contained in:
@@ -13,6 +13,19 @@ namespace BITKit.UX
|
||||
public string message = "message";
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public sealed class UXAlertPopup : IUXPopup
|
||||
{
|
||||
public void Popup(string content, float duration = 3f)
|
||||
{
|
||||
Alert.Print(new AlertMessage()
|
||||
{
|
||||
title = "Alert",
|
||||
message = content
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public static class Alert
|
||||
{
|
||||
public static void Print(AlertMessage message)
|
||||
|
@@ -22,7 +22,7 @@ namespace BITKit.UX
|
||||
this.visualElement = visualElement;
|
||||
contextLabel = visualElement.Q<Label>(UXConstant.ContextLabel);
|
||||
titleLabel = visualElement.Q<Label>(UXConstant.TitleLabel);
|
||||
numberLabel = visualElement.Q<Label>(UXConstant.TitleLabel);
|
||||
numberLabel = visualElement.Q<Label>(UXConstant.NumberLabel);
|
||||
descriptionLabel = visualElement.Q<Label>(UXConstant.DescriptionLabel);
|
||||
button = visualElement.Q<Button>(UXConstant.MainButton);
|
||||
secButton = visualElement.Q<Button>(UXConstant.SecButton);
|
||||
|
83
Assets/BITKit/Unity/Scripts/UX/Popup/UXPopup.cs
Normal file
83
Assets/BITKit/Unity/Scripts/UX/Popup/UXPopup.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITKit;
|
||||
using BITKit.UX;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
[Serializable]
|
||||
public class UXPopupSelector:IUXPopup
|
||||
{
|
||||
[SerializeReference,SubclassSelector] private IReference popupPath;
|
||||
public void Popup(string content, float duration = 3)
|
||||
{
|
||||
UXPopup.Dictionary[popupPath.Value].Popup(content,duration);
|
||||
}
|
||||
}
|
||||
public sealed class UXPopup : MonoBehaviour,IUXPopup
|
||||
{
|
||||
internal static readonly Dictionary<string,UXPopup> Dictionary=new();
|
||||
|
||||
[SerializeField] private UIDocument document;
|
||||
[SerializeReference, SubclassSelector] private IReference containerPath;
|
||||
[SerializeReference, SubclassSelector] private IReference popupPath;
|
||||
[SerializeField] private VisualTreeAsset popupTemplate;
|
||||
|
||||
private VisualElement container;
|
||||
|
||||
private readonly List<ViewModel> instances = new();
|
||||
|
||||
private class ViewModel
|
||||
{
|
||||
public VisualElement visualElement;
|
||||
public float exitTime;
|
||||
public bool disposed;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
container = document.rootVisualElement.Q<VisualElement>(containerPath.Value);
|
||||
|
||||
Dictionary.Add(popupPath.Value,this);
|
||||
|
||||
destroyCancellationToken.Register(() => Dictionary.Remove(popupPath.Value));
|
||||
|
||||
container.Clear();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
foreach (var x in instances.Where(x=>Time.time > x.exitTime))
|
||||
{
|
||||
x.visualElement.RemoveFromHierarchy();
|
||||
x.visualElement.SetOpacity(
|
||||
Mathf.MoveTowards(x.visualElement.GetOpacity(),0,Time.deltaTime)
|
||||
);
|
||||
x.disposed = x.visualElement.GetOpacity() is 0;
|
||||
}
|
||||
instances.RemoveAll(x =>x.disposed);
|
||||
|
||||
container.SetActive(container.childCount > 0);
|
||||
}
|
||||
|
||||
public void Popup(string content, float duration = 3)
|
||||
{
|
||||
var visualElement =new UXContainer(container.Create<VisualElement>(popupTemplate.CloneTree))
|
||||
{
|
||||
contextLabel =
|
||||
{
|
||||
text = content
|
||||
}
|
||||
};
|
||||
instances.Add(new ViewModel()
|
||||
{
|
||||
visualElement = visualElement,
|
||||
exitTime = Time.time + duration
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user