Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Margins.cs
1using System.Threading.Tasks;
2using System.Xml;
3using SkiaSharp;
5
7{
11 public class Margins : LayoutContainer
12 {
13 private LengthAttribute left;
14 private LengthAttribute right;
15 private LengthAttribute top;
16 private LengthAttribute bottom;
17
23 public Margins(Layout2DDocument Document, ILayoutElement Parent)
24 : base(Document, Parent)
25 {
26 }
27
31 public override string LocalName => "Margins";
32
37 {
38 get => this.left;
39 set => this.left = value;
40 }
41
46 {
47 get => this.right;
48 set => this.right = value;
49 }
50
55 {
56 get => this.top;
57 set => this.top = value;
58 }
59
64 {
65 get => this.bottom;
66 set => this.bottom = value;
67 }
68
73 public override Task FromXml(XmlElement Input)
74 {
75 this.left = new LengthAttribute(Input, "left", this.Document);
76 this.right = new LengthAttribute(Input, "right", this.Document);
77 this.top = new LengthAttribute(Input, "top", this.Document);
78 this.bottom = new LengthAttribute(Input, "bottom", this.Document);
79
80 return base.FromXml(Input);
81 }
82
87 public override void ExportAttributes(XmlWriter Output)
88 {
89 base.ExportAttributes(Output);
90
91 this.left?.Export(Output);
92 this.right?.Export(Output);
93 this.top?.Export(Output);
94 this.bottom?.Export(Output);
95 }
96
103 public override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
104 {
105 return new Margins(Document, Parent);
106 }
107
112 public override void CopyContents(ILayoutElement Destination)
113 {
114 base.CopyContents(Destination);
115
116 if (Destination is Margins Dest)
117 {
118 Dest.left = this.left?.CopyIfNotPreset(Destination.Document);
119 Dest.right = this.right?.CopyIfNotPreset(Destination.Document);
120 Dest.top = this.top?.CopyIfNotPreset(Destination.Document);
121 Dest.bottom = this.bottom?.CopyIfNotPreset(Destination.Document);
122 }
123 }
124
130 public override async Task DoMeasureDimensions(DrawingState State)
131 {
132 EvaluationResult<Length> L = await this.left.TryEvaluate(State.Session);
133 if (L.Ok)
134 State.CalcDrawingSize(L.Result, ref this.leftMargin, true, this);
135 else
136 this.leftMargin = 0;
137
138 L = await this.right.TryEvaluate(State.Session);
139 if (L.Ok)
140 State.CalcDrawingSize(L.Result, ref this.rightMargin, true, this);
141 else
142 this.rightMargin = 0;
143
144 L = await this.top.TryEvaluate(State.Session);
145 if (L.Ok)
146 State.CalcDrawingSize(L.Result, ref this.topMargin, true, this);
147 else
148 this.topMargin = 0;
149
150 L = await this.bottom.TryEvaluate(State.Session);
151 if (L.Ok)
152 State.CalcDrawingSize(L.Result, ref this.bottomMargin, true, this);
153 else
154 this.bottomMargin = 0;
155
156 float InnerWidth = this.ExplicitWidth ?? this.Parent?.InnerWidth ?? State.AreaWidth;
157 float InnerHeight = this.ExplicitHeight ?? this.Parent?.InnerHeight ?? State.AreaHeight;
158
159 SKSize Prev = State.SetAreaSize(new SKSize(
160 InnerWidth - this.leftMargin - this.rightMargin,
161 InnerHeight - this.topMargin - this.bottomMargin));
162
163 await base.DoMeasureDimensions(State);
164
165 State.SetAreaSize(Prev);
166 }
167
172 public override async Task AfterMeasureDimensions(DrawingState State)
173 {
174 await base.AfterMeasureDimensions(State);
175
176 this.innerWidth = this.Width;
177 this.Width = this.innerWidth + this.leftMargin + this.rightMargin;
178
179 this.innerHeight = this.Height;
180 this.Height = this.innerHeight + this.topMargin + this.bottomMargin;
181
182 }
183
187 public override float? InnerWidth => this.innerWidth;
188
192 public override float? InnerHeight => this.innerHeight;
193
197 public override float? PotentialWidth
198 {
199 get
200 {
201 float? f = base.PotentialWidth;
202
203 if (f.HasValue)
204 return f.Value - this.leftMargin - this.rightMargin;
205 else
206 return null;
207 }
208 }
209
213 public override float? PotentialHeight
214 {
215 get
216 {
217 float? f = base.PotentialHeight;
218
219 if (f.HasValue)
220 return f.Value - this.topMargin - this.bottomMargin;
221 else
222 return null;
223 }
224 }
225
230 public override void MeasurePositions(DrawingState State)
231 {
232 float InnerWidth = this.ExplicitWidth ?? this.Parent?.InnerWidth ?? State.AreaWidth;
233 float InnerHeight = this.ExplicitHeight ?? this.Parent?.InnerHeight ?? State.AreaHeight;
234
235 SKSize Prev = State.SetAreaSize(new SKSize(
236 InnerWidth - this.leftMargin - this.rightMargin,
237 InnerHeight - this.topMargin - this.bottomMargin));
238
239 base.MeasurePositions(State);
240
241 State.SetAreaSize(Prev);
242 }
243
244 private float? innerWidth;
245 private float? innerHeight;
246 private float leftMargin;
247 private float rightMargin;
248 private float topMargin;
249 private float bottomMargin;
250
255 public override async Task Draw(DrawingState State)
256 {
257 SKMatrix M = State.Canvas.TotalMatrix;
258 State.Canvas.Translate(this.leftMargin, this.topMargin);
259
260 await base.Draw(State);
261
262 State.Canvas.SetMatrix(M);
263 }
264
269 public override void ExportStateAttributes(XmlWriter Output)
270 {
271 base.ExportStateAttributes(Output);
272
273 this.left?.ExportState(Output);
274 this.right?.ExportState(Output);
275 this.top?.ExportState(Output);
276 this.bottom?.ExportState(Output);
277 }
278 }
279}
Contains a 2D layout document.
void Export(XmlWriter Output)
Exports the attribute.
Definition: Attribute.cs:187
void ExportState(XmlWriter Output)
Exports the state of the attribute.
Definition: Attribute.cs:199
static async Task< EvaluationResult< T > > TryEvaluate(Attribute< T > Attribute, Variables Session)
Tries to evaluate the attribute value.
Definition: Attribute.cs:256
LengthAttribute CopyIfNotPreset(Layout2DDocument ForDocument)
Copies the attribute object if undefined, or defined by an expression. Returns a reference to itself,...
SKCanvas Canvas
Current drawing canvas.
float AreaWidth
Width of current area
float AreaHeight
Height of current area
SKSize SetAreaSize(SKSize AreaSize)
Sets the current area size.
Variables Session
Current session.
Definition: DrawingState.cs:91
void CalcDrawingSize(Length L, ref float Size, bool Horizontal, ILayoutElement Element)
Converts a defined length to drawing size.
override async Task Draw(DrawingState State)
Draws layout entities.
Definition: Margins.cs:255
override? float PotentialHeight
Potential Height of element
Definition: Margins.cs:214
override? float InnerWidth
Inner Width of element
Definition: Margins.cs:187
override Task FromXml(XmlElement Input)
Populates the element (including children) with information from its XML definition.
Definition: Margins.cs:73
override void CopyContents(ILayoutElement Destination)
Copies contents (attributes and children) to the destination element.
Definition: Margins.cs:112
override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
Creates a new instance of the layout element.
Definition: Margins.cs:103
override string LocalName
Local name of type of element.
Definition: Margins.cs:31
override void ExportStateAttributes(XmlWriter Output)
Exports the local attributes of the current element.
Definition: Margins.cs:269
override void MeasurePositions(DrawingState State)
Measures layout entities and defines unassigned properties, related to positions.
Definition: Margins.cs:230
override async Task DoMeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions.
Definition: Margins.cs:130
override? float InnerHeight
Inner Height of element
Definition: Margins.cs:192
override void ExportAttributes(XmlWriter Output)
Exports attributes to XML.
Definition: Margins.cs:87
Margins(Layout2DDocument Document, ILayoutElement Parent)
A translation transform
Definition: Margins.cs:23
LengthAttribute BottomAttribute
Bottom
Definition: Margins.cs:64
override? float PotentialWidth
Potential Width of element
Definition: Margins.cs:198
override async Task AfterMeasureDimensions(DrawingState State)
Called when dimensions have been measured.
Definition: Margins.cs:172
LengthAttribute RightAttribute
Right
Definition: Margins.cs:46
Abstract base class for layout containers (area elements containing embedded layout elements).
Layout2DDocument Document
Layout document.
ILayoutElement Parent
Parent element.
Base interface for all layout elements.
float? InnerWidth
Inner Width of element
Layout2DDocument Document
Layout document.
float? InnerHeight
Inner Height of element