-
Notifications
You must be signed in to change notification settings - Fork 0
/
Widget.pde
206 lines (159 loc) · 3.63 KB
/
Widget.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.lang.reflect.*;
class Widget {
Interface intf;
float relX;
float relY;
float absX;
float absY;
float width;
float height;
float mouseX;
float mouseY;
float pmouseX;
float pmouseY;
boolean isFocused;
Widget parent;
ArrayList<Widget> children;
boolean isActive;
boolean isVisible;
String name;
String callback;
Widget() { }
Widget(Interface intf) {
this(intf, 0, 0, 0, 0, "", "");
}
Widget(Interface intf, float x, float y, float w, float h) {
this(intf, x, y, w, h, "", "");
}
Widget(Interface intf, float x, float y, float w, float h, String name) {
this(intf, x, y, w, h, name, "");
}
Widget(Interface intf, float x, float y, float w, float h, String name, String callback) {
this.intf = intf;
relX = intf.scaleFactor * x;
relY = intf.scaleFactor * y;
absX = intf.scaleFactor * x;
absY = intf.scaleFactor * y;
width = intf.scaleFactor * w;
height = intf.scaleFactor * h;
mouseX = 0;
mouseY = 0;
pmouseX = 0;
pmouseY = 0;
isFocused = false;
parent = null;
children = new ArrayList<Widget>();
isActive = true;
isVisible = true;
this.name = name;
this.callback = callback;
}
//void runCallback() {
// runCallback(new Object[] { });
//}
void runCallback() {
PApplet p = intf.sketch;
try {
Method method = p.getClass().getMethod(callback, new Class[] {String.class});
method.invoke(p, new Object[] {name});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
} catch (NoSuchMethodException nsme) {
System.err.println("There is no public " + callback + "() method " +
"in the class " + getClass().getName());
} catch (Exception e) {
e.printStackTrace();
}
}
void setParent(Widget p) {
parent = p;
absX = p.absX + relX;
absY = p.absY + relY;
}
void setCallback(String callback) {
this.callback = callback;
}
void addChildren(Widget c) {
children.add(c);
c.setParent(this);
}
void updateChildren() {
for (Widget child: children) {
child.setRelMousePos();
child.setFocusedState();
child.updateChildren();
}
}
void drawChildren() {
PApplet p = intf.sketch;
for (Widget child: children) {
if (!child.isVisible) continue;
p.pushMatrix();
child.setOrigin();
p.pushStyle();
child.draw();
p.popStyle();
intf.addDrawn(child);
child.drawChildren();
p.popMatrix();
}
}
void setOrigin() {
intf.sketch.translate(relX, relY);
}
void setRelMousePos() {
PApplet p = intf.sketch;
mouseX = p.mouseX - absX;
mouseY = p.mouseY - absY;
pmouseX = p.pmouseX - absX;
pmouseY = p.pmouseY - absY;
}
void setFocusedState() {
isFocused = this == intf.focused;
}
boolean hasFocus(int mx, int my) {
return absX <= mx && mx <= absX + width && absY <= my && my <= absY + height;
}
void show() {
isVisible = true;
setVisible();
}
void hide() {
isVisible = false;
setInvisible();
}
void activate() {
isActive = true;
setActive();
}
void deactivate() {
isActive = false;
setInactive();
}
void setup() {
}
void draw() {
}
void press() {
}
void hover() {
}
void drag() {
}
void release() {
}
void lostFocus() {
}
void setActive() {
}
void setInactive() {
}
void setVisible() {
}
void setInvisible() {
}
}