This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUIBase.pde
465 lines (416 loc) · 12.4 KB
/
SUIBase.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Base Widget
color SUIWidgetFillColor = color(30);
color SUIWidgetStrokeColor = SUIWidgetFillColor;
color SUITextColor = color(255);
color SUIHandleColor = color(70);
color SUIFocusStrokeColor = color(255, 255, 0);
// Buttons
color SUIButtonColor = color(85);
color SUIButtonHoverColor = color(95);
color SUIButtonPressColor = color(105);
// Slider
color SUISliderBgColor = color(0);
color SUISliderColor = color(85);
// Window
color SUIWindowFillColor = color(45);
color SUIWindowStrokeColor = SUIHandleColor;
//TextBox
color SUITextBoxBgColor = color(0);
class ActionListener {
public void onMouseDown(int x, int y, int button){}
public void onMouseUp(int x, int y, int button){}
public void onMouseMove(int oldx, int oldy, int newx, int newy){}
public void onMouseEnter(){}
public void onMouseExit(){}
public void onKeyDown(char c, int code){}
public void onKeyUp(char c, int code){}
public void resize(int nw, int nh){}
public void onMouseWheel(int x, int y, int direction){}
}
class Layout {
// Un layout n'est utile que sur un widget qui peut contenir plusieurs widgets
private CompositeWidget parentWidget = null;
Layout() {}
public final void setParentWidget(CompositeWidget parentWidget) {
this.parentWidget = parentWidget;
}
public void rebuild() {
if (parentWidget == null) return;
for (Widget w : parentWidget.getChildren()) {
w.rebuild();
}
}
public final CompositeWidget getParentWidget() {
return parentWidget;
}
}
class Widget {
// A composite widget should have a layout object to define it's structure
private Layout layout = new Layout();
// parent widget
private Widget parent = null;
// position relative to the parent
private int x = 0;
private int y = 0;
// width and height
private int w = 100;
private int h = 100;
private int minW = 0;
private int minH = 0;
// List of action listener
private ArrayList<ActionListener> actions = new ArrayList<ActionListener>();
// If this widget can be in focus
private boolean canFocus = true;
// Do not draw and ingore input
private boolean visible = true;
// Widget color style
private boolean drawFill = true;
private boolean drawStroke = true;
private int strokeWeight = 1;
private color fillColor = SUIWidgetFillColor;
private color strokeColor = SUIWidgetStrokeColor;
private color textColor = SUITextColor;
private PFont textFont = createFont("Arial", 12);
// Constructor
public Widget() {
}
// --------------------------------------------- getters and setters -----------------------------------------------------
public Layout getLayout() {
return layout;
}
public void setCanFocus(boolean b) {
canFocus = b;
}
public void setLayout(Layout layout) {
this.layout =layout;
}
// coords
public final int getScreenX() {
return parent == null ? x : x+parent.getScreenX();
}
public final int getScreenY() {
return parent == null ? y : y+parent.getScreenY();
}
public final int getX() {
return x;
}
public final int getY() {
return y;
}
public final void setX(int x) {
this.x = x;
}
public final void setY(int y) {
this.y = y;
}
// dims
public final int getW() {
return w;
}
public final int getH() {
return h;
}
public final void setW(int w) {
this.w = w;
}
public final void setH(int h) {
this.h = h;
}
public final int getMinW() {
return minW;
}
public final int getMinH() {
return minH;
}
public final void setMinW(int w) {
minW = w;
}
public final void setMinH(int h) {
minH = h;
}
// Style
public final color getFillColor() {
return fillColor;
}
public final color getStrokeColor() {
return strokeColor;
}
public final color getTextColor() {
return textColor;
}
public final PFont getTextFont() {
return textFont;
}
public final int getStrokeWeight() {
return strokeWeight;
}
public final boolean doesDrawFill() {
return drawFill;
}
public final boolean doesDrawStroke() {
return drawStroke;
}
public final void setFillColor(color fillColor) {
this.fillColor = fillColor;
}
public final void setStrokeColor(color strokeColor) {
this.strokeColor = strokeColor;
}
public final void setTextColor(color textColor) {
this.textColor = textColor;
}
public final void setTextFont(PFont font) {
textFont = font;
}
public final void setStrokeWeight(int weight) {
strokeWeight = weight;
}
public final void setDrawFill(boolean v) {
drawFill = v;
}
public final void setDrawStroke(boolean v) {
drawStroke = v;
}
// visibility
public final boolean isVisible() {
return visible;
}
public final void setVisible(boolean visible) {
this.visible = visible;
}
// parent
public final boolean hasParent() {
return parent != null;
}
public final Widget getParent() {
return parent;
}
public final void setParent(Widget parent) {
this.parent = parent;
}
// Action listeners
public final void addActionListener(ActionListener action) {
actions.add(action);
}
// coords are in relative
public final boolean coordsInside(int x, int y) {
return (x >= this.x && x <= this.x+w && y >= this.y && y <= this.y+h);
}
public void rebuild() {
layout.rebuild();
}
// *********************************** OVERRIDES ***************************************
public void getFocus(Widget widget) {
if (parent != null) parent.getFocus(widget);
}
public void draw(PGraphics g) {
if (!visible) return;
// clip rendering
if (!drawClip(g)) return;
if (drawFill) g.fill(fillColor);
else g.noFill();
g.strokeWeight(strokeWeight);
if (drawStroke) g.stroke(strokeColor);
else g.noStroke();
g.rectMode(CORNER);
g.rect(getScreenX(), getScreenY(), w, h);
// unclip rendering
g.noClip();
}
// Needed for layout placing
public void calculateMinimumSize() {
}
// ----------------------- EVENTS ----------------------------
public void onMouseDown(int x, int y, int button) {
if (!visible) return;
if (canFocus) getFocus(this);
for (ActionListener a : actions) a.onMouseDown(x, y, button);
}
public void onMouseUp(int x, int y, int button) {
if (!visible) return;
for (ActionListener a : actions) a.onMouseUp(x, y, button);
}
public void onMouseMove(int oldx, int oldy, int newx, int newy) {
if (!visible) return;
for (ActionListener a : actions) a.onMouseMove(oldx, oldy, newx, newy);
}
public void onMouseWheel(int x, int y, int direction){
if (!visible) return;
for (ActionListener a : actions) a.onMouseWheel(x, y, direction);
}
public void onMouseEnter() {
if (!visible) return;
for (ActionListener a : actions) a.onMouseEnter();
}
public void onMouseExit() {
if (!visible) return;
for (ActionListener a : actions) a.onMouseExit();
}
public void onKeyDown(char c, int code) {
if (!visible) return;
for (ActionListener a : actions) a.onKeyDown(c, code);
}
public void onKeyUp(char c, int code) {
if (!visible) return;
for (ActionListener a : actions) a.onKeyUp(c, code);
}
public void resize(int nw, int nh) {
if (!visible) return;
w = nw;
h = nh;
for (ActionListener a : actions) a.resize(nw, nh);
layout.rebuild();
}
private final int[] getClipRect(int x1, int y1, int x2, int y2) {
// if no parent, return the current clip rect
if (parent == null) {
int[] rect = new int[4];
rect[0] = x1;
rect[1] = y1;
rect[2] = x2;
rect[3] = y2;
return rect;
}
int px1 = parent.getScreenX();
int py1 = parent.getScreenY();
int px2 = parent.getScreenX()+parent.getW();
int py2 = parent.getScreenY()+parent.getH();
// clip the child rect to it's parent rect
int nx1 = clamp(x1, px1, px2);
int ny1 = clamp(y1, py1, py2);
int nx2 = clamp(x2, px1, px2);
int ny2 = clamp(y2, py1, py2);
// nothing to display at this point, just stop
if (nx2-nx1 == 0 || ny2-ny1 == 0) return null;
// Now the parent have to clip it with it's parent
return parent.getClipRect(nx1, ny1, nx2, ny2);
}
public final boolean drawClip(PGraphics g) {
if(!visible) return false;
g.imageMode(CORNERS);
int[] clipRect = getClipRect(getScreenX(), getScreenY(), getScreenX()+getW(), getScreenY()+getH());
if (clipRect != null)
g.clip(clipRect[0], clipRect[1], clipRect[2]+1, clipRect[3]+1);
return clipRect != null;
}
}
class CompositeWidget extends Widget {
// list of widget
private ArrayList<Widget> children = new ArrayList<Widget>();
// focused widget
private Widget focusedChild = null;
// Constructor
public CompositeWidget() {
super();
}
public final Widget getFocusedChild() {return focusedChild;}
public final boolean hasFocusedChild() {return focusedChild != null;}
public final ArrayList<Widget> getChildren(){return children;}
public void setLayout(Layout layout){
super.setLayout(layout);
getLayout().setParentWidget(this);
getLayout().rebuild();
}
// ************************ OVERRIDE ************************
public void addChild(Widget widget) {
widget.setParent(this);
children.add(widget);
getLayout().rebuild();
}
public void getFocus(Widget widget) {
if( widget != this)
focusedChild = widget;
else
focusedChild = null;
if (hasParent()) getParent().getFocus(this);
}
public void draw(PGraphics g) {
if (!isVisible()) return;
super.draw(g);
if(!drawClip(g)) return;
// draw it's children
for (int i=children.size()-1; i >= 0; --i) {
if (children.get(i).isVisible())
children.get(i).draw(g);
}
g.noClip();
}
public void onKeyDown(char c, int code) {
if (!isVisible()) return;
super.onKeyDown(c, code);
if (focusedChild != null) focusedChild.onKeyDown(c, code);
}
public void onKeyUp(char c, int code) {
if (!isVisible()) return;
super.onKeyUp(c, code);
if (focusedChild != null) focusedChild.onKeyUp(c, code);
}
public void onMouseDown(int x, int y, int button) {
if (!isVisible()) return;
super.onMouseDown(x, y, button);
for (int i=0; i < children.size(); ++i) {
if (children.get(i).coordsInside(x - getX(), y - getY()) && children.get(i).isVisible()) {
children.get(i).onMouseDown(x- getX(), y- getY(), button);
break;
}
}
}
public void onMouseWheel(int x, int y, int direction){
if (!isVisible()) return;
super.onMouseWheel(x, y, direction);
for (int i=0; i < children.size(); ++i) {
if (children.get(i).coordsInside(x - getX(), y - getY()) && children.get(i).isVisible()) {
children.get(i).onMouseWheel(x- getX(), y- getY(), direction);
break;
}
}
}
public void onMouseUp(int x, int y, int button) {
if (!isVisible()) return;
super.onMouseUp(x, y, button);
for (int i=0; i < children.size(); ++i) {
if (children.get(i).coordsInside(x - getX(), y - getY()) && children.get(i).isVisible()) {
children.get(i).onMouseUp(x - getX(), y - getY(), button);
break;
}
}
}
public void onMouseMove(int oldx, int oldy, int newx, int newy) {
if (!isVisible()) return;
super.onMouseMove(oldx, oldy, newx, newy);
Widget prevWidget = null;
Widget nextWidget = null;
for (int i=0; i < children.size(); ++i) {
if (children.get(i).coordsInside(oldx - getX(), oldy - getY()) && children.get(i).isVisible()) {
prevWidget = children.get(i);
prevWidget.onMouseMove(oldx - getX(), oldy - getY(), newx - getX(), newy - getY());
break;
}
}
for (int i=0; i < children.size(); ++i) {
if (children.get(i).coordsInside(newx - getX(), newy - getY()) && children.get(i).isVisible()) {
nextWidget = children.get(i);
nextWidget.onMouseMove(oldx - getX(), oldy - getY(), newx - getX(), newy - getY());
if (prevWidget != nextWidget) {
if (prevWidget != null)
prevWidget.onMouseExit();
nextWidget.onMouseEnter();
}
break;
}
}
if (nextWidget == null && prevWidget != null)
prevWidget.onMouseExit();
}
public void calculateMinimumSize(){
int w = 0;
int h = 0;
for(Widget child : children){
child.calculateMinimumSize();
if(child.getX()+child.getMinW() > w) w = child.getX()+child.getMinW();
if(child.getY()+child.getMinH() > h) h = child.getY()+child.getMinH();
}
setMinW(w);
setMinH(h);
}
}