Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FlexibleHorizontalCells.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Script;
5
7{
12 {
13 private readonly List<Padding> currentRow = new List<Padding>();
14 private readonly List<Tuple<float, float, Padding[]>> rows = new List<Tuple<float, float, Padding[]>>();
15 private readonly Variables session;
16 private readonly float limitWidth;
17 private readonly HorizontalDirection horizontalDirection;
18 private readonly VerticalDirection verticalDirection;
19 private readonly HorizontalAlignment horizontalAlignment;
20 private float maxWidth = 0;
21 private float maxHeight = 0;
22 private float x = 0;
23 private float y = 0;
24
33 public FlexibleHorizontalCells(Variables Session, float WidthLimit,
36 {
37 this.session = Session;
38 this.limitWidth = WidthLimit;
39 this.horizontalDirection = HorizontalDirection;
40 this.verticalDirection = VerticalDirection;
41 this.horizontalAlignment = HorizontalAlignment;
42 }
43
48 public Task Add(ILayoutElement Element)
49 {
50 float Width = Element.Width ?? 0;
51 float Height = Element.Height ?? 0;
52
53 if (this.x + Width > this.limitWidth)
54 this.Flush();
55
56 this.currentRow.Add(new Padding(Element, 0, 0));
57 this.x += Width;
58
59 if (Height > this.maxHeight)
60 this.maxHeight = Height;
61
62 return Task.CompletedTask;
63 }
64
68 public void Flush()
69 {
70 if (this.currentRow.Count > 0)
71 {
72 this.rows.Add(new Tuple<float, float, Padding[]>(this.x, this.maxHeight, this.currentRow.ToArray()));
73 this.currentRow.Clear();
74 this.y += this.maxHeight;
75
76 if (this.x > this.maxWidth)
77 this.maxWidth = x;
78 }
79
80 this.maxHeight = 0;
81 this.x = 0;
82 }
83
88 public void MeasurePositions(DrawingState State)
89 {
90 ILayoutElement Element;
91
92 foreach (Tuple<float, float, Padding[]> Row in this.rows)
93 {
94 foreach (Padding P in Row.Item3)
95 {
96 Element = P.Element;
97
98 Element.MeasurePositions(State);
99 P.OffsetX -= Element.Left ?? 0;
100 P.OffsetY -= Element.Top ?? 0;
101 }
102 }
103 }
104
108 public float TotWidth => this.maxWidth;
109
113 public float TotHeight => this.y;
114
119 public void Distribute(bool SetPosition)
120 {
121 foreach (Tuple<float, float, Padding[]> Row in this.rows)
122 {
123 foreach (Padding P in Row.Item3)
124 P.Distribute(null, Row.Item2, this.session, SetPosition);
125 }
126 }
127
132 public Padding[] Align()
133 {
134 List<Padding> Result = new List<Padding>();
135 float X;
136 float Y = this.verticalDirection == VerticalDirection.TopDown ? 0 : this.y;
137 float Diff;
138
139 foreach (Tuple<float, float, Padding[]> Row in this.rows)
140 {
141 Diff = this.maxWidth - Row.Item1;
142 switch (this.horizontalAlignment)
143 {
144 case HorizontalAlignment.Left:
145 default:
146 if (this.horizontalDirection == HorizontalDirection.LeftRight)
147 X = 0;
148 else
149 X = this.maxWidth - Diff;
150 break;
151
152 case HorizontalAlignment.Center:
153 if (this.horizontalDirection == HorizontalDirection.LeftRight)
154 X = Diff / 2;
155 else
156 X = this.maxWidth - Diff / 2;
157 break;
158
159 case HorizontalAlignment.Right:
160 if (this.horizontalDirection == HorizontalDirection.LeftRight)
161 X = Diff;
162 else
163 X = this.maxWidth;
164 break;
165 }
166
167 if (this.verticalDirection == VerticalDirection.BottomUp)
168 Y -= Row.Item2;
169
170 foreach (Padding P in Row.Item3)
171 {
172 float Width = P.Element.Width ?? 0;
173
174 if (this.horizontalDirection == HorizontalDirection.RightLeft)
175 X -= Width;
176
177 P.OffsetX += X;
178 P.OffsetY += Y;
179 Result.Add(P);
180
181 if (this.horizontalDirection == HorizontalDirection.LeftRight)
182 X += Width;
183 }
184
185 if (this.verticalDirection == VerticalDirection.TopDown)
186 Y += Row.Item2;
187 }
188
189 return Result.ToArray();
190 }
191
192 }
193}
Lays out elements flexibly, first horizontally, then vertically.
void Distribute(bool SetPosition)
Distributes cells in layout.
void Flush()
Flushes any waiting elements int he layout pipeline.
Padding[] Align()
Aligns cells and returns an array of padded cells.
Task Add(ILayoutElement Element)
Adds a cell to the layout.
void MeasurePositions(DrawingState State)
Measures layout entities and defines unassigned properties, related to positions.
FlexibleHorizontalCells(Variables Session, float WidthLimit, HorizontalDirection HorizontalDirection, VerticalDirection VerticalDirection, HorizontalAlignment HorizontalAlignment)
Lays out elements flexibly, first horizontally, then vertically.
Provides padding for a layout element in a group contruct.
Definition: Padding.cs:11
Task Distribute(float? MaxWidth, float? MaxHeight, Variables Session, bool SetPosition)
Aligns a measured cell
Definition: Padding.cs:73
ILayoutElement Element
Embedded element
Definition: Padding.cs:36
Collection of variables.
Definition: Variables.cs:25
Basic interface for cell layout objects.
Definition: ICellLayout.cs:9
Base interface for all layout elements.
void MeasurePositions(DrawingState State)
Measures layout entities and defines unassigned properties, related to positions.
HorizontalDirection
Horizontal ordering
Definition: Flexible.cs:27
VerticalDirection
Vertical ordering
Definition: Flexible.cs:43
HorizontalAlignment
Horizontal alignment
Definition: Cell.cs:14