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