rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_oui_imgui.sc -rw-r--r-- 31.1 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
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.imgui.cimgui

using import ..tukan.random

let
    WIDGET_HEIGHT = 22:u32
    TOOL_WIDTH = 22:u32
    BND_ICON_GHOST = -1
    BND_ICON_PLAY = -1
    BND_ICON_REC = -1

    ROUNDING = 3.0
    TEXT_MARGIN = 5.0
    ImDrawCornerFlags_None = (bitcast 0 ImDrawCornerFlags)

inline get-color (idx)
    igGetColorU32 (idx as integer as i32) 1.0

let oui =
    import ..tukan.oui

global 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))

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

global imctx = (igCreateContext null)

igStyleColorsLight (igGetStyle)

#do
    let io = (igGetIO)
    global SansFont =
        ImFontAtlas_AddFontFromFileTTF io.Fonts sansfont 15 null null

    #ImFontAtlas_AddFontFromFileTTF io.Fonts fontawesome 13 icons_config icons.fa_icon_range
    local icon_range = icons.md_icon_range
    ImFontAtlas_AddFontFromFileTTF io.Fonts materialicons 15 &icons_config &icon_range
    #ImFontAtlas_AddFontFromFileTTF io.Fonts kenneyicons 13 icons_config icons.ki_icon_range
    let FixedFont =
        ImFontAtlas_AddFontFromFileTTF io.Fonts fixedfont 15 null null

ImGui_ImplSDL2_InitForOpenGL glmain.window glmain.glcontext
ImGui_ImplOpenGL3_Init null


#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
        size =
            ivec2
                ? (label == null) (TOOL_WIDTH as integer) 0:u32
                WIDGET_HEIGHT as integer

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 (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) (TOOL_WIDTH as integer) 0:u32
                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-item-content

fn get-dnd-item ()
    oui.NoItem

