1 /** 
2  * Copyright: Enalye
3  * License: Zlib
4  * Authors: Enalye
5  */
6 module atelier.ui.layout.anchoredlayout;
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 class AnchoredLayout : GuiElement {
14     private {
15         Vec2f[] _childrenPositions, _childrenSizes;
16     }
17 
18     /// Ctor
19     this() {
20     }
21 
22     /// Ctor
23     this(Vec2f newSize) {
24         size = newSize;
25     }
26 
27     override void appendChild(GuiElement gui) {
28         super.appendChild(gui);
29         _childrenPositions ~= Vec2f.half;
30         _childrenSizes ~= Vec2f.one;
31         resize();
32     }
33 
34     override void onPosition() {
35         resize();
36     }
37 
38     override void onSize() {
39         resize();
40     }
41 
42     void appendChild(GuiElement gui, Vec2f position, Vec2f size) {
43         gui.setAlign(GuiAlignX.left, GuiAlignY.top);
44         super.appendChild(gui);
45         _childrenPositions ~= position;
46         _childrenSizes ~= size;
47         resize();
48     }
49 
50     override void removeChildren() {
51         super.removeChildren();
52         _childrenPositions.length = 0L;
53         _childrenSizes.length = 0L;
54     }
55 
56     protected void resize() {
57         if (!_children.length)
58             return;
59         foreach (size_t id, GuiElement gui; _children) {
60             gui.position = size * _childrenPositions[id];
61             gui.size = size * _childrenSizes[id];
62         }
63     }
64 }