Files
2026-02-21 16:45:37 +08:00

277 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace UIWidgets
{
public class Dialog : MonoBehaviour, ITemplatable
{
[SerializeField]
private Button defaultButton;
[SerializeField]
private Text titleText;
[SerializeField]
private Text contentText;
[SerializeField]
private Image dialogIcon;
private bool isTemplate = true;
private static Templates<Dialog> templates;
protected int? ModalKey;
protected Stack<Button> buttonsCache = new Stack<Button>();
protected Dictionary<string, Button> buttonsInUse = new Dictionary<string, Button>();
protected Dictionary<string, UnityAction> buttonsActions = new Dictionary<string, UnityAction>();
public Button DefaultButton
{
get
{
return defaultButton;
}
set
{
defaultButton = value;
}
}
public Text TitleText
{
get
{
return titleText;
}
set
{
titleText = value;
}
}
public Text ContentText
{
get
{
return contentText;
}
set
{
contentText = value;
}
}
public Image Icon
{
get
{
return dialogIcon;
}
set
{
dialogIcon = value;
}
}
public bool IsTemplate
{
get
{
return isTemplate;
}
set
{
isTemplate = value;
}
}
public string TemplateName { get; set; }
public static Templates<Dialog> Templates
{
get
{
if (templates == null)
{
templates = new Templates<Dialog>();
}
return templates;
}
set
{
templates = value;
}
}
protected virtual void Awake()
{
if (IsTemplate)
{
base.gameObject.SetActive(false);
}
}
protected virtual void OnDestroy()
{
DeactivateButtons();
if (!IsTemplate)
{
templates = null;
}
else if (TemplateName != null)
{
Templates.Delete(TemplateName);
}
}
public static Dialog Template(string template)
{
return Templates.Instance(template);
}
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.HasValue)
{
position = new Vector3(0f, 0f, 0f);
}
if (title != null && TitleText != null)
{
TitleText.text = title;
}
if (message != null && ContentText != null)
{
contentText.text = message;
}
if (icon != null && Icon != null)
{
Icon.sprite = icon;
}
Transform transform = ((!(canvas != null)) ? Utilites.FindCanvas(base.gameObject.transform) : canvas.transform);
if (transform != null)
{
base.transform.SetParent(transform, false);
}
if (modal)
{
ModalKey = ModalHelper.Open(this, modalSprite, modalColor);
}
else
{
ModalKey = null;
}
base.transform.SetAsLastSibling();
base.transform.localPosition = position.Value;
base.gameObject.SetActive(true);
CreateButtons(buttons, focusButton);
}
public virtual void Hide()
{
int? modalKey = ModalKey;
if (modalKey.HasValue)
{
int? modalKey2 = ModalKey;
ModalHelper.Close(modalKey2.Value);
}
Return();
}
protected virtual void CreateButtons(DialogActions buttons, string focusButton)
{
defaultButton.gameObject.SetActive(false);
if (buttons == null)
{
return;
}
buttons.ForEach(delegate(KeyValuePair<string, Func<bool>> x)
{
Button button = GetButton();
UnityAction value = delegate
{
if (x.Value())
{
Hide();
}
};
buttonsInUse.Add(x.Key, button);
buttonsActions.Add(x.Key, value);
button.gameObject.SetActive(true);
button.transform.SetAsLastSibling();
Text componentInChildren = button.GetComponentInChildren<Text>();
if ((bool)componentInChildren)
{
componentInChildren.text = x.Key;
}
button.onClick.AddListener(buttonsActions[x.Key]);
if (x.Key == focusButton)
{
button.Select();
}
});
}
protected virtual Button GetButton()
{
if (buttonsCache.Count > 0)
{
return buttonsCache.Pop();
}
Button button = UnityEngine.Object.Instantiate(DefaultButton);
Utilites.FixInstantiated(DefaultButton, button);
button.transform.SetParent(DefaultButton.transform.parent, false);
return button;
}
protected virtual void Return()
{
Templates.ToCache(this);
DeactivateButtons();
ResetParametres();
}
protected virtual void DeactivateButtons()
{
buttonsInUse.ForEach(DeactivateButton);
buttonsInUse.Clear();
buttonsActions.Clear();
}
protected virtual void DeactivateButton(KeyValuePair<string, Button> button)
{
button.Value.gameObject.SetActive(false);
button.Value.onClick.RemoveListener(buttonsActions[button.Key]);
buttonsCache.Push(button.Value);
}
protected virtual void ResetParametres()
{
Dialog dialog = Templates.Get(TemplateName);
if (TitleText != null && dialog.TitleText != null)
{
TitleText.text = dialog.TitleText.text;
}
if (ContentText != null && dialog.ContentText != null)
{
ContentText.text = dialog.ContentText.text;
}
if (Icon != null && dialog.Icon != null)
{
Icon.sprite = dialog.Icon.sprite;
}
}
public static bool Close()
{
return true;
}
}
}