230 lines
6.0 KiB
C#
230 lines
6.0 KiB
C#
using System;
|
|
using DG.Tweening;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
public class MessageBoxDialog : MonoBehaviour
|
|
{
|
|
public const float ANIMATION_DURATION = 0.25f;
|
|
/// <summary>Above game/menu/loader canvases; below reconnect (500).</summary>
|
|
const int OverlaySortingOrder = 400;
|
|
|
|
public static MessageBoxDialog instance;
|
|
public static bool IsAvailable => instance != null;
|
|
|
|
public RectTransform dimmer;
|
|
public RectTransform basicMessageBox;
|
|
|
|
[Header("MessageBox")]
|
|
public RectTransform messageBox;
|
|
public TMP_Text txtTitle, txtMessage;
|
|
public Button btnOk, btnYes, btnNo;
|
|
|
|
Image dimmerImg;
|
|
TMP_Text basicMessageText;
|
|
Action<bool> onYesOrNo;
|
|
Sequence showHideSequence;
|
|
Canvas overlayCanvas;
|
|
|
|
void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
overlayCanvas = GetComponent<Canvas>();
|
|
if (overlayCanvas != null)
|
|
overlayCanvas.sortingOrder = OverlaySortingOrder;
|
|
|
|
if (dimmer != null)
|
|
dimmerImg = dimmer.GetComponent<Image>();
|
|
if (basicMessageBox != null)
|
|
basicMessageText = basicMessageBox.GetComponentInChildren<TMP_Text>();
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
if (instance != this)
|
|
return;
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
if (instance != this)
|
|
return;
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
}
|
|
|
|
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
transform.SetAsLastSibling();
|
|
}
|
|
|
|
public static void ShowBasic(string title)
|
|
{
|
|
if (instance == null)
|
|
{
|
|
Debug.LogWarning("MessageBoxDialog missing: " + title);
|
|
return;
|
|
}
|
|
|
|
instance.ShowMessageBox(title);
|
|
}
|
|
|
|
public static void Show(string title, string message, Action<bool> onResult = null)
|
|
{
|
|
if (instance == null)
|
|
{
|
|
Debug.LogWarning("MessageBoxDialog missing: " + title);
|
|
return;
|
|
}
|
|
|
|
instance.ShowMessageBox(title, message, onResult);
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
KillTweens();
|
|
if (instance == this)
|
|
instance = null;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
Button basicButton = basicMessageBox != null ? basicMessageBox.GetComponent<Button>() : null;
|
|
if (basicButton != null)
|
|
basicButton.onClick.AddListener(HideMessageBox);
|
|
|
|
if (btnOk != null)
|
|
btnOk.onClick.AddListener(HideMessageBox);
|
|
if (btnYes != null)
|
|
btnYes.onClick.AddListener(() => OnQuestionDialog(true));
|
|
if (btnNo != null)
|
|
btnNo.onClick.AddListener(() => OnQuestionDialog(false));
|
|
}
|
|
|
|
void OnQuestionDialog(bool val)
|
|
{
|
|
Action<bool> callback = onYesOrNo;
|
|
onYesOrNo = null;
|
|
HideMessageBox();
|
|
callback?.Invoke(val);
|
|
}
|
|
|
|
public void ShowMessageBox(string title)
|
|
{
|
|
PrepareShow();
|
|
HideBoxImmediate(messageBox);
|
|
|
|
if (basicMessageText != null)
|
|
basicMessageText.text = title;
|
|
|
|
AnimateIn(basicMessageBox);
|
|
}
|
|
|
|
public void ShowMessageBox(string title, string message, Action<bool> onResult = null)
|
|
{
|
|
PrepareShow();
|
|
HideBoxImmediate(basicMessageBox);
|
|
|
|
txtTitle.text = title;
|
|
txtMessage.text = message;
|
|
|
|
bool isQuestion = onResult != null;
|
|
btnNo.gameObject.SetActive(isQuestion);
|
|
btnYes.gameObject.SetActive(isQuestion);
|
|
btnOk.gameObject.SetActive(!isQuestion);
|
|
|
|
onYesOrNo = onResult;
|
|
AnimateIn(messageBox);
|
|
}
|
|
|
|
public void HideMessageBox()
|
|
{
|
|
bool basicActive = basicMessageBox != null && basicMessageBox.gameObject.activeSelf;
|
|
bool fullActive = messageBox != null && messageBox.gameObject.activeSelf;
|
|
if (!basicActive && !fullActive)
|
|
return;
|
|
|
|
if (basicActive && fullActive)
|
|
HideBoxImmediate(basicMessageBox);
|
|
|
|
AnimateOut(basicActive && !fullActive ? basicMessageBox : messageBox);
|
|
}
|
|
|
|
void PrepareShow()
|
|
{
|
|
KillTweens();
|
|
onYesOrNo = null;
|
|
transform.SetAsLastSibling();
|
|
|
|
dimmer.gameObject.SetActive(true);
|
|
if (dimmerImg != null)
|
|
{
|
|
Color c = dimmerImg.color;
|
|
dimmerImg.color = new Color(c.r, c.g, c.b, 0f);
|
|
}
|
|
}
|
|
|
|
void HideBoxImmediate(RectTransform box)
|
|
{
|
|
if (box == null)
|
|
return;
|
|
|
|
box.DOKill();
|
|
box.localScale = Vector3.zero;
|
|
box.gameObject.SetActive(false);
|
|
}
|
|
|
|
void AnimateIn(RectTransform box)
|
|
{
|
|
box.localScale = Vector3.zero;
|
|
box.gameObject.SetActive(true);
|
|
|
|
showHideSequence = DOTween.Sequence().SetUpdate(true);
|
|
if (dimmerImg != null)
|
|
showHideSequence.Join(dimmerImg.DOFade(0.8f, ANIMATION_DURATION));
|
|
showHideSequence.Join(box.DOScale(1f, ANIMATION_DURATION).SetEase(Ease.OutBack));
|
|
}
|
|
|
|
void AnimateOut(RectTransform box)
|
|
{
|
|
KillTweens();
|
|
|
|
RectTransform targetBox = box;
|
|
showHideSequence = DOTween.Sequence().SetUpdate(true);
|
|
if (dimmerImg != null)
|
|
showHideSequence.Join(dimmerImg.DOFade(0f, ANIMATION_DURATION));
|
|
showHideSequence.Join(targetBox.DOScale(0f, ANIMATION_DURATION).SetEase(Ease.InBack));
|
|
showHideSequence.OnComplete(() =>
|
|
{
|
|
if (targetBox != null)
|
|
targetBox.gameObject.SetActive(false);
|
|
if (dimmer != null)
|
|
dimmer.gameObject.SetActive(false);
|
|
});
|
|
}
|
|
|
|
void KillTweens()
|
|
{
|
|
if (showHideSequence != null && showHideSequence.IsActive())
|
|
showHideSequence.Kill();
|
|
showHideSequence = null;
|
|
|
|
if (dimmerImg != null)
|
|
dimmerImg.DOKill();
|
|
if (basicMessageBox != null)
|
|
basicMessageBox.DOKill();
|
|
if (messageBox != null)
|
|
messageBox.DOKill();
|
|
}
|
|
}
|