using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using GameCore; using asap.core; using DG.Tweening; using TMPro; public class MessageDialog : MonoBehaviour { public TextMeshProUGUI Title; public TextMeshProUGUI Content; public TextMeshProUGUI OkButtonText; public TextMeshProUGUI CancleButtonText; public RectTransform OkButton; public RectTransform CancleButton; public RectTransform FrameTrans; public GameObject kingImage; public GameObject kingCoinImage; public GameObject bearImage; public GameObject yellowButton; public GameObject infoButton; [HideInInspector] public UnityEvent OnOkButtonClick; [HideInInspector] public UnityEvent OnCancleButtonClick; [HideInInspector] public UnityEvent OnInfoButtonClick; private float _countDownTimer; private int _countDownTimerInt; private bool _isPreviousInputBlocked; private RectTransform _contentCachTrans; private float _frameBaseHeight; private float _contentBaseHeight; const int MAX_DIALOG_NUM = 10; static int sortingOrder = 100; static List _messageDialogList = new List(); static Stack _messageDialogStack = new Stack(); public enum DialogType { Normal, BearOK, BearInfo, KingCoin } public static MessageDialog ShowBear(string message, bool useCancle = false, string title = "", string ok = "", string cancle = "") { return Show(message, useCancle, title, ok, cancle, DialogType.BearOK); } public static MessageDialog ShowBearInfo(string message, bool useCancle = false, string title = "", string ok = "", string cancle = "") { return Show(message, useCancle, title, ok, cancle, DialogType.BearInfo); } public static MessageDialog ShowKingCoin(string message, bool useCancle = false, string title = "", string ok = "", string cancle = "") { return Show(message, useCancle, title, ok, cancle, DialogType.KingCoin); } public static MessageDialog ShowErrorDialog(int error_code, string error_reason) { var title = string.Empty; var msg = string.Empty; var button_label = string.Empty; if (error_code == 10001) { title = LocalizationMgr.GetText("Error"); msg = LocalizationMgr.GetText("ErrorMsg"); button_label = LocalizationMgr.GetText("Confirm"); } else if (error_code == -1 || // spin timeout error_code == -3 || // HTTPRequest Error error_code == -5 || // HTTPRequest ConnectionTimedOut error_code == -6 || // HTTPRequest TimedOut error_code == -20) // Default Waiting Timeout { title = LocalizationMgr.GetText(TextIds.CONNECTION_LOST); msg = LocalizationMgr.GetText(TextIds.SERVER_IS_MAINTAINING); button_label = LocalizationMgr.GetText(TextIds.OK); } else { title = LocalizationMgr.GetText(TextIds.OOPS); msg = $"{LocalizationMgr.GetText(TextIds.STH_WENT_WRONG)}\nError Code: {error_code}"; button_label = LocalizationMgr.GetText(TextIds.OK); } GameDebug.LogError($"[MessageDialog]ShowErrorDialog: {error_reason}({error_code})"); GameEventMgr.Instance.Raise(GameEvents.ShowErrorDialog); var dialog = Show(msg, false, title, button_label); if (dialog != null) { //dialog.OnOkButtonClick.AddListener(() => //{ //}); } return dialog; } public static MessageDialog Show(string message, bool useCancle = false, string title = "", string ok = "", string cancle = "", DialogType type = DialogType.Normal) { if (UIManager.Instance == null) { GameDebug.LogError("[MessageDialog]Show: UIManager.Instance doesn't exist yet!"); return null; } // Make sure there's not same message dialog showing up. if (IsShowing(message)) { GameDebug.LogWarning("[MessageDialog]Show: Already showing a dialog with the same content: {0}!", message); return null; } if (_messageDialogList.Count > MAX_DIALOG_NUM) { GameDebug.LogError("[MessageDialog]Show: Already showing TOO MANY dialogs, adjust the max dialog number or find out what's going on!"); return null; } MessageDialog dialog = null; if (_messageDialogStack.Count > 0) { dialog = _messageDialogStack.Pop(); } if (dialog == null) { //var dialog_obj = AssetManager.Instantiate(UITypes.MessageDialog.Path, UIManager.Instance.transform); var dialog_obj = UnityEngine.AddressableAssets.Addressables.InstantiateAsync(UITypes.MessageDialog.Path, UIManager.Instance.transform).WaitForCompletion(); dialog = dialog_obj != null ? dialog_obj.GetComponent() : null; if (dialog != null) { dialog.gameObject.name = string.Format("MessageDialog{0}", _messageDialogList.Count); } } if (dialog != null) { UIManager.Instance.PutOnTop(dialog.transform); _messageDialogList.Add(dialog); dialog.Set(title, message, ok, cancle); dialog.gameObject.SetActiveAsNeed(true); if (type == DialogType.BearOK || type == DialogType.BearInfo) { dialog.kingImage.SetActiveAsNeed(false); dialog.kingCoinImage.SetActiveAsNeed(false); dialog.bearImage.SetActiveAsNeed(true); } else if (type == DialogType.KingCoin) { dialog.kingImage.SetActiveAsNeed(false); dialog.kingCoinImage.SetActiveAsNeed(true); dialog.bearImage.SetActiveAsNeed(false); } else { dialog.kingImage.SetActiveAsNeed(true); dialog.kingCoinImage.SetActiveAsNeed(false); dialog.bearImage.SetActiveAsNeed(false); } if (type == DialogType.BearOK) { dialog.yellowButton.SetActive(true); dialog.infoButton.SetActive(false); dialog.OkButton.gameObject.SetActiveAsNeed(false); dialog.CancleButton.gameObject.SetActiveAsNeed(false); } else if (type == DialogType.BearInfo) { dialog.yellowButton.SetActive(false); dialog.infoButton.SetActive(true); dialog.OkButton.gameObject.SetActiveAsNeed(false); dialog.CancleButton.gameObject.SetActiveAsNeed(false); } else if (useCancle) { dialog.OkButton.gameObject.SetActiveAsNeed(true); dialog.CancleButton.gameObject.SetActiveAsNeed(true); } else { //dialog.yellowButton.SetActive(false); //dialog.infoButton.SetActive(false); dialog.OkButton.gameObject.SetActiveAsNeed(true); dialog.CancleButton.gameObject.SetActiveAsNeed(false); } dialog.FrameTrans.Popup(dialog.OnScaleUpComplete, true); UIManager.ClearWaitingBlock(); GameDebug.Log($"[MessageDialog]Show: {message}"); } else { GameDebug.LogError("[MessageDialog]Show: Fail to get a valid dialog!"); } return dialog; } public static void ShowSomethingWrong() { MessageDialog dialog = Show(LocalizationMgr.GetText(TextIds.STH_WENT_WRONG), false, LocalizationMgr.GetText(TextIds.OOPS)); if (dialog != null) { dialog.OnOkButtonClick.AddListener(() => { //if (GameStateMgr.Instance != null) //{ //} }); } } public static MessageDialog ShowCountDownMessage(string content, float count_down_timer) { var dialog = Show(content); if (dialog != null) { dialog._countDownTimer = count_down_timer; dialog._countDownTimerInt = (int)count_down_timer; dialog.OkButtonText.text = string.Format(LocalizationMgr.GetText(TextIds.OK_COUNT_DOWN), (int)count_down_timer); } return dialog; } public static MessageDialog GetCurrent() { MessageDialog current_dialog = null; if (_messageDialogList.Count > 0) { current_dialog = _messageDialogList[_messageDialogList.Count - 1]; } return current_dialog; } public static bool IsShowing(string message) { bool is_showing = false; // Make sure there's not same message dialog showing up. if (_messageDialogList.Count > 0) { for (int i = 0; i < _messageDialogList.Count; i++) { if (string.Compare(message, _messageDialogList[i].Content.text) == 0) { is_showing = true; break; } } } return is_showing; } public static void CloseDialgWithMessage(string message) { // Make sure there's not same message dialog showing up. var dialog_cout = _messageDialogList.Count; if (dialog_cout > 0) { for (int i = dialog_cout - 1; i >= 0; i--) { var dialog = _messageDialogList[i]; if (string.Compare(message, dialog.Content.text) == 0) { dialog.Close(); } } } } public static void Reset() { CloseAll(); _messageDialogList.Clear(); _messageDialogStack.Clear(); sortingOrder = 100; } public static void CloseAll() { while (_messageDialogList.Count > 0) { var message_dialog = _messageDialogList[0]; if (message_dialog != null) { message_dialog.ClearListeners(); message_dialog.Close(); } else { GameDebug.LogError("[MessageDialog]CloseAll: _messageDialogList contains null dialog!"); _messageDialogList.RemoveAt(0); } } } private void Awake() { Title = transform.Find("Dialog/Title").GetComponent(); Content = transform.Find("Dialog/ContentArea/Content").GetComponent(); OkButtonText = transform.Find("Dialog/OKButton/Text").GetComponent(); CancleButtonText = transform.Find("Dialog/CancelButton/Text").GetComponent(); OkButton = transform.Find("Dialog/OKButton").GetComponent(); CancleButton = transform.Find("Dialog/CancelButton").GetComponent(); FrameTrans = transform.Find("Dialog").GetComponent(); _contentCachTrans = Content.rectTransform; _frameBaseHeight = FrameTrans.rect.height; _contentBaseHeight = _contentCachTrans.rect.height; } private void OnEnable() { var canvas = GetComponent(); if (canvas != null) { canvas.sortingOrder = sortingOrder + _messageDialogList.Count; } } private void Update() { if (_countDownTimer > Mathf.Epsilon) { _countDownTimer -= Time.deltaTime; if (_countDownTimerInt != (int)_countDownTimer) { _countDownTimerInt = (int)_countDownTimer; OkButtonText.text = string.Format(LocalizationMgr.GetText(TextIds.OK_COUNT_DOWN), _countDownTimerInt); } if (_countDownTimer < Mathf.Epsilon) { OkButtonClick(); } } } public void Set(string title, string message, string ok, string cancle) { _isPreviousInputBlocked = UIManager.isInputBlocked(); if (_isPreviousInputBlocked) { UIManager.UnblockInput(); } Content.text = message; // Update content and frame's RectTransform size according to the real content size _contentCachTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, Content.preferredHeight); float frame_height = _frameBaseHeight; if (Content.preferredHeight > _contentBaseHeight) { frame_height = _frameBaseHeight + Content.preferredHeight - _contentBaseHeight; } FrameTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, frame_height); if (Title != null) { if (!string.IsNullOrEmpty(title)) { Title.text = title; Title.gameObject.SetActiveAsNeed(true); } else { Title.gameObject.SetActiveAsNeed(false); } } if (OkButtonText != null) { if (string.IsNullOrEmpty(ok)) { //OkButtonText.text = LocalizationMgr.GetText(TextIds.OK); } else { //OkButtonText.text = ok; } } if (CancleButtonText != null) { if (string.IsNullOrEmpty(cancle)) { CancleButtonText.text = LocalizationMgr.GetText(TextIds.CANCEL); } else { CancleButtonText.text = cancle; } } } void OnScaleUpComplete() { FrameTrans.localScale = Vector3.one; } void OnEscape() { bool trigger_cancel = CancleButton.gameObject.activeSelf; Close(); if (trigger_cancel) { OnCancleButtonClick.Invoke(); } else { OnOkButtonClick.Invoke(); } OnCancleButtonClick.RemoveAllListeners(); OnOkButtonClick.RemoveAllListeners(); } void ClearListeners() { OnOkButtonClick.RemoveAllListeners(); OnCancleButtonClick.RemoveAllListeners(); } public void Close() { gameObject.SetActiveAsNeed(false); _messageDialogList.Remove(this); _messageDialogStack.Push(this); FrameTrans.DOKill(); if (_isPreviousInputBlocked) { UIManager.RestoreInputBlock(); } } public void CloseButtonClick() { Close(); // If cancle button is enabled, will trigger cancel button callback, otherwise trigger // ok button callback if (CancleButton.gameObject.activeSelf) { OnCancleButtonClick.Invoke(); } else { OnOkButtonClick.Invoke(); } ClearListeners(); } public void InfoButtonClick() { OnInfoButtonClick.Invoke(); } public void OkButtonClick() { Close(); OnOkButtonClick.Invoke(); ClearListeners(); } public void CancleButtonClick() { Close(); OnCancleButtonClick.Invoke(); ClearListeners(); } }