namespace UIWidgets
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UIWidgets.l10n;
using UIWidgets.Styles;
using UnityEngine;
using UnityEngine.UI;
///
/// Base class for the Dialogs.
///
public abstract class DialogBase : MonoBehaviour, ITemplatable, IStylable, IUpgradeable, IHideable
{
#pragma warning disable 0649
[SerializeField]
[HideInInspector]
[Obsolete("Replaced with buttonsTemplates")]
Button defaultButton;
#pragma warning restore 0649
///
/// Gets or sets the default button.
///
/// The default button.
[Obsolete("Replaced with ButtonsTemplates")]
public Button DefaultButton
{
get
{
Upgrade();
return defaultButton;
}
set
{
Upgrade();
var buttons = new List(ButtonsTemplates)
{
value,
};
ButtonsTemplates = buttons.AsReadOnly();
}
}
///
/// Buttons container.
///
[SerializeField]
protected RectTransform ButtonsContainer;
///
/// Buttons templates.
///
[SerializeField]
protected List buttonsTemplates = new List();
///
/// Gets or sets the default buttons.
///
/// The default buttons.
public abstract ReadOnlyCollection ButtonsTemplates
{
get;
set;
}
///
/// Content root.
///
[SerializeField]
[HideInInspector]
[Obsolete("Replaced with DialogInfo component.")]
public RectTransform ContentRoot;
[SerializeField]
[HideInInspector]
[Obsolete("Replaced with DialogInfo component.")]
Text titleText;
///
/// Gets or sets the text component.
///
/// The text.
[Obsolete("Replaced with DialogInfo component.")]
public Text TitleText
{
get
{
return titleText;
}
set
{
titleText = value;
}
}
[SerializeField]
[HideInInspector]
[Obsolete("Replaced with DialogInfo component.")]
Text contentText;
///
/// Gets or sets the text component.
///
/// The text.
[Obsolete("Replaced with DialogInfo component.")]
public Text ContentText
{
get
{
return contentText;
}
set
{
contentText = value;
}
}
[SerializeField]
[HideInInspector]
[Obsolete("Replaced with DialogInfo component.")]
Image dialogIcon;
///
/// Gets or sets the icon component.
///
/// The icon.
[Obsolete("Replaced with DialogInfo component.")]
public Image Icon
{
get
{
return dialogIcon;
}
set
{
dialogIcon = value;
}
}
[SerializeField]
DialogInfoBase dialogInfo;
///
/// Gets the dialog info.
///
/// The dialog info.
public DialogInfoBase DialogInfo
{
get
{
if (dialogInfo == null)
{
dialogInfo = GetComponent();
}
return dialogInfo;
}
}
bool isTemplate = true;
///
/// Gets a value indicating whether this instance is template.
///
/// true if this instance is template; otherwise, false .
public bool IsTemplate
{
get
{
return isTemplate;
}
set
{
isTemplate = value;
}
}
string dialogTemplateName;
///
/// Gets the name of the template.
///
/// The name of the template.
public string TemplateName
{
get
{
if (dialogTemplateName == null)
{
FindTemplates();
}
return dialogTemplateName;
}
set
{
dialogTemplateName = value;
}
}
[SerializeField]
Button closeButton;
///
/// Close button.
///
public Button CloseButton
{
get
{
return closeButton;
}
set
{
if (isInited && (closeButton != null))
{
closeButton.onClick.RemoveListener(Cancel);
}
closeButton = value;
if (isInited && (closeButton != null))
{
closeButton.onClick.AddListener(Cancel);
}
}
}
///
/// The modal key.
///
protected int? ModalKey;
///
/// Callback on dialog close.
///
public Action OnClose;
///
/// Callback on dialog cancel.
///
[Obsolete("Replaced with OnDialogCancel.")]
public Func OnCancel;
///
/// Callback on dialog cancel.
///
public Func OnDialogCancel;
bool isInited;
///
/// Start this instance.
///
protected virtual void Start()
{
Init();
}
///
/// Init this instance.
///
public virtual void Init()
{
if (isInited)
{
return;
}
if (closeButton != null)
{
closeButton.onClick.AddListener(Cancel);
}
isInited = true;
Localization.OnLocaleChanged += LocaleChanged;
}
///
/// Awake is called when the script instance is being loaded.
///
protected virtual void Awake()
{
if (IsTemplate)
{
gameObject.SetActive(false);
}
}
///
/// Find templates.
///
protected abstract void FindTemplates();
///
/// Locale changed.
///
protected abstract void LocaleChanged();
///
/// Process enable event.
///
protected abstract void OnEnable();
///
/// Process disable event.
///
protected abstract void OnDisable();
///
/// This function is called when the MonoBehaviour will be destroyed.
///
protected virtual void OnDestroy()
{
Localization.OnLocaleChanged -= LocaleChanged;
}
///
/// Show dialog.
///
/// Title. Also can be changed with SetInfo().
/// Message. Also can be changed with SetInfo().
/// Buttons. Also can be changed with SetButtons().
/// Set focus on button with specified name. Also can be changed with SetButtons() or FocusButton().
/// Position. Also can be changed with SetPosition().
/// Icon. Also can be changed with SetInfo(...).
/// If set to true modal. Also can be changed with SetModal().
/// Modal sprite. Also can be changed with SetModal().
/// Modal color. Also can be changed with SetModal().
/// Canvas. Also can be changed with SetCanvas().
/// Content. Also can be changed with SetContent().
/// On close callback. Also can be changed with OnClose field.
/// On cancel callback. Also can be changed with OnCancel field.
public virtual void Show(
string title = null,
string message = null,
IList buttons = null,
string focusButton = null,
Vector3? position = null,
Sprite icon = null,
bool modal = false,
Sprite modalSprite = null,
Color? modalColor = null,
Canvas canvas = null,
RectTransform content = null,
Action onClose = null,
Func onCancel = null)
{
if (IsTemplate)
{
Debug.LogWarning("Use the template clone, not the template itself: DialogTemplate.Clone().Show(...), not DialogTemplate.Show(...)");
}
OnClose = onClose;
#pragma warning disable 0618
OnCancel = onCancel;
#pragma warning restore 0618
SetInfo(title, null, message, null, icon);
SetContent(content);
var canvas_rt = SetCanvas(canvas);
SetModal(modal, modalSprite, modalColor, canvas_rt);
if (position.HasValue)
{
SetPosition(position.Value);
}
gameObject.SetActive(true);
SetButtons(buttons, focusButton);
}
///
/// Set modal mode.
/// Warning: modal block is created at the current root canvas.
///
/// If set to true modal.
/// Modal sprite.
/// Modal color.
/// Parent canvas.
public virtual void SetModal(bool modal = false, Sprite modalSprite = null, Color? modalColor = null, RectTransform parentCanvas = null)
{
if (ModalKey != null)
{
ModalHelper.Close(ModalKey.Value);
ModalKey = null;
}
if (modal)
{
ModalKey = ModalHelper.Open(this, modalSprite, modalColor, parent: parentCanvas);
}
else
{
ModalKey = null;
}
transform.SetAsLastSibling();
}
///
/// Set canvas.
///
/// Canvas.
/// Canvas RectTransform.
public virtual RectTransform SetCanvas(Canvas canvas)
{
var parent = (canvas != null) ? canvas.transform as RectTransform : UtilitiesUI.FindTopmostCanvas(gameObject.transform);
if (parent != null)
{
transform.SetParent(parent, false);
}
transform.SetAsLastSibling();
return parent;
}
///
/// Set position.
///
/// Position.
public virtual void SetPosition(Vector3 position)
{
transform.localPosition = position;
}
///
/// Sets the info. Pass null to leave default value.
///
/// Title.
/// Message.
/// Icon.
public virtual void SetInfo(string title = null, string message = null, Sprite icon = null)
{
DialogInfo.SetInfo(title, null, message, null, icon);
}
///
/// Sets the info. Pass null to leave default value.
///
/// Title.
/// Title arguments.
/// Message.
/// Message arguments.
/// Icon.
public virtual void SetInfo(string title = null, object[] titleArgs = null, string message = null, object[] messageArgs = null, Sprite icon = null)
{
DialogInfo.SetInfo(title, titleArgs, message, messageArgs, icon);
}
///
/// Sets the content.
///
/// Content.
public virtual void SetContent(RectTransform content)
{
if (content == null)
{
return;
}
DialogInfo.SetContent(content);
}
///
/// Cancel dialog.
///
public virtual void Cancel()
{
#pragma warning disable 0618
if (OnCancel != null)
{
if (!OnCancel(-1))
{
return;
}
}
#pragma warning restore 0618
if (OnDialogCancel != null)
{
if (!OnDialogCancel(this, -1))
{
return;
}
}
Hide();
}
///
/// Close dialog.
///
public virtual void Hide()
{
if (OnClose != null)
{
OnClose();
}
if (ModalKey != null)
{
ModalHelper.Close(ModalKey.Value);
}
Return();
}
///
/// Creates the buttons.
///
/// Buttons.
/// Focus button.
public abstract void SetButtons(IList buttons, string focusButton = null);
///
/// Set focus to the specified button.
///
/// Button label.
/// true if button found with specified label; otherwise false.
public abstract bool FocusButton(string focusButton);
///
/// Get button template index.
///
/// Button.
/// Template index,
protected abstract int GetTemplateIndex(DialogButton button);
///
/// Return this instance to cache.
///
protected virtual void Return()
{
ResetParameters();
}
///
/// Resets the parameters.
///
protected virtual void ResetParameters()
{
OnClose = null;
OnDialogCancel = null;
#pragma warning disable 0618
OnCancel = null;
#pragma warning restore 0618
DialogInfo.RestoreDefaultValues();
}
///
/// Default function to close dialog.
///
/// true if dialog can be closed; otherwise false.
[Obsolete("Replaced with DefaultClose().")]
public static bool Close()
{
return true;
}
///
/// Default function to close dialog.
///
/// Button index.
/// true if dialog can be closed; otherwise false.
[Obsolete("Replaced with DefaultClose().")]
public static bool AlwaysClose(int index)
{
return true;
}
///
/// Default function to close dialog.
///
/// Dialog.
/// Button index.
/// true if dialog can be closed; otherwise false.
public static bool DefaultClose(DialogBase dialog, int index)
{
return true;
}
#region IStylable implementation
///
public virtual bool SetStyle(Style style)
{
style.Dialog.Background.ApplyTo(GetComponent());
style.Dialog.ContentBackground.ApplyTo(transform.Find("Content"));
style.Dialog.Delimiter.ApplyTo(transform.Find("Delimiter/Delimiter"));
if (closeButton != null)
{
style.ButtonClose.ApplyTo(closeButton);
}
else
{
style.ButtonClose.Background.ApplyTo(transform.Find("Header/CloseButton"));
style.ButtonClose.Text.ApplyTo(transform.Find("Header/CloseButton/Text"));
}
if (DialogInfo != null)
{
DialogInfo.SetStyle(style.Dialog);
}
return true;
}
///
public virtual bool GetStyle(Style style)
{
style.Dialog.Background.GetFrom(GetComponent());
style.Dialog.ContentBackground.GetFrom(transform.Find("Content"));
style.Dialog.Delimiter.GetFrom(transform.Find("Delimiter/Delimiter"));
if (closeButton != null)
{
style.ButtonClose.GetFrom(closeButton);
}
else
{
style.ButtonClose.Background.GetFrom(transform.Find("Header/CloseButton"));
style.ButtonClose.Text.GetFrom(transform.Find("Header/CloseButton/Text"));
}
if (DialogInfo != null)
{
DialogInfo.GetStyle(style.Dialog);
}
return true;
}
#endregion
///
/// Upgrade fields data to the latest version.
///
public virtual void Upgrade()
{
#pragma warning disable 0618
if ((buttonsTemplates.Count == 0) && (defaultButton != null))
{
buttonsTemplates.Add(defaultButton);
}
foreach (var btn in buttonsTemplates)
{
if (btn != null)
{
var info = Utilities.GetOrAddComponent(btn);
info.Upgrade();
if (info.NameAdapter == null)
{
Utilities.GetOrAddComponent(Compatibility.GetComponentInChildren(info, true), ref info.NameAdapter);
}
}
}
if (ContentRoot == null)
{
ContentRoot = transform.Find("Content") as RectTransform;
}
if (dialogInfo == null)
{
dialogInfo = Utilities.GetOrAddComponent(this);
Utilities.GetOrAddComponent(titleText, ref dialogInfo.TitleAdapter);
Utilities.GetOrAddComponent(contentText, ref dialogInfo.MessageAdapter);
dialogInfo.Icon = Icon;
}
if (dialogInfo.ContentRoot == null)
{
dialogInfo.ContentRoot = ContentRoot;
}
#pragma warning restore 0618
}
#if UNITY_EDITOR
///
/// Update layout when parameters changed.
///
protected virtual void OnValidate()
{
Compatibility.Upgrade(this);
}
#endif
}
}