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.table;
8 import w4d.layout.base,
9        w4d.layout.exception,
10        w4d.layout.fill;
11 import gl3n.linalg;
12 
13 /// A layout object that lineups children like table.
14 class TableLayout : FillLayout
15 {
16     protected const size_t _columns;
17     protected const size_t _rows;
18 
19     protected @property childWidth ()
20     {
21         auto width = style.box.size.width.calced;
22         return width / _columns;
23     }
24     protected @property childHeight ()
25     {
26         auto height = style.box.size.height.calced;
27         return height / _rows;
28     }
29     protected @property childSize ()
30     {
31         return vec2( childWidth, childHeight );
32     }
33 
34     ///
35     this ( Layoutable owner, size_t cols, size_t rows )
36     {
37         enforce( cols > 0 && rows > 0,
38                 "Cols and Rows must be natural number(!=0)." );
39         super( owner );
40 
41         _columns = cols;
42         _rows    = rows;
43     }
44 
45     ///
46     override void place ( vec2 basePoint, vec2 parentSize )
47     {
48         fill( basePoint, parentSize );
49 
50         auto   size      = childSize;
51         auto   children  = children;
52         const  translate = style.clientLeftTop;
53         size_t index     = 0;
54         vec2   pos;
55 
56         for ( auto y = 0; y < _rows; y++ ) {
57             for ( auto x = 0; x < _columns; x++ ) {
58                 if ( index >= children.length ) return;
59 
60                 pos.x = size.x*x + translate.x;
61                 pos.y = size.y*y + translate.y;
62                 children[index++].layout( pos, size );
63             }
64         }
65     }
66 }