Tanoda
ProgressBar.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9#if UNITY_EDITOR
10using UnityEditor;
11#endif
12using System;
13using System.Collections.Generic;
14using System.Diagnostics;
15
16namespace Leap.Unity {
17
21 public interface IProgressView {
22
27 void Clear();
28
33 void DisplayProgress(string title, string info, float progress);
34 }
35
36#if UNITY_EDITOR
41 public class EditorProgressView : IProgressView {
46 public static readonly EditorProgressView Single = new EditorProgressView();
47
48 public void Clear() {
49 EditorUtility.ClearProgressBar();
50 }
51
52 public void DisplayProgress(string title, string info, float progress) {
53 EditorUtility.DisplayProgressBar(title, info, progress);
54 }
55 }
56#endif
57
66 public class ProgressBar {
67 private List<int> chunks = new List<int>();
68 private List<int> progress = new List<int>();
69 private List<string> titleStrings = new List<string>();
70 private List<string> infoStrings = new List<string>();
71 private Stopwatch stopwatch = new Stopwatch();
72
73 private bool _forceUpdate;
74
75 private IProgressView _view;
76
77#if UNITY_EDITOR
84 public ProgressBar() : this(EditorProgressView.Single) { }
85#endif
86
93 _view = view;
94 }
95
109 public void Begin(int sections, string title, string info, Action action) {
110 if (!stopwatch.IsRunning) {
111 stopwatch.Reset();
112 stopwatch.Start();
113 }
114
115 chunks.Add(sections);
116 progress.Add(0);
117 titleStrings.Add(title);
118 infoStrings.Add(info);
119
120 try {
121 _forceUpdate = true;
122 action();
123 } finally {
124 int lastIndex = chunks.Count - 1;
125 chunks.RemoveAt(lastIndex);
126 progress.RemoveAt(lastIndex);
127 titleStrings.RemoveAt(lastIndex);
128 infoStrings.RemoveAt(lastIndex);
129
130 lastIndex--;
131 if (lastIndex >= 0) {
132 progress[lastIndex]++;
133 }
134
135 if (chunks.Count == 0) {
136 _view.Clear();
137 stopwatch.Stop();
138 }
139 }
140 }
141
147 public void Step(string infoString = "") {
148 progress[progress.Count - 1]++;
149 if (stopwatch.ElapsedMilliseconds > 17 || _forceUpdate) {
150 displayBar(infoString);
151 stopwatch.Reset();
152 stopwatch.Start();
153 }
154 }
155
156 private void displayBar(string info = "") {
157 _forceUpdate = false;
158
159 float percent = 0.0f;
160 float fraction = 1.0f;
161 string titleString = "";
162 string infoString = "";
163 for (int i = 0; i < chunks.Count; i++) {
164 float chunkSize = chunks[i];
165 float chunkProgress = progress[i];
166
167 percent += fraction * (chunkProgress / chunkSize);
168 fraction /= chunkSize;
169
170 titleString += titleStrings[i];
171 infoString += infoStrings[i];
172 }
173
174 infoString += info;
175
176 _view.DisplayProgress(titleString, infoString, percent);
177 }
178 }
179}
This class allows you to easily give feedback of an action as it completes.
Definition: ProgressBar.cs:66
ProgressBar(IProgressView view)
Constructs a new progress bar given a progress view object that will display the progress information...
Definition: ProgressBar.cs:92
void Begin(int sections, string title, string info, Action action)
Begins a new chunk. If this call is made from within a chunk it will generate a sub-chunk that repres...
Definition: ProgressBar.cs:109
void Step(string infoString="")
Steps through one section of the current chunk. You can provide an optional info string that will be ...
Definition: ProgressBar.cs:147
This interface describes a generic way to update the progress of an action.
Definition: ProgressBar.cs:21
void Clear()
Clears the progress view. Is called if the action has been completed or canceled.
void DisplayProgress(string title, string info, float progress)
Updates the progress view with some title text, information, and a progress percentage that ranges fr...