-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.pde
87 lines (73 loc) · 2.32 KB
/
button.pde
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* button.pde
*
* Class for a Button
*
* Created on: July 25, 2020
* Author: Sean LaPlante
*/
class Button {
/*
* A button that can be pressed/clicked
*/
PVector location;
float buttonWidth;
float buttonHeight;
float cornerRadius;
color buttonColor;
color buttonHoverColor;
color textColor;
String text;
float textSize;
color DEFAULT_TEXT_COLOR = color(255); // white
color DEFAULT_BUTTON_COLOR = color(255, 53, 73); // red/pink
color DEFAULT_BUTTON_HOVER_COLOR = color(160, 33, 46); // darker
Button(String text, float x, float y, float buttonWidth, float buttonHeight, float cornerRadius) {
create(text, new PVector(x, y), buttonWidth, buttonHeight, cornerRadius);
this.buttonColor = DEFAULT_BUTTON_COLOR;
this.buttonHoverColor = DEFAULT_BUTTON_HOVER_COLOR;
this.textColor = DEFAULT_TEXT_COLOR;
this.textSize = DEFAULT_TEXT_SIZE;
}
void create(String text, PVector location, float buttonWidth, float buttonHeight, float cornerRadius) {
this.text = text;
this.location = location;
this.buttonWidth = buttonWidth;
this.buttonHeight = buttonHeight;
this.cornerRadius = cornerRadius;
}
void setColor(color c) {
this.buttonColor = c;
}
void setHoverColor(color c) {
this.buttonHoverColor = c;
}
void setTextColor(color c) {
this.textColor = c;
}
void setTextSize(float size) {
this.textSize = size;
}
void display() {
pushMatrix();
noStroke();
if (onButton(mouseX, mouseY)) {
fill(this.buttonHoverColor); // Set hover color
} else {
fill(this.buttonColor); // Set normal color
}
rect(this.location.x, this.location.y, this.buttonWidth, this.buttonHeight, this.cornerRadius);
fill(this.textColor); // Black text
textSize(this.textSize);
strokeWeight(1);
textAlign(CENTER, CENTER);
text(this.text, this.location.x + (this.buttonWidth / 2), this.location.y + (this.buttonHeight / 2));
popMatrix();
}
boolean onButton(float posX, float posY) {
if (posX > this.location.x && posX < (this.location.x + this.buttonWidth) && posY > this.location.y && posY < (this.location.y + this.buttonHeight)) {
return true;
}
return false;
}
}