1 // Written in the D programming language.
2 /++
3  + Authors: KanzakiKino
4  + Copyright: KanzakiKino 2018
5  + License: LGPL-3.0
6 ++/
7 module w4d.layout.lineup;
8 import w4d.layout.base,
9        w4d.layout.exception,
10        w4d.layout.fill,
11        w4d.style.widget,
12        w4d.util.vector;
13 import gl3n.linalg;
14 import std.algorithm;
15 
16 /// A layout object that lineups children.
17 /// You can specify Scalar.Auto at width or height to respect wantedSize.
18 /// And you can also specify Scalar.None to shrink as small as possible.
19 class LineupLayout (bool H) : FillLayout
20 {
21     /// Direction of lineup.
22     alias Horizon = H;
23 
24     protected vec2 _childSize;
25     protected vec2 _basePoint;
26     protected vec2 _usedSize;
27 
28     ///
29     this ( Layoutable owner )
30     {
31         super( owner );
32     }
33 
34     protected void clearStatus ()
35     {
36         _childSize = style.box.clientSize;
37         _basePoint = style.clientLeftTop;
38         _usedSize  = vec2(0,0);
39     }
40     protected void updateStatus ( vec2 placedSize )
41     {
42         const length = placedSize.getLength!H;
43         const weight = placedSize.getWeight!H;
44 
45         _basePoint.getLength!H += length;
46         _usedSize .getLength!H += length;
47 
48         _usedSize.getWeight!Horizon =
49             max( _usedSize.getWeight!H, weight );
50     }
51 
52     ///
53     override void place ( vec2 basepos, vec2 parentSize )
54     {
55         fill( basepos, parentSize );
56         clearStatus();
57         foreach ( child; children ) {
58             auto size = child.layout( _basePoint, _childSize );
59             updateStatus( size );
60         }
61         alterSize( _usedSize );
62     }
63 }
64 
65 /// A layout object that lineups horizontally.
66 alias HorizontalLineupLayout = LineupLayout!true;
67 /// A layout object that lineups vertically.
68 alias VerticalLineupLayout   = LineupLayout!false;