geometry-tower-defense-base/src-ref/UI/General/View/DialogForm.cs

208 lines
5.7 KiB
C#

using GameFramework;
using TMPro;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public class DialogForm : UGuiForm
{
[SerializeField] private TMP_Text _titleText = null;
[SerializeField] private TMP_Text _messageText = null;
[SerializeField] private GameObject[] _modeObjects = null;
[SerializeField] private TMP_Text[] _confirmTexts = null;
[SerializeField] private TMP_Text[] _cancelTexts = null;
[SerializeField] private TMP_Text[] _otherTexts = null;
private int _dialogMode = 1;
private bool _pauseGame = false;
private object _userData = null;
private GameFrameworkAction<object> _onClickConfirmGFAction = null;
private GameFrameworkAction<object> _onClickCancelGFAction = null;
private GameFrameworkAction<object> _onClickOtherGFAction = null;
private DialogFormContext _context = null;
public int DialogMode => _dialogMode;
public bool PauseGame => _pauseGame;
public object UserData => _userData;
public void OnConfirmButtonClick()
{
Close();
if (_onClickConfirmGFAction != null)
{
_onClickConfirmGFAction(_userData);
}
}
public void OnCancelButtonClick()
{
Close();
if (_onClickCancelGFAction != null)
{
_onClickCancelGFAction(_userData);
}
}
public void OnOtherButtonClick()
{
Close();
if (_onClickOtherGFAction != null)
{
_onClickOtherGFAction(_userData);
}
}
#if UNITY_2017_3_OR_NEWER
protected override void OnOpen(object userData)
#else
protected internal override void OnOpen(object userData)
#endif
{
base.OnOpen(userData);
if (userData is DialogFormContext context)
{
RefreshUI(context);
return;
}
if (userData is DialogFormRawData dialogParams)
{
RefreshUI(BuildContext(dialogParams));
return;
}
Log.Warning("DialogForm requires DialogFormContext or DialogParams as userData.");
}
#if UNITY_2017_3_OR_NEWER
protected override void OnClose(bool isShutdown, object userData)
#else
protected internal override void OnClose(bool isShutdown, object userData)
#endif
{
if (_pauseGame)
{
GameEntry.Base.ResumeGame();
}
_dialogMode = 1;
_titleText.text = string.Empty;
_messageText.text = string.Empty;
_pauseGame = false;
_userData = null;
_context = null;
RefreshConfirmText(string.Empty);
_onClickConfirmGFAction = null;
RefreshCancelText(string.Empty);
_onClickCancelGFAction = null;
RefreshOtherText(string.Empty);
_onClickOtherGFAction = null;
base.OnClose(isShutdown, userData);
}
public void RefreshUI(DialogFormContext context)
{
if (context == null)
{
Log.Warning("DialogForm context is invalid.");
return;
}
_context = context;
_dialogMode = context.Mode;
RefreshDialogMode();
_titleText.text = context.Title;
_messageText.text = context.Message;
_pauseGame = context.PauseGame;
RefreshPauseGame();
_userData = context.UserData;
RefreshConfirmText(context.ConfirmText);
_onClickConfirmGFAction = context.OnClickConfirm;
RefreshCancelText(context.CancelText);
_onClickCancelGFAction = context.OnClickCancel;
RefreshOtherText(context.OtherText);
_onClickOtherGFAction = context.OnClickOther;
}
private static DialogFormContext BuildContext(DialogFormRawData rawData)
{
if (rawData == null)
{
return null;
}
return new DialogFormContext
{
Mode = rawData.Mode,
Title = rawData.Title,
Message = rawData.Message,
PauseGame = rawData.PauseGame,
ConfirmText = rawData.ConfirmText,
OnClickConfirm = rawData.OnClickConfirm,
CancelText = rawData.CancelText,
OnClickCancel = rawData.OnClickCancel,
OtherText = rawData.OtherText,
OnClickOther = rawData.OnClickOther,
UserData = rawData.UserData
};
}
private void RefreshDialogMode()
{
for (int i = 1; i <= _modeObjects.Length; i++)
{
_modeObjects[i - 1].SetActive(i == _dialogMode);
}
}
private void RefreshPauseGame()
{
if (_pauseGame)
{
GameEntry.Base.PauseGame();
}
}
private void RefreshConfirmText(string confirmText)
{
foreach (var text in _confirmTexts)
{
text.text = confirmText;
}
}
private void RefreshCancelText(string cancelText)
{
foreach (var text in _cancelTexts)
{
text.text = cancelText;
}
}
private void RefreshOtherText(string otherText)
{
foreach (var text in _otherTexts)
{
text.text = otherText;
}
}
}
}