using System; using System.Collections; using System.Collections.Generic; using System.Threading; using Cysharp.Threading.Tasks; using UnityEngine; 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 : UIToolkitOverlay,IUXDialogue { public static UXAlert Singleton; public UXAlert(IUXService uxService, CancellationTokenSource cancellationToken) : base(uxService, cancellationToken) { Singleton = this; } protected override string DocumentPath => "ux_global_alert"; [UXBindPath("title-label")] private Label _titleLabel; [UXBindPath("context-label")] private TextElement _contextLabel; [UXBindPath("confirm-button")] private Button _confirmButton; [UXBindPath("cancel-button")] private Button _cancelButton; public override async UniTask InitializeAsync() { await base.InitializeAsync(); UXUtils.Inject(this); } internal async void PrintAlertMessage(AlertMessage message) { if(CancellationToken.IsCancellationRequested)return; BITAppForUnity.AllowCursor.AddElement(this); await InitializeAsync(); _titleLabel.text = message.title; _contextLabel.text = message.message; _confirmButton.clickable = null; _cancelButton.clickable = null; _confirmButton.clicked += Close; _cancelButton.clicked += Close; if (message.OnConfirm is not null) { _confirmButton.SetActive(true); _cancelButton.SetActive(false); _confirmButton.clicked += message.OnConfirm; }else if (message.OnChoice is not null) { _confirmButton.SetActive(true); _cancelButton.SetActive(true); _confirmButton.clicked += () => message.OnChoice.Invoke(true); _cancelButton.clicked += () => message.OnChoice.Invoke(false); } else { _confirmButton.SetActive(true); _cancelButton.SetActive(false); } } private void Close() { Dispose(); BITAppForUnity.AllowCursor.RemoveElement(this); } public async void Show(string content, string title = "Alert", Action confirmAction = null, Action onChoose = null) { PrintAlertMessage(new AlertMessage() { title = title, message = content, OnConfirm = confirmAction, OnChoice = onChoose }); } } }