-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.js
43 lines (37 loc) · 1.09 KB
/
button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class CustomButton {
constructor(x, y, w, h, text, p5, visible = true) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.text = text;
this.p5 = p5;
this.visible = visible;
}
setVisible(visible) {
this.visible = visible;
}
setText(text) {
this.text = text;
}
clicked(mx, my) {
// since we r using rectMode(CENTER), (x, y) would be the center point of the rect
return (mx > this.x - this.w / 2 && mx < this.x + this.w / 2 && my > this.y - this.h / 2 && my < this.y + this.h / 2);
}
draw(color) {
if (!this.visible)
return;
this.p5.push();
this.p5.stroke(0);
this.p5.strokeWeight(0.5);
this.p5.rectMode(this.p5.CENTER);
this.p5.textAlign(this.p5.CENTER);
this.p5.fill(color);
this.p5.rect(this.x - 1, this.y, this.w - 1, this.h);
this.p5.textSize(20);
this.p5.fill(0);
this.p5.strokeWeight(0);
this.p5.text(this.text, this.x, this.y + this.h / 4.5);
this.p5.pop();
}
}