Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CanvasGraph.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using SkiaSharp;
9
11{
15 public class CanvasGraph : Graph
16 {
17 private readonly LinkedList<CanvasOperation> operations = new LinkedList<CanvasOperation>();
18 private SKColor? defaultColor;
19 private SKColor? bgColor;
20 private int width;
21 private int height;
22
28 : this(Variables, null, null)
29 {
30 }
31
38 public CanvasGraph(Variables Variables, int? DefaultWidth, int? DefaultHeight)
39 : base(Variables, DefaultWidth, DefaultHeight)
40 {
41 this.width = this.Settings.Width;
42 this.height = this.Settings.Height;
43 this.bgColor = this.Settings.BackgroundColor;
44 }
45
54 public CanvasGraph(Variables Variables, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
55 : this(Variables, Width, Height)
56 {
57 this.bgColor = BackgroundColor;
58 this.defaultColor = DefaultColor;
59 }
60
69 public CanvasGraph(GraphSettings Settings, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
70 : base(Settings, Width, Height)
71 {
72 this.width = this.Settings.Width;
73 this.height = this.Settings.Height;
74 this.bgColor = BackgroundColor;
75 this.defaultColor = DefaultColor;
76 }
77
81 public SKColor? BgColor => this.bgColor;
82
86 public new SKColor? DefaultColor => this.defaultColor;
87
91 public int Width => this.width;
92
96 public int Height => this.height;
97
101 public override bool UsesDefaultColor => this.defaultColor != SKColors.Empty;
102
108 public override bool TrySetDefaultColor(SKColor Color)
109 {
110 this.defaultColor = Color;
111 return true;
112 }
113
120 {
121 if (Element is CanvasGraph G)
122 {
123 CanvasGraph Result = new CanvasGraph(G.Settings, G.width, G.height, G.bgColor, G.defaultColor);
124
125 foreach (CanvasOperation Op in G.operations)
126 Result.operations.AddLast(Op);
127
128 foreach (CanvasOperation Op in this.operations)
129 Result.operations.AddLast(Op);
130
131 return Result;
132 }
133 else
134 return null;
135 }
136
143 {
144 if (Element is CanvasGraph G)
145 {
146 CanvasGraph Result = new CanvasGraph(this.Settings, this.width, this.height, this.bgColor, this.defaultColor);
147
148 foreach (CanvasOperation Op in this.operations)
149 Result.operations.AddLast(Op);
150
151 foreach (CanvasOperation Op in G.operations)
152 Result.operations.AddLast(Op);
153
154 return Result;
155 }
156 else
157 return null;
158 }
159
161 public override bool Equals(object obj)
162 {
163 if (!(obj is CanvasGraph G) ||
164 this.width != G.width ||
165 this.height != G.height ||
166 this.bgColor != G.bgColor ||
167 this.defaultColor != G.defaultColor)
168 {
169 return false;
170 }
171
172 LinkedList<CanvasOperation>.Enumerator e1 = this.operations.GetEnumerator();
173 LinkedList<CanvasOperation>.Enumerator e2 = G.operations.GetEnumerator();
174 bool b1, b2;
175
176 while (true)
177 {
178 b1 = e1.MoveNext();
179 b2 = e2.MoveNext();
180
181 if (b1 ^ b2)
182 return false;
183
184 if (!b1)
185 return true;
186
187 if (!e1.Current.Equals(e2.Current))
188 return false;
189 }
190 }
191
193 public override int GetHashCode()
194 {
195 int Result = this.width.GetHashCode();
196 Result ^= Result << 5 ^ this.height.GetHashCode();
197 Result ^= Result << 5 ^ this.bgColor.GetHashCode();
198 Result ^= Result << 5 ^ this.defaultColor.GetHashCode();
199
200 foreach (CanvasOperation Op in this.operations)
201 Result ^= Result << 5 ^ Op.GetHashCode();
202
203 return Result;
204 }
205
213 public override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
214 {
215 using (SKSurface Surface = SKSurface.Create(new SKImageInfo(this.width, this.height, SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
216 {
217 SKCanvas Canvas = Surface.Canvas;
218 CanvasState State = new CanvasState()
219 {
220 FgColor = this.defaultColor ?? Graph.DefaultColor,
221 BgColor = this.bgColor ?? Settings.BackgroundColor
222 };
223
224 Canvas.Clear(State.BgColor);
225
226 foreach (CanvasOperation Op in this.operations)
227 Op.Draw(Canvas, State);
228
229 using (SKImage Result = Surface.Snapshot())
230 {
231 States = new object[0];
232 return PixelInformation.FromImage(Result);
233 }
234 }
235 }
236
241 public override void ExportGraph(XmlWriter Output)
242 {
243 Output.WriteStartElement("CanvasGraph");
244 Output.WriteAttributeString("width", this.width.ToString());
245 Output.WriteAttributeString("height", this.height.ToString());
246
247 if (this.bgColor.HasValue)
248 Output.WriteAttributeString("bgColor", Expression.ToString(this.bgColor.Value));
249
250 if (this.defaultColor.HasValue)
251 Output.WriteAttributeString("defaultColor", Expression.ToString(this.defaultColor.Value));
252
253 foreach (CanvasOperation Op in this.operations)
254 {
255 Output.WriteStartElement(Op.GetType().Name);
256 Op.ExportGraph(Output);
257 Output.WriteEndElement();
258 }
259
260 Output.WriteEndElement();
261 }
262
267 public override async Task ImportGraphAsync(XmlElement Xml)
268 {
270
271 foreach (XmlAttribute Attr in Xml.Attributes)
272 {
273 switch (Attr.Name)
274 {
275 case "width":
276 this.width = int.Parse(Attr.Value);
277 break;
278
279 case "height":
280 this.height = int.Parse(Attr.Value);
281 break;
282
283 case "bgColor":
284 this.bgColor = Graph.ToColor((await ParseAsync(Attr.Value, Variables)).AssociatedObjectValue);
285 break;
286
287 case "defaultColor":
288 this.defaultColor = Graph.ToColor((await ParseAsync(Attr.Value, Variables)).AssociatedObjectValue);
289 break;
290 }
291 }
292
293 string OperationNamespace = typeof(CanvasOperation).Namespace + ".";
294
295 foreach (XmlNode N in Xml.ChildNodes)
296 {
297 if (N is XmlElement E)
298 {
299 Type T = Types.GetType(OperationNamespace + E.LocalName);
300 if (T is null)
301 continue;
302
303 if (!(Activator.CreateInstance(T) is CanvasOperation Op))
304 continue;
305
306 await Op.ImportGraph(E, Variables);
307
308 this.operations.AddLast(Op);
309 }
310 }
311 }
312
320 public override string GetBitmapClickScript(double X, double Y, object[] States)
321 {
322 return "[" + Expression.ToString(X) + "," + Expression.ToString(Y) + "]";
323 }
324
328 public void LineTo(double x, double y)
329 {
330 this.operations.AddLast(new LineTo((float)x, (float)y));
331 }
332
336 public void MoveTo(double x, double y)
337 {
338 this.operations.AddLast(new MoveTo((float)x, (float)y));
339 }
340
348 public void Line(double x1, double y1, double x2, double y2)
349 {
350 this.operations.AddLast(new Line((float)x1, (float)y1, (float)x2, (float)y2));
351 }
352
357 public void PenWidth(double Width)
358 {
359 this.operations.AddLast(new PenWidth((float)Width));
360 }
361
366 public void Color(SKColor Color)
367 {
368 this.operations.AddLast(new PenColor(Color));
369 }
370
375 public void Color(object Color)
376 {
377 this.operations.AddLast(new PenColor(Graph.ToColor(Color)));
378 }
379
387 public void Rectangle(double x1, double y1, double x2, double y2)
388 {
389 this.operations.AddLast(new Rectangle((float)x1, (float)y1, (float)x2, (float)y2));
390 }
391
399 public void FillRectangle(double x1, double y1, double x2, double y2)
400 {
401 this.operations.AddLast(new FillRectangle((float)x1, (float)y1, (float)x2, (float)y2));
402 }
403
409 public void Plot(double x, double y)
410 {
411 this.operations.AddLast(new Plot((float)x, (float)y));
412 }
413
421 public void Ellipse(double x1, double y1, double x2, double y2)
422 {
423 this.operations.AddLast(new Ellipse((float)x1, (float)y1, (float)x2, (float)y2));
424 }
425
433 public void FillEllipse(double x1, double y1, double x2, double y2)
434 {
435 this.operations.AddLast(new FillEllipse((float)x1, (float)y1, (float)x2, (float)y2));
436 }
437
441 public void Clear()
442 {
443 this.operations.AddLast(new Clear());
444 }
445
446 }
447}
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static Type GetType(string FullName)
Gets a type, given its full name.
Definition: Types.cs:41
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:16
void Rectangle(double x1, double y1, double x2, double y2)
Draws a rectangle defined by two opposing corner points.
Definition: CanvasGraph.cs:387
void PenWidth(double Width)
Sets the current pen width.
Definition: CanvasGraph.cs:357
override bool UsesDefaultColor
If graph uses default color
Definition: CanvasGraph.cs:101
CanvasGraph(Variables Variables, int? DefaultWidth, int? DefaultHeight)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:38
void FillRectangle(double x1, double y1, double x2, double y2)
Fills a rectangle defined by two opposing corner points.
Definition: CanvasGraph.cs:399
void Color(SKColor Color)
Sets the current pen color.
Definition: CanvasGraph.cs:366
CanvasGraph(Variables Variables, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:54
void Color(object Color)
Sets the current pen color.
Definition: CanvasGraph.cs:375
override string GetBitmapClickScript(double X, double Y, object[] States)
Gets script corresponding to a point in a generated bitmap representation of the graph.
Definition: CanvasGraph.cs:320
SKColor? BgColor
Background color.
Definition: CanvasGraph.cs:81
override ISemiGroupElement AddRight(ISemiGroupElement Element)
Tries to add an element to the current element, from the right.
Definition: CanvasGraph.cs:142
CanvasGraph(GraphSettings Settings, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:69
override void ExportGraph(XmlWriter Output)
Exports graph specifics to XML.
Definition: CanvasGraph.cs:241
void LineTo(double x, double y)
Draws a line to a specific coordinate
Definition: CanvasGraph.cs:328
void Clear()
Clears the canvas with the current brush.
Definition: CanvasGraph.cs:441
override bool TrySetDefaultColor(SKColor Color)
Tries to set the default color.
Definition: CanvasGraph.cs:108
void FillEllipse(double x1, double y1, double x2, double y2)
Fills a ellipse defined by two opposing corner points.
Definition: CanvasGraph.cs:433
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: CanvasGraph.cs:193
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: CanvasGraph.cs:161
override ISemiGroupElement AddLeft(ISemiGroupElement Element)
Tries to add an element to the current element, from the left.
Definition: CanvasGraph.cs:119
CanvasGraph(Variables Variables)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:27
void Ellipse(double x1, double y1, double x2, double y2)
Draws a ellipse defined by two opposing corner points.
Definition: CanvasGraph.cs:421
void Line(double x1, double y1, double x2, double y2)
Draws a line between two points.
Definition: CanvasGraph.cs:348
void MoveTo(double x, double y)
Draws a line to a specific coordinate
Definition: CanvasGraph.cs:336
override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
Creates a bitmap of the graph.
Definition: CanvasGraph.cs:213
new? SKColor DefaultColor
Default color.
Definition: CanvasGraph.cs:86
override async Task ImportGraphAsync(XmlElement Xml)
Imports graph specifics from XML.
Definition: CanvasGraph.cs:267
void Plot(double x, double y)
Plots a pixel at a given coordinate.
Definition: CanvasGraph.cs:409
Abstract base class for canvas operations.
abstract void ExportGraph(XmlWriter Output)
Exports graph specifics to XML.
abstract void Draw(SKCanvas Canvas, CanvasState State)
Performs a drawing operation.
Current drawing state in a canvas graph.
Definition: CanvasState.cs:12
Draws a line to a specific coordinate
Definition: LineTo.cs:10
Plots a pixel at a given coordinate.
Definition: Plot.cs:10
Base class for graphs.
Definition: Graph.cs:79
static readonly SKColor DefaultColor
Default color: Red
Definition: Graph.cs:118
GraphSettings Settings
Graph settings available during creation.
Definition: Graph.cs:188
static SKColor ToColor(object Object)
Converts an object to a color.
Definition: Graph.cs:828
static async Task< IElement > ParseAsync(string s, Variables Variables)
Parses an element expression string.
Definition: Graph.cs:1585
SKColor BackgroundColor
Background color.
int Width
Width of graph, in pixels. (Default=640 pixels.)
int Height
Height of graph, in pixels. (Default=480 pixels.)
Contains pixel information
static PixelInformation FromImage(SKImage Image)
Gets the pixel information from an SKImage.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of semigroup elements.