AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ProgressIndicator.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using HoloToolkit.Unity;
5 using UnityEngine;
6 
7 namespace HoloToolkit.UX.Progress
8 {
13  public class ProgressIndicator : Singleton<ProgressIndicator>
14  {
15  const float SmoothProgressSpeed = 10f;
16 
20  public bool IsLoading
21  {
22  get
23  {
24  return Instance.gameObject.activeSelf;
25  }
26  }
27 
28  [SerializeField]
29  private IndicatorStyleEnum defaultIndicatorStyle = IndicatorStyleEnum.AnimatedOrbs;
30  [SerializeField]
31  private ProgressStyleEnum defaultProgressStyle = ProgressStyleEnum.Percentage;
32  //[SerializeField]
33  //private ProgressMessageStyleEnum defaultMessageStyle = ProgressMessageStyleEnum.Visible;
34 
35  // The default prefab used by the 'Prefab' indicator style
36  [SerializeField]
37  private GameObject defaultPrefab = null;
38 
39  // The default icon used by the 'StaticIcon' indicator style
40  [SerializeField]
41  private GameObject defaultIconPrefab = null;
42 
43  [SerializeField]
44  private GameObject defaultOrbsPrefab = null;
45 
46  // The progress bar container object
47  [SerializeField]
48  private GameObject progressBarContainer = null;
49 
50  // The animated progress bar object
51  [SerializeField]
52  private Transform progressBar = null;
53 
54  // The message text used by the 'Visible' message style
55  [SerializeField]
56  private TextMesh messageText = null;
57 
58  // The progress text used by all non-'None' progress styles
59  [SerializeField]
60  private TextMesh progressText = null;
61 
62  [SerializeField]
63  private Animator animator = null;
64 
65  public float Progress
66  {
67  get
68  {
69  return smoothProgress;
70  }
71  }
72 
73  private float smoothProgress = 0f;
74  private float targetProgress = 0f;
75  private bool closing = false;
76  private GameObject instantiatedCustomObject;
77 
81  public string ProgressFormat = "0.0";
82 
91  public void Open(IndicatorStyleEnum indicatorStyle, ProgressStyleEnum progressStyle, ProgressMessageStyleEnum messageStyle, string message = "", GameObject prefab = null)
92  {
93  if (gameObject.activeSelf)
94  {
95  return;
96  }
97 
98  // Make sure we aren't parented under anything
99  transform.parent = null;
100 
101  // Turn our common objects on
102  closing = false;
103  gameObject.SetActive(true);
104  progressText.gameObject.SetActive(progressStyle == ProgressStyleEnum.Percentage);
105  progressBarContainer.gameObject.SetActive(progressStyle == ProgressStyleEnum.ProgressBar);
106  messageText.gameObject.SetActive(messageStyle != ProgressMessageStyleEnum.None);
107  messageText.text = message;
108 
109  // Reset our loading progress
110  smoothProgress = 0f;
111  targetProgress = 0f;
112 
113  // Re-enable objects based on our style
114  switch (indicatorStyle)
115  {
116  case IndicatorStyleEnum.None:
117  break;
118 
119  case IndicatorStyleEnum.StaticIcon:
120  // Instantiate our custom object under our animator
121  if (defaultIconPrefab == null)
122  {
123  UnityEngine.Debug.LogError("No Icon prefab available in loading dialog, spawning without one");
124  }
125  else
126  {
127  instantiatedCustomObject = GameObject.Instantiate(defaultIconPrefab) as GameObject;
128  instantiatedCustomObject.transform.localPosition = new Vector3(0.0f, 13.0f, 0.0f);
129  instantiatedCustomObject.transform.localRotation = Quaternion.identity;
130  instantiatedCustomObject.transform.localScale = new Vector3(10.0f, 10.0f, 10.0f);
131 
132  instantiatedCustomObject.transform.Translate(messageText.transform.position);
133  instantiatedCustomObject.transform.SetParent(messageText.transform, false);
134  }
135  break;
136 
137  case IndicatorStyleEnum.AnimatedOrbs:
138  if (defaultOrbsPrefab != null)
139  {
140  instantiatedCustomObject = GameObject.Instantiate(defaultOrbsPrefab) as GameObject;
141  instantiatedCustomObject.transform.localPosition = new Vector3(0.0f, 25.0f, 0.0f);
142  //instantiatedCustomObject.transform.localRotation = Quaternion.identity;
143  instantiatedCustomObject.transform.localScale = new Vector3(3.0f, 3.0f, 3.0f);
144 
145  instantiatedCustomObject.transform.Translate(messageText.transform.position);
146  instantiatedCustomObject.transform.SetParent(messageText.transform, false);
147  }
148  break;
149 
150  case IndicatorStyleEnum.Prefab:
151  // Instantiate our custom object under our animator
152  if (defaultPrefab == null && prefab == null)
153  {
154  UnityEngine.Debug.LogError("No prefab available in loading dialog, spawning without one");
155  }
156  else
157  {
158  instantiatedCustomObject = GameObject.Instantiate(defaultPrefab) as GameObject;
159  instantiatedCustomObject.transform.localPosition = new Vector3(0.0f, 20.0f, 0.0f);
160  instantiatedCustomObject.transform.localRotation = Quaternion.identity;
161  instantiatedCustomObject.transform.localScale = new Vector3(10.0f, 10.0f, 10.0f);
162 
163  instantiatedCustomObject.transform.Translate(messageText.transform.position);
164  instantiatedCustomObject.transform.SetParent(messageText.transform, false);
165  }
166  break;
167  }
168  animator.SetTrigger("Open");
169  }
170 
175  public void Open(string message)
176  {
177  Open(defaultIndicatorStyle, defaultProgressStyle, ProgressMessageStyleEnum.Visible, message, null);
178  }
179 
185  public void SetMessage(string message)
186  {
187  if (!gameObject.activeSelf) { return; }
188 
189  messageText.text = message;
190  }
191 
197  public void SetProgress(float progress)
198  {
199  targetProgress = Mathf.Clamp01(progress) * 100;
200  // If progress is 100, assume we want to snap to that value
201  if (targetProgress == 100)
202  {
203  smoothProgress = targetProgress;
204  if (instantiatedCustomObject != null && instantiatedCustomObject.name.Contains("Orbs"))
205  {
206  instantiatedCustomObject.GetComponent<ProgressIndicatorOrbsRotator>().Stop();
207  }
208  }
209  }
210 
214  public void Close()
215  {
216  if (!gameObject.activeSelf) { return; }
217 
218  closing = true;
219  progressText.gameObject.SetActive(false);
220  messageText.gameObject.SetActive(false);
221  animator.SetTrigger("Close");
222  }
223 
224  private void Start()
225  {
226  gameObject.SetActive(false);
227  progressText.gameObject.SetActive(false);
228  messageText.gameObject.SetActive(false);
229  }
230 
231  private void Update()
232  {
233  smoothProgress = Mathf.Lerp(smoothProgress, targetProgress, Time.deltaTime * SmoothProgressSpeed);
234  progressBar.localScale = new Vector3(smoothProgress / 100, 1f, 1f);
235  progressText.text = smoothProgress.ToString(ProgressFormat) + "%";
236 
237  // If we're closing, wait for the animator to reach the closed state
238  if (closing)
239  {
240  if (animator.GetCurrentAnimatorStateInfo(0).IsName("Closed"))
241  {
242  // Once we've reached the cloesd state shut down completely
243  closing = false;
244  transform.parent = null;
245  gameObject.SetActive(false);
246 
247  // Destroy our custom object if we made one
248  if (instantiatedCustomObject != null)
249  {
250  GameObject.Destroy(instantiatedCustomObject);
251  }
252  }
253  }
254  }
255  }
256 }
void Open(IndicatorStyleEnum indicatorStyle, ProgressStyleEnum progressStyle, ProgressMessageStyleEnum messageStyle, string message="", GameObject prefab=null)
Opens the dialog with full custom options
void SetProgress(float progress)
Updates progress. Has no effect until Open is called.
void SetMessage(string message)
Updates message. Has no effect until Open is called.
ProgressMessageStyleEnum
The style of the Message portion of a Progress Indicator
IndicatorStyleEnum
Enum describing the style of the Indicator portion
void Close()
Initiates the process of closing the dialog.
void Open(string message)
Opens the dialog with default settings for indicator and progress
This class manages the &#39;rotating circle of dots&#39; effect that is used as a Progress Indicator effect...
Singleton for displaying a simple loading dialog Can be combined with a radial solver to keep locked ...
Singleton behaviour class, used for components that should only have one instance.
Definition: Singleton.cs:14