201 lines
6.3 KiB
C#
201 lines
6.3 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Octopus.Simulator.UI.Popup
|
|
{
|
|
[RequireComponent(typeof(CanvasGroup))]
|
|
public class UIAlert : MonoBehaviour
|
|
{
|
|
private string title;
|
|
private string message;
|
|
|
|
private string okButtonText = "확인";
|
|
private string cancelButtonText = "취소";
|
|
|
|
private bool showCancelButton = false;
|
|
|
|
public UnityEvent OnOk;
|
|
public UnityEvent OnCancel;
|
|
|
|
private Button okButton;
|
|
private Button cancelButton;
|
|
private TextMeshProUGUI titleText;
|
|
private TextMeshProUGUI messageText;
|
|
private CanvasGroup canvasGroup;
|
|
|
|
private bool animatting = false;
|
|
|
|
private float target = 0;
|
|
private float duration = 0.25f;
|
|
private float alpha = 1;
|
|
|
|
private bool useKeyboard = false;
|
|
|
|
public void Init(string title, string message, string okButtonText = "확인", string cancelButtonText = "취소", bool showCancelButton = true)
|
|
{
|
|
|
|
this.title = title;
|
|
this.message = message;
|
|
this.okButtonText = okButtonText;
|
|
this.cancelButtonText = cancelButtonText;
|
|
this.showCancelButton = showCancelButton;
|
|
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
|
|
var okBtn = GetComponentInChildren<UIPopupButtonOk>();
|
|
var cancelBtn = GetComponentInChildren<UIPopupButtonCancel>();
|
|
var titleTxt = GetComponentInChildren<UIPopupTitle>();
|
|
var messageTxt = GetComponentInChildren<UIPopupMessage>();
|
|
|
|
|
|
if (okButton == null) okButton = okBtn.GetComponent<Button>();
|
|
|
|
if (okButton != null)
|
|
{
|
|
if (useKeyboard) okButton.navigation = new Navigation() { mode = Navigation.Mode.None };
|
|
okButton.GetComponentInChildren<TextMeshProUGUI>().text = okButtonText;
|
|
okButton.onClick.AddListener(() =>
|
|
{
|
|
if (OnOk != null) OnOk.Invoke();
|
|
Close();
|
|
});
|
|
}
|
|
|
|
if (cancelButton == null) cancelButton = cancelBtn.GetComponent<Button>();
|
|
|
|
if (cancelButton != null)
|
|
{
|
|
if (useKeyboard) cancelButton.navigation = new Navigation() { mode = Navigation.Mode.None };
|
|
if (showCancelButton)
|
|
{
|
|
cancelButton.gameObject.SetActive(true);
|
|
cancelButton.GetComponentInChildren<TextMeshProUGUI>().text = cancelButtonText;
|
|
cancelButton.onClick.AddListener(() =>
|
|
{
|
|
if (OnCancel != null) OnCancel.Invoke();
|
|
Close();
|
|
});
|
|
}
|
|
else
|
|
{
|
|
cancelButton.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
if (titleTxt != null)
|
|
{
|
|
titleText = titleTxt.GetComponent<TextMeshProUGUI>();
|
|
titleText.text = title;
|
|
}
|
|
|
|
if (messageTxt != null)
|
|
{
|
|
messageText = messageTxt.GetComponent<TextMeshProUGUI>();
|
|
messageText.text = message;
|
|
}
|
|
|
|
canvasGroup.alpha = 0;
|
|
canvasGroup.interactable = false;
|
|
canvasGroup.blocksRaycasts = false;
|
|
}
|
|
|
|
public void Open()
|
|
{
|
|
canvasGroup.interactable = true;
|
|
canvasGroup.blocksRaycasts = true;
|
|
target = 1;
|
|
alpha = canvasGroup.alpha;
|
|
animatting = true;
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
target = 0;
|
|
alpha = canvasGroup.alpha;
|
|
animatting = true;
|
|
}
|
|
|
|
|
|
void LateUpdate()
|
|
{
|
|
if (animatting)
|
|
{
|
|
if (duration < 0)
|
|
{
|
|
duration = 0.001f;
|
|
}
|
|
|
|
alpha = Mathf.MoveTowards(alpha, target, (1 / duration) * Time.deltaTime);
|
|
canvasGroup.alpha = alpha;
|
|
if (alpha == target)
|
|
{
|
|
if (target == 0)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
animatting = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (alpha == 1 && target == 1 && useKeyboard)
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
if (showCancelButton)
|
|
{
|
|
cancelButton.OnPointerDown(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
|
|
}
|
|
}
|
|
else if (Input.GetKeyUp(KeyCode.Escape))
|
|
{
|
|
if (showCancelButton)
|
|
{
|
|
cancelButton.OnPointerUp(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
|
|
}
|
|
if (OnCancel != null) OnCancel.Invoke();
|
|
Close();
|
|
}
|
|
else if (Input.GetKeyDown(KeyCode.Space))
|
|
{
|
|
okButton.OnPointerDown(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
|
|
}
|
|
else if (Input.GetKeyUp(KeyCode.Space))
|
|
{
|
|
okButton.OnPointerUp(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
|
|
if (OnOk != null) OnOk.Invoke();
|
|
Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private void OnDestroy()
|
|
{
|
|
if (okButton != null)
|
|
{
|
|
okButton.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
if (cancelButton != null)
|
|
{
|
|
cancelButton.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
if (OnOk != null)
|
|
{
|
|
OnOk.RemoveAllListeners();
|
|
OnOk = null;
|
|
}
|
|
|
|
if (OnCancel != null)
|
|
{
|
|
OnCancel.RemoveAllListeners();
|
|
OnCancel = null;
|
|
}
|
|
}
|
|
}
|
|
}
|