using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem.Composites; using UnityEngine.UIElements; namespace BITKit.UX { [Serializable] public record AlertMessage { public string title = "Alert"; public string message = "message"; public Action OnConfirm; public Action OnChoice; } [Serializable] public sealed class UXAlertPopup : IUXPopup { public void Popup(string content, float duration = 3f) { Alert.Print(new AlertMessage() { title = "Alert", message = content }); } } [Serializable] public sealed class RequestOpenUrl : BITAppForUnity.OpenUrl { [SerializeReference,SubclassSelector] private AlertMessage alertMessage; public override void Execute() { Alert.Print(alertMessage with { OnConfirm = base.Execute }); } } public struct UnityDialogue : IUXDialogue { public void Show(string content, string title = "Alert", Action confirmAction = null, Action onChoose = null) { switch (confirmAction,onChoose) { case (null, null): Alert.Print(title, content); break; case (not null, null): Alert.Print(new AlertMessage() { title = title, message = content, OnConfirm = confirmAction }); break; case (null, not null): Alert.Print(new AlertMessage() { title = title, message = content, OnChoice = onChoose }); break; case (not null, not null): Alert.Print(new AlertMessage() { title = title, message = content, OnConfirm = confirmAction, OnChoice = onChoose }); break; } } } public static class Alert { public static void Print(AlertMessage message) { UXAlert.Singleton.PrintAlertMessage(message); } public static void Print(string title, string content) { Print(new AlertMessage() { title = title, message = content }); } } public class UXAlert : MonoBehaviour { internal static UXAlert Singleton; [SerializeField] private UIDocument document; [UXBindPath(UXConstant.TitleLabel)] private Label _titleLabel; [UXBindPath(UXConstant.ContextLabel)] private TextElement _contextLabel; [UXBindPath(UXConstant.MainButton)] private Button _comfirmButton; [UXBindPath(UXConstant.SecButton)] private Button _cancelButton; private void Start() { DI.Register(); Singleton = this; UXUtils.Inject(this); Close(); } internal void PrintAlertMessage(AlertMessage message) { document.rootVisualElement.SetActive(true); _titleLabel.text = message.title; _contextLabel.text = message.message; _comfirmButton.clicked += Close; _cancelButton.clicked += Close; _cancelButton.SetActive(true); if (message.OnConfirm is not null) { _comfirmButton.clicked += message.OnConfirm; }else if (message.OnChoice is not null) { _comfirmButton.clicked += () => message.OnChoice.Invoke(true); _cancelButton.clicked += () => message.OnChoice.Invoke(false); } else { _cancelButton.SetActive(false); } BITAppForUnity.AllowCursor.AddElement(this); } private void Close() { _comfirmButton.clickable = null; _cancelButton.clickable = null; document.rootVisualElement.SetActive(false); BITAppForUnity.AllowCursor.RemoveElement(this); } } }