-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.pde
118 lines (95 loc) · 3.62 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
Float BUTTON_HEIGHT = 55.0;
color DEFAULT_BUTTON_BACKGROUND_COLLOR = #FF6000;
//Create an interface to handle click event for the callback method
interface ClickHandler {
//create abstract method, i.e., clickEventHandler() that will act as callback
void handleClick();
}
class Button {
int pointAmount = 2;
int thickness = 2;
color collor = 255;
String content = "";
Boolean active;
float w;
float h;
ArrayList<Point> points;
ArrayList<LineBoundary> lines;
private ClickHandler clickHandler;
Button(String content, ClickHandler clickHandler) {
this.active = false;
this.clickHandler = clickHandler;
PVector position = new PVector(0, 0);
this.content = content;
this.w = (float) (content.length() * 19.5 + 30);
ArrayList<Point> points = new ArrayList<Point>();
points.add(new Point(position.x, position.y));
points.add(new Point(position.x + this.w, position.y));
points.add(new Point(position.x + this.w, position.y + BUTTON_HEIGHT));
points.add(new Point(position.x, position.y + BUTTON_HEIGHT));
this.points = new ArrayList<Point>();
this.lines = new ArrayList<LineBoundary>();
this.pointAmount = points.size();
points.forEach((pt) -> this.points.add(new Point(pt.x, pt.y)));
for (int i = 0; i < this.pointAmount; i++) {
Point pt1 = this.points.get(i);
Point pt2 = this.points.get((i + 1) % this.pointAmount);
lines.add(new LineBoundary(pt1, pt2, this.collor, this.thickness));
}
}
Button(String content, PVector position, ClickHandler clickHandler) {
this.active = false;
this.clickHandler = clickHandler;
this.content = content;
this.w = (float) (content.length() * 19.5 + 30);
ArrayList<Point> points = new ArrayList<Point>();
points.add(new Point(position.x, position.y));
points.add(new Point(position.x + this.w, position.y));
points.add(new Point(position.x + this.w, position.y + BUTTON_HEIGHT));
points.add(new Point(position.x, position.y + BUTTON_HEIGHT));
this.points = new ArrayList<Point>();
this.lines = new ArrayList<LineBoundary>();
this.pointAmount = points.size();
points.forEach((pt) -> this.points.add(new Point(pt.x, pt.y)));
for (int i = 0; i < this.pointAmount; i++) {
Point pt1 = this.points.get(i);
Point pt2 = this.points.get((i + 1) % this.pointAmount);
lines.add(new LineBoundary(pt1, pt2, this.collor, this.thickness));
}
}
Button show() {
this.lines.forEach((line) ->
line.show(this.collor, false, this.thickness));
fill(color(DEFAULT_BUTTON_BACKGROUND_COLLOR, 200));
rect(this.points.get(0).x, this.points.get(1).y, this.w, BUTTON_HEIGHT);
fill(#FFFFFF);
text(content, this.points.get(0).x + 15, this.points.get(0).y + 40);
return this;
}
Boolean collides(PVector origin) {
Point topLeft = this.points.get(0);
Point topRight = this.points.get(1);
Point bottomRight = this.points.get(2);
Boolean containX = origin.x >= topLeft.x && origin.x <= topRight.x;
Boolean containY = origin.y >= topLeft.y && origin.y <= bottomRight.y;
Boolean contained = containX && containY;
if (contained) this.thickness = 4;
else this.thickness = 2;
return containX && containY;
}
float getMaxX() {
return this.points.get(1).x;
}
void move(float x, float y) {
this.points.get(0).set(x, y);
this.points.get(1).set(x + this.w, y);
this.points.get(2).set(x + this.w, y + BUTTON_HEIGHT);
this.points.get(3).set(x, y + BUTTON_HEIGHT);
}
void onClick() {
this.active = !this.active;
if (clickHandler != null) {
clickHandler.handleClick();
}
};
}