iFactory.Cutting.Unity/Assets/BITKit/Unity/Scripts/UX/Alert/UXAlert.cs

155 lines
4.5 KiB
C#
Raw Normal View History

2024-01-23 02:56:26 +08:00
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<bool> 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
});
}
}
2024-03-04 18:45:21 +08:00
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;
}
}
}
2024-01-23 02:56:26 +08:00
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)]
2024-04-16 04:15:06 +08:00
private TextElement _contextLabel;
2024-01-23 02:56:26 +08:00
[UXBindPath(UXConstant.MainButton)]
private Button _comfirmButton;
[UXBindPath(UXConstant.SecButton)]
private Button _cancelButton;
private void Start()
{
2024-03-04 18:45:21 +08:00
DI.Register<IUXDialogue,UnityDialogue>();
2024-01-23 02:56:26 +08:00
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);
}
}
}