-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.pde
149 lines (121 loc) · 3.87 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
import de.looksgood.ani.*; //<>// //<>// //<>// //<>// //<>// //<>//
class ImageButton {
int x;
int y;
String buttonFilename;
PImage buttonImage;
PImage onHoverButtonImage;
int alpha = 0;
float onHoverTweenDuration = 0.25;
int to = 0;
Ani anim;
int playDir;
int colliderWidth = 100;
int colliderHeight = 100;
color overlayColor = color(255, 0, 0);
ImageButton( String buttonImageFilename, int newX, int newY ) {
buttonFilename = buttonImageFilename;
x = newX;
y = newY;
if (buttonFilename != null) {
buttonImage = loadImage( buttonFilename );
}
if (buttonImage != null) {
colliderWidth = buttonImage.width;
colliderHeight = buttonImage.height;
}
anim = new Ani(this, onHoverTweenDuration, 0, "alpha", to, Ani.QUAD_OUT);
}
ImageButton( String buttonImageFilename, int newX, int newY, String onHoverButtonImageFilename ) {
this(buttonImageFilename, newX, newY);
onHoverButtonImage = loadImage( onHoverButtonImageFilename );
colliderWidth = onHoverButtonImage.width;
colliderHeight = onHoverButtonImage.height;
}
void display() {
if (allowMousePressed) {
if (buttonImage != null)
image( buttonImage, x, y, buttonImage.width * widthRatio, buttonImage.height * heightRatio);
if (onHoverButtonImage != null) {
if (alpha < 255 && isPointInside(mouseX, mouseY) && playDir != 1) {
to = 255;
anim.setBegin(alpha);
anim.setEnd(to);
playDir = 1;
anim.start();
} else if (alpha > 0 && isPointInside(mouseX, mouseY) == false && playDir != -1) {
to = 0;
anim.setBegin(alpha);
anim.setEnd(to);
playDir = -1;
anim.start();
}
pushStyle();
tint(overlayColor, alpha);
imageMode(CORNER);
image(onHoverButtonImage, x, y, onHoverButtonImage.width * widthRatio, onHoverButtonImage.height * heightRatio);
popStyle();
}
}
}
boolean isPointInside( int px, int py ) {
return isPointInRectangle( px, py, x, y, colliderWidth * widthRatio, colliderHeight * heightRatio );
}
}
class TextButton {
int x;
int y;
String buttonText;
int buttonTextSize;
int buttonWidth;
int buttonHeight;
int offset;
TextButton( int newX, int newY, String aButtonText, int aButtonTextSize ) {
x = newX;
y = newY;
buttonText = aButtonText;
buttonTextSize = aButtonTextSize;
buttonWidth = 0;
offset = buttonTextSize / 5;
}
void display() {
stroke( 0 );
strokeWeight( 3 );
fill( 255, 128 );
rect( x - offset, y - offset, buttonWidth + 2 * offset, buttonHeight + 2 * offset);
fill( 0 );
textSize( buttonTextSize );
if ( buttonWidth == 0 ) {
buttonWidth = (int) textWidth( buttonText );
buttonHeight = buttonTextSize;
}
text( buttonText, x, y + buttonTextSize * 0.9);
}
boolean isPointInside( int px, int py ) {
return isPointInRectangle( px, py, x - offset, y - offset,
buttonWidth + 2 * offset, buttonTextSize + 2 * offset);
}
}
class HintButton extends ImageButton {
TextButton textButton;
int max_distance;
HintButton( String buttonImageFilename, String hintText, int newX, int newY ) {
super( buttonImageFilename, newX, newY );
textButton = new TextButton( x - 30, y - 30, hintText, 20 );
}
void display() {
if ( buttonImage == null ) {
buttonImage = loadImage( buttonFilename );
max_distance = buttonImage.width * 6;
}
float distance = dist( mouseX, mouseY, x + buttonImage.width / 2, y + buttonImage.height / 2);
if ( distance < 30 ) {
image( buttonImage, x, y );
textButton.display();
} else if ( distance < max_distance ) {
tint(255, map( distance, 50, max_distance, 255, 0 ) ); // Apply transparency without changing color
image( buttonImage, x, y );
tint(255, 255);
}
}
}