BITKit/Src/Unity/Scripts/UX/Alert/UXAlert.cs

173 lines
5.3 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-11-03 16:38:17 +08:00
using System.Threading;
using Cysharp.Threading.Tasks;
2023-06-05 19:57:17 +08:00
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
2024-03-31 23:31:00 +08:00
[Serializable]
2023-06-05 19:57:17 +08:00
public record AlertMessage
{
public string title = "Alert";
public string message = "message";
2024-03-31 23:31:00 +08:00
public Action OnConfirm;
public Action<bool> OnChoice;
2023-06-05 19:57:17 +08:00
}
2024-03-31 23:31:00 +08:00
[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<bool> 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;
}
}
}
2023-06-05 19:57:17 +08:00
public static class Alert
{
public static void Print(AlertMessage message)
{
UXAlert.Singleton.PrintAlertMessage(message);
}
2023-10-06 23:43:19 +08:00
public static void Print(string title, string content)
{
Print(new AlertMessage()
{
title = title,
message = content
});
}
2023-06-05 19:57:17 +08:00
}
2024-11-03 16:38:17 +08:00
public class UXAlert : UIToolkitOverlay,IUXDialogue
2023-06-05 19:57:17 +08:00
{
2024-11-03 16:38:17 +08:00
public static UXAlert Singleton;
public UXAlert(IUXService uxService, CancellationTokenSource cancellationToken) : base(uxService, cancellationToken)
{
Singleton = this;
}
protected override string DocumentPath => "ux_global_alert";
2024-03-31 23:31:00 +08:00
2024-08-13 18:42:51 +08:00
[UXBindPath("title-label")]
2024-03-31 23:31:00 +08:00
private Label _titleLabel;
2024-08-13 18:42:51 +08:00
[UXBindPath("context-label")]
2024-03-31 23:31:00 +08:00
private TextElement _contextLabel;
2024-08-13 18:42:51 +08:00
[UXBindPath("confirm-button")]
2024-06-12 17:38:05 +08:00
private Button _confirmButton;
2024-08-13 18:42:51 +08:00
[UXBindPath("cancel-button")]
2024-03-31 23:31:00 +08:00
private Button _cancelButton;
2024-11-03 16:38:17 +08:00
public override async UniTask InitializeAsync()
2023-06-05 19:57:17 +08:00
{
2024-11-03 16:38:17 +08:00
await base.InitializeAsync();
2024-03-31 23:31:00 +08:00
UXUtils.Inject(this);
2024-06-14 16:16:13 +08:00
}
2024-11-03 16:38:17 +08:00
internal async void PrintAlertMessage(AlertMessage message)
2023-06-05 19:57:17 +08:00
{
2024-11-03 16:38:17 +08:00
if(CancellationToken.IsCancellationRequested)return;
BITAppForUnity.AllowCursor.AddElement(this);
await InitializeAsync();
2024-06-14 16:16:13 +08:00
2024-03-31 23:31:00 +08:00
_titleLabel.text = message.title;
_contextLabel.text = message.message;
2024-06-12 17:38:05 +08:00
_confirmButton.clickable = null;
_cancelButton.clickable = null;
_confirmButton.clicked += Close;
2024-03-31 23:31:00 +08:00
_cancelButton.clicked += Close;
2024-06-12 17:38:05 +08:00
2024-03-31 23:31:00 +08:00
if (message.OnConfirm is not null)
{
2024-06-12 17:38:05 +08:00
_confirmButton.SetActive(true);
_cancelButton.SetActive(false);
_confirmButton.clicked += message.OnConfirm;
2024-03-31 23:31:00 +08:00
}else if (message.OnChoice is not null)
{
2024-06-12 17:38:05 +08:00
_confirmButton.SetActive(true);
_cancelButton.SetActive(true);
_confirmButton.clicked += () => message.OnChoice.Invoke(true);
2024-03-31 23:31:00 +08:00
_cancelButton.clicked += () => message.OnChoice.Invoke(false);
}
else
{
2024-06-12 17:38:05 +08:00
_confirmButton.SetActive(true);
2024-03-31 23:31:00 +08:00
_cancelButton.SetActive(false);
}
2024-11-03 16:38:17 +08:00
2023-06-05 19:57:17 +08:00
}
2024-03-31 23:31:00 +08:00
private void Close()
2023-06-05 19:57:17 +08:00
{
2024-11-03 16:38:17 +08:00
Dispose();
2024-03-31 23:31:00 +08:00
BITAppForUnity.AllowCursor.RemoveElement(this);
2023-06-05 19:57:17 +08:00
}
2024-12-25 11:35:30 +08:00
public async void Show(string content, string title = "Alert", Action confirmAction = null, Action<bool> onChoose = null)
2024-11-03 16:38:17 +08:00
{
PrintAlertMessage(new AlertMessage()
{
title = title,
message = content,
OnConfirm = confirmAction,
OnChoice = onChoose
});
}
2023-06-05 19:57:17 +08:00
}
}