1 module jcli.text.layout;
2 
3 import std, jcli.text;
4 
5 struct Layout
6 {
7     private
8     {
9         Rect _area;
10         int _horizBlocks;
11         int _vertBlocks;
12     }
13 
14     @safe @nogc
15     this(Rect area, int horizBlocks, int vertBlocks) nothrow pure
16     {
17         this._area = area;
18         this._horizBlocks = horizBlocks;
19         this._vertBlocks = vertBlocks;
20     }
21 
22     @safe
23     Rect blockRectToRealRect(Rect blockRect) const
24     {
25         blockRect = oobRect(OnOOB.constrain, Rect(0, 0, this._horizBlocks, this._vertBlocks), blockRect);
26         return Rect(
27             this._area.left + (this.blockWidth * blockRect.left).round.to!int,
28             this._area.top  + (this.blockHeight * blockRect.top).round.to!int,
29             this._area.left + (this.blockWidth * blockRect.right).round.to!int,
30             this._area.top  + (this.blockHeight * blockRect.bottom).round.to!int,
31         );
32     }
33 
34     @safe
35     private float blockWidth() const
36     {
37         return cast(float)this._area.width / cast(float)this._horizBlocks;
38     }
39 
40     @safe
41     private float blockHeight() const
42     {
43         return cast(float)this._area.height / cast(float)this._vertBlocks;
44     }
45 }
46 
47 struct LayoutBuilder
48 {
49     private Layout _layout;
50 
51     @safe @nogc nothrow pure:
52 
53     LayoutBuilder withArea(int left, int top, int right, int bottom)
54     {
55         this._layout._area = Rect(left, top, right, bottom);
56         return this;
57     }
58 
59     LayoutBuilder withArea(Rect rect)
60     {
61         this._layout._area = rect;
62         return this;
63     }
64 
65     LayoutBuilder withHorizontalBlocks(int amount)
66     {
67         this._layout._horizBlocks = amount;
68         return this;
69     }
70 
71     LayoutBuilder withVerticalBlocks(int amount)
72     {
73         this._layout._vertBlocks = amount;
74         return this;
75     }
76 
77     Layout build()
78     {
79         return this._layout;
80     }
81 }