-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleUI.pde
More file actions
1580 lines (1184 loc) · 39.9 KB
/
Copy pathSimpleUI.pde
File metadata and controls
1580 lines (1184 loc) · 39.9 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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SimpleUI_Classes version 2
// Started Dec 12th 2018
// This update March 2021
// Simon Schofield
// introduces image icons for buttons
//////////////////////////////////////////////////////////////////
// SimpleUI() is the only class you have to use in your
// application to build and use the UI.
// With it you can add buttons, sliders, menus and text boxes
// You can use a file dialogue to load and save files
//
//
//
//
// Once a mouse event has been received by a UI item (button, menu etc) it calls a function called
// simpleUICallback(...) which you have to include in the
// main part of the project (below setup() and draw() etc.)
//
// Also, you need to call uiManager.drawMe() in the main draw() function
//
public class SimpleUI {
SimpleUIRect canvasRect;
boolean useAutomaticMouseEventHandling = true;
ArrayList<Widget> widgetList = new ArrayList<Widget>();
String UIManagerName;
SimpleUIRect backgroundRect = null;
color backgroundRectColor;
// these are for capturing user events
boolean pmousePressed = false;
boolean pkeyPressed = false;
String fileDialogPrompt = "";
public SimpleUI() {
UIManagerName = "";
}
public SimpleUI(String uiname) {
UIManagerName = uiname;
}
////////////////////////////////////////////////////////////////////////////
// file dialogue
//
public void openFileLoadDialog(String prompt) {
fileDialogPrompt = prompt;
selectInput(prompt, "fileLoadCallback", null, this);
}
void fileLoadCallback(File selection) {
// cancelled
if (selection == null) {
return;
}
// is directory not file
if (selection.isDirectory()) {
return;
}
UIEventData uied = new UIEventData(UIManagerName, "fileLoadDialog", "fileLoadDialog", "mouseReleased", mouseX, mouseY);
uied.fileSelection = selection.getPath();
uied.fileDialogPrompt = this.fileDialogPrompt;
handleUIEvent( uied);
}
public void openFileSaveDialog(String prompt) {
fileDialogPrompt = prompt;
selectOutput(prompt, "fileSaveCallback", null, this);
}
void fileSaveCallback(File selection) {
// cancelled
if (selection == null) {
return;
}
String path = selection.getPath();
//System.out.println(path);
UIEventData uied = new UIEventData(UIManagerName, "fileSaveDialog", "fileSaveDialog", "mouseReleased", mouseX, mouseY);
uied.fileSelection = selection.getPath();
uied.fileDialogPrompt = this.fileDialogPrompt;
handleUIEvent(uied);
}
////////////////////////////////////////////////////////////////////////////
// canvas creation
//
public void addCanvas(int x, int y, int w, int h) {
canvasRect = new SimpleUIRect(x, y, x+w, y+h);
}
public void checkForCanvasEvent(String mouseEventType, int x, int y) {
if (canvasRect==null) return;
if ( canvasRect.isPointInside(x, y)) {
UIEventData uied = new UIEventData(UIManagerName, "canvas", "canvas", mouseEventType, x, y);
handleUIEvent(uied);
}
}
public void drawCanvas() {
if (canvasRect==null) return;
// now draw "surrounds"
int appWidth = width;
int appHeight = height;
// left, top, right, bottom
pushStyle();
fill(200, 200, 200);
//stroke(0, 0, 0);
noStroke();
rect(0, 0, canvasRect.left, appHeight);
rect(canvasRect.left, 0, canvasRect.getWidth(), canvasRect.top);
rect(canvasRect.right, 0, appWidth-canvasRect.left, appHeight);
rect(canvasRect.left, canvasRect.bottom, canvasRect.getWidth(), appHeight-canvasRect.getHeight());
// fine line round the outside of canvas rect
noFill();
stroke(0, 0, 0);
strokeWeight(1);
rect(canvasRect.left, canvasRect.top, canvasRect.getWidth(), canvasRect.getHeight());
popStyle();
}
////////////////////////////////////////////////////////////////////////////
// widget creation
//
boolean widgetNameAlreadyExists(String label, String uiComponentType) {
for (Widget w : widgetList) {
if (w.UILabel.equals(label) && w.UIComponentType.equals(uiComponentType) ) {
System.out.println("SimpleUI: that label name - "+ label+ " - already exists for widget of type "+ uiComponentType);
return true;
}
}
return false;
}
// label creation
public SimpleLabel addSimpleLabel(String label, int x, int y, String txt) {
if (widgetNameAlreadyExists(label, "SimpleLabel")) return null;
SimpleLabel l = new SimpleLabel(UIManagerName, x, y, label, txt);
widgetList.add(l);
return l;
}
// button creation
public SimpleButton addSimpleButton(String label, int x, int y) {
if (widgetNameAlreadyExists(label, "SimpleButton")) return null;
SimpleButton b = new SimpleButton(UIManagerName, x, y, label);
widgetList.add(b);
return b;
}
public ToggleButton addToggleButton(String label, int x, int y) {
if (widgetNameAlreadyExists(label, "SimpleButton")) return null;
ToggleButton b = new ToggleButton(UIManagerName, x, y, label);
widgetList.add(b);
return b;
}
public ToggleButton addToggleButton(String label, int x, int y, boolean initialState) {
if (widgetNameAlreadyExists(label, "SimpleButton")) return null;
ToggleButton b = new ToggleButton(UIManagerName, x, y, label);
b.selected = initialState;
widgetList.add(b);
return b;
}
public RadioButton addRadioButton(String label, int x, int y, String groupID) {
if (widgetNameAlreadyExists(label, "SimpleButton")) return null;
RadioButton b = new RadioButton(UIManagerName, x, y, label, groupID, this);
widgetList.add(b);
return b;
}
// label creation
public TextDisplayBox addLabel(String label, int x, int y, String txt) {
if (widgetNameAlreadyExists(label, "SimpleLabel")) return null;
TextDisplayBox sl = new TextDisplayBox(UIManagerName, label, x, y, txt);
widgetList.add(sl);
return sl;
}
// menu creation
public Menu addMenu(String label, int x, int y, String[] menuItems) {
if (widgetNameAlreadyExists(label, "Menu")) return null;
Menu m = new Menu(UIManagerName, label, x, y, menuItems, this);
widgetList.add(m);
return m;
}
// slider creation
public Slider addSlider(String label, int x, int y) {
if (widgetNameAlreadyExists(label, "Slider")) return null;
Slider s = new Slider(UIManagerName, label, x, y);
widgetList.add(s);
return s;
}
// text display creation
public TextDisplayBox addTextDisplayBox(String label, int x, int y, String txt) {
if (widgetNameAlreadyExists(label, "SimpleLabel")) return null;
TextDisplayBox sl = new TextDisplayBox(UIManagerName, label, x, y, txt);
widgetList.add(sl);
return sl;
}
// text input box creation
public TextInputBox addTextInputBox(String label, int x, int y) {
int maxNumChars = 14;
if (widgetNameAlreadyExists(label, "TextInputBox")) return null;
TextInputBox tib = new TextInputBox(UIManagerName, label, x, y, maxNumChars);
widgetList.add(tib);
return tib;
}
public TextInputBox addTextInputBox(String label, int x, int y, String content) {
if (widgetNameAlreadyExists(label, "TextInputBox")) return null;
TextInputBox tib = addTextInputBox( label, x, y);
tib.setText(content);
return tib;
}
void removeWidget(String uilabel) {
Widget w = getWidget(uilabel);
if (w == null) return;
widgetList.remove(w);
}
// getting widget data by lable
//
Widget getWidget(String uilabel) {
for (Widget w : widgetList) {
if (w.UILabel.equals(uilabel)) return w;
}
System.out.println(" getWidgetByLabel: cannot find widget with label "+ uilabel);
return null;
}
// get toggle state
public boolean getToggleButtonState(String uilabel) {
Widget w = getWidget(uilabel);
if ( w.UIComponentType.equals("ToggleButton") || w.UIComponentType.equals("RadioButton")) return w.selected;
System.out.println(" getToggleButtonState: cannot find widget with label "+ uilabel);
return false;
}
// get selected radio button in a group - returns the label name
public String getRadioGroupSelected(String groupName) {
for (Widget w : widgetList) {
if ( w.UIComponentType.equals("RadioButton")) {
if ( ((RadioButton)w).radioGroupName.equals(groupName) && w.selected) return w.UILabel;
}
}
return "";
}
public float getSliderValue(String uilabel) {
Widget w = getWidget(uilabel);
if ( w.UIComponentType.equals("Slider") ) return ((Slider)w).getSliderValue();
return 0;
}
public void setSliderValue(String uilabel, float v) {
Widget w = getWidget(uilabel);
if ( w.UIComponentType.equals("Slider") ) ((Slider)w).setSliderValue(v);
}
public String getText(String uilabel) {
Widget w = getWidget(uilabel);
if (w.UIComponentType.equals("TextInputBox")) {
return ((TextInputBox)w).getText();
}
if (w.UIComponentType.equals("SimpleLabel")) {
return ((TextDisplayBox)w).getText();
}
return "";
}
public void setText(String uilabel, String content) {
Widget w = getWidget(uilabel);
if (w.UIComponentType.equals("TextInputBox")) {
((TextInputBox)w).setText(content);
}
if (w.UIComponentType.equals("SimpleLabel")) {
((TextDisplayBox)w).setText(content);
}
}
// setting a background Color region for the UI. This is drawn first.
// to do: this should also set an offset for subsequent placement of the buttons
void setBackgroundRect(int left, int top, int right, int bottom, int r, int g, int b) {
backgroundRect = new SimpleUIRect(left, top, right, bottom);
backgroundRectColor = color(r, g, b);
}
void setRadioButtonOff(String groupName) {
for (Widget w : widgetList) {
if ( w.UIComponentType.equals("RadioButton")) {
if ( ((RadioButton)w).radioGroupName.equals(groupName)) w.selected = false;
}
}
}
void setRadioButtonOn(String buttonName, String groupName) {
// used for controlling group via code, eg using quick keys
setRadioButtonOff(groupName);
getWidget(buttonName).selected = true;
}
void setMenusOff() {
for (Widget w : widgetList) {
if ( w.UIComponentType.equals("Menu")) {
((Menu)w).visible = false;
}
}
}
// this is an alternative to using the seperate event handlers provided by Processing
// It therefor easier to use, but more sluggish in response
void checkForUserInputEvents() {
// this gets called in the drawMe() method, instead of having to link up
// to the native mousePressed() etc. methods
if ( pmousePressed == false && mousePressed) {
handleMouseEvent("mousePressed", mouseX, mouseY);
}
if ( pmousePressed == true && mousePressed == false) {
handleMouseEvent("mouseReleased", mouseX, mouseY);
}
if ( (pmouseX != mouseX || pmouseY != mouseY) && mousePressed ==false) {
handleMouseEvent("mouseMoved", mouseX, mouseY);
}
if ( (pmouseX != mouseX || pmouseY != mouseY) && mousePressed) {
handleMouseEvent("mouseDragged", mouseX, mouseY);
}
if ( pkeyPressed == false && keyPressed == true) {
handleKeyEvent(key, keyCode, "pressed");
}
if ( pkeyPressed == true && keyPressed == false) {
handleKeyEvent(key, keyCode, "released");
}
pmousePressed = mousePressed;
pkeyPressed = keyPressed;
}
// this is a key method. It is either called by the above method checkForUserInputEvents() (for a simpler implementation) or by the
// individual application-level mouse-event handlers. The widgets are polled to see if any one is handling this event
// if it is handled, then the loop is broken and the event is handled. The allows for addition and removal of
// widgets during run-time, which would othrwise create a concurrency issue.
void handleMouseEvent(String mouseEventType, int x, int y) {
checkForCanvasEvent(mouseEventType, x, y);
Widget widgetActing = null;
for (Widget w : widgetList) {
boolean eventAbsorbed = w.handleMouseEvent(mouseEventType, x, y);
if (eventAbsorbed) {
widgetActing = w;
}
if (eventAbsorbed) break;
}
if (widgetActing == null) return;
if (widgetActing.isVisible() == false) return;
widgetActing.doEventAction(mouseEventType, x, y);
}
boolean handleKeyEvent(char k, int kcode, String keyEventType) {
for (Widget w : widgetList) {
boolean eventHandled = w.handleKeyEvent( k, kcode, keyEventType);
if (eventHandled) return true;
}
return false;
}
void update() {
if ( useAutomaticMouseEventHandling ) checkForUserInputEvents();
if ( backgroundRect != null ) {
pushStyle();
fill(backgroundRectColor);
rect(backgroundRect.left, backgroundRect.top, backgroundRect.getWidth(), backgroundRect.getHeight());
popStyle();
}
drawCanvas();
for (Widget w : widgetList) {
if (w.isVisible()==false) continue;
w.drawMe();
}
}
void clearAll() {
widgetList = new ArrayList<Widget>();
}
}// end of SimpleUIManager
//////////////////////////////////////////////////////////////////
// UIEventData
// when a UI component calls the simpleUICallback() function, it passes this object back
// which contains EVERY CONCEIVABLE bit of extra information about the event that you could imagine
//
public class UIEventData {
// set by the constructor
public String callingUIManager; // this is the name of the UIManager, because you might have more than one
public String uiComponentType; // this is the type of widet e.g. SimpleButton, ToggleButton, Slider - it is identical to the class name
public String uiLabel; // this is the unique shown label for each widget, and is used to idetify the calling widget
public String mouseEventType;
public int mousex; // this is the x location of the recieved mouse event, in window space
public int mousey;
// extra stuff, which is specific to particular widgets
public boolean toggleSelectState = false;
public String radioGroupName = "";
public String menuItem = "";
public int menuItemNum = 0;
public float sliderValue = 0.0;
public String fileDialogPrompt = "";
public String fileSelection = "";
// key press and text content information for text widgets
public char keyPress;
public String textContent;
public UIEventData() {
}
public UIEventData(String uiname, String thingType, String label, String mouseEvent, int x, int y) {
initialise(uiname, thingType, label, mouseEvent, x, y);
}
void initialise(String uiname, String thingType, String label, String mouseEvent, int x, int y) {
callingUIManager = uiname;
uiComponentType = thingType;
uiLabel = label;
mouseEventType = mouseEvent;
mousex = x;
mousey = y;
}
boolean eventIsFromWidget(String lab) {
if ( uiLabel.equals( lab )) return true;
if ( menuItem.equals(lab) ) return true;
return false;
}
void print(int verbosity) {
if (verbosity != 3 && this.mouseEventType.equals("mouseMoved")) return;
if (verbosity == 0) return;
if (verbosity >= 1) {
System.out.println("UIEventData:" + this.uiComponentType + " UILabel " + this.uiLabel);
if ( this.uiComponentType.equals("canvas")) {
System.out.println("mouse event:" + this.mouseEventType + " at (" + this.mousex +"," + this.mousey + ")");
}
}
if (verbosity >= 2) {
System.out.println("toggleSelectState " + this.toggleSelectState);
System.out.println("radioGroupName " + this.radioGroupName);
System.out.println("sliderValue " + this.sliderValue);
System.out.println("menuItem " + this.menuItem);
System.out.println("keyPress " + keyPress);
System.out.println("textContent " + textContent);
System.out.println("fileDialogPrompt " + this.fileDialogPrompt);
System.out.println("fileSelection " + this.fileSelection);
}
if (verbosity == 3 ) {
if (this.mouseEventType.equals("mouseMoved")) {
System.out.println("mouseMove at (" + this.mousex +"," + this.mousey + ")");
}
}
System.out.println(" ");
}
}
//////////////////////////////////////////////////////////////////
// Everything below here is stuff wrapped up by the SimpleUI class
// so you don't need to to look at it, or use it directly. But you can if you
// want to!
//
//////////////////////////////////////////////////////////////////
// Base class to all components
class Widget {
// Color for overall application
color SimpleUIAppBackgroundColor = color(240, 240, 240);// the light neutralgrey of the overall application surrounds
// Color for UI components
color SimpleUIBackgroundRectColor = color(230, 230, 240); // slightly purpley background rect Color for alternative UI's
color SimpleUIWidgetFillColor = color(200, 200, 200);// darker grey for butttons
color SimpleUIWidgetRolloverColor = color(215, 215, 215);// slightly lighter rollover Color
color SimpleUITextColor = color(0, 0, 0);
// should any widgets need to "talk" to other widgets (RadioButtons, Menus)
SimpleUI parentManager = null;
// Because you can have more than one UIManager in a system,
// e.g. a seperate one for popups, or tool modes
String UIManagerName;
// this should be the best way to identify a widget, so make sure
// that all UILabels are unique
String UILabel;
boolean displayLabel = true;
// type of component e.g. "UIButton", should be absolutely same as class name
public String UIComponentType = "WidgetBaseClass";
// location and size of widget
int widgetWidth, widgetHeight;
int locX, locY;
public SimpleUIRect bounds;
// needed by most, but not all widgets
boolean rollover = false;
// needed by some widgets but not all
boolean selected = false;
boolean isVisible = true;
public Widget(String uiname) {
UIManagerName = uiname;
}
public Widget(String uiname, String uilabel, int x, int y, int w, int h) {
UIManagerName = uiname;
UILabel = uilabel;
setBounds(x, y, w, h);
}
// virtual functions
//
public void setBounds(int x, int y, int w, int h) {
locX = x;
locY = y;
widgetWidth = w;
widgetHeight = h;
bounds = new SimpleUIRect(x, y, x+w, y+h);
}
void setVisible(boolean v) {
isVisible = v;
}
boolean isVisible() {
return isVisible;
}
public boolean isInMe(int x, int y) {
if ( bounds.isPointInside(x, y)) return true;
return false;
}
public void setParentManager(SimpleUI manager) {
parentManager = manager;
}
public void setWidgetDims(int w, int h) {
setBounds(locX, locY, w, h);
}
public void setWidth(int w) {
setBounds(locX, locY, w, widgetHeight);
}
public void setHeight(int h) {
setBounds(locX, locY, widgetWidth, h);
}
public void showLabel(boolean show) {
displayLabel = show;
}
// "virtual" functions here
//
public void drawMe() {
}
// every widget implements this - it returns true or false only, but does not "do" the actual event action.
// All it does is report whereter of not this widget handles the mouse event, after whihc the mouse event is "absorbed"
public boolean handleMouseEvent(String mouseEventType, int x, int y) {
return false;
}
// this happens IF this widget handles this mouse event.
public void doEventAction(String mouseEventType, int x, int y) {
UIEventData uied = new UIEventData(UIManagerName, UIComponentType, UILabel, mouseEventType, x, y);
handleUIEvent(uied);
}
boolean handleKeyEvent(char k, int kcode, String keyEventType) {
return false;
}
void setSelected(boolean s) {
selected = s;
}
boolean isSelected(){ return selected; }
}
//////////////////////////////////////////////////////////////////
// A label class for just showing some text, with or without a boarder. End-user uneditable.
// the displayed text can be set at run-time
// Because there is no interaction with the SimpleLabel, it does not show its
// UILabel ever, but shows the text field.
class SimpleLabel extends Widget {
boolean showBoarders = true;
int textPad = 5;
String text;
int textSize = 12;
public SimpleLabel(String uiname, int x, int y, String uilable, String txt) {
super(uiname, uilable, x, y, 100, 20);
UIComponentType = "SimpleLabel";
this.text = txt;
}
void showBoarders(boolean b) {
this.showBoarders = b;
}
public void drawMe() {
pushStyle();
stroke(100, 100, 100);
strokeWeight(1);
fill(SimpleUIBackgroundRectColor);
if (showBoarders) rect(locX, locY, widgetWidth, widgetHeight);
if ( this.text.length() < width/5) {
textSize = 12;
} else {
textSize = 8;
}
fill(SimpleUITextColor);
textSize(textSize);
strokeWeight(1);
text(this.text, locX+textPad-2, locY+textPad, widgetWidth, widgetHeight);
popStyle();
}
void setText(String txt) {
this.text = txt;
}
String getText() {
return this.text;
}
}
//////////////////////////////////////////////////////////////////
// Base button class, functions as a simple button, and is the base class for
// toggle and radio buttons
class SimpleButton extends Widget {
int textPad = 5;
int textSize = 10;
PImage icon = null;
public SimpleButton(String uiname, int x, int y, String uilable) {
super(uiname, uilable, x, y, 60, 20);
UIComponentType = "SimpleButton";
}
void setIcon(PImage iconImg) {
icon = iconImg.copy();
icon.resize(widgetWidth-2, widgetHeight-2);
}
public void setButtonDims(int w, int h) {
setBounds(locX, locY, w, h);
}
public boolean handleMouseEvent(String mouseEventType, int x, int y) {
if ( isInMe(x, y) /*&& (mouseEventType.equals("mouseMoved") || mouseEventType.equals("mousePressed"))*/) {
rollover = true;
} else {
rollover = false;
}
if ( isInMe(x, y) && mouseEventType.equals("mouseReleased")) {
return true;
}
return false;
}
public void drawMe() {
pushStyle();
stroke(0, 0, 0);
strokeWeight(1);
if (rollover) {
fill(SimpleUIWidgetRolloverColor);
} else {
fill(SimpleUIWidgetFillColor);
}
rect(locX, locY, widgetWidth, widgetHeight);
fill(SimpleUITextColor);
if ( this.UILabel.length() < 10) {
textSize = 12;
} else {
textSize = 9;
}
textSize(textSize);
strokeWeight(1);
text(this.UILabel, locX+textPad, locY+textPad, widgetWidth, widgetHeight);
popStyle();
drawIconImage();
}
void drawIconImage() {
if (icon == null) return;
pushStyle();
tint(127, 127, 127, 255);
if (rollover) tint(200, 200, 200, 255);
if (rollover && mousePressed) tint(255, 255, 255, 255);
if (selected) tint(255, 255, 255, 255);
image(icon, locX+1, locY+1, widgetWidth-2, widgetHeight-2);
popStyle();
}
}
//////////////////////////////////////////////////////////////////
// ToggleButton
class ToggleButton extends SimpleButton {
public ToggleButton(String uiname, int x, int y, String labelString) {
super(uiname, x, y, labelString);
UIComponentType = "ToggleButton";
}
public boolean handleMouseEvent(String mouseEventType, int x, int y) {
if ( isInMe(x, y) && (mouseEventType.equals("mouseMoved") || mouseEventType.equals("mousePressed"))) {
rollover = true;
} else {
rollover = false;
}
if ( isInMe(x, y) && mouseEventType.equals("mouseReleased")) {
return true;
}
return false;
}
public void doEventAction(String mouseEventType, int x, int y) {
swapSelectedState();
UIEventData uied = new UIEventData(UIManagerName, UIComponentType, UILabel, mouseEventType, x, y);
uied.toggleSelectState = selected;
handleUIEvent(uied);
}
public void swapSelectedState() {
selected = !selected;
}
public void drawMe() {
pushStyle();
stroke(0, 0, 0);
if (rollover) {
fill(SimpleUIWidgetRolloverColor);
} else {
fill(SimpleUIWidgetFillColor);
}
if (selected) {
strokeWeight(2);
rect(locX+1, locY+1, widgetWidth-2, widgetHeight-2);
} else {
strokeWeight(1);
rect(locX, locY, widgetWidth, widgetHeight);
}
stroke(0, 0, 0);
strokeWeight(1);
fill(SimpleUITextColor);
textSize(textSize);
strokeWeight(1);
text(this.UILabel, locX+textPad, locY+textPad, widgetWidth, widgetHeight);
popStyle();
}
}
//////////////////////////////////////////////////////////////////
// RadioButton
class RadioButton extends ToggleButton {
// these have to be part of the base class as is accessed by manager
public String radioGroupName = "";
public RadioButton(String uiname, int x, int y, String labelString, String groupName, SimpleUI manager) {
super(uiname, x, y, labelString);
radioGroupName = groupName;
UIComponentType = "RadioButton";
parentManager = manager;
}
public boolean handleMouseEvent(String mouseEventType, int x, int y) {
if ( isInMe(x, y) && (mouseEventType.equals("mouseMoved") || mouseEventType.equals("mousePressed"))) {
rollover = true;
} else {
rollover = false;
}
if ( isInMe(x, y) && mouseEventType.equals("mouseReleased")) {
return true;
}
return false;
}
public void doEventAction(String mouseEventType, int x, int y) {
parentManager.setRadioButtonOff(this.radioGroupName);
selected = true;
UIEventData uied = new UIEventData(UIManagerName, UIComponentType, UILabel, mouseEventType, x, y);
uied.toggleSelectState = selected;
uied.radioGroupName = this.radioGroupName;
handleUIEvent(uied);
}
public void turnOff(String groupName) {
if (groupName.equals(radioGroupName)) {
selected = false;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// menu stuff
//
//
/////////////////////////////////////////////////////////////////////////////
// the menu class
//
class Menu extends Widget {
int textPad = 5;
//String title;
int textSize = 12;
int numItems = 0;
SimpleUI parentManager;
boolean showLastSelection = false;
String lastSelection = "";
public boolean visible = false;
ArrayList<String> itemList = new ArrayList<String>();
public Menu(String uiname, String uilabel, int x, int y, String[] menuItems, SimpleUI manager) {
super(uiname, uilabel, x, y, 100, 20);
parentManager = manager;
UIComponentType = "Menu";
setMenuItems(menuItems);
}
public void drawMe() {
//System.out.println("drawing menu " + title);
drawTitle();
if ( visible ) {
drawItems();
}