-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.pde
More file actions
115 lines (96 loc) · 2.55 KB
/
Button.pde
File metadata and controls
115 lines (96 loc) · 2.55 KB
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
class Button extends SceneElement implements InputReceiver {
// ----- Fields
private final int borderRoundness = 10;
private final Vec2Int size;
private final String text;
private final Runnable callback;
private Color fillColor;
private final Color initialColor;
private boolean currentlyHovered;
private boolean clicked;
private float clickedTimer;
private boolean executeCallbackPending;
// ----- Constructors
public Button(Vec2Int size, String text, Color fillColor, Runnable callback) {
this.size = size;
this.text = text;
this.fillColor = fillColor;
this.callback = callback;
initialColor = fillColor;
}
// ----- Methods
@Override
public void draw() {
rectMode(CENTER);
fill(fillColor);
stroke(0);
strokeWeight(2);
rect(0, 0, size.x, size.y, borderRoundness);
fill(0);
textAlign(CENTER, CENTER);
textSize(min(size.x * .25f, size.y * .75f));
text(text, 0, 0);
handleHover();
handleClick();
}
@Override
public void onInput(InputAction action, Object param) {
switch (action) {
case HOVER_ON:
if (param == this)
currentlyHovered = true;
break;
case CLICKED:
if (param == this)
clicked = true;
break;
case MOUSE_RELEASED:
onMouseReleased();
break;
default:
return;
};
}
@Override
public RectCollider getCollider() {
return new RectCollider(
new Vec2Int(width / 2 + position.x, height / 2 + position.y),
new Vec2Int(size.x * scale, size.y * scale)
);
}
private void handleHover() {
if (currentlyHovered && scale == 1f) {
TweenEngineHelpers.tween(this, SceneElementTweenAccessor.SCALE_ID, .1f)
.target(1.05f)
.ease(TweenEquations.easeInOutSine)
.start(tweenManager);
}
if (!currentlyHovered) {
TweenEngineHelpers.tween(this, SceneElementTweenAccessor.SCALE_ID, .1f)
.target(1f)
.ease(TweenEquations.easeInOutSine)
.start(tweenManager);
}
currentlyHovered = false;
}
private void handleClick() {
if (!clicked) return;
clickedTimer += 1f / frameRate;
float t = clickedTimer * 9f;
if (t < 1) {
fillColor = Color.gradient(initialColor, Color.brighten(initialColor, .75f), t);
return;
}
if (executeCallbackPending) callback.run();
}
private void onMouseReleased() {
if (!clicked) return;
if (currentlyHovered) {
executeCallbackPending = true;
} else {
fillColor = initialColor;
clicked = false;
clickedTimer = 0f;
}
}
}