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
/
SUIWidgets.pde
623 lines (541 loc) · 17.4 KB
/
SUIWidgets.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
public class SplitWidget extends CompositeWidget {
// Axis options
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
// For when we move the slider
private boolean dragging = false;
// To calculate the slider postion
private float offset = 0.5;
private int sliderWidth = 8;
// to know wich axis to split
private int axis = 0;
SplitWidget(int axis){
super();
this.axis = axis;
setLayout(new SplitLayout());
}
public void setAxis(int axis){
this.axis = axis;
rebuild();
}
public int getAxis(){return axis;}
public void addChild(Widget widget) {
if(getChildren().size() >= 2) return;
widget.setParent(this);
getChildren().add(widget);
getLayout().rebuild();
}
public float getOffset(){return offset;}
public void setOffset(float offset){this.offset = offset;}
public int getSliderWidth(){return sliderWidth;}
public void draw(PGraphics g){
super.draw(g);
// clip rendering
if(!drawClip(g)) return;
g.fill(SUIHandleColor);
g.stroke(getStrokeColor());
g.rectMode(CENTER);
if(axis == HORIZONTAL)
g.rect(getScreenX()+getW()/2, getScreenY()+(int)(getH()*offset), getW(), sliderWidth);
else
g.rect(getScreenX()+(int)(getW()*offset), getScreenY()+getH()/2, sliderWidth, getH());
// Unclip rendering
g.noClip();
}
public void onMouseDown(int x, int y, int button){
super.onMouseDown(x, y, button);
// hit the slider top bar ?
if(axis == HORIZONTAL){
if(y >= getY()+(int)(getH()*offset)-sliderWidth/2 && y <= getY()+(int)(getH()*offset)+sliderWidth/2 && button == LEFT){
dragging = true;
return;
}
}
else{
if(x >= getX()+(int)(getW()*offset)-sliderWidth/2 && x <= getX()+(int)(getW()*offset)+sliderWidth/2 && button == LEFT){
dragging = true;
return;
}
}
}
public void onMouseUp(int x, int y, int button){
super.onMouseUp(x, y, button);
dragging = false;
}
public void onMouseMove(int oldx, int oldy, int newx, int newy){
super.onMouseMove(oldx, oldy, newx, newy);
if(dragging){
if(axis == HORIZONTAL)
offset = (float) clamp(newy-getY(), sliderWidth/2.0, getH()-sliderWidth/2.0) / (float) getH();
else
offset = (float) clamp(newx-getX(), sliderWidth/2.0, getW()-sliderWidth/2.0) / (float) getW();
rebuild();
}
}
public void onMouseExit(){
dragging = false;
}
}
class Button extends Widget {
private String text;
Button(String text) {
super();
this.text = text;
calculateMinimumSize();
setW(getMinW());
setH(getMinH());
setFillColor(SUIButtonColor);
}
public void calculateMinimumSize(){
textFont(getTextFont());
int w = (int) textWidth(text);
int h = (int) (textAscent()+textDescent());
setMinW(w+8);
setMinH(h+8);
}
public void draw(PGraphics g) {
super.draw(g);
if(!drawClip(g)) return;
g.fill(getTextColor());
g.textFont(getTextFont());
g.textAlign(CENTER, CENTER);
g.text(text,getScreenX()+getW()/2, getScreenY()+getH()/2);
g.noClip();
}
public void onMouseDown(int x, int y, int button) {
super.onMouseDown(x, y, button);
setFillColor(SUIButtonPressColor);
}
public void onMouseUp(int x, int y, int button) {
super.onMouseUp(x, y, button);
setFillColor(SUIButtonHoverColor);
}
public void onMouseEnter() {
super.onMouseEnter();
setFillColor(SUIButtonHoverColor);
}
public void onMouseExit() {
super.onMouseExit();
setFillColor(SUIButtonColor);
}
}
class Label extends Widget {
private int hAlign = CENTER;
private String text;
Label(String text) {
super();
this.text = text;
calculateMinimumSize();
setW(getMinW());
setH(getMinH());
setDrawFill(false);
setDrawStroke(false);
}
public void calculateMinimumSize(){
int lines = text.split("\r\n|\r|\n").length+1;
textFont(getTextFont());
int w = (int) textWidth(text);
int h = (int) (textAscent()+textDescent());
setMinW(w+8);
setMinH(h*lines+8);
}
public void setHAlign(int a){hAlign = a;}
public void draw(PGraphics g) {
super.draw(g);
if(!drawClip(g)) return;
g.fill(getTextColor());
g.textFont(getTextFont());
g.textAlign(hAlign, CENTER);
int x = hAlign == LEFT ? getScreenX()+4 : hAlign == RIGHT ? getScreenX()+getW()-4: getScreenX()+getW()/2;
g.text(text, x, getScreenY()+getH()/2);
g.noClip();
}
}
class Slider extends Widget {
// Axis options
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
private int minLength = 100;
private String text = "";
private float minVal = 0;
private float maxVal = minLength;
private int sliderWidth = 16;
private float val = minVal;
private float axis = HORIZONTAL;
private boolean dragging = false;
public boolean useInt = false;
Slider(int minLength, float minVal, float maxVal, int axis) {
super();
this.minLength= minLength;
this.minVal = minVal;
this.maxVal = maxVal;
this.axis = axis;
this.val = minVal;
setDrawFill(false);
setDrawStroke(false);
calculateMinimumSize();
}
public void setVal(float v){val=v;}
public float getVal(){return val;}
public void setUseInt(boolean u){
useInt = u;
}
public void setText(String text){
this.text = text;
}
public void calculateMinimumSize(){
textFont(getTextFont());
if(axis == HORIZONTAL){
int w = (int) textWidth(text)+minLength;
int h = (int) max((textAscent()+textDescent()), sliderWidth);
setMinW(w+8);
setMinH(h+8);
} else {
int w = (int) max(textWidth(text), sliderWidth);
int h = (int) (textAscent()+textDescent())+minLength;
setMinW(w+8);
setMinH(h+8);
}
}
public void draw(PGraphics g) {
super.draw(g);
if (!drawClip(g)) return;
g.noStroke();
g.fill(SUISliderBgColor);
g.rectMode(CORNER);
if (axis == HORIZONTAL) {
// draw the slider background
g.rect(getScreenX(), getScreenY()+getH()/2-sliderWidth/2, minLength, sliderWidth);
// draw the slider bar
g.fill(SUISliderColor);
g.rect(getScreenX(), getScreenY()+getH()/2-sliderWidth/2, map(val, minVal, maxVal, 0, minLength), sliderWidth);
// draw the val
g.fill(getTextColor());
g.textFont(getTextFont());
g.textAlign(CENTER, CENTER);
g.text(nf(val, 0, 2), getScreenX()+minLength/2, getScreenY()+getH()/2);
// draw the slider label
g.textAlign(CENTER, CENTER);
int tw = (int) g.textWidth(text);
g.text(text, getScreenX()+minLength+tw/2+4, getScreenY()+getH()/2);
} else {
// draw the slider background
g.rect(getScreenX()+getW()/2-sliderWidth/2, getScreenY(), sliderWidth, minLength);
// draw the slider bar
g.fill(SUISliderColor);
g.rect(getScreenX()+getW()/2-sliderWidth/2, getScreenY(), sliderWidth, map(val, minVal, maxVal, 0, minLength));
// draw the val
g.fill(getTextColor());
g.textFont(getTextFont());
g.textAlign(CENTER, CENTER);
g.text(nf(val, 0, 2), getScreenX()+getW()/2, getScreenY()+minLength/2);
// draw the slider label
g.textAlign(CENTER, CENTER);
int th = (int) (textAscent()+textDescent());
g.text(text, getScreenX()+getW()/2, getScreenY()+minLength+th/2.0);
}
g.noClip();
}
public void onMouseDown(int x, int y, int button) {
// hit the slider top bar ?
if (axis == HORIZONTAL) {
if (x <= getX()+minLength && y >= getY()+getH()/2-sliderWidth/2 && y <= getY()+getH()/2+sliderWidth/2 && button == LEFT) {
val = map(clamp(x-getX(), 0, minLength), 0, minLength, minVal, maxVal);
if(useInt) val = (int) val;
dragging = true;
super.onMouseDown(x, y, button);
return;
}
} else {
if (y <= getY()+minLength && x >= getX()+getW()/2-sliderWidth/2 && x <= getX()+getW()/2+sliderWidth/2 && button == LEFT) {
val = map(clamp(y-getY(), 0, minLength), 0, minLength, minVal, maxVal);
if(useInt) val = (int) val;
dragging = true;
super.onMouseDown(x, y, button);
return;
}
}
}
public void onMouseUp(int x, int y, int button) {
super.onMouseUp(x, y, button);
dragging = false;
}
public void onMouseMove(int oldx, int oldy, int newx, int newy) {
if (dragging) {
super.onMouseMove(oldx, oldy, newx, newy);
if (axis == HORIZONTAL){
val = map(clamp(newx-getX(), 0, minLength), 0, minLength, minVal, maxVal);
if(useInt) val = (int) val;
}else{
val = map(clamp(newy-getY(), 0, minLength), 0, minLength, minVal, maxVal);
if(useInt) val = (int) val;
}
}
}
public void onMouseExit() {
dragging = false;
}
}
class Window extends CompositeWidget {
// Content panel
private CompositeWidget contentPanel = new CompositeWidget();
// can it be resized ?
private boolean resizable = true;
private boolean dragging = false;
private boolean resizing = false;
private boolean canClose = true;
// Distance where the mouse hit, relative to the window
private int dx, dy;
// Decoration size
private int topHeight = 16;
private int scaleHandleSize = 16;
private String title = "";
// Constructor
public Window(int x, int y, int w, int h) {
super();
setX(x);
setY(y);
setW(w);
setH(h);
setStrokeColor(SUIWindowStrokeColor);
setDrawFill(false);
// define it's content panel
super.addChild(contentPanel);
contentPanel.setFillColor(SUIWindowFillColor);
contentPanel.setX(1);
contentPanel.setY(topHeight+1);
contentPanel.setW(getW()-2);
contentPanel.setH(getH()-topHeight-2);
}
public void getFocus(Widget widget) {
if (hasParent()) getParent().getFocus(contentPanel);
}
public void setTitle(String txt){title = txt;}
// Children are added to the content panel and not the window itself.
public void addChild(Widget widget) {
widget.setParent(contentPanel);
contentPanel.addChild(widget);
rebuild();
}
public void draw(PGraphics g){
super.draw(g);
if(!drawClip(g)) return;
g.fill(SUIHandleColor);
g.stroke(SUIHandleColor);
g.rectMode(CORNER);
g.rect(getScreenX(), getScreenY(), getW(), topHeight);
if(canClose){
g.fill(SUIButtonColor);
g.rect(getScreenX()+getW()-topHeight, getScreenY(), topHeight, topHeight);
g.stroke(SUIWidgetStrokeColor);
g.line(getScreenX()+getW()-topHeight, getScreenY(), getScreenX()+getW(), getScreenY()+topHeight);
g.line(getScreenX()+getW(), getScreenY(), getScreenX()+getW()-topHeight, getScreenY()+topHeight);
}
g.stroke(SUIHandleColor);
g.fill(getTextColor());
g.textFont(getTextFont());
g.textAlign(LEFT, CENTER);
g.text(title, getScreenX()+2, getScreenY()+topHeight/2);
if(resizable){
g.fill(SUIHandleColor);
g.rect(getScreenX()+getW()-scaleHandleSize, getScreenY()+getH()-scaleHandleSize, scaleHandleSize, scaleHandleSize);
}
g.noClip();
}
public void setResizable(boolean b){resizable = b;}
public void onMouseDown(int x, int y, int button){
// get mouse relative dist
dx = getX()-x;
dy = getY()-y;
// hit the top bar ?
if( x < getX()+getW()-topHeight && y <= getY() + topHeight && button == LEFT){
dragging = true;
return;
}
// hit the resize button
if(x >= getX()+getW()-scaleHandleSize && y >= getY()+getH()-scaleHandleSize && button == LEFT && resizable){
resizing = true;
return;
}
super.onMouseDown(x, y, button);
}
public void onMouseUp(int x, int y, int button){
if(!resizing && !dragging){
if(x >= getX()+getW()-topHeight && y <= getY() + topHeight && button == LEFT){
super.setVisible(false);
}
super.onMouseUp(x, y, button);
}
dragging = false;
resizing = false;
}
public void onMouseMove(int oldx, int oldy, int newx, int newy){
if(dragging){
setX(newx+dx);
setY(newy+dy);
} else if(resizing){
int w = newx-getX()+scaleHandleSize/2;
int nw = w < scaleHandleSize ? scaleHandleSize : w;
int h = newy-getY()+scaleHandleSize/2;
int nh = h < topHeight+scaleHandleSize ? topHeight+scaleHandleSize : h;
resize(nw, nh);
}
super.onMouseMove(oldx, oldy, newx, newy);
}
public void onMouseExit(){
dragging = false;
resizing = false;
}
public void resize(int nw, int nh){
if(!isVisible()) return;
super.resize(nw, nh);
contentPanel.resize(nw-2,nh-topHeight-2);
}
public void rebuild(){
super.rebuild();
contentPanel.rebuild();
}
public void calculateMinimumSize(){
contentPanel.calculateMinimumSize();
setMinW(contentPanel.getMinW());
setMinH(contentPanel.getMinH()+topHeight);
}
public final Layout getLayout(){return contentPanel.getLayout();}
public final void setLayout(Layout layout){
contentPanel.setLayout(layout);
}
}
class CheckBox extends Widget {
private boolean checked = false;
private String text;
private int boxSize = 16;
CheckBox(String text) {
super();
this.text = text;
calculateMinimumSize();
setW(getMinW());
setH(getMinH());
setFillColor(SUIButtonColor);
setDrawFill(false);
setDrawStroke(false);
}
public void calculateMinimumSize() {
textFont(getTextFont());
int textW = (int) textWidth(text);
int textH = (int) (textAscent()+textDescent());
setMinW(boxSize+textW+8);
setMinH(max(boxSize, textH)+8);
}
public void setChecked(boolean b) {
checked = b;
}
public boolean getChecked() {
return checked;
}
public void draw(PGraphics g) {
super.draw(g);
if (!drawClip(g)) return;
g.fill(getFillColor());
g.stroke(SUIWidgetStrokeColor);
g.rectMode(CORNER);
g.rect(getScreenX(), getScreenY()+getH()/2-boxSize/2, boxSize, boxSize);
if (checked) {
g.line(getScreenX(), getScreenY()+getH()/2-boxSize/2, getScreenX()+boxSize, getScreenY()+getH()/2+boxSize/2);
g.line(getScreenX()+boxSize, getScreenY()+getH()/2-boxSize/2, getScreenX(), getScreenY()+getH()/2+boxSize/2);
}
g.fill(getTextColor());
g.textFont(getTextFont());
g.textAlign(LEFT, CENTER);
g.text(text, getScreenX()+boxSize+4, getScreenY()+getH()/2);
g.noClip();
}
public void onMouseDown(int x, int y, int button) {
if (x >= getX() && x <= getX()+boxSize && y >= getY()+getH()/2-boxSize/2 && y <= getY()+getH()/2+boxSize/2 && button == LEFT) {
setFillColor(SUIButtonPressColor);
super.onMouseDown(x, y, button);
}
}
public void onMouseUp(int x, int y, int button) {
if (x >= getX() && x <= getX()+boxSize && y >= getY()+getH()/2-boxSize/2 && y <= getY()+getH()/2+boxSize/2 && button == LEFT) {
checked = !checked;
super.onMouseUp(x, y, button);
setFillColor(SUIButtonHoverColor);
}
}
}
class TextBox extends Widget {
private int boxLength = 100;
private String text = "";
private int boxHeight;
private String insideText = "0";
private boolean hitEnter = false;
TextBox(int boxLength, String text){
super();
this.boxLength = boxLength;
this.text = text;
textFont(getTextFont());
int textH = (int) (textAscent()+textDescent());
boxHeight = textH+4;
setDrawFill(false);
setDrawStroke(false);
}
public void calculateMinimumSize() {
textFont(getTextFont());
int textW = (int) textWidth(text);
int textH = (int) (textAscent()+textDescent());
boxHeight = textH+4;
setMinW(boxLength+textW+8);
setMinH(textH+8);
}
public String getInsideText(){return insideText;}
public void setInsideText(String txt){insideText = txt;}
public void draw(PGraphics g){
super.draw(g);
if(!drawClip(g)) return;
g.rectMode(CORNER);
g.textFont(getTextFont());
g.stroke(SUIWidgetStrokeColor);
g.fill(SUITextBoxBgColor);
g.rect(getScreenX(), getScreenY()+getH()/2.0-boxHeight/2.0, boxLength, boxHeight);
g.fill(SUITextColor);
g.textAlign(LEFT, CENTER);
g.text(text, getScreenX()+boxLength+4, getScreenY()+getH()/2.0);
g.text(insideText, getScreenX()+4, getScreenY()+getH()/2.0);
if(((CompositeWidget) getParent()).getFocusedChild() == this){
g.stroke(SUIFocusStrokeColor);
g.noFill();
g.rect(getScreenX(), getScreenY()+getH()/2.0-boxHeight/2.0, boxLength, boxHeight);
g.stroke(SUITextColor);
int w = (int) textWidth(insideText);
if(!hitEnter)
g.line(getScreenX()+w+4, getScreenY()+getH()/2.0-boxHeight/2.0, getScreenX()+w+4, getScreenY()+getH()/2.0+boxHeight/2.0);
}
g.noClip();
}
public void onMouseDown(int x, int y, int button) {
if (x >= getX() && x <= getX()+boxLength && y >= getY()+getH()/2-boxHeight/2 && y <= getY()+getH()/2+boxHeight/2 && button == LEFT) {
super.onMouseDown(x, y, button);
hitEnter = false;
}
}
public void onMouseUp(int x, int y, int button) {
if (x >= getX() && x <= getX()+boxLength && y >= getY()+getH()/2-boxHeight/2 && y <= getY()+getH()/2+boxHeight/2 && button == LEFT) {
super.onMouseUp(x, y, button);
}
}
public void onKeyDown(char c, int code) {
hitEnter = false;
super.onKeyDown(c, code);
if(code == BACKSPACE ){
if(insideText.length() <= 1) insideText = "";
else {
StringBuilder sb = new StringBuilder(insideText);
sb.deleteCharAt(insideText.length()-1);
insideText = sb.toString();
}
} else if(code == ENTER)
hitEnter = true;
else
insideText+=c;
}
}