221 lines
7.0 KiB
C#
221 lines
7.0 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Studio.UI.Popup
|
|
{
|
|
|
|
public class UIPopupEventArgs : EventArgs
|
|
{
|
|
public readonly object Data;
|
|
|
|
public UIPopupEventArgs(object data)
|
|
{
|
|
Data = data;
|
|
}
|
|
}
|
|
|
|
[RequireComponent(typeof(CanvasGroup))]
|
|
public class UIPopup : UnityEngine.MonoBehaviour
|
|
{
|
|
|
|
private string title = "";
|
|
private IUIPopupContent content;
|
|
|
|
private string okButtonText = "확인";
|
|
private string cancelButtonText = "취소";
|
|
|
|
private bool showCancelButton = true;
|
|
|
|
private Button okButton;
|
|
private Button cancelButton;
|
|
private TextMeshProUGUI titleText;
|
|
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 EventHandler<UIPopupEventArgs> OnOk { get; set; }
|
|
public EventHandler<EventArgs> OnCancel { get; set; }
|
|
|
|
|
|
public void Init(string title, string okButtonText = "확인", string cancelButtonText = "취소", bool showCancelButton = true)
|
|
{
|
|
|
|
this.title = title;//LocaleService.Instance.GetLocalizedString(title);
|
|
this.okButtonText = okButtonText;//LocaleService.Instance.GetLocalizedString(okButtonText);
|
|
this.cancelButtonText = cancelButtonText;//LocaleService.Instance.GetLocalizedString(cancelButtonText);
|
|
this.showCancelButton = showCancelButton;
|
|
|
|
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
|
|
var okBtn = GetComponentInChildren<UIPopupButtonOk>();
|
|
var cancelBtn = GetComponentInChildren<UIPopupButtonCancel>();
|
|
var titleTxt = GetComponentInChildren<UIPopupTitle>();
|
|
content = GetComponentInChildren<IUIPopupContent>();
|
|
|
|
if (okBtn != null) okButton = okBtn.GetComponent<Button>();
|
|
if (okButton != null)
|
|
{
|
|
if(useKeyboard) okButton.navigation = new Navigation() { mode = Navigation.Mode.None };
|
|
okButton.GetComponentInChildren<TextMeshProUGUI>().text = this.okButtonText;
|
|
okButton.onClick.AddListener(() =>
|
|
{
|
|
if (content != null && content.IsOkable)
|
|
{
|
|
if (OnOk != null) OnOk.Invoke(this, new UIPopupEventArgs(content.GetResult()));
|
|
Close();
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
if (cancelBtn != null) cancelButton = cancelBtn.GetComponent<Button>();
|
|
|
|
if (cancelButton != null)
|
|
{
|
|
if (useKeyboard) cancelButton.navigation = new Navigation() { mode = Navigation.Mode.None };
|
|
if (this.showCancelButton)
|
|
{
|
|
cancelButton.gameObject.SetActive(true);
|
|
cancelButton.GetComponentInChildren<TextMeshProUGUI>().text = this.cancelButtonText;
|
|
cancelButton.onClick.AddListener(() =>
|
|
{
|
|
if (content != null && content.IsCancelable)
|
|
{
|
|
if (OnCancel != null) OnCancel.Invoke(this, EventArgs.Empty);
|
|
Close();
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
cancelButton.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
if (titleTxt != null)
|
|
{
|
|
titleText = titleTxt.GetComponent<TextMeshProUGUI>();
|
|
titleText.text = this.title;
|
|
}
|
|
|
|
|
|
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 Update()
|
|
{
|
|
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 (content != null && content.IsCancelable)
|
|
{
|
|
if (showCancelButton)
|
|
{
|
|
cancelButton.OnPointerUp(new UnityEngine.EventSystems.PointerEventData(EventSystem.current));
|
|
}
|
|
if (OnCancel != null) OnCancel.Invoke(this, EventArgs.Empty);
|
|
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 (content != null && content.IsOkable)
|
|
{
|
|
if (OnOk != null) OnOk.Invoke(this, new UIPopupEventArgs(content.GetResult()));
|
|
Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (okButton != null)
|
|
{
|
|
okButton.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
if (cancelButton != null)
|
|
{
|
|
cancelButton.onClick.RemoveAllListeners();
|
|
}
|
|
|
|
if (OnOk != null)
|
|
{
|
|
OnOk = null;
|
|
}
|
|
|
|
if (OnCancel != null)
|
|
{
|
|
OnCancel = null;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|