-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClickable.pde
91 lines (72 loc) · 1.93 KB
/
Clickable.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
/**
* Class for clickable objects
*/
public abstract class Clickable extends Obj {
protected int x = 500;
protected int y = 500;
// * Variables whitch represent the borders of the clickable area
// * Are computed in the setup method
protected int left;
protected int right;
protected int top;
protected int bottom;
// Should be set to true if the object can move around the canvas
// If true the corners of the object will be recalculated on very re render
// ! So only set to true if neccessary!
protected boolean canMove = false;
protected int w = 150;
protected int h = 75;
// If the user has clicked on the button
// Will only be true on the first update when the user first clicks on the button
public boolean clicked = false;
// If the user is currently clicking on the button
public boolean clicking = false;
public boolean hovered = false;
protected void postSetup() {
updateCorners();
}
protected void preUpdate() {
push();
if (canMove) {
updateCorners();
}
// Check if the obj was clicked
checkHover();
checkClick();
}
private void checkClick() {
if (mousePressed && clicking) {
clicked = false;
return;
} else {
clicking = false;
}
if (!shouldClick() || !mousePressed) {
clicked = false;
return;
}
clicked = hovered;
clicking = hovered;
}
protected void checkHover() {
hovered = mouseX >= left && mouseX <= right && mouseY >= bottom && mouseY <= top;
}
protected void updateCorners() {
left = x - w / 2;
bottom = y - h / 2;
right = x + w / 2;
top = y + h / 2;
}
protected boolean shouldClick() {
return !m.transitioning;
}
/**
* Updates the x and y variables by converting them from corner values to
* their corresponding center values
*/
protected void cornerToCenter() {
x += w / 2;
y -= h / 2;
}
public Clickable(Snap app) { super(app); }
}