Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Layout2D.cs
1using SkiaSharp;
2using System.Threading.Tasks;
3using System.Xml;
8
10{
15 {
16 private StringAttribute font;
17 private StringAttribute pen;
18 private StringAttribute background;
19 private ColorAttribute textColor;
20
26 public Layout2D(Layout2DDocument Document, ILayoutElement Parent)
27 : base(Document, Parent)
28 {
29 }
30
34 public override string LocalName => "Layout2D";
35
40 {
41 get => this.font;
42 set => this.font = value;
43 }
44
49 {
50 get => this.pen;
51 set => this.pen = value;
52 }
53
58 {
59 get => this.background;
60 set => this.background = value;
61 }
62
67 {
68 get => this.textColor;
69 set => this.textColor = value;
70 }
71
76 {
77 get => this.background;
78 set => this.background = value;
79 }
80
85 public override Task FromXml(XmlElement Input)
86 {
87 this.font = new StringAttribute(Input, "font", this.Document);
88 this.pen = new StringAttribute(Input, "pen", this.Document);
89 this.background = new StringAttribute(Input, "background", this.Document);
90 this.textColor = new ColorAttribute(Input, "textColor", this.Document);
91
92 return base.FromXml(Input);
93 }
94
99 public override void ExportAttributes(XmlWriter Output)
100 {
101 base.ExportAttributes(Output);
102
103 this.font?.Export(Output);
104 this.pen?.Export(Output);
105 this.background?.Export(Output);
106 this.textColor?.Export(Output);
107 }
108
115 public override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
116 {
117 return new Layout2D(Document, Parent);
118 }
119
124 public override void CopyContents(ILayoutElement Destination)
125 {
126 base.CopyContents(Destination);
127
128 if (Destination is Layout2D Dest)
129 {
130 Dest.font = this.font?.CopyIfNotPreset(Destination.Document);
131 Dest.pen = this.pen?.CopyIfNotPreset(Destination.Document);
132 Dest.background = this.background?.CopyIfNotPreset(Destination.Document);
133 Dest.textColor = this.textColor?.CopyIfNotPreset(Destination.Document);
134 }
135 }
136
137 private Font fontDef = null;
138 private Pen penDef = null;
139 private Background backgroundDef = null;
140
146 public override async Task DoMeasureDimensions(DrawingState State)
147 {
148 SKFont FontBak = null;
149 SKPaint TextPenBak = null;
150 SKPaint PenBak = null;
151 SKPaint BackgroundBak = null;
152
153 EvaluationResult<string> RefId = await this.font.TryEvaluate(State.Session);
154 if (RefId.Ok &&
155 this.Document.TryGetElement(RefId.Result, out ILayoutElement Element) &&
156 Element is Font Font)
157 {
158 this.fontDef = Font;
159
160 await Font.MeasureDimensions(State);
161
162 FontBak = State.Font;
163 State.Font = this.fontDef.FontDef;
164
165 TextPenBak = State.TextPen;
166 State.TextPen = this.fontDef.TextPen;
167 }
168
169 RefId = await this.pen.TryEvaluate(State.Session);
170 if (RefId.Ok &&
171 this.Document.TryGetElement(RefId.Result, out Element) &&
172 Element is Pen Pen)
173 {
174 this.penDef = Pen;
175 PenBak = State.ShapePen;
176 State.ShapePen = Pen.Paint;
177 }
178
179 RefId = await this.background.TryEvaluate(State.Session);
180 if (RefId.Ok &&
181 this.Document.TryGetElement(RefId.Result, out Element) &&
182 Element is Background Background)
183 {
184 this.backgroundDef = Background;
185 BackgroundBak = State.ShapeFill;
186 State.ShapeFill = Background.Paint;
187 }
188
189 await base.DoMeasureDimensions(State);
190
191 if (!(FontBak is null))
192 State.Font = FontBak;
193
194 if (!(TextPenBak is null))
195 State.TextPen = TextPenBak;
196
197 if (!(PenBak is null))
198 State.ShapePen = PenBak;
199
200 if (!(BackgroundBak is null))
201 State.ShapeFill = BackgroundBak;
202 }
203
208 public override void MeasurePositions(DrawingState State)
209 {
210 SKFont FontBak = null;
211 SKPaint TextPenBak = null;
212 SKPaint PenBak = null;
213 SKPaint BackgroundBak = null;
214
215 if (!(this.fontDef is null))
216 {
217 FontBak = State.Font;
218 State.Font = this.fontDef.FontDef;
219
220 TextPenBak = State.TextPen;
221 State.TextPen = this.fontDef.TextPen;
222 }
223
224 if (!(this.penDef is null))
225 {
226 PenBak = State.ShapePen;
227 State.ShapePen = this.penDef.Paint;
228 }
229
230 if (!(this.backgroundDef is null))
231 {
232 BackgroundBak = State.ShapeFill;
233 State.ShapeFill = this.backgroundDef.Paint;
234 }
235
236 base.MeasurePositions(State);
237
238 if (!(FontBak is null))
239 State.Font = FontBak;
240
241 if (!(TextPenBak is null))
242 State.TextPen = TextPenBak;
243
244 if (!(PenBak is null))
245 State.ShapePen = PenBak;
246
247 if (!(BackgroundBak is null))
248 State.ShapeFill = BackgroundBak;
249 }
250
255 public override async Task Draw(DrawingState State)
256 {
257 SKFont FontBak = null;
258 SKPaint TextPenBak = null;
259 SKPaint PenBak = null;
260 SKPaint BackgroundBak = null;
261
262 if (!(this.fontDef is null))
263 {
264 FontBak = State.Font;
265 State.Font = this.fontDef.FontDef;
266
267 TextPenBak = State.TextPen;
268 State.TextPen = this.fontDef.TextPen;
269 }
270
271 if (!(this.penDef is null))
272 {
273 PenBak = State.ShapePen;
274 State.ShapePen = this.penDef.Paint;
275 }
276
277 if (!(this.backgroundDef is null))
278 {
279 BackgroundBak = State.ShapeFill;
280 State.ShapeFill = this.backgroundDef.Paint;
281
282 if (!(this.backgroundDef is SolidBackground))
283 State.Canvas.DrawRect(0, 0, State.AreaWidth, State.AreaHeight, State.ShapeFill);
284 }
285
286 await base.Draw(State);
287
288 if (!(FontBak is null))
289 State.Font = FontBak;
290
291 if (!(TextPenBak is null))
292 State.TextPen = TextPenBak;
293
294 if (!(PenBak is null))
295 State.ShapePen = PenBak;
296
297 if (!(BackgroundBak is null))
298 State.ShapeFill = BackgroundBak;
299 }
300
305 public override void ExportStateAttributes(XmlWriter Output)
306 {
307 base.ExportStateAttributes(Output);
308
309 this.font?.ExportState(Output);
310 this.pen?.ExportState(Output);
311 this.background?.ExportState(Output);
312 this.textColor?.ExportState(Output);
313 }
314 }
315}
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
ColorAttribute CopyIfNotPreset(Layout2DDocument ForDocument)
Copies the attribute object if undefined, or defined by an expression. Returns a reference to itself,...
StringAttribute CopyIfNotPreset(Layout2DDocument ForDocument)
Copies the attribute object if undefined, or defined by an expression. Returns a reference to itself,...
Abstract base class for backgrounds.
Definition: Background.cs:10
SKCanvas Canvas
Current drawing canvas.
float AreaWidth
Width of current area
float AreaHeight
Height of current area
Variables Session
Current session.
Definition: DrawingState.cs:89
SKPaint ShapeFill
Fill to use for shape, if no other is specified in the shape.
SKPaint TextPen
Current text paint settings
SKPaint ShapePen
Pen to use for shape, if no other is specified in the shape.
Abstract base class for fonts.
Definition: Font.cs:12
SKFont FontDef
Measured Font
Definition: Font.cs:241
Root node for two-dimensional layouts
Definition: Layout2D.cs:15
StringAttribute FontIdAttribute
Font ID
Definition: Layout2D.cs:40
StringAttribute BackgroundColorAttribute
Background Color
Definition: Layout2D.cs:76
override void MeasurePositions(DrawingState State)
Measures layout entities and defines unassigned properties, related to positions.
Definition: Layout2D.cs:208
override async Task Draw(DrawingState State)
Draws layout entities.
Definition: Layout2D.cs:255
override void ExportAttributes(XmlWriter Output)
Exports attributes to XML.
Definition: Layout2D.cs:99
StringAttribute BackgroundIdAttribute
Background ID
Definition: Layout2D.cs:58
override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
Creates a new instance of the layout element.
Definition: Layout2D.cs:115
override string LocalName
Local name of type of element.
Definition: Layout2D.cs:34
ColorAttribute TextColorAttribute
Text Color
Definition: Layout2D.cs:67
StringAttribute PenIdAttribute
Pen ID
Definition: Layout2D.cs:49
override void CopyContents(ILayoutElement Destination)
Copies contents (attributes and children) to the destination element.
Definition: Layout2D.cs:124
override async Task DoMeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions.
Definition: Layout2D.cs:146
override void ExportStateAttributes(XmlWriter Output)
Exports the local attributes of the current element.
Definition: Layout2D.cs:305
Layout2D(Layout2DDocument Document, ILayoutElement Parent)
Root node for two-dimensional layouts
Definition: Layout2D.cs:26
override Task FromXml(XmlElement Input)
Populates the element (including children) with information from its XML definition.
Definition: Layout2D.cs:85
Abstract base class for layout containers (area elements containing embedded layout elements).
async Task MeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions....
Layout2DDocument Document
Layout document.
Abstract base class for pens.
Definition: Pen.cs:13
SKPaint Paint
Current pen
Definition: Pen.cs:48
Base interface for all layout elements.
Layout2DDocument Document
Layout document.