1
This commit is contained in:
@@ -2,17 +2,81 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem.Composites;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITKit.UX
|
||||
{
|
||||
[System.Serializable]
|
||||
[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
|
||||
});
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class Alert
|
||||
{
|
||||
public static void Print(AlertMessage message)
|
||||
@@ -33,28 +97,59 @@ namespace BITKit.UX
|
||||
{
|
||||
internal static UXAlert Singleton;
|
||||
[SerializeField] private UIDocument document;
|
||||
Label titleLabel;
|
||||
Label contextLabel;
|
||||
Button comfirmButton;
|
||||
private void Awake()
|
||||
|
||||
[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<IUXDialogue,UnityDialogue>();
|
||||
|
||||
Singleton = this;
|
||||
var container = document.rootVisualElement;
|
||||
titleLabel = container.Q<Label>(UXConstant.TitleLabel);
|
||||
contextLabel = container.Q<Label>(UXConstant.ContextLabel);
|
||||
comfirmButton = container.Q<Button>(UXConstant.MainButton);
|
||||
comfirmButton.clicked += Close;
|
||||
UXUtils.Inject(this);
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
internal void PrintAlertMessage(AlertMessage message)
|
||||
{
|
||||
document.rootVisualElement.SetActive(true);
|
||||
titleLabel.text = message.title;
|
||||
contextLabel.text = message.message;
|
||||
_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);
|
||||
}
|
||||
void Close()
|
||||
|
||||
private void Close()
|
||||
{
|
||||
_comfirmButton.clickable = null;
|
||||
_cancelButton.clickable = null;
|
||||
|
||||
document.rootVisualElement.SetActive(false);
|
||||
BITAppForUnity.AllowCursor.RemoveElement(this);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user