-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGUI.pde
More file actions
226 lines (186 loc) · 4.79 KB
/
GUI.pde
File metadata and controls
226 lines (186 loc) · 4.79 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.util.*;
// I don't care. Cheers!
public static class Control
{
public static final color LIGHT = #EDEDED;
public static final color MIDDLE = #A89C83;
public static final color DARK = #4A4335;
private static List<Control> controls = new ArrayList<Control>();
private static Control dragged = null;
public static void drawAll() {
for (Control control : controls)
control.draw();
}
public static boolean mousePressedAll(int mouseX, int mouseY) {
for (Control control : controls) {
if (control.contains(mouseX, mouseY) && control.mousePressed()) {
dragged = control;
return true;
}
}
return false;
}
public static void mouseReleasedAll() {
if (dragged != null) {
dragged.mouseReleased();
dragged = null;
}
}
public static void mouseDraggedAll() {
if (dragged != null)
dragged.mouseDragged();
}
public float x, y, width, height;
public Control(float x, float y, float width, float height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
controls.add(this);
}
public boolean contains(PVector p) {
return x <= p.x && p.x <= x + width
&& y <= p.y && p.y <= y + height;
}
public boolean contains(float x, float y) {
return contains(new PVector(x, y));
}
protected void draw() { }
protected boolean mousePressed() { return false; }
protected void mouseReleased() { }
protected void mouseDragged() { }
}
class Label extends Control
{
public String value;
public PFont font;
public Label(
float x, float y, float width, float height,
String value, PFont font
) {
super(x, y, width, height);
this.value = value;
this.font = font;
}
@Override
protected void draw() {
if (value == null || font == null)
return;
noStroke();
fill(DARK);
textFont(font);
text(value, x, y + (height - textAscent() - textDescent()) / 2 + textAscent());
}
}
class Button extends Control
{
public String value;
public PFont font;
List<IButtonListener> listeners = new ArrayList<IButtonListener>();
public Button(
float x, float y, float width, float height,
String value, PFont font
) {
super(x, y, width, height);
this.value = value;
this.font = font;
}
@Override
protected void draw() {
noStroke();
fill(MIDDLE);
rectMode(CORNER);
rect(x, y, width, height);
if (value == null || font == null)
return;
textFont(font);
float tw = textWidth(value);
float ta = textAscent();
float th = ta + textDescent();
fill(DARK);
text(value, x + (width - tw) / 2, y + (height - th) / 2 + ta + 1);
}
@Override
protected boolean mousePressed() {
for (IButtonListener listener : listeners)
listener.onClick(this);
return true;
}
void addListener(IButtonListener listener) {
listeners.add(listener);
}
}
interface IButtonListener
{
void onClick(Button sender);
}
class CheckBox extends Control
{
public boolean checked = false;
public CheckBox(float x, float y, float width, float height) {
super(x, y, width, height);
}
@Override
protected void draw() {
rectMode(CORNER);
noStroke();
fill(MIDDLE);
rect(x, y, width, height);
if (checked) {
fill(DARK);
float margin = max(1, min(width, height) / 5);
rect(x + margin, y + margin, width - 2 * margin, height - 2 * margin);
}
}
@Override
protected boolean mousePressed() {
checked = !checked;
return true;
}
}
class Slider extends Control
{
final int SLIDER_SIZE = 15;
final int SLIDE_WEIGHT = 2;
private float value = 0.0f;
public float value() {
return value;
}
public void setValue(float value) {
this.value = clamp(value, 0, 1);
}
private boolean dragging = false;
Slider(float x, float y, float width, float height) {
super(x, y, width, height);
}
PVector sliderPosition(float value) {
return new PVector(
x + SLIDER_SIZE / 2 + value * (width - SLIDER_SIZE),
y + height / 2
);
}
@Override
protected void draw() {
PVector p0 = sliderPosition(0);
PVector pValue = sliderPosition(value);
PVector p1 = sliderPosition(1);
strokeWeight(SLIDE_WEIGHT);
stroke(DARK);
line(p0.x, p0.y, p1.x, p1.y);
ellipseMode(CENTER);
noStroke();
fill(MIDDLE);
ellipse(pValue.x, pValue.y, SLIDER_SIZE, SLIDER_SIZE);
}
@Override
protected boolean mousePressed() {
PVector m = new PVector(mouseX, mouseY);
dragging = PVector.dist(m, sliderPosition(value)) < SLIDER_SIZE / 2;
return dragging;
}
@Override
protected void mouseDragged() {
float newValue = (mouseX - sliderPosition(0).x) / (width - SLIDER_SIZE);
value = clamp(newValue, 0, 1);
}
}