aboutsummaryrefslogtreecommitdiffstats
path: root/code/editor/editor_ui.c
blob: d2f707a9dba52123c716de27fddba12495f81571 (plain) (blame)
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
//~ Misc
internal inline b32
EqualsWithEpsilon(f32 A, f32 B, f32 Epsilon)
{
    b32 Result = (A < (B + Epsilon) && 
                  A > (B - Epsilon));
    return Result;
}

//~ Functions
internal ui_size
UI_Size(ui_size_kind Kind, f32 Value, f32 Strictness)
{
    ui_size Size = {0};
    Size.Kind = Kind;
    Size.Value = Value;
    Size.Strictness = Strictness;
    return Size;
}

internal ui_box *
UI_BoxAlloc(arena *Arena)
{
    ui_box *Result = PushStructZero(Arena, ui_box);
    Result->First = Result->Last = Result->Next = Result->Prev = Result->Parent = UI_NilBox;
    Result->HashNext = Result->HashPrev = UI_NilBox;
    
    return Result;
}

internal b32
UI_IsNilBox(ui_box *Box)
{
    b32 Result = (Box == 0) || (Box == UI_NilBox);
    return Result;
}

internal b32
UI_IsFloatingBox(ui_box *Box, axis2 Axis)
{
    b32 Floating = ((Box->Flags & UI_BoxFlag_FloatingX && Axis == Axis2_X) ||
                    (Box->Flags & UI_BoxFlag_FloatingY && Axis == Axis2_Y));
    
    return Floating;
}

internal void
UI_PushBox(void)
{
    // NOTE(luca): You may not double push cause it makes no sense
    Assert(!UI_State->AppendToParent);
    
    UI_State->AppendToParent = true;
}

internal void
UI_PopBox(void)
{
    UI_State->Current = UI_State->Current->Parent;
}

#define UI_Push() DeferLoop(UI_PushBox(), UI_PopBox()) 

internal ui_key
UI_KeyNull(void)
{
    ui_key Result = {0};
    return Result;
}

internal b32
UI_KeyMatch(ui_key A, ui_key B)
{
    b32 Result = (A.U64[0] == B.U64[0]);
    return Result;
}

internal b32
UI_IsActive(ui_box *Box)
{
    b32 Result = UI_KeyMatch(UI_State->Active, Box->Key);
    return Result;
}

internal b32
UI_IsHot(ui_box *Box)
{
    b32 Result = UI_KeyMatch(UI_State->Hot, Box->Key);
    return Result;
}

internal ui_box *
UI_BoxFromKey(ui_key Key)
{
    u64 Slot = (Key.U64[0] % UI_State->BoxTableSize);
    ui_box *HashBox = UI_State->BoxTable + Slot;
    
    while(!UI_KeyMatch(HashBox->Key, Key) &&
          !UI_IsNilBox(HashBox))
    {
        HashBox = HashBox->Next;
    }
    
    return HashBox;
}

