-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIController.cs
190 lines (166 loc) · 6.85 KB
/
UIController.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
/************************************************************************************************************************
* UICOntroller.cs - manages UI objects and their event handlers. See AudioManager for additional UI controls.
* Attached to HUDisplay in hierarchy.
* NOTE: Due to recent regression (by Unity Technologies) to v2020.1.3, et. al., dynamic parameter passing from the
* Inspector is no longer working, thus we resort to the bool 'arg' & 'showTimeControlsIsOn' work around method. See:
* h ttps://forum.unity.com/threads/unityevent-with-dynamic-parameters-not-showing-anymore-in-the-unity-inspector.746354/
************************************************************************************************************************/
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UIController : MonoBehaviour {
public static UIController Instance = null;
#pragma warning disable 0649
[SerializeField] private TMP_Text simName;
[SerializeField] private Button leftSkybox;
[SerializeField] private Button rightSkyBox;
[SerializeField] private Button autoLeft;
[SerializeField] private Button autoRight;
[SerializeField] private TMP_Text autoTimeText;
[SerializeField] private Slider time2NextIndex;
[SerializeField] private TMP_Text lightSeconds;
[SerializeField] private Button toggleUI;
[SerializeField] private GameObject[] UIMembers;
[SerializeField] private GameObject[] autoUIMembers; // Workaround method - see ToggleUI()
[SerializeField] private string[] galaxyNames;
#pragma warning restore 0649
private Coroutine autoIndexSimulation;
private float _time2NextGalaxy;
private bool pause;
private bool autoIsRunning;
private bool arg, showTimeControlsIsOn; // Workaround method - see ToggleUI()
void Awake() {
if (Instance == null) {
Instance = this;
}
else {
Destroy(gameObject);
}
}
void Start() {
_time2NextGalaxy = time2NextIndex.value;
lightSeconds.text = "";
ShowTimeControls(false);
}
void OnEnable() {
AddEventListeners();
}
void OnDestroy() {
RemoveEventListeners();
}
void Update() {
// Quit <ESC>.
if (Input.GetKeyDown(KeyCode.Escape)) {
Application.Quit();
}
// Pause <P>.
if (Input.GetKeyDown(KeyCode.P)) {
pause = !pause;
if (pause) {
Time.timeScale = 0;
}
else {
Time.timeScale = 1f;
}
}
}
public void SetGalaxyNameOnStart(int arg) {
simName.text = galaxyNames[arg];
}
// This would normally be called dynamically from Inspector. See Note in header comment.
private void ToggleUI() {
arg = !arg;
for (int i = 0; i < UIMembers.Length; i++) {
if (arg) {
UIMembers[i].SetActive(false);
}
else {
UIMembers[i].SetActive(true);
}
}
// Activate/Deactivate Time Control UI objects.
for (int i = 0; i < autoUIMembers.Length; i++) {
if (!arg && showTimeControlsIsOn) {
autoUIMembers[i].SetActive(true);
}
else {
autoUIMembers[i].SetActive(false);
}
}
}
private void SwapSkyBox(bool direction, bool playBip) {
int arg = ObjectAccessor.Instance.SkyBoxIndex;
if (direction) {
arg++;
}
else {
arg--;
}
RenderSettings.skybox = ObjectAccessor.Instance.GetSkyBox(arg); // Will also set ObjectAccessor _skyboxIndex
ObjectAccessor.Instance.SetBlackHoleProperties(); // Parameters handled at ObjectAccessor.
SwirlColorer.Instance.SetSwirlColors(); // _skyboxIndex has already been set!
GasPSOrbitor.Instance.SwapParticleOrbitor(arg); // Sets Orbiting Particle Swarm coloring.
simName.text = galaxyNames[ObjectAccessor.Instance.SkyBoxIndex];
if (playBip) {
AudioManager.Instance.PlayBip();
}
}
private void InitAutoIndexer(bool direction) {
if (!autoIsRunning) {
autoIndexSimulation = StartCoroutine(AutoIndexer(direction));
ShowTimeControls(true);
lightSeconds.gameObject.SetActive(true);
lightSeconds.text = _time2NextGalaxy.ToString("N0");
AudioManager.Instance.PlayBip();
}
}
private void StopAutoIndexer() {
if (autoIndexSimulation != null) {
StopCoroutine(autoIndexSimulation);
autoIsRunning = false;
ShowTimeControls(false);
}
}
private IEnumerator AutoIndexer(bool direction) {
autoIsRunning = true;
while (true) {
SwapSkyBox(direction, false);
yield return new WaitForSeconds(_time2NextGalaxy);
}
}
private void Time2NextIndexChange() {
_time2NextGalaxy = time2NextIndex.value;
lightSeconds.text = time2NextIndex.value.ToString("N0");
}
private void ShowTimeControls(bool showSliderAndTime) {
if (showSliderAndTime) {
time2NextIndex.gameObject.SetActive(true);
lightSeconds.text = time2NextIndex.value.ToString("N0");
autoTimeText.gameObject.SetActive(true);
showTimeControlsIsOn = true;
}
else {
time2NextIndex.gameObject.SetActive(false);
lightSeconds.text = "";
autoTimeText.gameObject.SetActive(false);
showTimeControlsIsOn = false;
}
}
private void AddEventListeners() {
toggleUI.onClick.AddListener(ToggleUI);
leftSkybox.onClick.AddListener(delegate { StopAutoIndexer(); SwapSkyBox(false, true); });
rightSkyBox.onClick.AddListener(delegate { StopAutoIndexer(); SwapSkyBox(true, true); });
autoLeft.onClick.AddListener(delegate { StopAutoIndexer(); InitAutoIndexer(false); });
autoRight.onClick.AddListener(delegate { StopAutoIndexer(); InitAutoIndexer(true); });
time2NextIndex.onValueChanged.AddListener(delegate { Time2NextIndexChange(); });
}
private void RemoveEventListeners() {
toggleUI.onClick.RemoveListener(ToggleUI);
leftSkybox.onClick.RemoveListener(delegate { StopAutoIndexer(); SwapSkyBox(false, false); });
rightSkyBox.onClick.RemoveListener(delegate { StopAutoIndexer(); SwapSkyBox(true, false); });
autoLeft.onClick.RemoveListener(delegate { StopAutoIndexer(); InitAutoIndexer(false); });
autoRight.onClick.RemoveListener(delegate { StopAutoIndexer(); InitAutoIndexer(true); });
time2NextIndex.onValueChanged.RemoveListener(delegate { Time2NextIndexChange(); });
}
}