using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections.Generic;
namespace UIWidgets {
///
/// Dialog.
///
[AddComponentMenu("UI/UIWidgets/Dialog")]
public class Dialog : MonoBehaviour, ITemplatable
{
[SerializeField]
Button defaultButton;
///
/// Gets or sets the default button.
///
/// The default button.
public Button DefaultButton {
get {
return defaultButton;
}
set {
defaultButton = value;
}
}
[SerializeField]
Text titleText;
///
/// Gets or sets the text component.
///
/// The text.
public Text TitleText {
get {
return titleText;
}
set {
titleText = value;
}
}
[SerializeField]
Text contentText;
///
/// Gets or sets the text component.
///
/// The text.
public Text ContentText {
get {
return contentText;
}
set {
contentText = value;
}
}
[SerializeField]
Image dialogIcon;
///
/// Gets or sets the icon component.
///
/// The icon.
public Image Icon {
get {
return dialogIcon;
}
set {
dialogIcon = value;
}
}
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;
}
}
///
/// Gets the name of the template.
///
/// The name of the template.
public string TemplateName {
get;
set;
}
static Templates templates;
///
/// Dialog templates.
///
public static Templates Templates {
get {
if (templates==null)
{
templates = new Templates();
}
return templates;
}
set {
templates = value;
}
}
///
/// Awake is called when the script instance is being loaded.
///
protected virtual void Awake()
{
if (IsTemplate)
{
gameObject.SetActive(false);
}
}
///
/// This function is called when the MonoBehaviour will be destroyed.
///
protected virtual void OnDestroy()
{
DeactivateButtons();
if (!IsTemplate)
{
templates = null;
return ;
}
//if FindTemplates never called than TemplateName==null
if (TemplateName!=null)
{
Templates.Delete(TemplateName);
}
}
///
/// Return dialog instance by the specified template name.
///
/// Template name.
static public Dialog Template(string template)
{
return Templates.Instance(template);
}
///
/// Return Dialog instance using current instance as template.
///
public Dialog Template()
{
if ((TemplateName!=null) && Templates.Exists(TemplateName))
{
//do nothing
}
else if (!Templates.Exists(gameObject.name))
{
Templates.Add(gameObject.name, this);
}
else if (Templates.Get(gameObject.name)!=this)
{
Templates.Add(gameObject.name, this);
}
return Templates.Instance(gameObject.name);
}
///
/// The modal key.
///
protected int? ModalKey;
///
/// Show dialog.
///
/// Title.
/// Message.
/// Buttons.
/// Set focus on button with specified name.
/// Position.
/// Icon.
/// If set to true modal.
/// Modal sprite.
/// Modal color.
/// Canvas.
public virtual void Show(string title = null,
string message = null,
DialogActions buttons = null,
string focusButton = null,
Vector3? position = null,
Sprite icon = null,
bool modal = false,
Sprite modalSprite = null,
Color? modalColor = null,
Canvas canvas = null)
{
if (position==null)
{
position = new Vector3(0, 0, 0);
}
SetInfo(title, message, icon);
var parent = (canvas!=null) ? canvas.transform : Utilites.FindTopmostCanvas(gameObject.transform);
if (parent!=null)
{
transform.SetParent(parent, false);
}
if (modal)
{
ModalKey = ModalHelper.Open(this, modalSprite, modalColor);
}
else
{
ModalKey = null;
}
transform.SetAsLastSibling();
transform.localPosition = (Vector3)position;
gameObject.SetActive(true);
CreateButtons(buttons, focusButton);
}
///
/// Sets the info.
///
/// Title.
/// Message.
/// Icon.
public virtual void SetInfo(string title=null, string message=null, Sprite icon=null)
{
if (DialogInfo!=null)
{
DialogInfo.SetInfo(title, message, icon);
}
else
{
if ((title!=null) && (TitleText!=null))
{
TitleText.text = title;
}
if ((message!=null) && (ContentText!=null))
{
ContentText.text = message;
}
if ((icon!=null) && (Icon!=null))
{
Icon.sprite = icon;
}
}
}
///
/// Close dialog.
///
public virtual void Hide()
{
if (ModalKey!=null)
{
ModalHelper.Close((int)ModalKey);
}
Return();
}
///
/// The buttons cache.
///
protected Stack buttonsCache = new Stack();
///
/// The buttons in use.
///
protected Dictionary buttonsInUse = new Dictionary();
///
/// The buttons actions.
///
protected Dictionary buttonsActions = new Dictionary();
///
/// Creates the buttons.
///
/// Buttons.
/// Focus button.
protected virtual void CreateButtons(DialogActions buttons, string focusButton)
{
defaultButton.gameObject.SetActive(false);
if (buttons==null)
{
return ;
}
buttons.ForEach(x => {
var button = GetButton();
UnityAction callback = () => {
if (x.Value())
{
Hide();
}
};
buttonsInUse.Add(x.Key, button);
buttonsActions.Add(x.Key, callback);
button.gameObject.SetActive(true);
button.transform.SetAsLastSibling();
var dialog_button = button.GetComponentInChildren();
if (dialog_button!=null)
{
dialog_button.SetButtonName(x.Key);
}
else
{
var text = button.GetComponentInChildren();
if (text!=null)
{
text.text = x.Key;
}
}
button.onClick.AddListener(buttonsActions[x.Key]);
if (x.Key==focusButton)
{
button.Select();
}
});
}
///
/// Gets the button.
///
/// The button.
protected virtual Button GetButton()
{
if (buttonsCache.Count > 0)
{
return buttonsCache.Pop();
}
var button = Instantiate(DefaultButton) as Button;
button.transform.SetParent(DefaultButton.transform.parent, false);
//Utilites.FixInstantiated(DefaultButton, button);
return button;
}
///
/// Return this instance to cache.
///
protected virtual void Return()
{
Templates.ToCache(this);
DeactivateButtons();
ResetParametres();
}
///
/// Deactivates the buttons.
///
protected virtual void DeactivateButtons()
{
buttonsInUse.ForEach(DeactivateButton);
buttonsInUse.Clear();
buttonsActions.Clear();
}
///
/// Deactivates the button.
///
/// Button.
protected virtual void DeactivateButton(KeyValuePair button)
{
button.Value.gameObject.SetActive(false);
button.Value.onClick.RemoveListener(buttonsActions[button.Key]);
buttonsCache.Push(button.Value);
}
///
/// Resets the parametres.
///
protected virtual void ResetParametres()
{
var template = Templates.Get(TemplateName);
var title = template.TitleText!=null ? template.TitleText.text : "";
var content = template.ContentText!=null ? template.ContentText.text : "";
var icon = template.Icon!=null ? template.Icon.sprite : null;
SetInfo(title, content, icon);
}
///
/// Default function to close dialog.
///
static public bool Close()
{
return true;
}
}
}