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