1 module jcli.text.layout;
2 
3 import 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         import std.math : round;
26         import std.conv : to;
27 
28         blockRect = oobRect(OnOOB.constrain, Rect(0, 0, this._horizBlocks, this._vertBlocks), blockRect);
29         return Rect(
30             this._area.left + (this.blockWidth * blockRect.left).round.to!int,
31             this._area.top  + (this.blockHeight * blockRect.top).round.to!int,
32             this._area.left + (this.blockWidth * blockRect.right).round.to!int,
33             this._area.top  + (this.blockHeight * blockRect.bottom).round.to!int,
34         );
35     }
36 
37     @safe
38     private float blockWidth() const
39     {
40         return cast(float)this._area.width / cast(float)this._horizBlocks;
41     }
42 
43     @safe
44     private float blockHeight() const
45     {
46         return cast(float)this._area.height / cast(float)this._vertBlocks;
47     }
48 }
49 
50 struct LayoutBuilder
51 {
52     private Layout _layout;
53 
54     @safe @nogc nothrow pure:
55 
56     LayoutBuilder withArea(int left, int top, int right, int bottom)
57     {
58         this._layout._area = Rect(left, top, right, bottom);
59         return this;
60     }
61 
62     LayoutBuilder withArea(Rect rect)
63     {
64         this._layout._area = rect;
65         return this;
66     }
67 
68     LayoutBuilder withHorizontalBlocks(int amount)
69     {
70         this._layout._horizBlocks = amount;
71         return this;
72     }
73 
74     LayoutBuilder withVerticalBlocks(int amount)
75     {
76         this._layout._vertBlocks = amount;
77         return this;
78     }
79 
80     Layout build()
81     {
82         return this._layout;
83     }
84 }