internal ui_box *
UI_AddBox(str8 String, s32 Flags)
{
    str8 DisplayString = String;
    ui_box *Box;
    ui_key Key = UI_KeyNull();
    b32 FirstTime = true;
    
    if(String.Size)
    {
        for EachIndex(Idx, String.Size - 1)
        {
            if(S8Match(S8From(String, Idx), S8("###"), true))
            {
                DisplayString = S8To(String, Idx);
                
                String = S8From(String, Idx + 3);
                break;
            }
            if(S8Match(S8From(String, Idx), S8("##"), true))
            {
                DisplayString = S8To(String, Idx);
                break;
            }
        }
        
        if(S8Match(DisplayString, S8("Open"), false))
        {
            //DebugBreak();
        }
        
        // TODO(luca): Seed with sibling's hash as well?
        // if parent key is null, you sould find top most parent whose key is not null
            
        u64 AncestorKey = UI_State->Current->Parent->Key.U64[0];
        Key.U64[0] = U64HashFromSeedStr8(AncestorKey, String);
        
        // Get the box based on key
        {    
            u64 Slot = (Key.U64[0] % UI_State->BoxTableSize);
            ui_box *HashBox = UI_State->BoxTable + Slot;
            ui_box *HashLast = 0;
            
            // There are three scenarios
            //1. We find the slot empty
            //2. We find the slot occupied
            //2.1 The key matches the slot or one from the linked list
            //2.2 The key did not match
            
            if(UI_KeyMatch(UI_KeyNull(), HashBox->Key))
            {
                // 1.
                Box = HashBox;
                Box->HashNext = Box->HashPrev = UI_NilBox;
            }
            else
            {
                //2.
                
                while(!UI_IsNilBox(HashBox))
                {        
                    if(UI_KeyMatch(HashBox->Key, Key))
                    {
                        break;
                    }
                    
                    HashLast = HashBox;
                    HashBox = HashBox->HashNext;
                }
                
                if(!UI_IsNilBox(HashBox))
                {
                    //2.1
                    Box = HashBox;
                    FirstTime = false;
                }
                else
                {
                    //2.2
                    HashLast->HashNext = PushStruct(UI_State->Arena, ui_box);
                    Box = HashLast->HashNext;
                    Box->HashPrev = HashLast;
                }
            }
        }
    }
    else
    {
        Box = PushStruct(UI_FrameArena, ui_box);
    }
    
    Box->First = Box->Last = Box->Next = Box->Prev = Box->Parent = UI_NilBox;
    
    Box->Key = Key;
    Box->DisplayString = DisplayString;
    Box->Flags = Flags;
    
    Box->BackgroundColor = UI_State->BackgroundColorTop->Value;
    Box->BorderColor = UI_State->BorderColorTop->Value;
    Box->TextColor = UI_State->TextColorTop->Value;
    Box->BorderThickness = UI_State->BorderThicknessTop->Value;
    Box->Softness = UI_State->SoftnessTop->Value;
    Box->CornerRadii = UI_State->CornerRadiiTop->Value;
    Box->LayoutAxis = UI_State->LayoutAxisTop->Value;
    Box->SemanticSize[Axis2_X] = UI_State->SemanticWidthTop->Value;
    Box->SemanticSize[Axis2_Y] = UI_State->SemanticHeightTop->Value;
    Box->HeightPx = UI_State->HeightPxTop->Value;
    Box->FontKind = UI_State->FontKindTop->Value;
    Box->CustomDraw = 0;
    Box->CustomDrawData = 0;
    Box->Clicked = false;
    
    if(!FirstTime)
    {
        b32 Hovered = false;
        b32 Clicked = false;
        b32 Pressed = false;
        
        app_input *Input = UI_State->Input;
        
        if(!Input->Consumed && Box->Flags & UI_BoxFlag_MouseClickability)
        {        
            v2 MouseP = V2S32(Input->Mouse.X, Input->Mouse.Y);
            
            app_button_state MouseLeft = Input->Mouse.Buttons[PlatformMouseButton_Left];
            
            Hovered = IsInsideRectV2(MouseP, Box->Rec);
            Pressed = (Hovered && MouseLeft.EndedDown);
            
            // Set active and hot state
{            
                b32 MouseWentUp = (!MouseLeft.EndedDown);
                
                if(Hovered)
                    {
                    if(UI_IsActive(UI_NilBox) ||
                       UI_IsActive(Box)) 
{
                        UI_State->Hot = Box->Key;
}
                }
                else if(UI_IsHot(Box))
                {
                    UI_State->Hot = UI_KeyNull();
                }
                
                if(UI_IsActive(Box))
            {
                    if(MouseWentUp)
                    {
                        if(UI_IsHot(Box))
                        {
                            Clicked = true;
                        }
                        UI_State->Active = UI_KeyNull();
                    }
                }
                else if(UI_IsHot(Box))
                {
                    if(MouseLeft.EndedDown)
                    {
                        UI_State->Active = Box->Key;
                    }
                }
                
            }
        }
        
        Box->Clicked = Clicked;
        
        f32 Speed = 20.f;
        f32 HotTarget    = UI_IsHot(Box)    ? 1.f : 0.f;
        f32 ActiveTarget = UI_IsActive(Box) ? 1.f : 0.f;
            Box->tActive += (ActiveTarget - Box->tActive) * Speed * Input->dtForFrame;
        Box->tHot += (HotTarget - Box->tHot) * Speed * Input->dtForFrame;
    }
    
    // Add box to the tree
    {    
        if(UI_State->AppendToParent)
        {
            UI_State->Current->First = Box;
            Box->Parent = UI_State->Current;
        }
        else
        {    
            Box->Parent = UI_State->Current->Parent;
            
            if(!UI_IsNilBox(Box->Parent))
            {
                Box->Parent->Last = Box;
            }
            
            Box->Prev = UI_State->Current;
            UI_State->Current->Next = Box;
        }
        
        UI_State->Current = Box;
        
        UI_State->AppendToParent = false;
    }
    
    return Box;
}