fn _draw-item (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 state pos item idx corner
        #nvgRestore vg
    else
        draw-item-content state pos item idx corner

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

fn draw-child-items (pos item corner)
    for i kid in (enumerate ('kids uictx item))
        draw-item pos kid i corner

fn draw-hbox (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 pos kid 0 ImDrawCornerFlags_All
    else
        draw-item pos kid 0 ImDrawCornerFlags_Left
        local count = 1
        local kid = nextkid
        while (('next-sibling uictx kid) != oui.NoItem)
            draw-item pos (deref kid) (deref count) ImDrawCornerFlags_None
            kid = ('next-sibling uictx kid)
            count += 1
        draw-item pos kid (deref count) ImDrawCornerFlags_Right

fn draw-vbox (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 pos kid 0 ImDrawCornerFlags_All
    else
        draw-item pos kid 0 ImDrawCornerFlags_Top
        local count = 1
        local kid = nextkid
        while (('next-sibling uictx kid) != oui.NoItem)
            draw-item pos (deref kid) (deref count) ImDrawCornerFlags_None
            kid = ('next-sibling uictx kid)
            count += 1
        draw-item pos kid (deref count) ImDrawCornerFlags_Bot

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

fn draw-item-content (state pos item idx corner)

    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 pos item corner
            return;
    let dl = (igGetBackgroundDrawList)
    let corner = (corner as integer as i32)
    dispatch obj.content
    case HBox ()
        draw-hbox pos item
    case VBox ()
        draw-vbox pos item
    case Panel ()
        #bndBevel x y w h
        draw-child-items pos item corner
    case Label (iconid label)
        #bndLabel x y w h iconid label
        let label = (label as rawstring)
        if (label != null)
            let lh = (igGetFontSize)
            ImDrawList_AddText dl
                ImVec2 ((x as f32) + TEXT_MARGIN) ((y - (lh - h) / 2) as f32)
                get-color ImGuiCol_Text
                label
                null
    case Button (iconid label)
        ImDrawList_AddRectFilled dl
            ImVec2 (x as f32) (y as f32)
            ImVec2 ((x + w) as f32) ((y + h) as f32)
            get-color
                switch state
                case oui.Hot ImGuiCol_ButtonHovered
                case oui.Active ImGuiCol_ButtonActive
                default ImGuiCol_Button
            ROUNDING
            corner
        let label = (label as rawstring)
        if (label != null)
            let lh = (igGetFontSize)
            ImDrawList_AddText dl
                ImVec2 ((x as f32) + TEXT_MARGIN) ((y - (lh - h) / 2) as f32)
                get-color ImGuiCol_Text
                label
                null
    case Check (label option)
        let state =
            if @option oui.Active
            else state
        #bndOptionButton x y w h state label
        let lh = (igGetFontSize)
        ImDrawList_AddRectFilled dl
            ImVec2 (x as f32) (y as f32)
            ImVec2 ((x + w) as f32) ((y + h) as f32)
            get-color
                switch state
                case oui.Hot ImGuiCol_FrameBgHovered
                case oui.Active ImGuiCol_CheckMark
                default ImGuiCol_FrameBg
            ROUNDING
            corner
        if (label != null)
            ImDrawList_AddText dl
                ImVec2 ((x as f32) + TEXT_MARGIN) ((y - (lh - h) / 2) as f32)
                get-color
                    if (state == oui.Frozen) ImGuiCol_TextDisabled
                    else ImGuiCol_Text
                label
                null
    case Slider (label progress)
        global value : (array i8 32)
        C.sprintf &value "%.0f%%" (@progress * 100.0)
        let lh = (igGetFontSize)
        ImDrawList_AddRectFilled dl
            ImVec2 (x as f32) (y as f32)
            ImVec2 ((x + w) as f32) ((y + h) as f32)
            get-color
                switch state
                case oui.Hot ImGuiCol_FrameBgHovered
                case oui.Active ImGuiCol_FrameBgActive
                default ImGuiCol_FrameBg
            ROUNDING
            corner
        let progress = @progress
        let ww = (w * progress)
        ImDrawList_AddRectFilled dl
            ImVec2 (x as f32) (y as f32)
            ImVec2 ((x as f32) + ww) ((y + h) as f32)
            get-color
                switch state
                case oui.Hot ImGuiCol_SliderGrab
                case oui.Active ImGuiCol_SliderGrabActive
                default ImGuiCol_SliderGrab
            ROUNDING
            corner
        ImDrawList_AddText dl
            ImVec2 ((x as f32) + TEXT_MARGIN) ((y - (lh - h) / 2) as f32)
            get-color
                if (state == oui.Frozen) ImGuiCol_TextDisabled
                else ImGuiCol_Text
            &value
            null
    case Radio (iconid label value)
        let state =
            if ((@ value) == item) oui.Active
            else state
        ImDrawList_AddRectFilled dl
            ImVec2 (x as f32) (y as f32)
            ImVec2 ((x + w) as f32) ((y + h) as f32)
            get-color
                switch state
                case oui.Hot ImGuiCol_ButtonHovered
                case oui.Active ImGuiCol_ButtonActive
                default ImGuiCol_Button
            ROUNDING
            corner
        let label = (label as rawstring)
        if (label != null)
            let lh = (igGetFontSize)
            ImDrawList_AddText dl
                ImVec2 ((x as f32) + TEXT_MARGIN) ((y - (lh - h) / 2) as f32)
                get-color ImGuiCol_Text
                label
                null
    case Text (text ...)
        let idx = (strlen text)
        #bndTextField x y w h corner state -1 text (idx as i32) (idx as i32)
    case Rect (label color)
        if ((x != 0) & (y != 0))

            x := x + 0.5
            y := y + 0.5

            ImDrawList_AddRectFilled dl
                ImVec2 (x as f32) (y as f32)
                ImVec2 ((x + w) as f32) ((y + h) as f32)
                igColorConvertFloat4ToU32
                    ImVec4 color.r color.g color.b
                        if (state == oui.Cold) 0.1
                        else 0.5
                ROUNDING
                ImDrawCornerFlags_All as integer as i32

            ImDrawList_AddRect dl
                ImVec2 (x as f32) (y as f32)
                ImVec2 ((x + w) as f32) ((y + h) as f32)
                igColorConvertFloat4ToU32
                    ImVec4 color.r color.g color.b 0.9
                ROUNDING
                ImDrawCornerFlags_All as integer as i32
                2.0

            if ((label != null) & (state != oui.Cold))
                let lh = (igGetFontSize)
                ImDrawList_AddText dl
                    ImVec2 ((x as f32) + TEXT_MARGIN) ((y - (lh - h) / 2) as f32)
                    igColorConvertFloat4ToU32
                        ImVec4 0 0 0 1.0
                    label
                    null
        ImDrawList_PushClipRect dl
            ImVec2 (x as f32) (y as f32)
            ImVec2 ((x + w) as f32) ((y + h) as f32)
            true
        draw-child-items pos item corner
        ImDrawList_PopClipRect dl
    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)
    local r : f32
    local g : f32
    local b : f32
    igColorConvertHSVtoRGB hue 1.0 0.8 &r &g &b
    let color = (vec3 r g b)
    let item = (colorrect label color)
    '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 (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 (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 (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 1 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 1 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
    ;

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

    ImGui_ImplOpenGL3_NewFrame;
    ImGui_ImplSDL2_NewFrame glmain.window
    igNewFrame;

    '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)
    if (choice == oui.NoItem)
        choice = opt_oui_demo

    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
    default;

    #build-ui content
    'end-layout uictx

    let dl = (igGetBackgroundDrawList)
    ImDrawList_AddRectFilled dl
        ImVec2 0 0
        ImVec2 (unpack (vec2 size))
        get-color ImGuiCol_WindowBg
        0.0
        0

    draw-item (ivec2 0) root 0 ImDrawCornerFlags_All

    igRender;
    ImGui_ImplOpenGL3_RenderDrawData (igGetDrawData)

    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
ImGui_ImplOpenGL3_Shutdown;
ImGui_ImplSDL2_Shutdown;
igDestroyContext imctx
__drop glmain

#glmain.shutdown;