-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionBar.pde
More file actions
137 lines (123 loc) · 3.99 KB
/
SessionBar.pde
File metadata and controls
137 lines (123 loc) · 3.99 KB
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
//
// IceTimer - Graphical timer and match scheduler for pick-up ice hockey
// Copyright (C) 2014 Joe Cridge
//
// This file is part of IceTimer.
//
// IceTimer is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// IceTimer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with IceTimer. If not, see <http://www.gnu.org/licenses/>.
//
// Joe Cridge, June 2014.
// <joe.cridge@me.com>
//
class SessionBar {
int startMillis, totalMillis, millisElapsed, millisRemaining, hoursElapsed, minsElapsed, hoursRemaining, minsRemaining;
boolean isActive, hasChangedColour;
color highlightColour;
float progress;
String timeElapsed, timeRemaining;
PFont barFont;
SessionBar() {
// Load font
barFont = loadFont("fonts/SquarishSansCTRegular-24.vlw");
// Initialise variables
timeElapsed = String.format("%02d" + ":" + "%02d", 0, 0);
timeRemaining = String.format("%02d" + ":" + "%02d", 0, 0);
isActive = false;
hasChangedColour = false;
}
void activate(int endHour, int endMin) {
// Calculate time remaining until inputted end time
int hours = endHour - hour();
if (hours < 0) {
hours += 24;
}
int mins = endMin - minute();
int secs = -1 * second();
totalMillis = 1000 * (secs + 60 * (mins + 60 * hours));
startMillis = millis();
millisElapsed = 0;
millisRemaining = totalMillis;
progress = 0.0;
isActive = true;
}
void deactivate() {
millisRemaining = 0;
isActive = false;
hasChangedColour = false;
}
void display() {
if (isActive) {
// Set highlight according to time remaining
// Boolean flags change so that rest of UI will update
if (millisRemaining > 1000*60*15) {
// Green if more than 15 mins
if (highlightColour != TIMER_GREEN) {
highlightColour = TIMER_GREEN;
hasChangedColour = true;
}
} else if (millisRemaining > 1000*60*5) {
// Amber for 5 to 25 mins
if (highlightColour != TIMER_AMBER) {
highlightColour = TIMER_AMBER;
hasChangedColour = true;
}
} else {
if (highlightColour != TIMER_RED) {
// Red for less than 5 mins
highlightColour = TIMER_RED;
hasChangedColour = true;
}
}
} else {
highlightColour = TIMER_GREY;
}
// Generate clock string
hoursRemaining = floor(millisRemaining / (1000.0*60.0*60.0));
minsRemaining = ceil((millisRemaining - hoursRemaining*1000.0*60.0*60.0) / (1000.0*60.0));
timeRemaining = String.format("%02d" + ":" + "%02d", hoursRemaining, minsRemaining);
// Draw panel
fill(0);
rect(0, height-40, width, 40);
// Draw shadow
for (int i = 0; i < 15; i++) {
fill(0, 0, 0, 90-6*i);
rect(0, height-(40+i), width, 1);
}
// Print session clock
fill(highlightColour);
textFont(barFont);
textAlign(LEFT);
text("Session", 10, height-13);
textAlign(RIGHT);
text(timeRemaining, width-11, height-13);
// Draw progress bar
fill(highlightColour);
rect(121, height-20, width-223, 2);
fill(TIMER_GREY);
rect(121, height-20, (width-223.0)*progress, 2);
}
color getHighlightColour() {
return highlightColour;
}
void update() {
// Update session timer during session
if (isActive && millisRemaining > 0) {
millisElapsed = millis() - startMillis;
progress = (1.0*millisElapsed) / (1.0*totalMillis);
millisRemaining = totalMillis - millisElapsed;
} else if (isActive) {
deactivate();
}
}
}