internal rune 
UI_GetShiftForFont(font_kind Kind)
{
    rune Result = 0;
    
    font_atlas *Atlas = UI_State->Atlas;
    
    switch(Kind)
    {        
        case FontKind_Text:
        {
            Result = 0;
        } break;
        case FontKind_Icon:
        {        
            Result = (Atlas->FirstCodepoint + 
                     Atlas->CodepointsCount -
                     Atlas->IconsFirstCodepoint);
        } break;
        default: break;
    }
    
    return Result;
}

internal f32
UI_MeasureTextWidth(str8 String, font_kind FontKind)
{
    f32 Result = 0.f;
    
    font_atlas *Atlas = UI_State->Atlas;
    
    rune Shift = UI_GetShiftForFont(FontKind) - Atlas->FirstCodepoint;
    
    for EachIndex(Idx, String.Size)
    {
        rune Char = (rune)(String.Data[Idx]) + Shift;
        f32 CharWidth = (Atlas->PackedChars[Char].xadvance);
        
        Result += CharWidth;
    }
    
    return Result;
}

internal void
UI_DefaultState(ui_box *Root, f32 HeightPx)
{
UI_State->Current = Root;
UI_State->AppendToParent = true;

// Defaults
// NOTE(luca): This is slightly since what we should be doing here is *setting* and not *pushing*.  But since we don't modify the top item this shouldn't be a problem in practice.t
UI_PushBackgroundColor(Color_Background);
UI_PushTextColor(Color_ButtonText);
UI_PushBorderColor(Color_ButtonBorder);
UI_PushSoftness(.5f);
UI_PushBorderThickness(2.f);
    UI_PushCornerRadii(V4F32(0.f));
UI_PushLayoutAxis(Axis2_X);
UI_PushSemanticWidth(UI_SizeParent(1.f, 1.f));
UI_PushSemanticHeight(UI_SizeParent(1.f, 1.f));
UI_PushHeightPx(HeightPx);
UI_PushFontKind(FontKind_Text);
}

//~ Calculations Start 

internal void
UI_CalculateStandaloneSizes(ui_box *Box, axis2 Axis)
{
    switch(Box->SemanticSize[Axis].Kind)
    {
        default: {}break;
        case UI_SizeKind_Pixels:
        {
            Box->FixedSize.e[Axis] = Box->SemanticSize[Axis].Value;
        } break;
        case UI_SizeKind_TextContent:
        {
            // TODO(luca): This is the only part that needs the UI_State variable, if we can rid of it, we could get totally UI_State agnostic layout computation. 
            if(Axis == Axis2_X)
            {
                Box->FixedSize.e[Axis] = (UI_MeasureTextWidth(Box->DisplayString, Box->FontKind) + 
                                          2.f*Box->SemanticSize[Axis].Value);
            }
            else
            {
                Box->FixedSize.e[Axis] = (UI_State->Atlas->HeightPx + 
                                          2.f*Box->SemanticSize[Axis].Value);
            }
        } break;
    }
    
    if(!UI_IsNilBox(Box->Next))
    {
        UI_CalculateStandaloneSizes(Box->Next, Axis);
    }
    if(!UI_IsNilBox(Box->First))
    {
        UI_CalculateStandaloneSizes(Box->First, Axis);
    }
}

