Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LayoutContainer.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
6using Waher.Script;
7
9{
14 public abstract class LayoutContainer : LayoutArea
15 {
16 private ILayoutElement[] children;
17
25 : base(Document, Parent)
26 {
27 }
28
33 {
34 get => this.children;
35 set => this.children = value;
36 }
37
41 public bool HasChildren => !(this.children is null) && this.children.Length > 0;
42
46 public override void Dispose()
47 {
48 base.Dispose();
49
50 if (this.HasChildren)
51 {
52 foreach (ILayoutElement E in this.children)
53 E.Dispose();
54 }
55 }
56
61 public override async Task FromXml(XmlElement Input)
62 {
63 await base.FromXml(Input);
64
65 List<ILayoutElement> Children = null;
66
67 foreach (XmlNode Node in Input.ChildNodes)
68 {
69 if (Node is XmlElement E)
70 {
71 if (Children is null)
72 Children = new List<ILayoutElement>();
73
74 Children.Add(await this.Document.CreateElement(E, this));
75 }
76 }
77
78 this.children = Children?.ToArray();
79 }
80
85 public override void ExportChildren(XmlWriter Output)
86 {
87 base.ExportChildren(Output);
88
89 if (this.HasChildren)
90 {
91 foreach (ILayoutElement Child in this.children)
92 Child.ToXml(Output);
93 }
94 }
95
100 public override void CopyContents(ILayoutElement Destination)
101 {
102 base.CopyContents(Destination);
103
104 if (Destination is LayoutContainer Dest)
105 {
106 if (this.HasChildren)
107 {
108 int i, c = this.children.Length;
109
111
112 for (i = 0; i < c; i++)
113 Children[i] = this.children[i].Copy(Dest);
114
115 Dest.children = Children;
116 }
117 }
118 }
119
124 public override void BeforeMeasureDimensions(DrawingState State)
125 {
126 this.left0 = this.right0 = this.top0 = this.bottom0 = this.width0 = this.height0 = null;
127 }
128
134 public override async Task DoMeasureDimensions(DrawingState State)
135 {
136 await base.DoMeasureDimensions(State);
137
138 if (this.HasChildren && this.MeasureChildrenDimensions)
139 {
140 foreach (ILayoutElement E in this.children)
141 {
143
144 await E.DoMeasureDimensions(State);
145
146 await E.AfterMeasureDimensions(State);
147 this.IncludeElement(E);
148 }
149 }
150 }
151
156 public override Task AfterMeasureDimensions(DrawingState State)
157 {
158 float f;
159
160 if (this.left0.HasValue && this.right0.HasValue)
161 {
162 f = this.right0.Value - this.left0.Value;
163
164 if (!this.width0.HasValue || f > this.width0.Value)
165 this.width0 = f;
166 }
167
168 if (this.top0.HasValue && this.bottom0.HasValue)
169 {
170 f = this.bottom0.Value - this.top0.Value;
171
172 if (!this.height0.HasValue || f > this.height0.Value)
173 this.height0 = f;
174 }
175
176 this.Left = this.Right = this.Width = null;
177 this.Top = this.Bottom = this.Height = null;
178
179 if (this.width0.HasValue || this.ExplicitWidth.HasValue)
180 {
181 this.Width = this.ExplicitWidth ?? this.width0;
182 if (this.left0.HasValue)
183 this.Left = this.left0;
184 else
185 this.Right = this.right0;
186 }
187 else
188 {
189 this.Right = this.right0;
190 this.Left = this.left0;
191 }
192
193 if (this.height0.HasValue || this.ExplicitHeight.HasValue)
194 {
195 this.Height = this.ExplicitHeight ?? this.height0;
196 if (this.top0.HasValue)
197 this.Top = this.top0;
198 else
199 this.Bottom = this.bottom0;
200 }
201 else
202 {
203 this.Bottom = this.bottom0;
204 this.Top = this.top0;
205 }
206
207 return Task.CompletedTask;
208 }
209
210 private float? left0;
211 private float? right0;
212 private float? top0;
213 private float? bottom0;
214 private float? width0;
215 private float? height0;
216
221 protected void IncludeElement(ILayoutElement Element)
222 {
223 float? X = Element.Left;
224 float? Y = Element.Top;
225 float? V;
226
227 if (X.HasValue && Y.HasValue)
228 this.IncludePoint(X.Value, Y.Value);
229
230 V = Element.Width;
231 if (!this.width0.HasValue || (V.HasValue && V.Value > this.width0.Value))
232 this.width0 = V;
233
234 X = Element.Right;
235 Y = Element.Bottom;
236
237 if (X.HasValue && Y.HasValue)
238 this.IncludePoint(X.Value, Y.Value);
239
240 V = Element.Height;
241 if (!this.height0.HasValue || (V.HasValue && V.Value > this.height0.Value))
242 this.height0 = V;
243 }
244
248 protected virtual bool MeasureChildrenDimensions => true;
249
254 public override void MeasurePositions(DrawingState State)
255 {
256 base.MeasurePositions(State);
257
258 if (this.HasChildren && this.MeasureChildrenPositions)
259 {
260 foreach (ILayoutElement E in this.children)
261 E.MeasurePositions(State);
262 }
263 }
264
268 protected virtual bool MeasureChildrenPositions => true;
269
278 protected void IncludePoint(float X, float Y, float RX, float RY, float Angle)
279 {
280 float Px = (float)(X + RX * Math.Cos(Angle * DegreesToRadians));
281 float Py = (float)(Y + RY * Math.Sin(Angle * DegreesToRadians));
282
283 this.IncludePoint(Px, Py);
284 }
285
291 public void IncludePoint(float X, float Y)
292 {
293 if (!this.left0.HasValue || X < this.left0.Value)
294 this.left0 = X;
295
296 if (!this.right0.HasValue || X > this.right0.Value)
297 this.right0 = X;
298
299 if (!this.top0.HasValue || Y < this.top0.Value)
300 this.top0 = Y;
301
302 if (!this.bottom0.HasValue || Y > this.bottom0.Value)
303 this.bottom0 = Y;
304 }
305
316 protected async Task<CalculatedPoint> IncludePoint(DrawingState State, LengthAttribute XAttribute,
317 LengthAttribute YAttribute, StringAttribute RefAttribute, float X, float Y)
318 {
319 CalculatedPoint Result = await this.CalcPoint(State, XAttribute, YAttribute, RefAttribute, X, Y);
320 if (Result.Ok)
321 this.IncludePoint(Result.X, Result.Y);
322
323 return Result;
324 }
325
330 public override async Task Draw(DrawingState State)
331 {
332 if (this.HasChildren)
333 {
334 foreach (ILayoutElement E in this.children)
335 {
336 if (E.IsVisible)
337 await E.Draw(State);
338 }
339 }
340 }
341
346 public override async Task RegisterIDs(Variables Session)
347 {
348 await base.RegisterIDs(Session);
349
350 if (this.HasChildren)
351 {
352 foreach (ILayoutElement E in this.children)
353 await E.RegisterIDs(Session);
354 }
355 }
356
361 public override void ExportStateChildren(XmlWriter Output)
362 {
363 base.ExportStateChildren(Output);
364
365 if (this.HasChildren)
366 {
367 foreach (ILayoutElement Child in this.children)
368 Child.ExportState(Output);
369 }
370 }
371
372 }
373}
Contains a 2D layout document.
Abstract base class for angle elements.
Definition: Angle.cs:11
Abstract base class for layout elements with an implicit area.
Definition: LayoutArea.cs:27
Abstract base class for layout containers (area elements containing embedded layout elements).
override void CopyContents(ILayoutElement Destination)
Copies contents (attributes and children) to the destination element.
void IncludePoint(float X, float Y, float RX, float RY, float Angle)
Includes a point in the area measurement.
bool HasChildren
If the element has children or not.
override void ExportStateChildren(XmlWriter Output)
Exports the current state of child nodes of the current element.
override async Task RegisterIDs(Variables Session)
Registers any IDs defined with the encapsulating document.
override void BeforeMeasureDimensions(DrawingState State)
Called before dimensions are measured.
virtual bool MeasureChildrenPositions
If children positions are to be measured.
virtual bool MeasureChildrenDimensions
If children dimensions are to be measured.
ILayoutElement[] Children
Child elements
override async Task FromXml(XmlElement Input)
Populates the element (including children) with information from its XML definition.
override void ExportChildren(XmlWriter Output)
Exports child elements to XML.
void IncludeElement(ILayoutElement Element)
Includes element in dimension measurement.
LayoutContainer(Layout2DDocument Document, ILayoutElement Parent)
Abstract base class for layout containers (area elements containing embedded layout elements).
override void MeasurePositions(DrawingState State)
Measures layout entities and defines unassigned properties, related to positions.
override async Task DoMeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions.
async Task< CalculatedPoint > IncludePoint(DrawingState State, LengthAttribute XAttribute, LengthAttribute YAttribute, StringAttribute RefAttribute, float X, float Y)
Includes a point in the area measurement.
override void Dispose()
IDisposable.Dispose
override async Task Draw(DrawingState State)
Draws layout entities.
override Task AfterMeasureDimensions(DrawingState State)
Called when dimensions have been measured.
void IncludePoint(float X, float Y)
Includes a point in the area measurement.
ILayoutElement Copy(ILayoutElement Parent)
Creates a copy of the layout element.
Layout2DDocument Document
Layout document.
async Task< CalculatedPoint > CalcPoint(DrawingState State, LengthAttribute XAttribute, LengthAttribute YAttribute, StringAttribute RefAttribute, float X, float Y)
Includes a point in the area measurement.
Collection of variables.
Definition: Variables.cs:25
Base interface for all layout elements.
float? Bottom
Bottom coordinate of bounding box, after measurement.
Task RegisterIDs(Variables Session)
Registers any IDs defined with the encapsulating document.
float? Right
Right coordinate of bounding box, after measurement.
Task Draw(DrawingState State)
Draws layout entities.
void BeforeMeasureDimensions(DrawingState State)
Called before dimensions are measured.
bool IsVisible
If the element is visible or not.
float? Left
Left coordinate of bounding box, after measurement.
Task DoMeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions.
float? Top
Top coordinate of bounding box, after measurement.
string ExportState()
Exports the internal state of the layout.
void MeasurePositions(DrawingState State)
Measures layout entities and defines unassigned properties, related to positions.
Task AfterMeasureDimensions(DrawingState State)
Called when dimensions have been measured.
string ToXml()
Exports the element to XML.
Represents a calculated point.
bool Ok
If point is successfully evaluated