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

155 lines
4.7 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2024-03-31 23:31:00 +08:00
using UnityEngine.InputSystem.Composites;
2023-06-05 19:57:17 +08:00
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-06-12 17:38:05 +08:00
public class UXAlert : MonoBehaviourSingleton<UXAlert>
2023-06-05 19:57:17 +08:00
{
[SerializeField] private UIDocument document;
2024-03-31 23:31:00 +08:00
[UXBindPath(UXConstant.TitleLabel)]
private Label _titleLabel;
[UXBindPath(UXConstant.ContextLabel)]
private TextElement _contextLabel;
[UXBindPath(UXConstant.MainButton)]
2024-06-12 17:38:05 +08:00
private Button _confirmButton;
2024-03-31 23:31:00 +08:00
[UXBindPath(UXConstant.SecButton)]
private Button _cancelButton;
private void Start()
2023-06-05 19:57:17 +08:00
{
2024-03-31 23:31:00 +08:00
DI.Register<IUXDialogue,UnityDialogue>();
UXUtils.Inject(this);
2023-06-05 19:57:17 +08:00
Close();
}
2024-03-31 23:31:00 +08:00
2023-06-05 19:57:17 +08:00
internal void PrintAlertMessage(AlertMessage message)
{
document.rootVisualElement.SetActive(true);
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);
}
BITAppForUnity.AllowCursor.AddElement(this);
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
{
document.rootVisualElement.SetActive(false);
2024-03-31 23:31:00 +08:00
BITAppForUnity.AllowCursor.RemoveElement(this);
2023-06-05 19:57:17 +08:00
}
}
}