-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.pde
158 lines (136 loc) · 2.84 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//**********************************
//Button Class - This makes Buttons!
//**********************************
class Button
{
boolean state;
boolean showBorder;
color fillOnColour;
color fillOffColour;
color textColour;
color borderColour;
float x, y, h, w;
int id;
String label, align;
Button (String inputLabel, int inputId)
{
label = inputLabel;
id = inputId;
x = 0;
y = 0;
h = 15;
w = 75;
fillOffColour = color(255);
fillOnColour = color(65, 109, 243);
textColour = color(0);
borderColour = color(220);
state = false;
showBorder = false;
align = "CENTER";
}
// Set height and width
void setDim(float inH, float inW) {
h = inH;
w = inW;
}
// Set x and y position
void setPos(float inX, float inY) {
x = inX;
y = inY;
}
// Set alignment of button label
void setAlign(String ali) {
if (ali.equals("LEFT") || ali.equals("CENTER") || ali.equals("RIGHT")) {
align = ali;
} else {
align = "CENTER";
}
}
// State stuff (is the button 'ON' or 'OFF')
void flipState() {
state = (state) ? false : true;
}
void setState (boolean inputState) {
state = inputState;
}
boolean getState() {
return state;
}
// Set button fill colour
void setFillOnColour (color inColour) {
fillOnColour = inColour;
}
void setFillOffColour(color inputColour) {
fillOffColour = inputColour;
}
// Checks if cursor is inside button area
boolean checkMouse() {
if ((mouseX < x + w) && (mouseX > x) && (mouseY < y + h) && (mouseY > y)) {
showBorder = true;
return true;
} else {
showBorder = false;
return false;
}
}
// Return button label
String getLabel() {
return label;
}
int getId()
{
return id;
}
void setBorder(boolean inputBorder)
{
showBorder = inputBorder;
}
void draw()
{
if (state) {
if (checkMouse()) {
fill(fillOffColour);
} else {
fill(fillOnColour);
}
} else {
if (checkMouse()) {
fill(fillOnColour);
} else {
fill(fillOffColour);
}
}
if (showBorder) {
strokeWeight(1);
stroke(borderColour);
} else {
noStroke();
}
rectMode(CORNER);
rect(x, y, w, h, 30);
if (state) {
if (checkMouse()) {
fill(0);
} else {
fill(255);
}
} else {
if (checkMouse()) {
fill(255);
} else {
fill(0);
}
}
textSize(14);
if (align.equals("CENTER")) {
textAlign(CENTER);
text(label, x + (0.5 * (w)), y + 4 + (0.5 * h));
} else if (align.equals("LEFT")) {
textAlign(LEFT);
text(label, x + 5, y + 4 + (0.5 * h));
} else {
textAlign(RIGHT);
text(label, x + w - 5, y + 4 + (0.5 * h));
}
}
}