-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.c
1301 lines (1122 loc) · 41.3 KB
/
example.c
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
//
// based on NanoVG's example code by Mikko Mononen
#include <stdio.h>
#ifdef NANOVG_GLEW
#define GLEW_STATIC
# include <GL/glew.h>
#endif
#ifdef __APPLE__
# define GLFW_INCLUDE_GLCOREARB
#endif
#include <GLFW/glfw3.h>
#include "nanovg.h"
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg_gl.h"
#define BLENDISH_IMPLEMENTATION
#include "blendish.h"
#define OUI_IMPLEMENTATION
#include "oui.h"
////////////////////////////////////////////////////////////////////////////////
typedef enum {
// label
ST_LABEL = 0,
// button
ST_BUTTON = 1,
// radio button
ST_RADIO = 2,
// progress slider
ST_SLIDER = 3,
// column
ST_COLUMN = 4,
// row
ST_ROW = 5,
// check button
ST_CHECK = 6,
// panel
ST_PANEL = 7,
// text
ST_TEXT = 8,
//
ST_IGNORE = 9,
ST_DEMOSTUFF = 10,
// colored rectangle
ST_RECT = 11,
ST_HBOX = 12,
ST_VBOX = 13,
} SubType;
typedef struct {
int subtype;
UIhandler handler;
} UIData;
typedef struct {
UIData head;
const char *label;
NVGcolor color;
} UIRectData;
typedef struct {
UIData head;
int iconid;
const char *label;
} UIButtonData;
typedef struct {
UIData head;
const char *label;
int *option;
} UICheckData;
typedef struct {
UIData head;
int iconid;
const char *label;
int *value;
} UIRadioData;
typedef struct {
UIData head;
const char *label;
float *progress;
} UISliderData;
typedef struct {
UIData head;
char *text;
int maxsize;
} UITextData;
////////////////////////////////////////////////////////////////////////////////
void draw_demostuff(NVGcontext *vg, int x, int y, float w, float h);
static struct NVGcontext* _vg = NULL;
void ui_handler(int item, UIevent event) {
UIData *data = (UIData *)uiGetHandle(item);
if (data && data->handler) {
data->handler(item, event);
}
}
void init(NVGcontext *vg) {
bndSetFont(nvgCreateFont(vg, "system", "../DejaVuSans.ttf"));
bndSetIconImage(nvgCreateImage(vg, "../blender_icons16.png", 0));
}
void testrect(NVGcontext *vg, UIrect rect) {
#if 0
nvgBeginPath(vg);
nvgRect(vg,rect.x+0.5,rect.y+0.5,rect.w-1,rect.h-1);
nvgStrokeColor(vg,nvgRGBf(1,0,0));
nvgStrokeWidth(vg,1);
nvgStroke(vg);
#endif
}
void drawUI(NVGcontext *vg, int item, int corners);
void drawUIItems(NVGcontext *vg, int item, int corners) {
int kid = uiFirstChild(item);
while (kid > 0) {
drawUI(vg, kid, corners);
kid = uiNextSibling(kid);
}
}
void drawUIItemsHbox(NVGcontext *vg, int item) {
int kid = uiFirstChild(item);
if (kid < 0) return;
int nextkid = uiNextSibling(kid);
if (nextkid < 0) {
drawUI(vg, kid, BND_CORNER_NONE);
} else {
drawUI(vg, kid, BND_CORNER_RIGHT);
kid = nextkid;
while (uiNextSibling(kid) > 0) {
drawUI(vg, kid, BND_CORNER_ALL);
kid = uiNextSibling(kid);
}
drawUI(vg, kid, BND_CORNER_LEFT);
}
}
void drawUIItemsVbox(NVGcontext *vg, int item) {
int kid = uiFirstChild(item);
if (kid < 0) return;
int nextkid = uiNextSibling(kid);
if (nextkid < 0) {
drawUI(vg, kid, BND_CORNER_NONE);
} else {
drawUI(vg, kid, BND_CORNER_DOWN);
kid = nextkid;
while (uiNextSibling(kid) > 0) {
drawUI(vg, kid, BND_CORNER_ALL);
kid = uiNextSibling(kid);
}
drawUI(vg, kid, BND_CORNER_TOP);
}
}
void drawUI(NVGcontext *vg, int item, int corners) {
const UIData *head = (const UIData *)uiGetHandle(item);
UIrect rect = uiGetRect(item);
if (uiGetState(item) == UI_FROZEN) {
nvgGlobalAlpha(vg, BND_DISABLED_ALPHA);
}
if (head) {
switch(head->subtype) {
default: {
testrect(vg,rect);
drawUIItems(vg,item,corners);
} break;
case ST_HBOX: {
drawUIItemsHbox(vg, item);
} break;
case ST_VBOX: {
drawUIItemsVbox(vg, item);
} break;
case ST_PANEL: {
bndBevel(vg,rect.x,rect.y,rect.w,rect.h);
drawUIItems(vg,item,corners);
} break;
case ST_LABEL: {
assert(head);
const UIButtonData *data = (UIButtonData*)head;
bndLabel(vg,rect.x,rect.y,rect.w,rect.h,
data->iconid,data->label);
} break;
case ST_BUTTON: {
const UIButtonData *data = (UIButtonData*)head;
bndToolButton(vg,rect.x,rect.y,rect.w,rect.h,
corners,(BNDwidgetState)uiGetState(item),
data->iconid,data->label);
} break;
case ST_CHECK: {
const UICheckData *data = (UICheckData*)head;
BNDwidgetState state = (BNDwidgetState)uiGetState(item);
if (*data->option)
state = BND_ACTIVE;
bndOptionButton(vg,rect.x,rect.y,rect.w,rect.h, state,
data->label);
} break;
case ST_RADIO:{
const UIRadioData *data = (UIRadioData*)head;
BNDwidgetState state = (BNDwidgetState)uiGetState(item);
if (*data->value == item)
state = BND_ACTIVE;
bndRadioButton(vg,rect.x,rect.y,rect.w,rect.h,
corners,state,
data->iconid,data->label);
} break;
case ST_SLIDER:{
const UISliderData *data = (UISliderData*)head;
BNDwidgetState state = (BNDwidgetState)uiGetState(item);
static char value[32];
sprintf(value,"%.0f%%",(*data->progress)*100.0f);
bndSlider(vg,rect.x,rect.y,rect.w,rect.h,
corners,state,
*data->progress,data->label,value);
} break;
case ST_TEXT: {
const UITextData *data = (UITextData*)head;
BNDwidgetState state = (BNDwidgetState)uiGetState(item);
int idx = strlen(data->text);
bndTextField(vg,rect.x,rect.y,rect.w,rect.h,
corners,state, -1, data->text, idx, idx);
} break;
case ST_DEMOSTUFF: {
draw_demostuff(vg, rect.x, rect.y, rect.w, rect.h);
} break;
case ST_RECT: {
const UIRectData *data = (UIRectData*)head;
if (rect.w && rect.h) {
BNDwidgetState state = (BNDwidgetState)uiGetState(item);
nvgSave(vg);
nvgStrokeColor(vg, nvgRGBAf(data->color.r,data->color.g,data->color.b,0.9f));
if (state != BND_DEFAULT) {
nvgFillColor(vg, nvgRGBAf(data->color.r,data->color.g,data->color.b,0.5f));
} else {
nvgFillColor(vg, nvgRGBAf(data->color.r,data->color.g,data->color.b,0.1f));
}
nvgStrokeWidth(vg,2);
nvgBeginPath(vg);
#if 0
nvgRect(vg,rect.x,rect.y,rect.w,rect.h);
#else
nvgRoundedRect(vg,rect.x,rect.y,rect.w,rect.h,3);
#endif
nvgFill(vg);
nvgStroke(vg);
if (state != BND_DEFAULT) {
nvgFillColor(vg, nvgRGBAf(0.0f,0.0f,0.0f,1.0f));
nvgFontSize(vg, 15.0f);
nvgBeginPath(vg);
nvgTextAlign(vg, NVG_ALIGN_TOP|NVG_ALIGN_CENTER);
nvgTextBox(vg, rect.x, rect.y+rect.h*0.3f, rect.w, data->label, NULL);
}
nvgRestore(vg);
}
nvgSave(vg);
nvgIntersectScissor(vg, rect.x, rect.y, rect.w, rect.h);
drawUIItems(vg,item,corners);
nvgRestore(vg);
} break;
}
} else {
testrect(vg,rect);
drawUIItems(vg,item,corners);
}
if (uiGetState(item) == UI_FROZEN) {
nvgGlobalAlpha(vg, 1.0);
}
}
int colorrect(const char *label, NVGcolor color) {
int item = uiItem();
UIRectData *data = (UIRectData *)uiAllocHandle(item, sizeof(UIRectData));
data->head.subtype = ST_RECT;
data->head.handler = NULL;
data->label = label;
data->color = color;
uiSetEvents(item, UI_BUTTON0_DOWN);
return item;
}
int label(int iconid, const char *label) {
int item = uiItem();
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
UIButtonData *data = (UIButtonData *)uiAllocHandle(item, sizeof(UIButtonData));
data->head.subtype = ST_LABEL;
data->head.handler = NULL;
data->iconid = iconid;
data->label = label;
return item;
}
void demohandler(int item, UIevent event) {
const UIButtonData *data = (const UIButtonData *)uiGetHandle(item);
printf("clicked: %p %s\n", uiGetHandle(item), data->label);
}
int button(int iconid, const char *label, UIhandler handler) {
// create new ui item
int item = uiItem();
// set size of wiget; horizontal size is dynamic, vertical is fixed
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
uiSetEvents(item, UI_BUTTON0_HOT_UP);
// store some custom data with the button that we use for styling
UIButtonData *data = (UIButtonData *)uiAllocHandle(item, sizeof(UIButtonData));
data->head.subtype = ST_BUTTON;
data->head.handler = handler;
data->iconid = iconid;
data->label = label;
return item;
}
void checkhandler(int item, UIevent event) {
const UICheckData *data = (const UICheckData *)uiGetHandle(item);
*data->option = !(*data->option);
}
int check(const char *label, int *option) {
// create new ui item
int item = uiItem();
// set size of wiget; horizontal size is dynamic, vertical is fixed
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
// attach event handler e.g. demohandler above
uiSetEvents(item, UI_BUTTON0_DOWN);
// store some custom data with the button that we use for styling
UICheckData *data = (UICheckData *)uiAllocHandle(item, sizeof(UICheckData));
data->head.subtype = ST_CHECK;
data->head.handler = checkhandler;
data->label = label;
data->option = option;
return item;
}
// simple logic for a slider
// starting offset of the currently active slider
static float sliderstart = 0.0;
// event handler for slider (same handler for all sliders)
void sliderhandler(int item, UIevent event) {
// retrieve the custom data we saved with the slider
UISliderData *data = (UISliderData *)uiGetHandle(item);
switch(event) {
default: break;
case UI_BUTTON0_DOWN: {
// button was pressed for the first time; capture initial
// slider value.
sliderstart = *data->progress;
} break;
case UI_BUTTON0_CAPTURE: {
// called for every frame that the button is pressed.
// get the delta between the click point and the current
// mouse position
UIvec2 pos = uiGetCursorStartDelta();
// get the items layouted rectangle
UIrect rc = uiGetRect(item);
// calculate our new offset and clamp
float value = sliderstart + ((float)pos.x / (float)rc.w);
value = (value<0)?0:(value>1)?1:value;
// assign the new value
*data->progress = value;
} break;
}
}
int slider(const char *label, float *progress) {
// create new ui item
int item = uiItem();
// set size of wiget; horizontal size is dynamic, vertical is fixed
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
// attach our slider event handler and capture two classes of events
uiSetEvents(item, UI_BUTTON0_DOWN | UI_BUTTON0_CAPTURE);
// store some custom data with the button that we use for styling
// and logic, e.g. the pointer to the data we want to alter.
UISliderData *data = (UISliderData *)uiAllocHandle(item, sizeof(UISliderData));
data->head.subtype = ST_SLIDER;
data->head.handler = sliderhandler;
data->label = label;
data->progress = progress;
return item;
}
void textboxhandler(int item, UIevent event) {
UITextData *data = (UITextData *)uiGetHandle(item);
switch(event) {
default: break;
case UI_BUTTON0_DOWN: {
uiFocus(item);
} break;
case UI_KEY_DOWN: {
unsigned int key = uiGetKey();
switch(key) {
default: break;
case GLFW_KEY_BACKSPACE: {
int size = strlen(data->text);
if (!size) return;
data->text[size-1] = 0;
} break;
case GLFW_KEY_ENTER: {
uiFocus(-1);
} break;
}
} break;
case UI_CHAR: {
unsigned int key = uiGetKey();
if ((key > 255)||(key < 32)) return;
int size = strlen(data->text);
if (size >= (data->maxsize-1)) return;
data->text[size] = (char)key;
} break;
}
}
int textbox(char *text, int maxsize) {
int item = uiItem();
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
uiSetEvents(item, UI_BUTTON0_DOWN | UI_KEY_DOWN | UI_CHAR);
// store some custom data with the button that we use for styling
// and logic, e.g. the pointer to the data we want to alter.
UITextData *data = (UITextData *)uiAllocHandle(item, sizeof(UITextData));
data->head.subtype = ST_TEXT;
data->head.handler = textboxhandler;
data->text = text;
data->maxsize = maxsize;
return item;
}
// simple logic for a radio button
void radiohandler(int item, UIevent event) {
UIRadioData *data = (UIRadioData *)uiGetHandle(item);
*data->value = item;
}
int radio(int iconid, const char *label, int *value) {
int item = uiItem();
uiSetSize(item, label?0:BND_TOOL_WIDTH, BND_WIDGET_HEIGHT);
UIRadioData *data = (UIRadioData *)uiAllocHandle(item, sizeof(UIRadioData));
data->head.subtype = ST_RADIO;
data->head.handler = radiohandler;
data->iconid = iconid;
data->label = label;
data->value = value;
uiSetEvents(item, UI_BUTTON0_DOWN);
return item;
}
int panel() {
int item = uiItem();
UIData *data = (UIData *)uiAllocHandle(item, sizeof(UIData));
data->subtype = ST_PANEL;
data->handler = NULL;
return item;
}
int hbox() {
int item = uiItem();
UIData *data = (UIData *)uiAllocHandle(item, sizeof(UIData));
data->subtype = ST_HBOX;
data->handler = NULL;
uiSetBox(item, UI_ROW);
return item;
}
int vbox() {
int item = uiItem();
UIData *data = (UIData *)uiAllocHandle(item, sizeof(UIData));
data->subtype = ST_VBOX;
data->handler = NULL;
uiSetBox(item, UI_COLUMN);
return item;
}
int column_append(int parent, int item) {
uiInsert(parent, item);
// fill parent horizontally, anchor to previous item vertically
uiSetLayout(item, UI_HFILL);
uiSetMargins(item, 0, 1, 0, 0);
return item;
}
int column() {
int item = uiItem();
uiSetBox(item, UI_COLUMN);
return item;
}
int vgroup_append(int parent, int item) {
uiInsert(parent, item);
// fill parent horizontally, anchor to previous item vertically
uiSetLayout(item, UI_HFILL);
return item;
}
int vgroup() {
int item = uiItem();
uiSetBox(item, UI_COLUMN);
return item;
}
int hgroup_append(int parent, int item) {
uiInsert(parent, item);
uiSetLayout(item, UI_HFILL);
return item;
}
int hgroup_append_fixed(int parent, int item) {
uiInsert(parent, item);
return item;
}
int hgroup() {
int item = uiItem();
uiSetBox(item, UI_ROW);
return item;
}
int row_append(int parent, int item) {
uiInsert(parent, item);
uiSetLayout(item, UI_HFILL);
return item;
}
int row() {
int item = uiItem();
uiSetBox(item, UI_ROW);
return item;
}
void draw_noodles(NVGcontext *vg, int x, int y) {
int w = 200;
int s = 70;
bndNodeBackground(vg, x+w, y-50, 100, 200, BND_DEFAULT, BND_ICONID(6,3),
"Default", nvgRGBf(0.392f,0.392f,0.392f));
bndNodeBackground(vg, x+w+120, y-50, 100, 200, BND_HOVER, BND_ICONID(6,3),
"Hover", nvgRGBf(0.392f,0.392f,0.392f));
bndNodeBackground(vg, x+w+240, y-50, 100, 200, BND_ACTIVE, BND_ICONID(6,3),
"Active", nvgRGBf(0.392f,0.392f,0.392f));
for (int i = 0; i < 9; ++i) {
int a = i%3;
int b = i/3;
bndNodeWire(vg, x, y+s*a, x+w, y+s*b, (BNDwidgetState)a, (BNDwidgetState)b);
}
bndNodePort(vg, x, y, BND_DEFAULT, nvgRGBf(0.5f, 0.5f, 0.5f));
bndNodePort(vg, x+w, y, BND_DEFAULT, nvgRGBf(0.5f, 0.5f, 0.5f));
bndNodePort(vg, x, y+s, BND_HOVER, nvgRGBf(0.5f, 0.5f, 0.5f));
bndNodePort(vg, x+w, y+s, BND_HOVER, nvgRGBf(0.5f, 0.5f, 0.5f));
bndNodePort(vg, x, y+2*s, BND_ACTIVE, nvgRGBf(0.5f, 0.5f, 0.5f));
bndNodePort(vg, x+w, y+2*s, BND_ACTIVE, nvgRGBf(0.5f, 0.5f, 0.5f));
}
static void roothandler(int parent, UIevent event) {
switch(event) {
default: break;
case UI_SCROLL: {
UIvec2 pos = uiGetScroll();
printf("scroll! %d %d\n", pos.x, pos.y);
} break;
case UI_BUTTON0_DOWN: {
printf("%d clicks\n", uiGetClicks());
} break;
}
}
void draw_demostuff(NVGcontext *vg, int x, int y, float w, float h) {
nvgSave(vg);
nvgTranslate(vg, x, y);
bndSplitterWidgets(vg, 0, 0, w, h);
x = 10;
y = 10;
bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
BND_ICONID(6,3),"Default");
y += 25;
bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
BND_ICONID(6,3),"Hovered");
y += 25;
bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
BND_ICONID(6,3),"Active");
y += 40;
bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
-1,"Default");
y += 25;
bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
-1,"Hovered");
y += 25;
bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
-1,"Active");
y += 25;
bndLabel(vg,x,y,120,BND_WIDGET_HEIGHT,-1,"Label:");
y += BND_WIDGET_HEIGHT;
bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
-1, "Default");
y += 25;
bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
-1, "Hovered");
y += 25;
bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
-1, "Active");
y += 25;
int ry = y;
int rx = x;
y = 10;
x += 130;
bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_DEFAULT,"Default");
y += 25;
bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_HOVER,"Hovered");
y += 25;
bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_ACTIVE,"Active");
y += 40;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_DOWN,BND_DEFAULT,
"Top","100");
y += BND_WIDGET_HEIGHT-2;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
"Center","100");
y += BND_WIDGET_HEIGHT-2;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_TOP,BND_DEFAULT,
"Bottom","100");
int mx = x-30;
int my = y-12;
int mw = 120;
bndMenuBackground(vg,mx,my,mw,120,BND_CORNER_TOP);
bndMenuLabel(vg,mx,my,mw,BND_WIDGET_HEIGHT,-1,"Menu Title");
my += BND_WIDGET_HEIGHT-2;
bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_DEFAULT,
BND_ICONID(17,3),"Default");
my += BND_WIDGET_HEIGHT-2;
bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_HOVER,
BND_ICONID(18,3),"Hovered");
my += BND_WIDGET_HEIGHT-2;
bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_ACTIVE,
BND_ICONID(19,3),"Active");
y = 10;
x += 130;
int ox = x;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
"Default","100");
y += 25;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
"Hovered","100");
y += 25;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
"Active","100");
y += 40;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,BND_DEFAULT,
-1,"One");
x += 60-1;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
-1,"Two");
x += 60-1;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
-1,"Three");
x += 60-1;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,BND_ACTIVE,
-1,"Butts");
x = ox;
y += 40;
float progress_value = fmodf(glfwGetTime()/10.0,1.0);
char progress_label[32];
sprintf(progress_label, "%d%%", (int)(progress_value*100+0.5f));
bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
progress_value,"Default",progress_label);
y += 25;
bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
progress_value,"Hovered",progress_label);
y += 25;
bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
progress_value,"Active",progress_label);
int rw = x+240-rx;
float s_offset = sinf(glfwGetTime()/2.0)*0.5+0.5;
float s_size = cosf(glfwGetTime()/3.11)*0.5+0.5;
bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_DEFAULT,s_offset,s_size);
ry += 20;
bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_HOVER,s_offset,s_size);
ry += 20;
bndScrollBar(vg,rx,ry,rw,BND_SCROLLBAR_HEIGHT,BND_ACTIVE,s_offset,s_size);
const char edit_text[] = "The quick brown fox";
int textlen = strlen(edit_text)+1;
int t = (int)(glfwGetTime()*2);
int idx1 = (t/textlen)%textlen;
int idx2 = idx1 + (t%(textlen-idx1));
ry += 25;
bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
-1, edit_text, idx1, idx2);
ry += 25;
bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
-1, edit_text, idx1, idx2);
ry += 25;
bndTextField(vg,rx,ry,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
-1, edit_text, idx1, idx2);
draw_noodles(vg, 20, ry+50);
rx += rw + 20;
ry = 10;
bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_DEFAULT,s_offset,s_size);
rx += 20;
bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_HOVER,s_offset,s_size);
rx += 20;
bndScrollBar(vg,rx,ry,BND_SCROLLBAR_WIDTH,240,BND_ACTIVE,s_offset,s_size);
x = ox;
y += 40;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
BND_DEFAULT,BND_ICONID(0,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(1,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(2,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(3,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(4,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
BND_DEFAULT,BND_ICONID(5,10),NULL);
x += BND_TOOL_WIDTH-1;
x += 5;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
BND_DEFAULT,BND_ICONID(0,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(1,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(2,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(3,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_ACTIVE,BND_ICONID(4,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
BND_DEFAULT,BND_ICONID(5,11),NULL);
nvgRestore(vg);
}
static int enum1 = -1;
void build_democontent(int parent) {
// some persistent variables for demonstration
static float progress1 = 0.25f;
static float progress2 = 0.75f;
static int option1 = 1;
static int option2 = 0;
static int option3 = 0;
int col = column();
uiInsert(parent, col);
uiSetMargins(col, 10, 10, 10, 10);
uiSetLayout(col, UI_TOP|UI_HFILL);
column_append(col, button(BND_ICON_GHOST, "Item 1", demohandler));
if (option3)
column_append(col, button(BND_ICON_GHOST, "Item 2", demohandler));
{
int h = column_append(col, hbox());
hgroup_append(h, radio(BND_ICON_GHOST, "Item 3.0", &enum1));
if (option2)
uiSetMargins(hgroup_append_fixed(h, radio(BND_ICON_REC, NULL, &enum1)), -1,0,0,0);
uiSetMargins(hgroup_append_fixed(h, radio(BND_ICON_PLAY, NULL, &enum1)), -1,0,0,0);
uiSetMargins(hgroup_append(h, radio(BND_ICON_GHOST, "Item 3.3", &enum1)), -1,0,0,0);
}
{
int rows = column_append(col, row());
int coll = row_append(rows, vgroup());
vgroup_append(coll, label(-1, "Items 4.0:"));
coll = vgroup_append(coll, vbox());
vgroup_append(coll, button(BND_ICON_GHOST, "Item 4.0.0", demohandler));
uiSetMargins(vgroup_append(coll, button(BND_ICON_GHOST, "Item 4.0.1", demohandler)),0,-2,0,0);
int colr = row_append(rows, vgroup());
uiSetMargins(colr, 8, 0, 0, 0);
uiSetFrozen(colr, option1);
vgroup_append(colr, label(-1, "Items 4.1:"));
colr = vgroup_append(colr, vbox());
vgroup_append(colr, slider("Item 4.1.0", &progress1));
uiSetMargins(vgroup_append(colr, slider("Item 4.1.1", &progress2)),0,-2,0,0);
}
column_append(col, button(BND_ICON_GHOST, "Item 5", NULL));
static char textbuffer[1024] = "The quick brown fox.";
column_append(col, textbox(textbuffer, 1024));
column_append(col, check("Frozen", &option1));
column_append(col, check("Item 7", &option2));
column_append(col, check("Item 8", &option3));
}
int demorect(int parent, const char *label, float hue, int box, int layout, int w, int h, int m1, int m2, int m3, int m4) {
int item = colorrect(label, nvgHSL(hue, 1.0f, 0.8f));
uiSetLayout(item, layout);
uiSetBox(item, box);
uiSetMargins(item, m1, m2, m3, m4);
uiSetSize(item, w, h);
uiInsert(parent, item);
return item;
}
void build_layoutdemo(int parent) {
const int M = 10;
const int S = 150;
int box = demorect(parent, "Box( UI_LAYOUT )\nLayout( UI_FILL )", 0.6f, UI_LAYOUT, UI_FILL, 0, 0, M, M, M, M);
demorect(box, "Layout( UI_HFILL | UI_TOP )", 0.7f, 0, UI_HFILL|UI_TOP, S, S+M, M, M, M, 0);
demorect(box, "Layout( UI_HFILL )", 0.7f, 0, UI_HFILL, S, S+2*M, M, 0, M, 0);
demorect(box, "Layout( UI_HFILL | UI_DOWN )", 0.7f, 0, UI_HFILL|UI_DOWN, S, S+M, M, 0, M, M);
demorect(box, "Layout( UI_LEFT | UI_VFILL )", 0.7f, 0, UI_LEFT|UI_VFILL, S+M, S, M, M, 0, M);
demorect(box, "Layout( UI_VFILL )", 0.7f, 0, UI_VFILL, S+2*M, S, 0, M, 0, M);
demorect(box, "Layout( UI_RIGHT | UI_VFILL )", 0.7f, 0, UI_RIGHT|UI_VFILL, S+M, S, 0, M, M, M);
demorect(box, "Layout( UI_LEFT | UI_TOP )", 0.55f, 0, UI_LEFT|UI_TOP, S, S, M, M, 0, 0);
demorect(box, "Layout( UI_TOP )", 0.57f, 0, UI_TOP, S, S, 0, M, 0, 0);
demorect(box, "Layout( UI_RIGHT | UI_TOP )", 0.55f, 0, UI_RIGHT|UI_TOP, S, S, 0, M, M, 0);
demorect(box, "Layout( UI_LEFT )", 0.57f, 0, UI_LEFT, S, S, M, 0, 0, 0);
demorect(box, "Layout( UI_CENTER )", 0.59f, 0, UI_CENTER, S, S, 0, 0, 0, 0);
demorect(box, "Layout( UI_RIGHT )", 0.57f, 0, UI_RIGHT, S, S, 0, 0, M, 0);
demorect(box, "Layout( UI_LEFT | UI_DOWN )", 0.55f, 0, UI_LEFT|UI_DOWN, S, S, M, 0, 0, M);
demorect(box, "Layout( UI_DOWN)", 0.57f, 0, UI_DOWN, S, S, 0, 0, 0, M);
demorect(box, "Layout( UI_RIGHT | UI_DOWN )", 0.55f, 0, UI_RIGHT|UI_DOWN, S, S, 0, 0, M, M);
}
void build_rowdemo(int parent) {
uiSetBox(parent, UI_COLUMN);
const int M = 10;
const int S = 200;
const int T = 100;
{
int box = demorect(parent, "Box( UI_ROW )\nLayout( UI_LEFT | UI_VFILL )", 0.6f, UI_ROW, UI_LEFT|UI_VFILL, 0, S, M, M, M, M);
demorect(box, "Layout( UI_TOP )", 0.05f, 0, UI_TOP, T, T, M, M, M, 0);
demorect(box, "Layout( UI_VCENTER )", 0.1f, 0, UI_VCENTER, T, T, 0, 0, M, 0);
demorect(box, "Layout( UI_VFILL )", 0.15f, 0, UI_VFILL, T, T, 0, M, M, M);
demorect(box, "Layout( UI_DOWN )", 0.25f, 0, UI_DOWN, T, T, 0, 0, M, M);
}
{
int box = demorect(parent, "Box( UI_ROW | UI_JUSTIFY )\nLayout( UI_FILL )", 0.6f, UI_ROW|UI_JUSTIFY, UI_FILL, 0, S, M, 0, M, M);
demorect(box, "Layout( UI_TOP )", 0.05f, 0, UI_TOP, T, T, M, M, M, 0);
demorect(box, "Layout( UI_VCENTER )", 0.1f, 0, UI_VCENTER, T, T, 0, 0, M, 0);
demorect(box, "Layout( UI_VFILL )", 0.15f, 0, UI_VFILL, T, T, 0, M, M, M);
demorect(box, "Layout( UI_DOWN )", 0.25f, 0, UI_DOWN, T, T, 0, 0, M, M);
}
{
int box = demorect(parent, "Box( UI_ROW )\nLayout( UI_FILL )", 0.6f, UI_ROW, UI_FILL, 0, S, M, 0, M, M);
demorect(box, "Layout( UI_TOP )", 0.05f, 0, UI_TOP, T, T, M, M, M, 0);
demorect(box, "Layout( UI_VCENTER )", 0.1f, 0, UI_VCENTER, T, T, 0, 0, M, 0);
demorect(box, "Layout( UI_VFILL )", 0.15f, 0, UI_VFILL, T, T, 0, M, M, M);
demorect(box, "Layout( UI_HFILL )", 0.2f, 0, UI_HFILL, T, T, 0, 0, M, 0);
demorect(box, "Layout( UI_HFILL )", 0.2f, 0, UI_HFILL, T, T, 0, 0, M, 0);
demorect(box, "Layout( UI_HFILL )", 0.2f, 0, UI_HFILL, T, T, 0, 0, M, 0);
demorect(box, "Layout( UI_DOWN )", 0.25f, 0, UI_DOWN, T, T, 0, 0, M, M);
}
}
void build_columndemo(int parent) {
uiSetBox(parent, UI_ROW);
const int M = 10;
const int S = 200;
const int T = 100;
{
int box = demorect(parent, "Box( UI_COLUMN )\nLayout( UI_TOP | UI_HFILL )", 0.6f, UI_COLUMN, UI_TOP|UI_HFILL, S, 0, M, M, M, M);
demorect(box, "Layout( UI_LEFT )", 0.05f, 0, UI_LEFT, T, T, M, M, 0, M);
demorect(box, "Layout( UI_HCENTER )", 0.1f, 0, UI_HCENTER, T, T, 0, 0, 0, M);
demorect(box, "Layout( UI_HFILL )", 0.15f, 0, UI_HFILL, T, T, M, 0, M, M);
demorect(box, "Layout( UI_RIGHT )", 0.25f, 0, UI_RIGHT, T, T, 0, 0, M, M);
}
{
int box = demorect(parent, "Box( UI_COLUMN )\nLayout( UI_FILL )", 0.6f, UI_COLUMN, UI_FILL, S, 0, 0, M, M, M);
demorect(box, "Layout( UI_LEFT )", 0.05f, 0, UI_LEFT, T, T, M, M, 0, M);
demorect(box, "Layout( UI_HCENTER )", 0.1f, 0, UI_HCENTER, T, T, 0, 0, 0, M);
demorect(box, "Layout( UI_HFILL )", 0.15f, 0, UI_HFILL, T, T, M, 0, M, M);
demorect(box, "Layout( UI_RIGHT )", 0.25f, 0, UI_RIGHT, T, T, 0, 0, M, M);
}
{
int box = demorect(parent, "Box( UI_COLUMN )\nLayout( UI_FILL )", 0.6f, UI_COLUMN, UI_FILL, S, 0, 0, M, M, M);
demorect(box, "Layout( UI_LEFT )", 0.05f, 0, UI_LEFT, T, T, M, M, 0, M);
demorect(box, "Layout( UI_HCENTER )", 0.1f, 0, UI_HCENTER, T, T, 0, 0, 0, M);
demorect(box, "Layout( UI_HFILL )", 0.15f, 0, UI_HFILL, T, T, M, 0, M, M);
demorect(box, "Layout( UI_VFILL )", 0.2f, 0, UI_VFILL, T, T, 0, 0, 0, M);
demorect(box, "Layout( UI_VFILL )", 0.2f, 0, UI_VFILL, T, T, 0, 0, 0, M);
demorect(box, "Layout( UI_VFILL )", 0.2f, 0, UI_VFILL, T, T, 0, 0, 0, M);
demorect(box, "Layout( UI_RIGHT )", 0.25f, 0, UI_RIGHT, T, T, 0, 0, M, M);
}
}
void fill_wrap_row_box(int box) {
const int M = 5;
const int S = 100;
const int T = 50;
srand(303);
for (int i = 0; i < 20; ++i) {
float hue = (float)(rand()%360)/360.0f;
int width = 10 + (rand()%5)*10;
int u;
switch(rand()%4) {
default: break;
case 0: {
u = demorect(box, "Layout( UI_TOP )",
hue, 0, UI_TOP, width, T, M, M, M, M);
} break;
case 1: {
u = demorect(box, "Layout( UI_VCENTER )",
hue, 0, UI_VCENTER, width, T/2, M, M, M, M);
} break;
case 2: {
u = demorect(box, "Layout( UI_VFILL )",
hue, 0, UI_VFILL, width, T, M, M, M, M);
} break;
case 3: {
u = demorect(box, "Layout( UI_DOWN )",
hue, 0, UI_DOWN, width, T/2, M, M, M, M);
} break;
}
if (rand()%10 == 0)
uiSetLayout(u, uiGetLayout(u)|UI_BREAK);
}
}
void fill_wrap_column_box(int box) {
const int M = 5;
const int S = 100;
const int T = 50;
srand(303);
for (int i = 0; i < 20; ++i) {
float hue = (float)(rand()%360)/360.0f;
int height = 10 + (rand()%5)*10;
int u;
switch(rand()%4) {
default: break;
case 0: {
u = demorect(box, "Layout( UI_LEFT )",
hue, 0, UI_LEFT, T, height, M, M, M, M);
} break;
case 1: {
u = demorect(box, "Layout( UI_HCENTER )",
hue, 0, UI_HCENTER, T/2, height, M, M, M, M);
} break;