xxxxxxxxxx
41
function Button(txt) {
this.txt = txt;
this.mouseIsOn = false;
this.setPosition = function(x, y) {
this.pos = createVector(x, y);
};
this.hover = function() {
if (mouseX > this.pos.x && mouseX < this.pos.x + textWidth(txt) && mouseY > this.pos.y && mouseY < this.pos.y + 15) {
return true;
}
return false;
};
this.show = function() {
push();
fill(200);
if (this.hover()) {
stroke(0);
} else {
stroke(100);
}
rect(this.pos.x, this.pos.y, textWidth(txt) + 10, 15);
fill(0);
noStroke();
text(this.txt, this.pos.x + 5, this.pos.y + 12.5);
pop();
};
this.whenPressed = function(func) {
if (mouseIsPressed && this.hover) {
if (!this.mouseIsOn) {
func();
this.mouseIsOn = true;
}
} else {
this.mouseIsOn = false;
}
};
}