rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_oui_nanovg.sc -rw-r--r-- 28.4 KiB View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months ago
                                                                                
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
using import struct
using import enum

using import glm
using import glsl
using import itertools
import UTF-8

using import ..tukan.sdl
using import ..tukan.GLMain

using import ..tukan.gl
using import ..tukan.nanovg

using import ..tukan.random


let oui =
    import ..tukan.oui

let glmain =
    GLMain
        title = "OUI Examples"
        width = 960
        height = 540
        resizable = true

struct UIData plain
    tmp : i32
    handler : oui.Handler

global uictx = (oui.Context 65536:u32 (65536:u32 * 256:u32))

global vg = (nvgCreateGL3 1)

let assets =
    .. module-dir "/../assets/core"

bndSetFont
    nvgCreateFont vg "system" (assets .. "/fonts/DejaVuSans.ttf")
bndSetIconImage
    nvgCreateImage vg (assets .. "/textures/blender_icons16.png") 0
let font_path = (assets .. "/fonts/monaco.ttf")

let mono_font_size = 14
#if not path.isfile(font_path) then
    -- good font sizes: 15, 17, 26 (6.5)
    _mono_font_size = 15
    font_path = assets .. '/fonts/UbuntuMono-R.ttf'
#else
    _mono_font_size = 14
    --_mono_font_size = 16

global monofont =
    nvgCreateFont vg "fixed"
        .. assets "/fonts/UbuntuMono-R.ttf"

global cursor = (ivec2 100 100)

enum WidgetClass
    # label
    Label :
        label = rawstring
        iconid = i32
    # button
    Button :
        label = rawstring
        iconid = i32
    # radio button
    Radio :
        iconid = i32
        label = rawstring
        value = (mutable pointer oui.Item)
    # progress slider
    Slider :
        label = rawstring
        progress = (mutable pointer f32)
    # column
    Column
    # row
    Row
    # check button
    Check :
        label = rawstring
        option = (mutable pointer bool)
    # panel
    Panel
    # text
    Text :
        text = (mutable rawstring)
        maxsize = i32
    #
    Ignore

    # colored rectangle
    Rect :
        label = rawstring
        color = vec3

    HBox
    VBox

struct Widget
    content : WidgetClass
    handler : oui.Handler

