ProgressBar.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using SongCore.Utilities;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEngine.UI;
  9. namespace SongCore
  10. {
  11. public class ProgressBar : MonoBehaviour
  12. {
  13. private Canvas _canvas;
  14. private TMP_Text _authorNameText;
  15. private TMP_Text _pluginNameText;
  16. private TMP_Text _headerText;
  17. internal Image _loadingBackg;
  18. internal Image _loadingBar;
  19. private static readonly Vector3 Position = new Vector3(0, 2.5f, 2.5f);
  20. private static readonly Vector3 Rotation = new Vector3(0, 0, 0);
  21. private static readonly Vector3 Scale = new Vector3(0.01f, 0.01f, 0.01f);
  22. private static readonly Vector2 CanvasSize = new Vector2(100, 50);
  23. private const string AuthorNameText = "";
  24. private const float AuthorNameFontSize = 7f;
  25. private static readonly Vector2 AuthorNamePosition = new Vector2(10, 31);
  26. private const string PluginNameText = "SongCore Loader";
  27. private const float PluginNameFontSize = 9f;
  28. private static readonly Vector2 PluginNamePosition = new Vector2(10, 23);
  29. private static readonly Vector2 HeaderPosition = new Vector2(10, 15);
  30. private static readonly Vector2 HeaderSize = new Vector2(100, 20);
  31. private const string HeaderText = "Loading songs...";
  32. private const float HeaderFontSize = 15f;
  33. private static readonly Vector2 LoadingBarSize = new Vector2(100, 10);
  34. private static readonly Color BackgroundColor = new Color(0, 0, 0, 0.2f);
  35. private bool _showingMessage;
  36. public static ProgressBar Create()
  37. {
  38. return new GameObject("Progress Bar").AddComponent<ProgressBar>();
  39. }
  40. public void ShowMessage(string message, float time)
  41. {
  42. StopAllCoroutines();
  43. _showingMessage = true;
  44. _headerText.text = message;
  45. _loadingBar.enabled = false;
  46. _loadingBackg.enabled = false;
  47. _canvas.enabled = true;
  48. StartCoroutine(DisableCanvasRoutine(time));
  49. }
  50. public void ShowMessage(string message)
  51. {
  52. StopAllCoroutines();
  53. _showingMessage = true;
  54. _headerText.text = message;
  55. _loadingBar.enabled = false;
  56. _loadingBackg.enabled = false;
  57. _canvas.enabled = true;
  58. }
  59. private void OnEnable()
  60. {
  61. SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
  62. Loader.LoadingStartedEvent += SongLoaderOnLoadingStartedEvent;
  63. Loader.SongsLoadedEvent += SongLoaderOnSongsLoadedEvent;
  64. }
  65. private void OnDisable()
  66. {
  67. SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
  68. Loader.LoadingStartedEvent -= SongLoaderOnLoadingStartedEvent;
  69. Loader.SongsLoadedEvent -= SongLoaderOnSongsLoadedEvent;
  70. }
  71. private void SceneManagerOnActiveSceneChanged(Scene oldScene, Scene newScene)
  72. {
  73. if (newScene.name == "MenuCore")
  74. {
  75. if (_showingMessage)
  76. {
  77. _canvas.enabled = true;
  78. }
  79. }
  80. else
  81. {
  82. _canvas.enabled = false;
  83. }
  84. }
  85. private void SongLoaderOnLoadingStartedEvent(Loader obj)
  86. {
  87. StopAllCoroutines();
  88. _showingMessage = false;
  89. _headerText.text = HeaderText;
  90. _loadingBar.enabled = true;
  91. _loadingBackg.enabled = true;
  92. _canvas.enabled = true;
  93. }
  94. private void SongLoaderOnSongsLoadedEvent(Loader loader, ConcurrentDictionary<string, CustomPreviewBeatmapLevel> customLevels)
  95. {
  96. _showingMessage = false;
  97. _headerText.text = customLevels.Count + " songs loaded.";
  98. _loadingBar.enabled = false;
  99. _loadingBackg.enabled = false;
  100. StartCoroutine(DisableCanvasRoutine(5f));
  101. }
  102. private IEnumerator DisableCanvasRoutine(float time)
  103. {
  104. yield return new WaitForSecondsRealtime(time);
  105. _canvas.enabled = false;
  106. _showingMessage = false;
  107. }
  108. private void Awake()
  109. {
  110. gameObject.transform.position = Position;
  111. gameObject.transform.eulerAngles = Rotation;
  112. gameObject.transform.localScale = Scale;
  113. _canvas = gameObject.AddComponent<Canvas>();
  114. _canvas.renderMode = RenderMode.WorldSpace;
  115. _canvas.enabled = false;
  116. var rectTransform = _canvas.transform as RectTransform;
  117. rectTransform.sizeDelta = CanvasSize;
  118. _authorNameText = Utils.CreateText(_canvas.transform as RectTransform, AuthorNameText, AuthorNamePosition);
  119. rectTransform = _authorNameText.transform as RectTransform;
  120. rectTransform.SetParent(_canvas.transform, false);
  121. rectTransform.anchoredPosition = AuthorNamePosition;
  122. rectTransform.sizeDelta = HeaderSize;
  123. _authorNameText.text = AuthorNameText;
  124. _authorNameText.fontSize = AuthorNameFontSize;
  125. _pluginNameText = Utils.CreateText(_canvas.transform as RectTransform, PluginNameText, PluginNamePosition);
  126. rectTransform = _pluginNameText.transform as RectTransform;
  127. rectTransform.SetParent(_canvas.transform, false);
  128. rectTransform.sizeDelta = HeaderSize;
  129. rectTransform.anchoredPosition = PluginNamePosition;
  130. _pluginNameText.text = PluginNameText;
  131. _pluginNameText.fontSize = PluginNameFontSize;
  132. _headerText = Utils.CreateText(_canvas.transform as RectTransform, HeaderText, HeaderPosition);
  133. rectTransform = _headerText.transform as RectTransform;
  134. rectTransform.SetParent(_canvas.transform, false);
  135. rectTransform.anchoredPosition = HeaderPosition;
  136. rectTransform.sizeDelta = HeaderSize;
  137. _headerText.text = HeaderText;
  138. _headerText.fontSize = HeaderFontSize;
  139. _loadingBackg = new GameObject("Background").AddComponent<Image>();
  140. rectTransform = _loadingBackg.transform as RectTransform;
  141. rectTransform.SetParent(_canvas.transform, false);
  142. rectTransform.sizeDelta = LoadingBarSize;
  143. _loadingBackg.color = BackgroundColor;
  144. _loadingBar = new GameObject("Loading Bar").AddComponent<Image>();
  145. rectTransform = _loadingBar.transform as RectTransform;
  146. rectTransform.SetParent(_canvas.transform, false);
  147. rectTransform.sizeDelta = LoadingBarSize;
  148. var tex = Texture2D.whiteTexture;
  149. var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.one * 0.5f, 100, 1);
  150. _loadingBar.sprite = sprite;
  151. _loadingBar.type = Image.Type.Filled;
  152. _loadingBar.fillMethod = Image.FillMethod.Horizontal;
  153. _loadingBar.color = new Color(1, 1, 1, 0.5f);
  154. DontDestroyOnLoad(gameObject);
  155. }
  156. private void Update()
  157. {
  158. if (!_canvas.enabled) return;
  159. _loadingBar.fillAmount = Loader.LoadingProgress;
  160. _loadingBar.color = HSBColor.ToColor(new HSBColor(Mathf.PingPong(Time.time * 0.35f, 1), 1, 1));
  161. _headerText.color = HSBColor.ToColor(new HSBColor(Mathf.PingPong(Time.time * 0.35f, 1), 1, 1));
  162. }
  163. }
  164. }