-
Notifications
You must be signed in to change notification settings - Fork 0
/
Progress.pde
72 lines (62 loc) · 1.98 KB
/
Progress.pde
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
public class Progress {
private float _progress;
private HashMap<String, ArrayList<Float[]>> _progressChanges;
Progress() {
_progress = 0;
_progressChanges = new HashMap();
}
void applyProgressChanges() {
ArrayList<String> idsToRemove = new ArrayList();
for (String dataId : _progressChanges.keySet()) {
ArrayList<Float[]> dataList = _progressChanges.get(dataId);
for (int i = dataList.size()-1; i >= 0; i--) {
Float[] data = dataList.get(i);
Float startTime = data[0];
Float duration = data[1];
Float progressChange = data[2];
if ((float)millis()/1000 - startTime < duration) {
changeProgressBy(progressChange);
} else {
dataList.remove(i);
}
}
if (dataList.size() == 0) idsToRemove.add(dataId);
}
for (String id : idsToRemove) _progressChanges.remove(id);
}
void addProgressChange(String id, float progressChange, float duration) {
if (!_progressChanges.containsKey(id)) {
_progressChanges.put(id, new ArrayList());
}
_progressChanges.get(id).add(new Float[] { (float)millis()/1000, duration, progressChange });
}
void removeProgressChange(String id) {
if (_progressChanges.containsKey(id)) {
_progressChanges.get(id).remove(_progressChanges.get(id).size()-1);
if (_progressChanges.get(id).size() == 0) _progressChanges.remove(id);
}
}
float getTotalProgressChange() {
float total = 0;
for (String id : _progressChanges.keySet()) {
for (Float[] data : _progressChanges.get(id)) {
total += data[2];
}
}
return total;
}
void resetProgress() {
_progress = 0;
_progressChanges.clear();
}
void changeProgressBy(float delta) { _progress = min(max(_progress+delta, 0), 1); }
float getProgress() { return _progress; }
}
public class Pair<T1, T2> {
T1 first;
T2 second;
Pair(T1 v1, T2 v2) {
first = v1;
second = v2;
}
}