textbox fixed

This commit is contained in:
Arlo Filley 2022-09-29 19:11:20 +01:00
parent d6d020d62f
commit b7daaa62c5
2 changed files with 43 additions and 10 deletions

View File

@ -7,11 +7,18 @@ function setup() {
canvas.resize(); canvas.resize();
canvas.center(); canvas.center();
textbox = new Textbox(); textbox = new Textbox(400, 200, 700);
} }
function draw() { function draw() {
background(255,100,100); background(255,100,100);
textbox.draw();
}
// whenever a key is pressed this function is called
function keyPressed() {
textbox.letterTyped(key);
} }

View File

@ -13,7 +13,11 @@ class Textbox {
this.letters = []; this.letters = [];
this.words = ""; this.words = "";
this.allowedLetters = [] this.allowedLetters = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'enter', '\'', '"', ',', '.', ' '
]
} }
getX() { getX() {
@ -105,18 +109,18 @@ class Textbox {
} }
letterTyped(pKey) { letterTyped(pKey) {
if (pkey == "backspace" && letters.length) { console.log(pKey);
letters.pop() if (pKey === "Backspace" && this.letters.length > 1) {
return this.letters.pop();
return;
} }
for (i in allowedLetters.length - 1) { for (let i = 0; i < this.allowedLetters.length; i++) {
if (pkey == i) { if (pKey.toLowerCase() === this.allowedLetters[i]) {
letters.push(pKey) this.letters.push(pKey);
return; return;
} }
} }
} }
getWords() { getWords() {
@ -134,4 +138,26 @@ class Textbox {
setAllowedLetters(pAllowedLetters) { setAllowedLetters(pAllowedLetters) {
this.allowedLetters = pAllowedLetters; this.allowedLetters = pAllowedLetters;
} }
draw() {
// sets the default characteristics these should be replaced with attributes
color(0);
textSize(23);
textFont('monospace');
// 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
}
}
}
} }