-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.pde
112 lines (107 loc) · 2.37 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
//button class V1.1.2
class Button {
protected float x, y, lengthX, lengthY;
private int fColor=#FFFFFF, sColor=#AAAAAA, textcolor=0;
private String text="";
private float textScaleFactor=2.903, strokeWeight=3;
Button(float X, float Y, float DX, float DY) {
x=X;
y=Y;
lengthX=DX;
lengthY=DY;
findTextScale();
strokeWeight=3;
}
Button(float X, float Y, float DX, float DY, String Text) {
x=X;
y=Y;
lengthX=DX;
lengthY=DY;
text=Text;
findTextScale();
strokeWeight=3;
}
Button(float X, float Y, float DX, float DY, int c1, int c2) {
x=X;
y=Y;
lengthX=DX;
lengthY=DY;
fColor=c1;
sColor=c2;
findTextScale();
strokeWeight=3;
}
Button(float X, float Y, float DX, float DY, String Text, int c1, int c2) {
x=X;
y=Y;
lengthX=DX;
lengthY=DY;
text=Text;
fColor=c1;
sColor=c2;
findTextScale();
strokeWeight=3;
}
void findTextScale() {
for (int i=1; i<300; i++) {
textSize(i);
if (textWidth(text)>lengthX||textAscent()+textDescent()>lengthY) {
textScaleFactor=i-1;
break;
}
}
}
public Button draw() {
strokeWeight(0);
fill(sColor);
rect(x-strokeWeight, y-strokeWeight, lengthX+strokeWeight*2, lengthY+strokeWeight*2);
fill(fColor);
rect(x, y, lengthX, lengthY);
fill(textcolor);
textAlign(CENTER, CENTER);
if (!text.equals("")) {
textSize(textScaleFactor);
text(text, lengthX/2+x, lengthY/2+y);
}
return this;
}
public Button setText(String t) {
text=t;
findTextScale();
return this;
}
public String getText() {
return text;
}
public boolean isMouseOver() {
return mouseX>=x&&mouseX<=x+lengthX&&mouseY>=y&&mouseY<=y+lengthY;
}
public Button setColor(int c1, int c2) {
fColor=c1;
sColor=c2;
return this;
}
public int getColor() {
return fColor;
}
public String toString() {
return "button at:"+x+" "+y+" length: "+lengthX+" height: "+lengthY+" with text: "+text+" and a color of: "+fColor;
}
@Deprecated
public Button setTextFactor(float factor) {
//textScaleFactor=factor;
return this;
}
public Button setTextColor(int c) {
textcolor=c;
return this;
}
public Button setX(float X) {
x=X;
return this;
}
public Button setStrokeWeight(float s) {
strokeWeight=s;
return this;
}
}