internal void
UI_CalculateUpwardSizes(ui_box *Box, axis2 Axis)
{
    if(Box->SemanticSize[Axis].Kind == UI_SizeKind_PercentOfParent)
    {
        Box->FixedSize.e[Axis] = (Box->SemanticSize[Axis].Value * 
                                  Box->Parent->FixedSize.e[Axis]);
    }
    
    if(!UI_IsNilBox(Box->First))
    {
        UI_CalculateUpwardSizes(Box->First, Axis);
    }
    if(!UI_IsNilBox(Box->Next))
    {
        UI_CalculateUpwardSizes(Box->Next, Axis);
    }
}

internal void
UI_CalculateDownwardSizes(ui_box *Box, axis2 Axis)
{
    if(!UI_IsNilBox(Box->First))
    {
        UI_CalculateDownwardSizes(Box->First, Axis);
    }
    if(!UI_IsNilBox(Box->Next))
    {
        UI_CalculateDownwardSizes(Box->Next, Axis);
    }
    
    if(Box->SemanticSize[Axis].Kind == UI_SizeKind_ChildrenSum)
    {
        f32 TotalSize = 0.f;
        
        ui_box *FirstNonFloatingChild = UI_NilBox;
        for UI_EachBox(Child, Box->First)
        {
            if(!UI_IsFloatingBox(Child, Axis))
            {
                FirstNonFloatingChild = Child;
                break;
            }
        }
        
        for UI_EachBox(Child, Box->First)
        {
            b32 IsFirstChild = (Child == FirstNonFloatingChild);
            b32 IsAligned = (Box->LayoutAxis == Axis);
            
            if(IsFirstChild)
            {
                TotalSize += Child->FixedSize.e[Axis];
            }
            else if(!UI_IsFloatingBox(Child, Axis))
            {
                if(IsAligned)
{
                TotalSize += Child->FixedSize.e[Axis];
                }
                else
                {
                    TotalSize = Max(TotalSize, Child->FixedSize.e[Axis]);
                }
            }
        }
        
        Box->FixedSize.e[Axis] = TotalSize;
    }
}

internal void
UI_CalculateViolations(ui_box *Box, axis2 Axis)
{
    ui_box *Parent = Box->Parent;
    
    f32 ParentSize = Parent->FixedSize.e[Axis];
    
    // TODO(luca): This is inefficient since the violations should be solved once per level.
    f32 TotalSize = 0.f;
    {    
        for UI_EachBox(Child, Parent->First)
        {
            if(Child->LayoutAxis == Axis)
            {            
                if(!UI_IsFloatingBox(Child, Axis))
                {
                    TotalSize += Child->FixedSize.e[Axis];
                }
            }
        }
    }
    
    f32 ViolationSize = TotalSize - ParentSize;
    
    if(ViolationSize > 0.f)
    {
        // NOTE(luca): Take size from all the siblings which have strictnes < 1.f
        
        ui_box *Child = Parent->First;
        // TODO(luca): epsilon compare?
        while(!UI_IsNilBox(Child) && ViolationSize != 0.f)
        {
            if(!UI_IsFloatingBox(Child, Axis))
            {                
                f32 Strictness = Child->SemanticSize[Axis].Strictness;
                f32 AllowedSizeToTake = ((1.f - Strictness)*Child->FixedSize.e[Axis]);
                f32 TakenSize = Min(ViolationSize, AllowedSizeToTake);
                
                Child->FixedSize.e[Axis] = Child->FixedSize.e[Axis] - TakenSize;
                ViolationSize -= TakenSize;
            }
            
            Child = Child->Next;
        }
        
        if(!EqualsWithEpsilon(ViolationSize, 0.f, .001f))
        {
#if 0
            DebugBreak();
            ErrorLog("Clipping");
#endif
        }
        
    }
    
    if(!UI_IsNilBox(Box->Next))
    {
        UI_CalculateViolations(Box->Next, Axis);
    }
    
    if(!UI_IsNilBox(Box->First))
    {
        UI_CalculateViolations(Box->First, Axis);
    }
}


internal void
UI_CalculatePositions(ui_box *Box)
{
    ui_box *Parent = Box->Parent;
    
    // TODO(luca): This won't work since the root box will never be passed only the first child of that box, so its index will never be set.
    Box->LastTouchedFrameIdx = UI_State->FrameIdx;
    
    if(Parent->First == Box)
    {
        Box->FixedPosition.X = Parent->FixedPosition.X;
        Box->FixedPosition.Y = Parent->FixedPosition.Y;
    }
    else
    {  
        axis2 Axis = Parent->LayoutAxis;
        Assert(Axis == Axis2_X || Axis == Axis2_Y);
        axis2 OtherAxis = 1 - Axis;
        
        ui_box *NonFloatingSibling = UI_NilBox;
        for(ui_box *Sibling = Box->Prev; !UI_IsNilBox(Sibling); Sibling = Sibling->Prev)
        {
            if(!UI_IsFloatingBox(Sibling, Axis2_X))
            {
                NonFloatingSibling = Sibling;
                break;
            }
        }
        
        if(!UI_IsNilBox(NonFloatingSibling))
        {
            Box->FixedPosition.e[Axis] = (NonFloatingSibling->FixedPosition.e[Axis] + 
                                          NonFloatingSibling->FixedSize.e[Axis]);
            Box->FixedPosition.e[OtherAxis] = (NonFloatingSibling->FixedPosition.e[OtherAxis]);
        }
        else
        {
            Box->FixedPosition = Parent->FixedPosition;
        }
        
        if(UI_IsFloatingBox(Box, Axis2_X))
        {
            Box->FixedPosition.X = NonFloatingSibling->FixedPosition.X;
        }
        if(UI_IsFloatingBox(Box, Axis2_Y))
        {
            Box->FixedPosition.Y = NonFloatingSibling->FixedPosition.Y;
        }
    }
    
    Box->Rec = RectFromSize(Box->FixedPosition, Box->FixedSize);
    
    if(!UI_IsNilBox(Box->First))
    {
        UI_CalculatePositions(Box->First);
    }
    if(!UI_IsNilBox(Box->Next))
    {
        UI_CalculatePositions(Box->Next);
    }
}

