Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active December 18, 2023 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/ce99845f185d6f236734fcf194efa9f8 to your computer and use it in GitHub Desktop.
Save KrabCode/ce99845f185d6f236734fcf194efa9f8 to your computer and use it in GitHub Desktop.
the timer is active when clickStartedMillis is > 0, and stopped when it is -1
import com.krab.lazy.*;
LazyGui gui;
long clickStartedMillis = -1;
String currentTime;
String endTime = "";
PFont bigFont, smallFont;
color blue = color(78, 135, 163);
color grey = color(126, 156, 171);
void setup() {
size(450, 400, P2D);
smallFont = createFont("Comic Sans MS", 20);
bigFont = createFont("Comic Sans MS", 50);
textFont(bigFont);
gui = new LazyGui(this, new LazyGuiSettings().setHideBuiltInFolders(true));
}
void draw() {
background(16);
if (gui.button("start timer")) {
startTimer();
}
if (gui.button("stop timer")) {
stopTimer();
}
fill(blue);
currentTime = elapsedTimePrettyFormat();
text(currentTime, 100, 250);
fill(grey);
text(endTime, 100, 350);
}
void startTimer() {
clickStartedMillis = millis();
}
void stopTimer() {
endTime = elapsedTimePrettyFormat();
clickStartedMillis = -1;
}
String elapsedTimePrettyFormat() {
if (clickStartedMillis < 0) {
return "00:00:00";
}
long elapsedTime = millis() - clickStartedMillis;
return nf((elapsedTime / (1000*60*60)) % 24, 2, 0) + ":" +
nf((elapsedTime / (1000*60)) % 60, 2, 0) + ":" +
nf((elapsedTime / 1000) % 60, 2, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment