1 /** 
2  * Copyright: Enalye
3  * License: Zlib
4  * Authors: Enalye
5  */
6 module atelier.ui.layout.hlayout;
7 
8 import std.conv : to;
9 import std.algorithm.comparison : max;
10 import atelier.render, atelier.core, atelier.common;
11 import atelier.ui.gui_element;
12 
13 /// Resize children to fit and align them horizontally.
14 class HLayout : GuiElement {
15     private {
16         Vec2f _spacing = Vec2f.zero;
17         uint _capacity;
18     }
19 
20     @property {
21         Vec2f spacing() const {
22             return _spacing;
23         }
24 
25         Vec2f spacing(Vec2f newPadding) {
26             _spacing = newPadding;
27             resize();
28             return _spacing;
29         }
30 
31         uint capacity() const {
32             return _capacity;
33         }
34 
35         uint capacity(uint newCapacity) {
36             _capacity = newCapacity;
37             resize();
38             return _capacity;
39         }
40     }
41 
42     /// Ctor
43     this() {
44     }
45 
46     /// Ctor
47     this(Vec2f newSize) {
48         size = newSize;
49     }
50 
51     override void appendChild(GuiElement child) {
52         child.setAlign(GuiAlignX.left, GuiAlignY.top);
53         super.appendChild(child);
54         resize();
55     }
56 
57     override void prependChild(GuiElement child) {
58         child.setAlign(GuiAlignX.left, GuiAlignY.top);
59         super.prependChild(child);
60         resize();
61     }
62 
63     override void onSize() {
64         resize();
65     }
66 
67     protected void resize() {
68         if (!_children.length)
69             return;
70         Vec2f childSize = Vec2f(size.x / (_capacity != 0u ? _capacity : _children.length), size.y);
71         foreach (size_t id, GuiElement child; _children) {
72             child.position = Vec2f(childSize.x * to!float(id), 0f);
73             child.size = childSize - _spacing;
74         }
75     }
76 }