internal void
UI_DrawBoxes(ui_box *Box)
{
    ui_box *Parent = Box->Parent;
    
    font_atlas *Atlas = UI_State->Atlas;
    
    v4 Dest = Box->Rec;
    
    if(Box->Flags & UI_BoxFlag_Clip)
    {
        Dest = RectIntersect(Parent->Rec, Dest);
        Box->Rec = Dest;
    }
    
    if(RectValid(Dest))
    {    
        if(Box->Flags & UI_BoxFlag_DrawShadow)
        {
            f32 ShadowSize = 4.f;
            v4 ShadowDest = RectV2(V2AddF32(Dest.Min, ShadowSize), 
                                   V2AddF32(Dest.Max, ShadowSize)); 
            rect_instance *Inst = DrawRect(ShadowDest, Color_Black, 0.f, ShadowSize, .5f*ShadowSize);
            Inst->CornerRadii = Box->CornerRadii;
        }
        
        if(Box->Flags & UI_BoxFlag_DrawBackground)
        {    
            rect_instance *Inst = DrawRect(Dest, Box->BackgroundColor, 0.f, 0.f, Box->Softness);
            Inst->CornerRadii = Box->CornerRadii;
            
            if(Box->Flags & UI_BoxFlag_MouseClickability)
            {
#if 1
                V3Math Inst->Color2.E *= 1.f - .4f*(Box->tActive);
                V3Math Inst->Color3.E *= 1.f - .4f*(Box->tActive);
                
                V3Math Inst->Color0.E *= 1.f - .4f*(Box->tHot - Box->tActive);
                V3Math Inst->Color1.E *= 1.f - .4f*(Box->tHot - Box->tActive);
#else
                
#endif
                }
            }
        
        if(Box->Flags & UI_BoxFlag_DrawDisplayString)
        {
            v2 TextPos = Box->FixedPosition;
            TextPos.Y += Box->BorderThickness;
            TextPos.X += Box->BorderThickness;
            
            v2 Cur = TextPos;
            
            if(Box->Flags & UI_BoxFlag_CenterTextHorizontally)
            {
                f32 TextWidth = UI_MeasureTextWidth(Box->DisplayString, Box->FontKind);
                Cur.X += .5f*((Box->FixedSize.X - 2.f*Box->BorderThickness) - TextWidth);
            }
            
            if(Box->Flags & UI_BoxFlag_CenterTextVertically)
            {
                f32 TextHeight = Atlas->HeightPx;
                Cur.Y += .5f*((Box->FixedSize.Y - 2.f*Box->BorderThickness) - TextHeight);
            }
            
            rune Shift = UI_GetShiftForFont(Box->FontKind);
            
            for EachIndex(Idx, Box->DisplayString.Size)
            {
                rune Char = (rune)(Box->DisplayString.Data[Idx]) + Shift;
                f32 CharWidth = (Atlas->PackedChars[Char - Atlas->FirstCodepoint].xadvance);
                f32 CharHeight = (Atlas->HeightPx);
                
                v2 CurMax = V2AddV2(Cur, V2(CharWidth, CharHeight));
                if(IsInsideRectV2(Cur, Dest) &&
                   IsInsideRectV2(CurMax, Dest))
                {
                    DrawRectChar(Atlas, Cur, Char, Box->TextColor);
                    
                    Cur.X += CharWidth;
                }
            }
        }
        
        if(Box->Flags & UI_BoxFlag_DrawBorders)
        {    
            if(Box->Flags & UI_BoxFlag_MouseClickability && 
               (UI_IsHot(Box) || UI_IsActive(Box)))
            {
                Box->BorderColor = Color_Snow2;
            }
            rect_instance *Inst = DrawRect(Dest, Box->BorderColor, 0.f, Box->BorderThickness, Box->Softness);
            V4Math Inst->CornerRadii.E = Box->CornerRadii.E;
        }
        
        
        if(Box->CustomDraw)
        {
            Box->CustomDraw(Box->CustomDrawData);
        }
    }
    
    b32 RectDebugMode = false;
    if(RectDebugMode || Box->Flags & UI_BoxFlag_DrawDebugBorder)
    {    
        DrawRect(Dest, V4(1.f, 0.f, 1.f, 1.f), 0.f, 1.f, 0.f);
    }
    
    if(!UI_IsNilBox(Box->First))
    {
        UI_DrawBoxes(Box->First);
    }
    if(!UI_IsNilBox(Box->Next))
    {
        UI_DrawBoxes(Box->Next);
    }
}

global_variable s32 UI_DebugIndentation = 0;

internal void
UI_DebugPrintBoxes(ui_box *Box)
{
    Log("%*s\"" S8Fmt "\":\n"
        "%*s %d,%d\n"
        "%*s %.0f,%.0f %.0fx%.0f\n"
        ,
        UI_DebugIndentation, "", S8Arg(Box->DisplayString),
        UI_DebugIndentation, "", Box->SemanticSize[0].Kind, Box->SemanticSize[1].Kind, 
        UI_DebugIndentation, "", 
        Box->FixedPosition.X, Box->FixedPosition.Y, Box->FixedSize.X, Box->FixedSize.Y);
    
    if(!UI_IsNilBox(Box->First))
    {
        UI_DebugIndentation += 1;
        UI_DebugPrintBoxes(Box->First);
        UI_DebugIndentation -= 1;
    }
    
    if(!UI_IsNilBox(Box->Next))
    {
        UI_DebugPrintBoxes(Box->Next);
    }
}

//~ Calculations End

internal void
UI_ResolveLayout(ui_box *Root)
{
    if(!UI_IsNilBox(Root))
    { 
        Root->LastTouchedFrameIdx = UI_State->FrameIdx;
        
        for EachIndex(Idx, (s32)Axis2_Count)
        {        
            axis2 Axis = (axis2)Idx;
            
            UI_CalculateStandaloneSizes(Root, Axis);
            UI_CalculateUpwardSizes(Root, Axis);
            UI_CalculateDownwardSizes(Root, Axis);
            UI_CalculateViolations(Root, Axis);
        }
        UI_CalculatePositions(Root);
        UI_DrawBoxes(Root);
    }
}