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.

218 lines
8.3 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 void CheckForUpdatesDone(int count)
  80. {
  81. _showingMessage = false;
  82. _headerText.text = $"{count} updates found";
  83. _loadingBar.enabled = false;
  84. _loadingBackg.enabled = false;
  85. _canvas.enabled = true;
  86. StartCoroutine(DisableCanvasRoutine(5f));
  87. }
  88. private void SceneManagerOnActiveSceneChanged(Scene oldScene, Scene newScene)
  89. {
  90. if (newScene.name == "MenuCore")
  91. {
  92. if (_showingMessage)
  93. {
  94. _canvas.enabled = true;
  95. }
  96. }
  97. else
  98. {
  99. _canvas.enabled = false;
  100. }
  101. }
  102. private void DownloaderStateChanged()
  103. {
  104. if (DownloadController.Instance.IsDownloading)
  105. {
  106. StopAllCoroutines();
  107. _showingMessage = false;
  108. _headerText.text = "Downloading updates...";
  109. _loadingBar.enabled = false;
  110. _loadingBackg.enabled = false;
  111. _canvas.enabled = true;
  112. }
  113. if (DownloadController.Instance.IsDone)
  114. {
  115. _showingMessage = false;
  116. _headerText.text = "Updates complete";
  117. _loadingBar.enabled = false;
  118. _loadingBackg.enabled = false;
  119. StartCoroutine(DisableCanvasRoutine(5f));
  120. }
  121. }
  122. private IEnumerator DisableCanvasRoutine(float time)
  123. {
  124. yield return new WaitForSecondsRealtime(time);
  125. _canvas.enabled = false;
  126. _showingMessage = false;
  127. }
  128. private static FloatingNotification instance;
  129. protected void Awake()
  130. {
  131. if (instance != null)
  132. {
  133. Destroy(this);
  134. return;
  135. }
  136. instance = this;
  137. gameObject.transform.position = Position;
  138. gameObject.transform.eulerAngles = Rotation;
  139. gameObject.transform.localScale = Scale;
  140. _canvas = gameObject.AddComponent<Canvas>();
  141. _canvas.renderMode = RenderMode.WorldSpace;
  142. _canvas.enabled = false;
  143. var rectTransform = _canvas.transform as RectTransform;
  144. rectTransform.sizeDelta = CanvasSize;
  145. _authorNameText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, AuthorNameText, AuthorNamePosition);
  146. rectTransform = _authorNameText.transform as RectTransform;
  147. rectTransform.SetParent(_canvas.transform, false);
  148. rectTransform.anchoredPosition = AuthorNamePosition;
  149. rectTransform.sizeDelta = HeaderSize;
  150. _authorNameText.text = AuthorNameText;
  151. _authorNameText.fontSize = AuthorNameFontSize;
  152. _pluginNameText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, PluginNameText, PluginNamePosition);
  153. rectTransform = _pluginNameText.transform as RectTransform;
  154. rectTransform.SetParent(_canvas.transform, false);
  155. rectTransform.sizeDelta = HeaderSize;
  156. rectTransform.anchoredPosition = PluginNamePosition;
  157. _pluginNameText.text = PluginNameText;
  158. _pluginNameText.fontSize = PluginNameFontSize;
  159. _headerText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, HeaderText, HeaderPosition);
  160. rectTransform = _headerText.transform as RectTransform;
  161. rectTransform.SetParent(_canvas.transform, false);
  162. rectTransform.anchoredPosition = HeaderPosition;
  163. rectTransform.sizeDelta = HeaderSize;
  164. _headerText.text = HeaderText;
  165. _headerText.fontSize = HeaderFontSize;
  166. _loadingBackg = new GameObject("Background").AddComponent<Image>();
  167. rectTransform = _loadingBackg.transform as RectTransform;
  168. rectTransform.SetParent(_canvas.transform, false);
  169. rectTransform.sizeDelta = LoadingBarSize;
  170. _loadingBackg.color = BackgroundColor;
  171. _loadingBar = new GameObject("Loading Bar").AddComponent<Image>();
  172. rectTransform = _loadingBar.transform as RectTransform;
  173. rectTransform.SetParent(_canvas.transform, false);
  174. rectTransform.sizeDelta = LoadingBarSize;
  175. var tex = Texture2D.whiteTexture;
  176. var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.one * 0.5f, 100, 1);
  177. _loadingBar.sprite = sprite;
  178. _loadingBar.type = Image.Type.Filled;
  179. _loadingBar.fillMethod = Image.FillMethod.Horizontal;
  180. _loadingBar.color = new Color(1, 1, 1, 0.5f);
  181. DontDestroyOnLoad(gameObject);
  182. }
  183. /*private void Update()
  184. {
  185. if (!_canvas.enabled) return;
  186. _loadingBar.fillAmount = SongLoader.LoadingProgress;
  187. }*/
  188. }
  189. }