2022-11-28 11:04:49 +00:00
|
|
|
/**
|
|
|
|
* @file This file provides the textbox class for taking user input
|
|
|
|
* @author Arlo Filley
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - add all characters a user could press
|
|
|
|
* - refactor the code displaying the characters. It can become slow after lots of typing
|
|
|
|
* - password mode, where the charcters are hidden from the user
|
|
|
|
* - getters and setters
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class takes input from the user and displays it some form
|
|
|
|
* it handles the test input from the user, and the login and sign
|
|
|
|
* up pages.
|
|
|
|
*/
|
2022-09-29 16:22:48 +01:00
|
|
|
class Textbox {
|
2022-11-04 12:18:45 +00:00
|
|
|
/**
|
|
|
|
* Creates a new instance of the Textbox class
|
|
|
|
* @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
|
|
|
|
*/
|
2022-11-18 12:27:54 +00:00
|
|
|
constructor(
|
|
|
|
pX, pY,
|
|
|
|
pWidth, pHeight,
|
|
|
|
pLayer, pVisible,
|
2022-12-01 14:41:14 +00:00
|
|
|
pTextColor = user.colorScheme.text,
|
2022-11-18 12:27:54 +00:00
|
|
|
pBorder, pBorderColor,
|
|
|
|
pBackgroundColor,
|
2022-12-01 14:41:14 +00:00
|
|
|
pLine, pIsTest,
|
|
|
|
pIsPassword = false
|
2022-11-18 12:27:54 +00:00
|
|
|
) {
|
2022-09-29 16:22:48 +01:00
|
|
|
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.letters = [];
|
2022-09-29 19:11:20 +01:00
|
|
|
this.allowedLetters = [
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
|
2022-10-03 10:02:55 +01:00
|
|
|
'l', 'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
2022-12-01 14:41:14 +00:00
|
|
|
'x', 'y', 'z',
|
|
|
|
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
|
|
|
|
'\'', '"', ',', '.', ' ', '!', '@', '$', '%', '^', '&', '*', '(', ')',
|
2022-09-29 19:11:20 +01:00
|
|
|
]
|
2022-11-18 12:27:54 +00:00
|
|
|
|
|
|
|
this.line = pLine;
|
|
|
|
this.isTest = pIsTest;
|
|
|
|
|
|
|
|
if (this.isTest) {
|
|
|
|
this.testContent = user.nextTest;
|
2022-11-28 14:17:27 +00:00
|
|
|
this.currentLine = 0;
|
|
|
|
this.words = [""];
|
|
|
|
} else {
|
|
|
|
this.words = "";
|
2022-11-18 12:27:54 +00:00
|
|
|
}
|
2022-12-01 14:41:14 +00:00
|
|
|
|
|
|
|
this.isPassword = pIsPassword;
|
|
|
|
|
|
|
|
this.goodColor = user.colorScheme.testGood;
|
|
|
|
this.badColor = user.colorScheme.testBad;
|
2022-09-29 16:22:48 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
getLetters() {
|
|
|
|
return this.letters;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLetters(pLetters) {
|
|
|
|
this.letters = pLetters;
|
|
|
|
}
|
|
|
|
|
2022-11-04 12:18:45 +00:00
|
|
|
/**
|
|
|
|
* takes a key and handles it in the textbox
|
|
|
|
* @param {String} pKey
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-09-29 16:22:48 +01:00
|
|
|
letterTyped(pKey) {
|
2022-11-04 12:18:45 +00:00
|
|
|
if (pKey === "Backspace" && this.letters.length > 0) {
|
2022-11-28 14:17:27 +00:00
|
|
|
this.letters.pop();
|
|
|
|
|
|
|
|
if (this.isTest) {
|
|
|
|
this.words[this.currentLine] = this.words[this.currentLine].substring(0, this.words[this.currentLine].length-1);
|
|
|
|
} else {
|
|
|
|
this.words = this.words.substring(0, this.words.length-1)
|
|
|
|
}
|
2022-09-29 19:11:20 +01:00
|
|
|
return;
|
2022-09-29 16:22:48 +01:00
|
|
|
}
|
2022-09-29 19:11:20 +01:00
|
|
|
|
2022-11-11 13:04:13 +00:00
|
|
|
for (let i = 0; i < this.allowedLetters.length; i++) {
|
2022-09-29 19:11:20 +01:00
|
|
|
if (pKey.toLowerCase() === this.allowedLetters[i]) {
|
|
|
|
this.letters.push(pKey);
|
2022-11-28 14:17:27 +00:00
|
|
|
if (this.isTest) {
|
|
|
|
this.words[this.currentLine] += pKey;
|
|
|
|
} else {
|
|
|
|
this.words += pKey;
|
|
|
|
}
|
2022-09-29 16:22:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getWords() {
|
2022-11-29 12:21:38 +00:00
|
|
|
let text = "";
|
|
|
|
for (let i = 0; i < this.words.length; i++) {
|
|
|
|
text += this.words[i];
|
|
|
|
}
|
|
|
|
return text;
|
2022-09-29 16:22:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setWords(pWords) {
|
|
|
|
this.words = pWords;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllowedLetters() {
|
|
|
|
return this.allowedLetters;
|
|
|
|
}
|
|
|
|
|
|
|
|
setAllowedLetters(pAllowedLetters) {
|
|
|
|
this.allowedLetters = pAllowedLetters;
|
|
|
|
}
|
2022-09-29 19:11:20 +01:00
|
|
|
|
2022-11-29 12:21:38 +00:00
|
|
|
getTestContent() {
|
|
|
|
let text = "";
|
|
|
|
for (let i = 0; i < this.testContent.length; i++) {
|
|
|
|
text += this.testContent[i];
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2022-11-04 12:18:45 +00:00
|
|
|
/**
|
|
|
|
* draws a Textbox
|
|
|
|
* @returns
|
|
|
|
*/
|
2022-09-29 19:11:20 +01:00
|
|
|
draw() {
|
2022-10-11 09:45:40 +01:00
|
|
|
// doesn't render the textbox if it should not be visible to the user.
|
2022-10-11 10:29:40 +01:00
|
|
|
if (!this.visible) {
|
2022-10-11 09:45:40 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-10-11 10:29:40 +01:00
|
|
|
|
|
|
|
noStroke();
|
|
|
|
// sets a border if there should be one
|
|
|
|
if (this.border) {
|
|
|
|
stroke(this.borderColor);
|
|
|
|
strokeWeight(1);
|
|
|
|
}
|
2022-10-11 09:45:40 +01:00
|
|
|
|
|
|
|
// sets the parameters of what the text should look like;
|
2022-10-11 10:29:40 +01:00
|
|
|
fill(this.textColor);
|
2022-09-29 19:11:20 +01:00
|
|
|
textSize(23);
|
2022-10-11 09:45:40 +01:00
|
|
|
textAlign(LEFT);
|
2022-11-28 14:17:27 +00:00
|
|
|
if (this.words.length === 0 && this.line) {
|
2022-11-18 12:27:54 +00:00
|
|
|
fill("#000")
|
|
|
|
rect(this.x, this.y-15, 1, 30)
|
|
|
|
}
|
2022-09-29 19:11:20 +01:00
|
|
|
|
|
|
|
// these variables allow me to use the values of x and y while updating them
|
|
|
|
let i = this.x;
|
|
|
|
let j = this.y;
|
|
|
|
|
2022-11-18 12:27:54 +00:00
|
|
|
if (this.isTest) {
|
|
|
|
let i = this.x;
|
|
|
|
let j = this.y;
|
|
|
|
|
2022-11-29 12:21:38 +00:00
|
|
|
if (this.words[this.currentLine].length >= this.testContent[this.currentLine].length) {
|
2022-11-28 14:17:27 +00:00
|
|
|
this.currentLine++;
|
|
|
|
this.words.push("");
|
2022-11-18 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 14:17:27 +00:00
|
|
|
if (this.currentLine > 0) {
|
|
|
|
for (let x = 0; x < this.testContent[this.currentLine-1].length; x++) {
|
|
|
|
if (x < this.words[this.currentLine-1].length) {
|
|
|
|
if (this.words[this.currentLine-1][x] === this.testContent[this.currentLine-1][x]) {
|
|
|
|
fill("#00AA0044");
|
|
|
|
} else {
|
|
|
|
fill("#AA000044");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fill("#00044");
|
|
|
|
}
|
|
|
|
text(this.testContent[this.currentLine-1][x], i, j);
|
|
|
|
i += 13;
|
|
|
|
}
|
|
|
|
j+= 30;
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:27:54 +00:00
|
|
|
i = this.x;
|
|
|
|
|
2022-11-28 14:17:27 +00:00
|
|
|
for (let x = 0; x < this.testContent[this.currentLine].length; x++) {
|
|
|
|
if (x < this.words[this.currentLine].length) {
|
|
|
|
if (this.words[this.currentLine][x] === this.testContent[this.currentLine][x]) {
|
2022-12-01 14:41:14 +00:00
|
|
|
fill(this.goodColor);
|
2022-11-18 12:27:54 +00:00
|
|
|
} else {
|
2022-12-01 14:41:14 +00:00
|
|
|
fill(this.badColor);
|
2022-11-18 12:27:54 +00:00
|
|
|
}
|
2022-11-28 14:17:27 +00:00
|
|
|
} else {
|
2022-12-01 14:41:14 +00:00
|
|
|
fill(this.textColor);
|
2022-11-18 12:27:54 +00:00
|
|
|
}
|
2022-11-28 14:17:27 +00:00
|
|
|
text(this.testContent[this.currentLine][x], i, j);
|
|
|
|
i += 13;
|
2022-11-18 12:27:54 +00:00
|
|
|
}
|
2022-11-28 14:17:27 +00:00
|
|
|
|
|
|
|
i = this.x;
|
2022-11-29 12:21:38 +00:00
|
|
|
j += 30;
|
2022-11-28 14:17:27 +00:00
|
|
|
|
2022-12-01 14:41:14 +00:00
|
|
|
fill(this.textColor);
|
2022-11-28 14:17:27 +00:00
|
|
|
for (let x = this.currentLine + 1; x < this.testContent.length; x++) {
|
|
|
|
text(this.testContent[x], i, j);
|
|
|
|
j += 30;
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:41:14 +00:00
|
|
|
} else if (this.isPassword) {
|
|
|
|
// these variables allow me to use the values of x and y while updating them
|
|
|
|
let i = this.x;
|
|
|
|
let j = this.y;
|
|
|
|
|
|
|
|
// currently this loop just prints out every letter in the array, including any enter characters
|
|
|
|
for (let x = 0; x < this.letters.length; x++) {
|
|
|
|
if (i > this.x + this.width) i = this.x, j += 30;
|
|
|
|
if (this.letters[x] === "Enter") {
|
|
|
|
i = this.x, j+= 30;
|
|
|
|
} else {
|
|
|
|
let char = "-";
|
|
|
|
text(char, i, j);
|
|
|
|
i += 13
|
|
|
|
}
|
|
|
|
if (this.letters.length > 0 && x == this.letters.length-1 && this.line) {
|
|
|
|
fill(this.textColor)
|
|
|
|
rect(i, j-15, 1, 30)
|
|
|
|
}
|
|
|
|
}
|
2022-11-18 12:27:54 +00:00
|
|
|
} else {
|
|
|
|
// these variables allow me to use the values of x and y while updating them
|
|
|
|
let i = this.x;
|
|
|
|
let j = this.y;
|
|
|
|
|
|
|
|
// currently this loop just prints out every letter in the array, including any enter characters
|
|
|
|
for (let x = 0; x < this.letters.length; x++) {
|
|
|
|
if (i > this.x + this.width) i = this.x, j += 30;
|
|
|
|
if (this.letters[x] === "Enter") {
|
|
|
|
i = this.x, j+= 30;
|
|
|
|
} else {
|
|
|
|
text(this.letters[x], i, j);
|
|
|
|
i += 13
|
|
|
|
}
|
|
|
|
if (this.letters.length > 0 && x == this.letters.length-1 && this.line) {
|
2022-12-01 14:41:14 +00:00
|
|
|
fill(this.textColor)
|
2022-11-18 12:27:54 +00:00
|
|
|
rect(i, j-15, 1, 30)
|
|
|
|
}
|
2022-09-29 19:11:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 16:22:48 +01:00
|
|
|
}
|