-
Notifications
You must be signed in to change notification settings - Fork 20
/
Button.pde
101 lines (96 loc) · 2.36 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
class Button {
float posX, posY, sizeXY;
color offColor, hoverColor, clickColor, nowColor;
PFont font;
String label;
int fontSize;
boolean hovered=false;
boolean clicked=false;
float degLocal;
boolean isSquare = false;
Button(float x, float y, float s, color oc, int fs, String d) {
posX = x;
posY = y;
sizeXY = s;
offColor = oc;
hoverColor = blendColor(offColor, color(40), ADD);
clickColor = blendColor(offColor, color(120), ADD);
nowColor = offColor;
fontSize=fs;
font = createFont("Arial", fontSize);
label = d;
}
void update() {
checkButton();
drawButton();
}
void checkButton() {
float kSize = 10;
if (hitDetect(mouseX, mouseY, 0, 0, posX, posY, sizeXY, sizeXY)) {
if (!mousePressed) {
hovered=true;
clicked=false;
}
else if (mousePressed && !buttonFreeze) {
hovered=true;
clicked=true;
}
/*
}
else if (hitDetect(x[1], y[1], kSize, kSize, posX, posY, sizeXY, sizeXY)||hitDetect(x[4], y[4], kSize, kSize, posX, posY, sizeXY, sizeXY)) {
hovered=true;
clicked=false;
}
else if (hitDetect(x[0], y[0], kSize, kSize, posX, posY, sizeXY, sizeXY)&&hitDetect(x[4], y[4], kSize, kSize, posX, posY, sizeXY, sizeXY)) {
hovered=true;
clicked=true;
*/
}
else {
hovered=false;
clicked=false;
}
}
void drawButton() {
ellipseMode(CENTER);
rectMode(CENTER);
noStroke();
if (hovered&&!clicked) {
nowColor = hoverColor;
}
else if (hovered&&clicked) {
nowColor = clickColor;
}
else if (!hovered&&!clicked) {
nowColor = offColor;
}
fill(0, 10);
if(!isSquare){
ellipse(posX+2, posY+2, sizeXY, sizeXY);
}else{
rect(posX+2, posY+2, sizeXY, sizeXY);
}
fill(nowColor);
if(!isSquare){
ellipse(posX, posY, sizeXY, sizeXY);
}else{
rect(posX, posY, sizeXY, sizeXY);
}
fill(0);
textFont(font, fontSize);
textAlign(CENTER, CENTER);
text(label, posX, posY-(fontSize/4));
}
boolean hitDetect(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) {
w1 /= 2;
h1 /= 2;
w2 /= 2;
h2 /= 2;
if (x1 + w1 >= x2 - w2 && x1 - w1 <= x2 + w2 && y1 + h1 >= y2 - h2 && y1 - h1 <= y2 + h2) {
return true;
}
else {
return false;
}
}
}