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.

237 lines
9.0 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. _headerText.alignment = TextAlignmentOptions.Left;
  44. _loadingBar.enabled = false;
  45. _loadingBackg.enabled = false;
  46. _canvas.enabled = true;
  47. StartCoroutine(DisableCanvasRoutine(time));
  48. }
  49. public void ShowMessage(string message)
  50. {
  51. StopAllCoroutines();
  52. _showingMessage = true;
  53. _headerText.text = message;
  54. _headerText.alignment = TextAlignmentOptions.Left;
  55. _loadingBar.enabled = false;
  56. _loadingBackg.enabled = false;
  57. _canvas.enabled = true;
  58. }
  59. protected void OnEnable()
  60. {
  61. SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
  62. DownloadController.Instance.OnDownloadStateChanged += DownloaderStateChanged;
  63. DownloadController.Instance.OnCheckForUpdates += CheckForUpdatesStart;
  64. DownloadController.Instance.OnCheckForUpdatesComplete += CheckForUpdatesDone;
  65. }
  66. protected void OnDisable()
  67. {
  68. SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
  69. DownloadController.Instance.OnDownloadStateChanged -= DownloaderStateChanged;
  70. DownloadController.Instance.OnCheckForUpdates -= CheckForUpdatesStart;
  71. DownloadController.Instance.OnCheckForUpdatesComplete -= CheckForUpdatesDone;
  72. }
  73. private void CheckForUpdatesStart()
  74. {
  75. StopAllCoroutines();
  76. _showingMessage = false;
  77. _headerText.text = HeaderText;
  78. _headerText.alignment = TextAlignmentOptions.Left;
  79. _loadingBar.enabled = false;
  80. _loadingBackg.enabled = false;
  81. _canvas.enabled = true;
  82. }
  83. private bool updatesZero = false;
  84. private void CheckForUpdatesDone(int count)
  85. {
  86. if (count == 0) updatesZero = true;
  87. else updatesZero = false;
  88. StopAllCoroutines();
  89. _showingMessage = false;
  90. _headerText.text = $"{count} updates found";
  91. _headerText.alignment = TextAlignmentOptions.Left;
  92. _loadingBar.enabled = false;
  93. _loadingBackg.enabled = false;
  94. _canvas.enabled = true;
  95. StartCoroutine(DisableCanvasRoutine(5f));
  96. }
  97. private void SceneManagerOnActiveSceneChanged(Scene oldScene, Scene newScene)
  98. {
  99. if (newScene.name == "MenuCore")
  100. {
  101. if (_showingMessage)
  102. {
  103. _canvas.enabled = true;
  104. }
  105. }
  106. else
  107. {
  108. _canvas.enabled = false;
  109. }
  110. }
  111. private void DownloaderStateChanged()
  112. {
  113. if (DownloadController.Instance.IsDownloading)
  114. {
  115. StopAllCoroutines();
  116. _showingMessage = false;
  117. _headerText.text = "Downloading updates...";
  118. _headerText.alignment = TextAlignmentOptions.Left;
  119. _loadingBar.enabled = false;
  120. _loadingBackg.enabled = false;
  121. _canvas.enabled = true;
  122. }
  123. if (DownloadController.Instance.IsDone && !updatesZero)
  124. {
  125. StopAllCoroutines();
  126. _showingMessage = false;
  127. _headerText.text = "Update complete. Restart to finish installation.";
  128. _headerText.alignment = TextAlignmentOptions.Center;
  129. _loadingBar.enabled = false;
  130. _loadingBackg.enabled = false;
  131. }
  132. }
  133. internal void Close()
  134. {
  135. StartCoroutine(DisableCanvasRoutine(0f));
  136. }
  137. private IEnumerator DisableCanvasRoutine(float time)
  138. {
  139. yield return new WaitForSecondsRealtime(time);
  140. _canvas.enabled = false;
  141. _showingMessage = false;
  142. }
  143. internal static FloatingNotification instance;
  144. protected void Awake()
  145. {
  146. if (instance != null)
  147. {
  148. Destroy(this);
  149. return;
  150. }
  151. instance = this;
  152. gameObject.transform.position = Position;
  153. gameObject.transform.eulerAngles = Rotation;
  154. gameObject.transform.localScale = Scale;
  155. _canvas = gameObject.AddComponent<Canvas>();
  156. _canvas.renderMode = RenderMode.WorldSpace;
  157. _canvas.enabled = false;
  158. var rectTransform = _canvas.transform as RectTransform;
  159. rectTransform.sizeDelta = CanvasSize;
  160. _authorNameText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, AuthorNameText, AuthorNamePosition);
  161. rectTransform = _authorNameText.transform as RectTransform;
  162. rectTransform.SetParent(_canvas.transform, false);
  163. rectTransform.anchoredPosition = AuthorNamePosition;
  164. rectTransform.sizeDelta = HeaderSize;
  165. _authorNameText.text = AuthorNameText;
  166. _authorNameText.fontSize = AuthorNameFontSize;
  167. _pluginNameText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, PluginNameText, PluginNamePosition);
  168. rectTransform = _pluginNameText.transform as RectTransform;
  169. rectTransform.SetParent(_canvas.transform, false);
  170. rectTransform.sizeDelta = HeaderSize;
  171. rectTransform.anchoredPosition = PluginNamePosition;
  172. _pluginNameText.text = PluginNameText;
  173. _pluginNameText.fontSize = PluginNameFontSize;
  174. _headerText = BeatSaberUI.CreateText(_canvas.transform as RectTransform, HeaderText, HeaderPosition);
  175. rectTransform = _headerText.transform as RectTransform;
  176. rectTransform.SetParent(_canvas.transform, false);
  177. rectTransform.anchoredPosition = HeaderPosition;
  178. rectTransform.sizeDelta = HeaderSize;
  179. _headerText.text = HeaderText;
  180. _headerText.fontSize = HeaderFontSize;
  181. _loadingBackg = new GameObject("Background").AddComponent<Image>();
  182. rectTransform = _loadingBackg.transform as RectTransform;
  183. rectTransform.SetParent(_canvas.transform, false);
  184. rectTransform.sizeDelta = LoadingBarSize;
  185. _loadingBackg.color = BackgroundColor;
  186. _loadingBar = new GameObject("Loading Bar").AddComponent<Image>();
  187. rectTransform = _loadingBar.transform as RectTransform;
  188. rectTransform.SetParent(_canvas.transform, false);
  189. rectTransform.sizeDelta = LoadingBarSize;
  190. var tex = Texture2D.whiteTexture;
  191. var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.one * 0.5f, 100, 1);
  192. _loadingBar.sprite = sprite;
  193. _loadingBar.type = Image.Type.Filled;
  194. _loadingBar.fillMethod = Image.FillMethod.Horizontal;
  195. _loadingBar.color = new Color(1, 1, 1, 0.5f);
  196. DontDestroyOnLoad(gameObject);
  197. }
  198. /*private void Update()
  199. {
  200. if (!_canvas.enabled) return;
  201. _loadingBar.fillAmount = SongLoader.LoadingProgress;
  202. }*/
  203. }
  204. }