-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interface.pde
145 lines (127 loc) · 3.08 KB
/
Interface.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
class Interface {
PApplet sketch;
float scaleFactor;
Widget focused;
ArrayList<Widget> drawn;
HashMap<String, PFont> fonts;
HashMap<String, Widget> widgets;
Widget root;
Interface(PApplet sketch) {
this(sketch, 1);
}
Interface(PApplet sketch, float scale) {
this.sketch = sketch;
scaleFactor = scale;
focused = null;
drawn = new ArrayList<Widget>();
fonts = new HashMap<String, PFont>();
widgets = new HashMap<String, Widget>();
root = new Widget(this);
}
void addWidget(Widget w) {
addWidget(w, null, "");
}
void addWidget(Widget w, Widget parent) {
addWidget(w, parent, "");
}
void addWidget(Widget w, String parentName) {
addWidget(w, null, parentName);
}
void addWidget(Widget w, Widget parent, String parentName) {
if (parent != null) {
parent.addChildren(w);
} else {
if (parentName != null && !parentName.equals("")) {
Widget namedParent = getWidget(parentName);
if (namedParent != null) {
namedParent.addChildren(w);
}
} else {
root.addChildren(w);
}
}
if (w.name != null && !w.name.equals("")) {
widgets.put(w.name, w);
}
w.setup();
}
Widget getWidget(String name) {
if (widgets.containsKey(name)) {
return widgets.get(name);
} else {
return null;
}
}
void addFont(String name, int size) {
PFont font = sketch.createFont(name, scaleFactor * size);
fonts.put(name + str(size), font);
}
void setFont(String name, int size) {
String key = name + str(size);
if (fonts.containsKey(key)) {
PFont font = fonts.get(key);
sketch.textFont(font, size);
}
}
void update() {
root.updateChildren();
drawn.clear();
root.drawChildren();
}
void addDrawn(Widget w) {
drawn.add(w);
}
boolean mousePressed() {
setFocused(sketch.mouseX, sketch.mouseY);
if (focused != null) {
focused.setRelMousePos();
focused.press();
return true;
}
return false;
}
boolean mouseMoved() {
setFocused(sketch.mouseX, sketch.mouseY);
if (focused != null) {
focused.setRelMousePos();
focused.hover();
return true;
}
return false;
}
boolean mouseDragged() {
setFocused(sketch.mouseX, sketch.mouseY);
if (focused != null) {
focused.setRelMousePos();
focused.drag();
return true;
}
return false;
}
boolean mouseReleased() {
setFocused(sketch.mouseX, sketch.mouseY);
if (focused != null) {
focused.setRelMousePos();
focused.release();
return true;
}
return false;
}
void setFocused(int mx, int my) {
Widget pfocused = this.focused;
focused = null;
for (int i = drawn.size() - 1; i >= 0; i--) {
Widget child = drawn.get(i);
if (child.isVisible && child.isActive && child.hasFocus(mx, my)) {
if (pfocused != null && pfocused != child) {
pfocused.lostFocus();
}
focused = child;
return;
}
}
if (pfocused != null) {
pfocused.lostFocus();
}
}
}