2022-11-28 11:04:49 +00:00
|
|
|
/**
|
|
|
|
* @file This file provides a time menu class for editing the length of a test
|
|
|
|
* @author Arlo Filley
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - implement visual changes (borders, etc)
|
|
|
|
* - fix the timer number becoming invisible after a
|
|
|
|
* it drops below a certain amount of time
|
|
|
|
* - use getters and setters
|
|
|
|
* - use the millis() p5.js function for if the framerate becomes
|
|
|
|
* slowed down by the amount being drawn to the screen
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides the timer, which handles when a test starts and ends as well
|
|
|
|
* as providing a visual element for the user to see
|
|
|
|
*/
|
2022-09-29 19:40:57 +01:00
|
|
|
class Timer {
|
2022-10-03 10:02:55 +01:00
|
|
|
/**
|
|
|
|
* @param {int} pX
|
|
|
|
* @param {int} pY
|
|
|
|
* @param {int} pWidth
|
|
|
|
* @param {int} pHeight
|
|
|
|
* @param {int} pLayer
|
|
|
|
* @param {bool} pVisible
|
|
|
|
* @param {hexcode} pTextColor
|
|
|
|
* @param {bool} pBorder
|
|
|
|
* @param {hexcode} pBorderColor
|
|
|
|
* @param {hexcode} pBackgroundColor
|
|
|
|
* @param {int} pTime
|
|
|
|
* @param {bool} pBar
|
|
|
|
*/
|
2022-09-29 19:40:57 +01:00
|
|
|
constructor(pX, pY, pWidth, pHeight, pLayer, pVisible, pTextColor, pBorder, pBorderColor, pBackgroundColor, pTime, pBar) {
|
|
|
|
this.x = pX;
|
|
|
|
this.y = pY;
|
|
|
|
this.width = pWidth;
|
|
|
|
this.height = pHeight;
|
|
|
|
this.layer = pLayer;
|
|
|
|
this.visible = pVisible;
|
|
|
|
this.textColor = pTextColor;
|
|
|
|
this.border = pBorder;
|
|
|
|
this.borderColor = pBorderColor;
|
|
|
|
this.backgroundColor = pBackgroundColor;
|
|
|
|
|
|
|
|
this.bar = pBar;
|
|
|
|
this.startTime;
|
|
|
|
this.time = pTime;
|
|
|
|
this.timeElapsed;
|
2022-10-03 10:02:55 +01:00
|
|
|
this.ended;
|
2022-09-29 19:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getX() {
|
|
|
|
return this.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
setX(pX) {
|
|
|
|
this.x = pX;
|
|
|
|
}
|
|
|
|
|
|
|
|
getY() {
|
|
|
|
return this.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
setY(pY) {
|
|
|
|
this.y = pY;
|
|
|
|
}
|
|
|
|
|
|
|
|
getWidth() {
|
|
|
|
return this.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
setWidth(pWidth) {
|
|
|
|
this.width = pWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
getHeight() {
|
|
|
|
return this.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHeight(pHeight) {
|
|
|
|
this.height = pHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
getLayer() {
|
|
|
|
return this.layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLayer(pLayer) {
|
|
|
|
this.layer = pLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
getVisible() {
|
|
|
|
return this.visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
setVisible(pVisible) {
|
|
|
|
this.visible = pVisible;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTextColor() {
|
|
|
|
return this.textColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTextColor(pTextColor) {
|
|
|
|
this.textColor = pTextColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBorder() {
|
|
|
|
return this.border;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBorder(pBorder) {
|
|
|
|
this.border = pBorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBorderColor() {
|
|
|
|
return this.borderColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBorderColor(pBorderColor) {
|
|
|
|
this.borderColor = pBorderColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
getBackgroundColor() {
|
|
|
|
return this.backgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBackgroundColor(pBackgroundColor) {
|
|
|
|
this.backgroundColor = pBackgroundColor;
|
|
|
|
}
|
|
|
|
|
2022-10-03 10:58:22 +01:00
|
|
|
/**
|
|
|
|
* gets the amount of time the timer will run for
|
|
|
|
*/
|
2022-09-29 19:40:57 +01:00
|
|
|
getTime() {
|
|
|
|
return this.time;
|
|
|
|
}
|
|
|
|
|
2022-10-03 10:58:22 +01:00
|
|
|
/**
|
|
|
|
* sets the amount of time the timer will run for
|
|
|
|
* @param {int} pTime
|
|
|
|
*/
|
2022-09-29 19:40:57 +01:00
|
|
|
setTime(pTime) {
|
|
|
|
this.time = pTime;
|
|
|
|
}
|
|
|
|
|
2022-10-03 10:58:22 +01:00
|
|
|
/**
|
|
|
|
* This method is called to start the timer
|
|
|
|
*/
|
2022-09-29 19:40:57 +01:00
|
|
|
start() {
|
2022-12-01 14:41:14 +00:00
|
|
|
this.startTime = millis();
|
2022-09-29 19:40:57 +01:00
|
|
|
// framecount is a special p5 value that counts the number of frames that have passed
|
2022-10-03 10:02:55 +01:00
|
|
|
// I am using the amount of frames passed to calculate the time, assuming that the website is running at 60q frames
|
2022-09-29 19:40:57 +01:00
|
|
|
// per second
|
|
|
|
this.timeElapsed = 0;
|
|
|
|
}
|
|
|
|
|
2022-10-03 10:58:22 +01:00
|
|
|
/**
|
|
|
|
* This method should be called once per frame
|
|
|
|
* it keeps track of the amount of time passed
|
|
|
|
*/
|
2022-09-29 19:40:57 +01:00
|
|
|
tick() {
|
2022-12-01 14:41:14 +00:00
|
|
|
this.timeElapsed = (millis() - this.startTime) / 1000;
|
|
|
|
if (this.timeElapsed >= this.time) {
|
|
|
|
this.end();
|
|
|
|
};
|
2022-10-03 10:02:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function is called at the end of the timer
|
2022-10-03 10:58:22 +01:00
|
|
|
*/
|
2022-10-03 10:02:55 +01:00
|
|
|
end() {
|
2022-10-05 14:10:32 +01:00
|
|
|
this.visible = false;
|
|
|
|
api.validateTest();
|
2022-10-03 10:02:55 +01:00
|
|
|
this.timeElapsed = 0;
|
|
|
|
this.time = 0;
|
2022-11-18 14:51:05 +00:00
|
|
|
api.getTest();
|
2022-10-03 10:02:55 +01:00
|
|
|
// Then this function will call all other functions necessary to complete the test
|
|
|
|
// this will likely including changing the screen and interacting with the api
|
2022-10-11 10:29:40 +01:00
|
|
|
screenManager.setScreen(new EndScreen());
|
2022-09-29 19:40:57 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 10:02:55 +01:00
|
|
|
/**
|
|
|
|
* Draws the timer, uses the attributes of the class as options
|
|
|
|
*/
|
2022-09-29 19:40:57 +01:00
|
|
|
draw() {
|
2022-10-03 10:58:22 +01:00
|
|
|
// if the time shouldn't be rendered it quickly exits out of this method
|
2022-10-03 10:02:55 +01:00
|
|
|
if (!this.visible) return;
|
2022-10-11 09:45:40 +01:00
|
|
|
textAlign(LEFT);
|
2022-10-03 10:02:55 +01:00
|
|
|
|
2022-10-03 10:58:22 +01:00
|
|
|
// adds a border for the bar if one is needed
|
|
|
|
if (this.border && this.bar) {
|
|
|
|
strokeWeight(1);
|
|
|
|
stroke(this.borderColor);
|
|
|
|
// this doesn't use the fill function like other drawings
|
|
|
|
// but this adds the necessary color to the border
|
|
|
|
} else {
|
|
|
|
noStroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
// draws a bar that move across the screen to show the time left
|
2022-10-03 10:02:55 +01:00
|
|
|
if (this.bar) {
|
2022-12-01 14:41:14 +00:00
|
|
|
fill(user.colorScheme.timerBar);
|
|
|
|
rect(this.y, this.x, windowWidth - windowWidth * (this.timeElapsed / this.time), this.height);
|
2022-10-03 10:02:55 +01:00
|
|
|
}
|
|
|
|
|
2022-10-03 10:58:22 +01:00
|
|
|
// draws the text in the corner of the screen
|
|
|
|
noStroke();
|
2022-12-01 14:41:14 +00:00
|
|
|
fill(user.colorScheme.timerText);
|
|
|
|
text(Math.ceil(this.time - this.timeElapsed), this.x + this.width / 6, this.y + this.height / 2)
|
2022-09-29 19:40:57 +01:00
|
|
|
}
|
|
|
|
}
|