inline widget-item (content handler item-attrs...)
    let item = ('item uictx item-attrs...)
    = ('alloc-handle uictx item Widget)
        Widget
            content = content
            static-if (none? handler) ()
            else (_ (handler = handler))
    item

fn colorrect (label color)
    widget-item
        WidgetClass.Rect
            label = label
            color = color
        events = oui.LButtonDown

fn textlabel (iconid label)
    widget-item
        WidgetClass.Label
            iconid = (iconid as integer as i32)
            label = label

let strlen =
    extern 'strlen (function usize rawstring)

fn textboxhandler (uictx item event)
    dispatch (('get-handle uictx item Widget) . content)
    case Text (text maxsize)
        switch event
        case oui.LButtonDown
            'focus uictx item
        case oui.KeyDown
            let key = ('get-key uictx)
            switch (key as (typeof SDLK_BACKSPACE))
            case SDLK_BACKSPACE
                let size = (strlen text)
                if (not size)
                    return;
                text @ (size - 1) = 0:i8
            case SDLK_RETURN
                'focus uictx oui.NoItem
            default;
        case oui.Char
            let key = ('get-key uictx)
            if ((key > 255) | (key < 32))
                return;
            let size = (strlen text)
            if (size >= (maxsize - 1))
                return;
            text @ size = (key as i8)
        default;
    default;

fn textbox (text maxsize)
    widget-item
        WidgetClass.Text
            text = text
            maxsize = maxsize
        handler = textboxhandler
        size = (ivec2 0 (BND_WIDGET_HEIGHT as integer))
        events = (| oui.LButtonDown oui.KeyDown oui.Char)

fn radiohandler (uictx item event)
    dispatch (('get-handle uictx item Widget) . content)
    case Radio (value ...)
        @value = item
    default;

fn radio (iconid label value)
    let label = (label as rawstring)
    widget-item
        WidgetClass.Radio
            label = label
            iconid = (iconid as integer as i32)
            value = &value
        handler = radiohandler
        size =
            ivec2
                ? (label == null) (BND_TOOL_WIDTH as integer) 0:u32
                BND_WIDGET_HEIGHT as integer
        events = oui.LButtonDown

fn panel ()
    widget-item
        WidgetClass.Panel;

fn roothandler (uictx item event)
    switch event
    case oui.Scroll
        print "scroll!" (unpack ('get-scroll uictx))
    case oui.LButtonDown
        print ('get-clicks uictx) "clicks"
    default;

fn ui-handler (uictx item event)
    let widget =
        try ('try-handle uictx item Widget)
        except (err)
            return;
    if (widget.handler != null)
        widget.handler uictx item event
'set-handler uictx ui-handler

fn build-ui (root)
    let rc =
        colorrect "test"
            vec3 0.5
    'setup rc
        size = (ivec2 64 64)
    'insert root rc

fn draw-cursor (cursor)
    nvgBeginPath vg
    nvgCircle vg (f32 cursor.x) (f32 cursor.y) 8
    nvgStrokeColor vg
        nvgRGBf 1 1 1
    nvgStrokeWidth vg 4
    nvgStroke vg
    nvgStrokeColor vg
        nvgRGBf 0 0 0
    nvgStrokeWidth vg 2
    nvgStroke vg

fn draw-item-content

fn get-dnd-item ()
    oui.NoItem

fn _draw-item (vg pos item corner idx)
    returning void
    let state = ('get-state uictx item)
    if (state == oui.Frozen or (get-dnd-item) == item)
        nvgSave vg
        nvgGlobalAlpha vg (BND_DISABLED_ALPHA as f32)
        draw-item-content vg state pos item corner idx
        nvgRestore vg
    else
        draw-item-content vg state pos item corner idx

inline... draw-item
case (vg, pos : ivec2, item : oui.Item, corner, idx : i32)
    let item = (dupe (deref item))
    corner := corner as integer as i32
    _draw-item vg pos item corner idx

fn draw-child-items (vg pos item corner)
    let corner =
        static-if (none? corner) BND_CORNER_NONE
        else corner
    for i kid in (enumerate ('kids uictx item))
        draw-item vg pos kid corner i

fn draw-hbox (vg pos item)
    let kid = ('first-kid uictx item)
    if (kid == oui.NoItem)
        return;
    let nextkid = ('next-sibling uictx kid)
    if (nextkid == oui.NoItem)
        draw-item vg pos kid BND_CORNER_NONE 0
    else
        draw-item vg pos kid BND_CORNER_RIGHT 0
        local count = 1
        local kid = nextkid
        while (('next-sibling uictx kid) != oui.NoItem)
            draw-item vg pos (deref kid) BND_CORNER_ALL (deref count)
            kid = ('next-sibling uictx kid)
            count += 1
        draw-item vg pos kid BND_CORNER_LEFT (deref count)

fn draw-vbox (vg pos item)
    let kid = ('first-kid uictx item)
    if (kid == oui.NoItem)
        return;
    let nextkid = ('next-sibling uictx kid)
    if (nextkid == oui.NoItem)
        draw-item vg pos kid BND_CORNER_NONE 0
    else
        draw-item vg pos kid BND_CORNER_DOWN 0
        local count = 1
        local kid = nextkid
        while (('next-sibling uictx kid) != oui.NoItem)
            draw-item vg pos (deref kid) BND_CORNER_ALL (deref count)
            kid = ('next-sibling uictx kid)
            count += 1
        draw-item vg pos kid BND_CORNER_TOP (deref count)

include
    import C
""""int sprintf ( char * str, const char * format, ... );

fn draw-item-content (vg state pos item corner idx)
    returning void

    let x y w h = (unpack (vec4 ('get-rect uictx item)))
    x := pos.x as f32 + x
    y := pos.y as f32 + y
    let pos =
        ivec2 x y
    let obj =
        try ('try-handle uictx item Widget)
        except (err)
            draw-child-items vg pos item corner
            return;
    let state = (bitcast ('get-state uictx item) BNDwidgetState)
    corner := corner as integer as i32
    dispatch obj.content
    case HBox ()
        draw-hbox vg pos item
    case VBox ()
        draw-vbox vg pos item
    case Panel ()
        bndBevel vg x y w h
        draw-child-items vg pos item corner
    case Label (iconid label)
        bndLabel vg x y w h iconid label
    case Button (iconid label)
        bndToolButton vg x y w h (corner as integer as i32) state iconid label
    case Check (label option)
        let state =
            if @option BND_ACTIVE
            else state
        bndOptionButton vg x y w h state label
    case Slider (label progress)
        global value : (array i8 32)
        C.sprintf &value "%.0f%%" (@progress * 100.0)
        bndSlider vg x y w h corner state @progress label &value
    case Radio (iconid label value)
        let state =
            if ((@ value) == item) BND_ACTIVE
            else state
        bndRadioButton vg x y w h (corner as integer as i32) state iconid label
    case Text (text ...)
        let idx = (strlen text)
        bndTextField vg x y w h corner state -1 text (idx as i32) (idx as i32)
    case Rect (label color)
        if ((x != 0) & (y != 0))
            nvgSave vg
            nvgStrokeColor vg
                nvgRGBAf color.r color.g color.b 0.9
            if (state != BND_DEFAULT)
                nvgFillColor vg
                    nvgRGBAf color.r color.g color.b 0.5
            else
                nvgFillColor vg
                    nvgRGBAf color.r color.g color.b 0.1
            nvgStrokeWidth vg 2
            nvgBeginPath vg
            nvgRoundedRect vg x y w h 3
            nvgFill vg
            nvgStroke vg
            if (state != BND_DEFAULT)
                nvgFillColor vg
                    nvgRGBAf 0.0 0.0 0.0 1.0
                nvgFontSize vg 15.0
                nvgBeginPath vg
                nvgTextAlign vg (NVG_ALIGN_TOP | NVG_ALIGN_CENTER)
                nvgTextBox vg x (y + h * 0.3) w label null
            nvgRestore vg
        if false
            nvgSave vg
            nvgIntersectScissor vg x y w h
            draw-child-items vg pos item corner
            nvgRestore vg
        else
            draw-child-items vg pos item corner
    default
        ;

fn menu-option (parent name choice)
    let opt = (radio -1 name choice)
    'insert uictx parent opt
    'setup-item uictx opt
        anchors = (| oui.HFill oui.Top)
        margins = (ivec4 1)
    opt

fn demorect (parent label hue box anchors w h m1 m2 m3 m4)
    let color = (extractvalue (nvgHSL hue 1.0 0.8) 0)
    let color = (vec4 (unpack color.rgba))
    let item = (colorrect label color.rgb)
    'setup-item uictx item
        anchors = anchors
        box = box
        margins = (ivec4 m1 m2 m3 m4)
        size = (ivec2 w h)
    'insert uictx parent item
    item

fn demohandler (uictx item event)
    dispatch (('get-handle uictx item Widget) . content)
    case Button (label ...)
        print "clicked:" item (string label)
    default;

fn column ()
    'item uictx
        box = oui.Column

fn column-append (parent item)
    'insert uictx parent item
    # fill parent horizontally, anchor to previous item vertically
    'setup-item uictx item
        anchors = oui.HFill
        margins = (ivec4 0 1 0 0)
    item

inline button (iconid label handler)
    # create new ui item
    widget-item
        WidgetClass.Button
            iconid = (iconid as integer as i32)
            label = label
        handler = handler
        # set size of wiget; horizontal size is dynamic, vertical is fixed
        size = (ivec2 0 (BND_WIDGET_HEIGHT as integer))
        events = oui.LButtonHotUp

fn checkhandler (uictx item event)
    dispatch (('get-handle uictx item Widget) . content)
    case Check (option ...)
        @option = (not @option)
    default;

fn check (label option)
    # create new ui item
    widget-item
        WidgetClass.Check
            label = label
            option = &option
        handler = checkhandler
        # set size of wiget; horizontal size is dynamic, vertical is fixed
        size = (ivec2 0 (BND_WIDGET_HEIGHT as integer))
        events = oui.LButtonDown

# simple logic for a slider

# starting offset of the currently active slider
global sliderstart = 0.0

# event handler for slider (same handler for all sliders)
fn sliderhandler (uictx item event)
    dispatch (('get-handle uictx item Widget) . content)
    case Slider (label progress)
        # retrieve the custom data we saved with the slider
        switch event
        case oui.LButtonDown
            #   button was pressed for the first time; capture initial
                slider value.
            sliderstart = (@ progress)
        case oui.LButtonCapture
            #   called for every frame that the button is pressed.
                get the delta between the click point and the current
                mouse position
            let pos = ('get-cursor-start-delta uictx)
            # get the items layouted rectangle
            let w h = ('get-size uictx item)
            # calculate our new offset and clamp
            value := (clamp (sliderstart + (pos.x / w)) 0.0 1.0)
            # assign the new value
            (@ progress) = value
        default;
    default;

fn slider (label progress)
    # create new ui item
    widget-item
        WidgetClass.Slider
            label = label
            progress = &progress
        handler = sliderhandler
        # set size of wiget; horizontal size is dynamic, vertical is fixed
        size = (ivec2 0 (BND_WIDGET_HEIGHT as integer))
        # attach our slider event handler and capture two classes of events
        events = (| oui.LButtonDown oui.LButtonCapture)

fn hbox ()
    widget-item
        WidgetClass.HBox;
        box = oui.Row

fn vbox ()
    widget-item
        WidgetClass.VBox;
        box = oui.Column

fn vgroup-append (parent item)
    'insert uictx parent item
    # fill parent horizontally, anchor to previous item vertically
    'set-anchors uictx item oui.HFill
    item

fn vgroup ()
    'item uictx
        box = oui.Column

fn hgroup-append (parent item)
    'insert uictx parent item
    'set-anchors uictx item oui.HFill
    item

fn hgroup-append-fixed (parent item)
    'insert uictx parent item
    item

fn row-append (parent item)
    'insert uictx parent item
    'set-anchors uictx item oui.HFill
    item

fn row ()
    'item uictx
        box = oui.Row

global enum1 = oui.NoItem

# some persistent variables for demonstration
global progress1 = 0.25
global progress2 = 0.75

global option1 = true
global option2 = false
global option3 = false

global textbuffer : (array i8 1024)
let strcpy = (extern 'strcpy (function rawstring rawstring rawstring))
strcpy &textbuffer "The quick brown fox."

fn build-democontent (parent)

    let col = (column)
    'insert uictx parent col
    'setup-item uictx col
        margins = (ivec4 10)
        anchors = (| oui.Top oui.HFill)

    column-append col (button BND_ICON_GHOST "Item 1" demohandler)
    if option3
        column-append col (button BND_ICON_GHOST "Item 2" demohandler)

    do
        let h = (column-append col (hbox))
        hgroup-append h (radio BND_ICON_GHOST "Item 3.0" enum1)
        if option2
            'set-margins uictx
                hgroup-append-fixed h
                    radio BND_ICON_REC null enum1
                \ -1 0 0 0
        'set-margins uictx
            hgroup-append-fixed h
                radio BND_ICON_PLAY null enum1
            \ -1 0 0 0
        'set-margins uictx
            hgroup-append h
                radio BND_ICON_GHOST "Item 3.3" enum1
            \ -1 0 0 0

    do
        let rows = (column-append col (row))
        let coll = (row-append rows (vgroup))
        vgroup-append coll (textlabel -1 "Items 4.0:")
        let coll = (vgroup-append coll (vbox))
        vgroup-append coll (button BND_ICON_GHOST "Item 4.0.0" demohandler)
        'set-margins uictx
            vgroup-append coll
                button BND_ICON_GHOST "Item 4.0.1" demohandler
            \ 0 -2 0 0
        let colr = (row-append rows (vgroup))
        'set-margins uictx colr 8 0 0 0
        'freeze uictx colr option1
        vgroup-append colr (textlabel -1 "Items 4.1:")
        let colr = (vgroup-append colr (vbox))
        vgroup-append colr (slider "Item 4.1.0" progress1)
        'set-margins uictx
            vgroup-append colr
                slider "Item 4.1.1" progress2
            \ 0 -2 0 0

    column-append col (button BND_ICON_GHOST "Item 5" null)

    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)

    ;

fn build-layoutdemo (parent)
    let
        M = 10
        S = 150

    let box =
        demorect parent "Box( Layout )\nAttach( Fill )" 0.6 oui.Layout oui.Fill 0 0 M M M M
    demorect box "Attach( HFill | Top )" 0.7 0:u32 (| oui.HFill oui.Top) S (S + M) M M M 0
    demorect box "Attach( HFill )" 0.7 0:u32 oui.HFill S (S + 2 * M) M 0 M 0
    demorect box "Attach( HFill | Down )" 0.7 0:u32 (| oui.HFill oui.Down) S (S + M) M 0 M M

    demorect box "Attach( Left | VFill )" 0.7 0:u32 (| oui.Left oui.VFill) (S + M) S M M 0 M
    demorect box "Attach( VFill )" 0.7 0:u32 oui.VFill (S + 2 * M) S 0 M 0 M
    demorect box "Attach( Right | VFill )" 0.7 0:u32 (| oui.Right oui.VFill) (S + M) S 0 M M M

    demorect box "Attach( Left | Top )" 0.55 0:u32 (| oui.Left oui.Top) S S M M 0 0
    demorect box "Attach( Top )" 0.57 0:u32 oui.Top S S 0 M 0 0
    demorect box "Attach( Right | Top )" 0.55 0:u32 (| oui.Right oui.Top) S S 0 M M 0
    demorect box "Attach( Left )" 0.57 0:u32 oui.Left S S M 0 0 0
    demorect box "Attach( Center )" 0.59 0:u32 oui.Center S S 0 0 0 0
    demorect box "Attach( Right )" 0.57 0:u32 oui.Right S S 0 0 M 0
    demorect box "Attach( Left | Down )" 0.55 0:u32 (| oui.Left oui.Down) S S M 0 0 M
    demorect box "Attach( Down )" 0.57 0:u32 oui.Down S S 0 0 0 M
    demorect box "Attach( Right | Down )" 0.55 0:u32 (| oui.Right oui.Down) S S 0 0 M M
    ;

fn build-rowdemo (parent)
    'set-box uictx parent oui.Column

    let
        M = 10
        S = 200
        T = 100

    do
        let box =
            demorect parent "Box( Row )\nAttach( Left | VFill )" 0.6 oui.Row (| oui.Left oui.VFill) 0 S M M M M

        demorect box "Attach( Top )" 0.05 0:u32 oui.Top T T M M M 0
        demorect box "Attach( VCenter )" 0.1 0:u32 oui.VCenter T T 0 0 M 0
        demorect box "Attach( VFill )" 0.15 0:u32 oui.VFill T T 0 M M M
        demorect box "Attach( Down )" 0.25 0:u32 oui.Down T T 0 0 M M
    do
        let box =
            demorect parent "Box( Row | Justify )\nAttach( Fill )" 0.6 (| oui.Row oui.Justify) oui.Fill 0 S M 0 M M

        demorect box "Attach( Top )" 0.05 0:u32 oui.Top T T M M M 0
        demorect box "Attach( VCenter )" 0.1 0:u32 oui.VCenter T T 0 0 M 0
        demorect box "Attach( VFill )" 0.15 0:u32 oui.VFill T T 0 M M M
        demorect box "Attach( Down )" 0.25 0:u32 oui.Down T T 0 0 M M
    do
        let box =
            demorect parent "Box( Row )\nAttach( Fill )" 0.6 oui.Row oui.Fill 0 S M 0 M M

        demorect box "Attach( Top )" 0.05 0:u32 oui.Top T T M M M 0
        demorect box "Attach( VCenter )" 0.1 0:u32 oui.VCenter T T 0 0 M 0
        demorect box "Attach( VFill )" 0.15 0:u32 oui.VFill T T 0 M M M
        demorect box "Attach( HFill )" 0.2 0:u32 oui.HFill T T 0 0 M 0
        demorect box "Attach( HFill )" 0.2 0:u32 oui.HFill T T 0 0 M 0
        demorect box "Attach( HFill )" 0.2 0:u32 oui.HFill T T 0 0 M 0
        demorect box "Attach( Down )" 0.25 0:u32 oui.Down T T 0 0 M M
    ;

fn build-columndemo (parent)
    'set-box uictx parent oui.Row

    let
        M = 10
        S = 200
        T = 100

    do
        let box =
            demorect parent "Box( Column )\nAttach( Top | HFill )" 0.6 oui.Column (| oui.Top oui.HFill) S 0 M M M M

        demorect box "Attach( Left )" 0.05 0:u32 oui.Left T T M M 0 M
        demorect box "Attach( HCenter )" 0.1 0:u32 oui.HCenter T T 0 0 0 M
        demorect box "Attach( HFill )" 0.15 0:u32 oui.HFill T T M 0 M M
        demorect box "Attach( Right )" 0.25 0:u32 oui.Right T T 0 0 M M

    do
        let box =
            demorect parent "Box( Column )\nAttach( Fill )" 0.6 oui.Column oui.Fill S 0 0 M M M

        demorect box "Attach( Left )" 0.05 0:u32 oui.Left T T M M 0 M
        demorect box "Attach( HCenter )" 0.1 0:u32 oui.HCenter T T 0 0 0 M
        demorect box "Attach( HFill )" 0.15 0:u32 oui.HFill T T M 0 M M
        demorect box "Attach( Right )" 0.25 0:u32 oui.Right T T 0 0 M M

    do
        let box =
            demorect parent "Box( Column )\nAttach( Fill )" 0.6 oui.Column oui.Fill S 0 0 M M M

        demorect box "Attach( Left )" 0.05 0:u32 oui.Left T T M M 0 M
        demorect box "Attach( HCenter )" 0.1 0:u32 oui.HCenter T T 0 0 0 M
        demorect box "Attach( HFill )" 0.15 0:u32 oui.HFill T T M 0 M M
        demorect box "Attach( VFill )" 0.2 0:u32 oui.VFill T T 0 0 0 M
        demorect box "Attach( VFill )" 0.2 0:u32 oui.VFill T T 0 0 0 M
        demorect box "Attach( VFill )" 0.2 0:u32 oui.VFill T T 0 0 0 M
        demorect box "Attach( Right )" 0.25 0:u32 oui.Right T T 0 0 M M
    ;

fn fill-wrap-row-box (box)
    let
        M = 5
        S = 100
        T = 50

    local rnd : (Random)
    'seed rnd 303
    for i in (range 20)
        let hue = ('random rnd)
        width := 10 + ('range rnd 5) * 10

        let u =
            switch ('range rnd 4)
            case 0
                demorect box "Attach( Top )" \
                    hue 0:u32 oui.Top width T M M M M
            case 1
                demorect box "Attach( VCenter )" \
                    hue 0:u32 oui.VCenter width (T // 2) M M M M
            case 2
                demorect box "Attach( VFill )" \
                    hue 0:u32 oui.VFill width T M M M M
            case 3
                demorect box "Attach( Down )" \
                    hue 0:u32 oui.Down width (T // 2) M M M M
            default oui.NoItem

        if (('range rnd 10) == 0)
            'set-anchors uictx u (| ('get-anchors uictx u) oui.Break)

fn fill-wrap-column-box (box)
    let
        M = 5
        S = 100
        T = 50

    local rnd : (Random)
    'seed rnd 303
    for i in (range 20)
        let hue = ('random rnd)
        height := 10 + ('range rnd 5) * 10

        let u =
            switch ('range rnd 4)
            case 0
                demorect box "Attach( Left )" \
                    hue 0:u32 oui.Left T height M M M M
            case 1
                demorect box "Attach( HCenter )" \
                    hue 0:u32 oui.HCenter (T // 2) height M M M M
            case 2
                demorect box "Attach( HFill )" \
                    hue 0:u32 oui.HFill T height M M M M
            case 3
                demorect box "Attach( Right )" \
                    hue 0:u32 oui.Right (T // 2) height M M M M
            default oui.NoItem

        if (('range rnd 10) == 0)
            'set-anchors uictx u (| ('get-anchors uictx u) oui.Break)

fn build-wrapdemo (parent)
    let col =
        'item uictx
            box = oui.Column
            anchors = oui.Fill
    'insert uictx parent col

    let
        M = 5
        S = 100
        T = 50

    fill-wrap-row-box
        demorect col "Box( Row | Wrap | Start )\nAttach( HFill | Top )" \
            0.6 (| oui.Row oui.Wrap oui.Start) (| oui.HFill oui.Top) 0 0 M M M M

    fill-wrap-row-box
        demorect col "Box( Row | Wrap | Middle )\nAttach( HFill | Top )" \
            0.6 (| oui.Row oui.Wrap oui.Middle) (| oui.HFill oui.Top) 0 0 M M M M

    fill-wrap-row-box
        demorect col "Box( Row | Wrap | End )\nAttach( HFill | Top )" \
            0.6 (| oui.Row oui.Wrap oui.End) (| oui.HFill oui.Top) 0 0 M M M M

    fill-wrap-row-box
        demorect col "Box( Row | Wrap | Justify )\nAttach( HFill | Top )" \
            0.6 (| oui.Row oui.Wrap oui.Justify) (| oui.HFill oui.Top) 0 0 M M M M

    fill-wrap-column-box
        demorect col "Box( Column | Wrap | Start )\nAttach( Left | VFill )" \
            0.6 (| oui.Column oui.Wrap oui.Start) (| oui.Left oui.VFill) 0 0 M M M M
    ;

fn build-treedemo (parent)
    let col =
        'item uictx
            box =
                | oui.Column oui.Start
            anchors = (| oui.HFill oui.Top)
    'insert uictx parent col

    let M = 2

    fn make-prop (col level)
        returning void
        level := level + 1
        let top =
            'item uictx
                box = oui.Layout
                anchors = oui.TopLeft
        'insert uictx col top
        demorect top "box" \
            0.6 0:u32 oui.Fill 0 0 M M M M
        let rc =
            'item uictx
                box =
                    | oui.Column oui.Start
                anchors = oui.TopLeft
                margins = (ivec4 M)
        'insert uictx top rc
        for i in (range 3)
            let row =
                'item uictx
                    box =
                        | oui.Row oui.Start
                    anchors = oui.Right
            'insert uictx rc row
            let title =
                demorect row "box 1" 0.0 0:u32 oui.TopLeft (80 + (i * 10)) 16 M M M M
            if (level < 3)
                let subprop =
                    'item uictx
                        box = oui.Layout
                        size = (ivec2 1 (? (i == 2) 1 0))
                        anchors = oui.Top
                        margins = (ivec4 -1 0 0 0)
                'insert uictx row subprop
                #let subbox =
                    demorect subprop "box 1.1" 0.0 (| oui.Column Start) oui.TopLeft 100 40 M M M M
                this-function subprop level
        ;
    make-prop col 0
    #demorect rc "box 2" 0.0 0:u32 0:u32 100 20 M 0 M M
    #demorect rc "box 3" 0.0 0:u32 0:u32 100 20 M 0 M M

    ;


global choice = oui.NoItem
fn render-view (size)
    GL.BindFramebuffer GL.FRAMEBUFFER 0

    nvgBeginGLFrame vg size

    'begin-layout uictx

    let root = (panel)
    ('get-handle uictx root Widget) . handler = roothandler
    'setup-item uictx root
        size = size
        events = (oui.Scroll | oui.LButtonDown)
        box = oui.Column

    let menu =
        'item uictx
            anchors = (| oui.HFill oui.Top)
            box = oui.Row
    'insert uictx root menu

    let
        #opt_blendish_demo = (menu-option menu "Blendish Demo" choice)
        opt_oui_demo = (menu-option menu "OUI Demo" choice)
        opt_layouts = (menu-option menu "Box.Layout" choice)
        opt_row = (menu-option menu "Box.Row" choice)
        opt_column = (menu-option menu "Box.Column" choice)
        opt_wrap = (menu-option menu "Box.Wrap" choice)
        opt_tree = (menu-option menu "Tree" choice)
    if (choice == oui.NoItem)
        #choice = opt_oui_demo
        choice = opt_row

    let content =
        'item uictx
            anchors = oui.Fill
    'insert uictx root content

    match choice
    case opt_oui_demo
        let democontent =
            'item uictx
                anchors = oui.Top
                size = (ivec2 250 0)
        'insert uictx content democontent
        build-democontent democontent
    case opt_layouts
        build-layoutdemo content
    case opt_row
        build-rowdemo content
    case opt_column
        build-columndemo content
    case opt_wrap
        build-wrapdemo content
    case opt_tree
        build-treedemo content
    default;

    #build-ui content
    'end-layout uictx

    bndBackground vg 0 0 (unpack (vec2 size))
    draw-item vg (ivec2 0) root BND_CORNER_NONE 0


    nvgEndFrame vg

    let cursor = ('get-cursor uictx)

    'process uictx ((SDL_GetTicks) as i32)

    let hot-item = ('get-hot-item uictx)

global buttons_down = 0

@@ 'on GLMain.on-event
fn (event glmain)
    switch (event.type as (typeof SDL_MOUSEMOTION))
    case SDL_KEYDOWN
        let keysym = event.key.keysym.sym
        'set-key uictx (keysym as u32) (event.key.keysym.mod as u32) true
    case SDL_KEYUP
        let keysym = event.key.keysym.sym
        'set-key uictx (keysym as u32) (event.key.keysym.mod as u32) false
    case SDL_TEXTINPUT
        ->> (range (strlen &event.text.text))
            map
                inline (i)
                    event.text.text @ i
            #UTF-8.decoder
            map
                inline (x)
                    'set-char uictx (x as u32)
            drain
    case SDL_MOUSEWHEEL
        'set-scroll uictx event.wheel.x (- event.wheel.y)
    case SDL_MOUSEBUTTONDOWN
        #if (buttons_down == 0)
            SDL_CaptureMouse SDL_TRUE
        buttons_down += 1
        'set-cursor uictx event.button.x event.button.y
        'set-button uictx (event.button.button - 1) (SDL_GetModState) true
    case SDL_MOUSEBUTTONUP
        'set-cursor uictx event.button.x event.button.y
        'set-button uictx (event.button.button - 1) (SDL_GetModState) false
        buttons_down -= 1
        #if (buttons_down == 0)
            SDL_CaptureMouse SDL_FALSE
    case SDL_MOUSEMOTION
        'set-cursor uictx event.motion.x event.motion.y
    default;

@@ 'on GLMain.on-draw
fn (time size glmain)
    render-view size

'run glmain

__drop uictx
nvgDeleteGL3 vg

#glmain.shutdown;