ProgressBar.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using HMUI;
  2. using SongLoaderPlugin;
  3. using SongLoaderPlugin.OverrideClasses;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using TMPro;
  11. using UnityEngine;
  12. using UnityEngine.SceneManagement;
  13. namespace SongBrowserPlugin.UI
  14. {
  15. // Modified version of:
  16. // https://raw.githubusercontent.com/xyonico/BeatSaberSongLoader/master/SongLoaderPlugin/ProgressBar.cs
  17. // - Adjusted position
  18. // - Repurposed.
  19. public class ProgressBar : MonoBehaviour
  20. {
  21. private Canvas _canvas;
  22. private TMP_Text _creditText;
  23. private TMP_Text _headerText;
  24. private Image _loadingBackg;
  25. private Image _loadingBar;
  26. private static readonly Vector3 Position = new Vector3(0, 0f, 2.5f);
  27. private static readonly Vector3 Rotation = new Vector3(0, 0, 0);
  28. private static readonly Vector3 Scale = new Vector3(0.01f, 0.01f, 0.01f);
  29. private static readonly Vector2 CanvasSize = new Vector2(200, 50);
  30. private static readonly Vector2 CreditPosition = new Vector2(50, 22);
  31. private const string CreditText = "Song Browser Plugin <size=75%>by Halsafar</size>";
  32. private const float CreditFontSize = 9f;
  33. private static readonly Vector2 HeaderPosition = new Vector2(50, 15);
  34. private static readonly Vector2 HeaderSize = new Vector2(200, 20);
  35. private const string HeaderText = "Processing songs...";
  36. private const float HeaderFontSize = 12f;
  37. private static readonly Vector2 LoadingBarSize = new Vector2(100, 10);
  38. private static readonly Color BackgroundColor = new Color(0, 0, 0, 0.2f);
  39. private bool _showingMessage;
  40. public static ProgressBar Create()
  41. {
  42. return new GameObject("Progress Bar").AddComponent<ProgressBar>();
  43. }
  44. public void ShowMessage(string message, float time)
  45. {
  46. _showingMessage = true;
  47. _headerText.text = message;
  48. _loadingBar.enabled = false;
  49. _loadingBackg.enabled = false;
  50. _canvas.enabled = true;
  51. StartCoroutine(DisableCanvasRoutine(time));
  52. }
  53. public void ShowMessage(string message)
  54. {
  55. _showingMessage = true;
  56. _headerText.text = message;
  57. _loadingBar.enabled = false;
  58. _loadingBackg.enabled = false;
  59. _canvas.enabled = true;
  60. }
  61. private void OnEnable()
  62. {
  63. SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged;
  64. SongLoader.SongsLoadedEvent += SongLoaderOnSongsLoadedEvent;
  65. SongBrowserModel.didFinishProcessingSongs += SongBrowserFinishedProcessingSongs;
  66. }
  67. private void OnDisable()
  68. {
  69. SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged;
  70. SongLoader.SongsLoadedEvent -= SongLoaderOnSongsLoadedEvent;
  71. SongBrowserModel.didFinishProcessingSongs -= SongBrowserFinishedProcessingSongs;
  72. }
  73. private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene arg1)
  74. {
  75. if (arg1.buildIndex == 1)
  76. {
  77. if (_showingMessage)
  78. {
  79. _canvas.enabled = true;
  80. }
  81. }
  82. else
  83. {
  84. _canvas.enabled = false;
  85. }
  86. }
  87. private void SongLoaderOnSongsLoadedEvent(SongLoader arg1, List<CustomLevel> arg2)
  88. {
  89. _showingMessage = false;
  90. _headerText.text = HeaderText;
  91. _loadingBar.enabled = true;
  92. _loadingBackg.enabled = true;
  93. _canvas.enabled = true;
  94. }
  95. private void SongBrowserFinishedProcessingSongs(List<CustomLevel> arg2)
  96. {
  97. _showingMessage = false;
  98. _headerText.text = arg2.Count + " songs processed.";
  99. _loadingBar.enabled = false;
  100. _loadingBackg.enabled = false;
  101. StartCoroutine(DisableCanvasRoutine(5f));
  102. }
  103. private IEnumerator DisableCanvasRoutine(float time)
  104. {
  105. yield return new WaitForSecondsRealtime(time);
  106. _canvas.enabled = false;
  107. _showingMessage = false;
  108. }
  109. private void Awake()
  110. {
  111. gameObject.transform.position = Position;
  112. gameObject.transform.eulerAngles = Rotation;
  113. gameObject.transform.localScale = Scale;
  114. _canvas = gameObject.AddComponent<Canvas>();
  115. _canvas.renderMode = RenderMode.WorldSpace;
  116. _canvas.enabled = false;
  117. var rectTransform = _canvas.transform as RectTransform;
  118. rectTransform.sizeDelta = CanvasSize;
  119. _creditText = new GameObject("Credit").AddComponent<TextMeshProUGUI>();
  120. rectTransform = _creditText.transform as RectTransform;
  121. rectTransform.SetParent(_canvas.transform, false);
  122. rectTransform.anchoredPosition = CreditPosition;
  123. rectTransform.sizeDelta = HeaderSize;
  124. _creditText.text = CreditText;
  125. _creditText.fontSize = CreditFontSize;
  126. _headerText = new GameObject("Header").AddComponent<TextMeshProUGUI>();
  127. rectTransform = _headerText.transform as RectTransform;
  128. rectTransform.SetParent(_canvas.transform, false);
  129. rectTransform.anchoredPosition = HeaderPosition;
  130. rectTransform.sizeDelta = HeaderSize;
  131. _headerText.text = HeaderText;
  132. _headerText.fontSize = HeaderFontSize;
  133. _loadingBackg = new GameObject("Background").AddComponent<Image>();
  134. rectTransform = _loadingBackg.transform as RectTransform;
  135. rectTransform.SetParent(_canvas.transform, false);
  136. rectTransform.sizeDelta = LoadingBarSize;
  137. _loadingBackg.color = BackgroundColor;
  138. _loadingBar = new GameObject("Loading Bar").AddComponent<Image>();
  139. rectTransform = _loadingBar.transform as RectTransform;
  140. rectTransform.SetParent(_canvas.transform, false);
  141. rectTransform.sizeDelta = LoadingBarSize;
  142. var tex = Texture2D.whiteTexture;
  143. var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.one * 0.5f, 100, 1);
  144. _loadingBar.sprite = sprite;
  145. _loadingBar.type = Image.Type.Filled;
  146. _loadingBar.fillMethod = Image.FillMethod.Horizontal;
  147. _loadingBar.color = new Color(1, 1, 1, 0.5f);
  148. DontDestroyOnLoad(gameObject);
  149. }
  150. private void Update()
  151. {
  152. if (!_canvas.enabled) return;
  153. }
  154. }
  155. }