PageRenderTime 21ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/UI/MaterialUI/Scripts/Managers/SnackbarManager.cs

https://gitlab.com/nicolas.diazb97/chrome-score
C# | 236 lines | 116 code | 27 blank | 93 comment | 9 complexity | 0b0ea3447726bf9cc3f1596cf7e4f30f MD5 | raw file
  1. // Copyright 2016 MaterialUI for Unity http://materialunity.com
  2. // Please see license file for terms and conditions of use, and more information.
  3. using UnityEngine;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace MaterialUI
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. /// <seealso cref="UnityEngine.MonoBehaviour" />
  12. [AddComponentMenu("MaterialUI/Managers/Snackbar Manager")]
  13. public class SnackbarManager : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// The singelton in-scene instance.
  17. /// </summary>
  18. private static SnackbarManager m_Instance;
  19. /// <summary>
  20. /// The singelton in-scene instance.
  21. /// If null, creates and sets the in-scene instance.
  22. /// </summary>
  23. private static SnackbarManager instance
  24. {
  25. get
  26. {
  27. if (m_Instance == null)
  28. {
  29. GameObject go = new GameObject();
  30. go.name = "SnackbarManager";
  31. go.AddComponent<CanvasRenderer>();
  32. RectTransform rectTransform = go.AddComponent<RectTransform>();
  33. rectTransform.anchoredPosition = Vector2.zero;
  34. rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
  35. m_Instance = go.AddComponent<SnackbarManager>();
  36. }
  37. return m_Instance;
  38. }
  39. }
  40. /// <summary>
  41. /// Should the Manager persist between scenes?
  42. /// </summary>
  43. [SerializeField]
  44. private bool m_KeepBetweenScenes = true;
  45. /// <summary>
  46. /// The parent canvas for the snackbar objects.
  47. /// </summary>
  48. [SerializeField]
  49. private Canvas m_ParentCanvas;
  50. /// <summary>
  51. /// The default duration to show snackbar messages.
  52. /// </summary>
  53. [Header("Default Snackbar parameters")]
  54. [SerializeField]
  55. private float m_DefaultDuration = 5f;
  56. /// <summary>
  57. /// The default panel color.
  58. /// </summary>
  59. [SerializeField]
  60. private Color m_DefaultPanelColor = MaterialColor.HexToColor("323232");
  61. /// <summary>
  62. /// The default text color.
  63. /// </summary>
  64. [SerializeField]
  65. private Color m_DefaultTextColor = MaterialColor.textLight;
  66. /// <summary>
  67. /// The default font size.
  68. /// </summary>
  69. [SerializeField]
  70. private int m_DefaultFontSize = 16;
  71. /// <summary>
  72. /// The queue of snackbars. To avoid multiple snackbars being shown at once, each snackbar is added to the queue when Show is called.
  73. /// Each queued snackbar will automatically be shown once the preceding snackbar has finished being shown.
  74. /// </summary>
  75. private Queue<Snackbar> m_Snackbars;
  76. /// <summary>
  77. /// Are snackbars actively/currently being shown?
  78. /// </summary>
  79. private bool m_IsActive;
  80. /// <summary>
  81. /// The SnackbarAnimator for the currently shown snackbar.
  82. /// </summary>
  83. private SnackbarAnimator m_CurrentAnimator;
  84. /// <summary>
  85. /// Has the system been initialized?
  86. /// </summary>
  87. private bool m_InitDone = false;
  88. /// <summary>
  89. /// See MonoBehaviour.Awake.
  90. /// </summary>
  91. void Awake()
  92. {
  93. if (!m_Instance)
  94. {
  95. m_Instance = this;
  96. if (m_KeepBetweenScenes)
  97. {
  98. DontDestroyOnLoad(this);
  99. }
  100. }
  101. else
  102. {
  103. Debug.LogWarning("More than one SnackbarManager exist in the scene, destroying one.");
  104. Destroy(gameObject);
  105. }
  106. }
  107. /// <summary>
  108. /// Initializes the snackbar system.
  109. /// </summary>
  110. private void InitSystem()
  111. {
  112. if (m_ParentCanvas == null)
  113. {
  114. m_ParentCanvas = FindObjectOfType<Canvas>();
  115. }
  116. transform.SetParent(m_ParentCanvas.transform, false);
  117. transform.localPosition = Vector3.zero;
  118. m_CurrentAnimator = PrefabManager.InstantiateGameObject(PrefabManager.ResourcePrefabs.snackbar, transform).GetComponent<SnackbarAnimator>();
  119. m_Snackbars = new Queue<Snackbar>();
  120. m_InitDone = true;
  121. }
  122. /// <summary>
  123. /// See MonoBehaviour.OnDestroy.
  124. /// </summary>
  125. void OnDestroy()
  126. {
  127. m_Instance = null;
  128. }
  129. /// <summary>
  130. /// See MonoBehaviour.OnApplicationQuit.
  131. /// </summary>
  132. void OnApplicationQuit()
  133. {
  134. m_Instance = null;
  135. }
  136. /// <summary>
  137. /// Shows a snackbar message. If a snackbar message is already being shown, then the manager will instead add this snackbar to the queue.
  138. /// The action button has text "Okay" and simply clears the snackbar when clicked.
  139. /// </summary>
  140. /// <param name="content">The message to show.</param>
  141. public static void Show(string content)
  142. {
  143. Show(content, "Okay", null);
  144. }
  145. /// <summary>
  146. /// Shows a snackbar message. If a snackbar message is already being shown, then the manager will instead add this snackbar to the queue.
  147. /// </summary>
  148. /// <param name="content">The message to show.</param>
  149. /// <param name="actionName">The text of the action button.</param>
  150. /// <param name="onActionButtonClicked">Called when the action button is clicked.</param>
  151. public static void Show(string content, string actionName, Action onActionButtonClicked)
  152. {
  153. Show(content, instance.m_DefaultDuration, instance.m_DefaultPanelColor, instance.m_DefaultTextColor, instance.m_DefaultFontSize, actionName, onActionButtonClicked);
  154. }
  155. /// <summary>
  156. /// Shows a snackbar message. If a snackbar message is already being shown, then the manager will instead add this snackbar to the queue.
  157. /// </summary>
  158. /// <param name="content">The message to show.</param>
  159. /// <param name="duration">The duration to show the snackbar.</param>
  160. /// <param name="actionName">The text of the action button.</param>
  161. /// <param name="onActionButtonClicked">Called when the action button is clicked.</param>
  162. public static void Show(string content, float duration, string actionName, Action onActionButtonClicked)
  163. {
  164. Show(content, duration, instance.m_DefaultPanelColor, instance.m_DefaultTextColor, instance.m_DefaultFontSize, actionName, onActionButtonClicked);
  165. }
  166. /// <summary>
  167. /// Shows a snackbar message. If a snackbar message is already being shown, then the manager will instead add this snackbar to the queue.
  168. /// </summary>
  169. /// <param name="content">The message to show.</param>
  170. /// <param name="duration">The duration to show the snackbar.</param>
  171. /// <param name="panelColor">Color of the background panel.</param>
  172. /// <param name="textColor">Color of the text.</param>
  173. /// <param name="fontSize">Size of the font.</param>
  174. /// <param name="actionName">The text of the action button.</param>
  175. /// <param name="onActionButtonClicked">Called when the action button is clicked.</param>
  176. public static void Show(string content, float duration, Color panelColor, Color textColor, int fontSize, string actionName, Action onActionButtonClicked)
  177. {
  178. if (!instance.m_InitDone)
  179. {
  180. instance.InitSystem();
  181. }
  182. instance.m_Snackbars.Enqueue(new Snackbar(content, duration, panelColor, textColor, fontSize, actionName, onActionButtonClicked));
  183. instance.StartQueue();
  184. }
  185. /// <summary>
  186. /// If <see cref="m_IsActive"/> is false, start showing each snackbar in the queue, one after another.
  187. /// Otherwise, do nothing.
  188. /// </summary>
  189. private void StartQueue()
  190. {
  191. if (m_Snackbars.Count > 0 && !m_IsActive)
  192. {
  193. m_CurrentAnimator.Show(m_Snackbars.Dequeue());
  194. m_IsActive = true;
  195. }
  196. }
  197. /// <summary>
  198. /// Called by <see cref="SnackbarAnimator"/> when a snackbar has finished showing.
  199. /// Calls <see cref="StartQueue"/>.
  200. /// </summary>
  201. /// <returns>True. I don't know why I put that return line below, and I'm too tired to figure out why now.</returns>
  202. public static bool OnSnackbarCompleted()
  203. {
  204. instance.m_IsActive = false;
  205. instance.StartQueue();
  206. return (instance.m_Snackbars.Count > -1);
  207. }
  208. }
  209. }