You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.5 KiB

  1. using CustomUI.BeatSaber;
  2. using System.Collections;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. namespace BSIPA_ModList.UI
  8. {
  9. internal class FloatingNotification : MonoBehaviour
  10. {
  11. private Canvas _canvas;
  12. private TMP_Text _authorNameText;
  13. private TMP_Text _pluginNameText;
  14. private TMP_Text _headerText;
  15. private Image _loadingBackg;
  16. private Image _loadingBar;
  17. private static readonly Vector3 Position = new Vector3(2.3f, 2.3f, 1.35f);
  18. private static readonly Vector3 Rotation = new Vector3(0, 60, 0);
  19. private static readonly Vector3 Scale = new Vector3(0.01f, 0.01f, 0.01f);
  20. private static readonly Vector2 CanvasSize = new Vector2(100, 50);
  21. private const string AuthorNameText = "BSIPA";
  22. private const float AuthorNameFontSize = 7f;
  23. private static readonly Vector2 AuthorNamePosition = new Vector2(10, 31);
  24. private const string PluginNameText = "Mod Updater";
  25. private const float PluginNameFontSize = 9f;
  26. private static readonly Vector2 PluginNamePosition = new Vector2(10, 23);
  27. private static readonly Vector2 HeaderPosition = new Vector2(10, 15);
  28. private static readonly Vector2 HeaderSize = new Vector2(100, 20);
  29. private const string HeaderText = "Checking for updates...";
  30. private const float HeaderFontSize = 15f;
  31. private static readonly Vector2 LoadingBarSize = new Vector2(100, 10);
  32. private static readonly Color BackgroundColor = new Color(0, 0, 0, 0.2f);
  33. private bool _showingMessage;
  34. public static FloatingNotification Create()
  35. {
  36. return new GameObject("Mod List Floating Notification").AddComponent<FloatingNotification>();
  37. }
  38. public void ShowMessage(string message, float time)
  39. {
  40. StopAllCoroutines();
  41. _showingMessage = true;
  42. _headerText.text = message;
  43. _loadingBar.enabled = false;
  44. _loadingBackg.enabled = false;
  45. _canvas.enabled = true;
  46. StartCoroutine(DisableCanvasRoutine(time));
  47. }
  48. public void ShowMessage(string message)
  49. {
  50. StopAllCoroutines();
  51. _showingMessage = true;
  52. _headerText.text = message;
  53. _loadingBar.enabled = false;
  54. _loadingBackg.enabled = false;
  55. _canvas.enabled = true;
  56. }
  57. protected void OnEnable()
  58. {
  59. SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
  60. DownloadController.Instance.OnDownloadStateChanged += DownloaderStateChanged;
  61. DownloadController.Instance.OnCheckForUpdates += CheckForUpdatesStart;
  62. DownloadController.Instance.OnCheckForUpdatesComplete += CheckForUpdatesDone;
  63. }
  64. protected void OnDisable()
  65. {
  66. SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
  67. DownloadController.Instance.OnDownloadStateChanged -= DownloaderStateChanged;
  68. DownloadController.Instance.OnCheckForUpdates -= CheckForUpdatesStart;
  69. DownloadController.Instance.OnCheckForUpdatesComplete -= CheckForUpdatesDone;
  70. }
  71. private void CheckForUpdatesStart()
  72. {
  73. _showingMessage = false;
  74. _headerText.text = HeaderText;
  75. _loadingBar.enabled = false;
  76. _loadingBackg.enabled = false;
  77. _canvas.enabled = true;
  78. }
  79. private bool updatesZero = false;
  80. private void CheckForUpdatesDone(int count)
  81. {
  82. if (count == 0) updatesZero = true;
  83. _showingMessage = false;
  84. _headerText.text = $"{count} updates found";
  85. _loadingBar.enabled = false;
  86. _loadingBackg.enabled = false;
  87. _canvas.enabled = true;
  88. StartCoroutine(DisableCanvasRoutine(5f));
  89. }
  90. private void SceneManagerOnActiveSceneChanged(Scene oldScene, Scene newScene)
  91. {
  92. if (newScene.name == "MenuCore")
  93. {
  94. if (_showingMessage)
  95. {
  96. _canvas.enabled = true;
  97. }
  98. }
  99. else
  100. {
  101. _canvas.enabled = false;
  102. }
  103. }
  104. private void DownloaderStateChanged()
  105. {
  106. if (DownloadController.Instance.IsDownloading)
  107. {
  108. StopAllCoroutines();
  109. _showingMessage = false;
  110. _headerText.text = "Downloading updates...";
  111. _loadingBar.enabled = false;
  112. _loadingBackg.enabled = false;
  113. _canvas.enabled = true;
  114. }
  115. if (DownloadController.Instance.IsDone && !updatesZero)
  116. {
  117. StopAllCoroutines();
  118. _showingMessage = false;
  119. _headerText.text = "Updates complete";
  120. _loadingBar.enabled = false;
  121. _loadingBackg.enabled = false;
  122. StartCoroutine(DisableCanvasRoutine(5f));
  123. }
  124. }
  125. private IEnumerator DisableCanvasRoutine(float time)
  126. {
  127. yield return new WaitForSecondsRealtime(time);
  128. _canvas.enabled = false;
  129. _showingMessage = false;
  130. }
  131. private static FloatingNotification instance;
  132. protected void Awake()
  133. {
  134. if (instance != null)
  135. {
  136. Destroy(this);
  137. return;
  138. }
  139. instance = this;
  140. gameObject.transform.position = Position;
  141. gameObject.transform.eulerAngles = Rotation;
  142. gameObject.transform.localScale = Scale;
  143. _canvas = gameObject.AddComponent<Canvas>();
  144. _canvas.renderMode = RenderMode.WorldSpace;
  145. _canvas.enabled = false;
  146. var rectTransform = _canvas.transform as RectTransform;
  147. rectTransform.sizeDelta = CanvasSize;
  148. _authorNameText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, AuthorNameText, AuthorNamePosition);
  149. rectTransform = _authorNameText.transform as RectTransform;
  150. rectTransform.SetParent(_canvas.transform, false);
  151. rectTransform.anchoredPosition = AuthorNamePosition;
  152. rectTransform.sizeDelta = HeaderSize;
  153. _authorNameText.text = AuthorNameText;
  154. _authorNameText.fontSize = AuthorNameFontSize;
  155. _pluginNameText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, PluginNameText, PluginNamePosition);
  156. rectTransform = _pluginNameText.transform as RectTransform;
  157. rectTransform.SetParent(_canvas.transform, false);
  158. rectTransform.sizeDelta = HeaderSize;
  159. rectTransform.anchoredPosition = PluginNamePosition;
  160. _pluginNameText.text = PluginNameText;
  161. _pluginNameText.fontSize = PluginNameFontSize;
  162. _headerText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, HeaderText, HeaderPosition);
  163. rectTransform = _headerText.transform as RectTransform;
  164. rectTransform.SetParent(_canvas.transform, false);
  165. rectTransform.anchoredPosition = HeaderPosition;
  166. rectTransform.sizeDelta = HeaderSize;
  167. _headerText.text = HeaderText;
  168. _headerText.fontSize = HeaderFontSize;
  169. _loadingBackg = new GameObject("Background").AddComponent<Image>();
  170. rectTransform = _loadingBackg.transform as RectTransform;
  171. rectTransform.SetParent(_canvas.transform, false);
  172. rectTransform.sizeDelta = LoadingBarSize;
  173. _loadingBackg.color = BackgroundColor;
  174. _loadingBar = new GameObject("Loading Bar").AddComponent<Image>();
  175. rectTransform = _loadingBar.transform as RectTransform;
  176. rectTransform.SetParent(_canvas.transform, false);
  177. rectTransform.sizeDelta = LoadingBarSize;
  178. var tex = Texture2D.whiteTexture;
  179. var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.one * 0.5f, 100, 1);
  180. _loadingBar.sprite = sprite;
  181. _loadingBar.type = Image.Type.Filled;
  182. _loadingBar.fillMethod = Image.FillMethod.Horizontal;
  183. _loadingBar.color = new Color(1, 1, 1, 0.5f);
  184. DontDestroyOnLoad(gameObject);
  185. }
  186. /*private void Update()
  187. {
  188. if (!_canvas.enabled) return;
  189. _loadingBar.fillAmount = SongLoader.LoadingProgress;
  190. }*/
  191. }
  192. }