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;
3using System.Threading.Tasks;
4using System.Xml;
5using SkiaSharp;
10
12{
16 public class CanvasGraph : Graph
17 {
18 private readonly ChunkedList<CanvasOperation> operations = new ChunkedList<CanvasOperation>();
19 private SKColor? defaultColor;
20 private SKColor? bgColor;
21 private int width;
22 private int height;
23
27 public CanvasGraph()
28 : this(new Variables())
29 {
30 }
31
37 : this(Variables, null, null)
38 {
39 }
40
47 public CanvasGraph(Variables Variables, int? DefaultWidth, int? DefaultHeight)
48 : base(Variables, DefaultWidth, DefaultHeight)
49 {
50 this.width = this.Settings.Width;
51 this.height = this.Settings.Height;
52 this.bgColor = this.Settings.BackgroundColor;
53 }
54
63 public CanvasGraph(Variables Variables, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
64 : this(Variables, Width, Height)
65 {
66 this.bgColor = BackgroundColor;
67 this.defaultColor = DefaultColor;
68 }
69
78 public CanvasGraph(GraphSettings Settings, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
79 : base(Settings, Width, Height)
80 {
81 this.width = this.Settings.Width;
82 this.height = this.Settings.Height;
83 this.bgColor = BackgroundColor;
84 this.defaultColor = DefaultColor;
85 }
86
90 public SKColor? BgColor => this.bgColor;
91
95 public new SKColor? DefaultColor => this.defaultColor;
96
100 public int Width => this.width;
101
105 public int Height => this.height;
106
110 public override bool UsesDefaultColor => this.defaultColor != SKColors.Empty;
111
117 public override bool TrySetDefaultColor(SKColor Color)
118 {
119 this.defaultColor = Color;
120 return true;
121 }
122
129 {
130 if (Element is CanvasGraph G)
131 {
132 CanvasGraph Result = new CanvasGraph(G.Settings, G.width, G.height, G.bgColor, G.defaultColor);
133
134 Result.operations.AddRange(G.operations);
135 Result.operations.AddRange(this.operations);
136
137 return Result;
138 }
139 else
140 return null;
141 }
142
149 {
150 if (Element is CanvasGraph G)
151 {
152 CanvasGraph Result = new CanvasGraph(this.Settings, this.width, this.height, this.bgColor, this.defaultColor);
153
154 Result.operations.AddRange(this.operations);
155 Result.operations.AddRange(G.operations);
156
157 return Result;
158 }
159 else
160 return null;
161 }
162
164 public override bool Equals(object obj)
165 {
166 if (!(obj is CanvasGraph G) ||
167 this.width != G.width ||
168 this.height != G.height ||
169 this.bgColor != G.bgColor ||
170 this.defaultColor != G.defaultColor)
171 {
172 return false;
173 }
174
175 IEnumerator<CanvasOperation> e1 = this.operations.GetEnumerator();
176 IEnumerator<CanvasOperation> e2 = G.operations.GetEnumerator();
177 bool b1, b2;
178
179 try
180 {
181 while (true)
182 {
183 b1 = e1.MoveNext();
184 b2 = e2.MoveNext();
185
186 if (b1 ^ b2)
187 return false;
188
189 if (!b1)
190 return true;
191
192 if (!e1.Current.Equals(e2.Current))
193 return false;
194 }
195 }
196 finally
197 {
198 e1.Dispose();
199 e2.Dispose();
200 }
201 }
202
204 public override int GetHashCode()
205 {
206 int Result = this.width.GetHashCode();
207 Result ^= Result << 5 ^ this.height.GetHashCode();
208 Result ^= Result << 5 ^ this.bgColor.GetHashCode();
209 Result ^= Result << 5 ^ this.defaultColor.GetHashCode();
210
211 foreach (CanvasOperation Op in this.operations)
212 Result ^= Result << 5 ^ Op.GetHashCode();
213
214 return Result;
215 }
216
224 public override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
225 {
226 using (SKSurface Surface = SKSurface.Create(new SKImageInfo(this.width, this.height, SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
227 {
228 SKCanvas Canvas = Surface.Canvas;
229 CanvasState State = new CanvasState()
230 {
231 FgColor = this.defaultColor ?? Graph.DefaultColor,
232 BgColor = this.bgColor ?? Settings.BackgroundColor
233 };
234
235 Canvas.Clear(State.BgColor);
236
237 foreach (CanvasOperation Op in this.operations)
238 Op.Draw(Canvas, State);
239
240 using (SKImage Result = Surface.Snapshot())
241 {
242 States = Array.Empty<object>();
243 return PixelInformation.FromImage(Result);
244 }
245 }
246 }
247
252 public override void ExportGraph(XmlWriter Output)
253 {
254 Output.WriteStartElement("CanvasGraph");
255 Output.WriteAttributeString("width", this.width.ToString());
256 Output.WriteAttributeString("height", this.height.ToString());
257
258 if (this.bgColor.HasValue)
259 Output.WriteAttributeString("bgColor", Expression.ToExpressionString(this.bgColor.Value));
260
261 if (this.defaultColor.HasValue)
262 Output.WriteAttributeString("defaultColor", Expression.ToExpressionString(this.defaultColor.Value));
263
264 foreach (CanvasOperation Op in this.operations)
265 {
266 Output.WriteStartElement(Op.GetType().Name);
267 Op.ExportGraph(Output);
268 Output.WriteEndElement();
269 }
270
271 Output.WriteEndElement();
272 }
273
278 public override async Task ImportGraphAsync(XmlElement Xml)
279 {
281
282 foreach (XmlAttribute Attr in Xml.Attributes)
283 {
284 switch (Attr.Name)
285 {
286 case "width":
287 this.width = int.Parse(Attr.Value);
288 break;
289
290 case "height":
291 this.height = int.Parse(Attr.Value);
292 break;
293
294 case "bgColor":
295 this.bgColor = Graph.ToColor((await ParseAsync(Attr.Value, Variables)).AssociatedObjectValue);
296 break;
297
298 case "defaultColor":
299 this.defaultColor = Graph.ToColor((await ParseAsync(Attr.Value, Variables)).AssociatedObjectValue);
300 break;
301 }
302 }
303
304 string OperationNamespace = typeof(CanvasOperation).Namespace + ".";
305
306 foreach (XmlNode N in Xml.ChildNodes)
307 {
308 if (N is XmlElement E)
309 {
310 Type T = Types.GetType(OperationNamespace + E.LocalName);
311 if (T is null)
312 continue;
313
314 if (!(Types.Instantiate(T) is CanvasOperation Op))
315 continue;
316
317 await Op.ImportGraph(E, Variables);
318
319 this.operations.Add(Op);
320 }
321 }
322 }
323
331 public override string GetBitmapClickScript(double X, double Y, object[] States)
332 {
333 return "[" + Expression.ToString(X) + "," + Expression.ToString(Y) + "]";
334 }
335
339 public void LineTo(double x, double y)
340 {
341 this.operations.Add(new LineTo((float)x, (float)y));
342 }
343
347 public void MoveTo(double x, double y)
348 {
349 this.operations.Add(new MoveTo((float)x, (float)y));
350 }
351
359 public void Line(double x1, double y1, double x2, double y2)
360 {
361 this.operations.Add(new Line((float)x1, (float)y1, (float)x2, (float)y2));
362 }
363
368 public void PenWidth(double Width)
369 {
370 this.operations.Add(new PenWidth((float)Width));
371 }
372
377 public void Color(SKColor Color)
378 {
379 this.operations.Add(new PenColor(Color));
380 }
381
386 public void Color(object Color)
387 {
388 this.operations.Add(new PenColor(Graph.ToColor(Color)));
389 }
390
398 public void Rectangle(double x1, double y1, double x2, double y2)
399 {
400 this.operations.Add(new Rectangle((float)x1, (float)y1, (float)x2, (float)y2));
401 }
402
410 public void FillRectangle(double x1, double y1, double x2, double y2)
411 {
412 this.operations.Add(new FillRectangle((float)x1, (float)y1, (float)x2, (float)y2));
413 }
414
420 public void Plot(double x, double y)
421 {
422 this.operations.Add(new Plot((float)x, (float)y));
423 }
424
432 public void Ellipse(double x1, double y1, double x2, double y2)
433 {
434 this.operations.Add(new Ellipse((float)x1, (float)y1, (float)x2, (float)y2));
435 }
436
444 public void FillEllipse(double x1, double y1, double x2, double y2)
445 {
446 this.operations.Add(new FillEllipse((float)x1, (float)y1, (float)x2, (float)y2));
447 }
448
452 public void Clear()
453 {
454 this.operations.Add(new Clear());
455 }
456
457 }
458}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void AddRange(IEnumerable< T > Collection)
Adds a range of elements (last) to the list.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static Type GetType(string FullName)
Gets a type, given its full name.
Definition: Types.cs:42
static object Instantiate(Type Type, params object[] Arguments)
Returns an instance of the type Type . If one needs to be created, it is. If the constructor requires...
Definition: Types.cs:1454
Base class for all types of elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
static string ToExpressionString(object Value)
Converts an object to a string, that can be parsed as part of an expression.
Definition: Expression.cs:5052
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:17
void Rectangle(double x1, double y1, double x2, double y2)
Draws a rectangle defined by two opposing corner points.
Definition: CanvasGraph.cs:398
void PenWidth(double Width)
Sets the current pen width.
Definition: CanvasGraph.cs:368
override bool UsesDefaultColor
If graph uses default color
Definition: CanvasGraph.cs:110
CanvasGraph(Variables Variables, int? DefaultWidth, int? DefaultHeight)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:47
void FillRectangle(double x1, double y1, double x2, double y2)
Fills a rectangle defined by two opposing corner points.
Definition: CanvasGraph.cs:410
void Color(SKColor Color)
Sets the current pen color.
Definition: CanvasGraph.cs:377
CanvasGraph(Variables Variables, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:63
void Color(object Color)
Sets the current pen color.
Definition: CanvasGraph.cs:386
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:331
SKColor? BgColor
Background color.
Definition: CanvasGraph.cs:90
override ISemiGroupElement AddRight(ISemiGroupElement Element)
Tries to add an element to the current element, from the right.
Definition: CanvasGraph.cs:148
CanvasGraph(GraphSettings Settings, int Width, int Height, SKColor? DefaultColor, SKColor? BackgroundColor)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:78
override void ExportGraph(XmlWriter Output)
Exports graph specifics to XML.
Definition: CanvasGraph.cs:252
void LineTo(double x, double y)
Draws a line to a specific coordinate
Definition: CanvasGraph.cs:339
void Clear()
Clears the canvas with the current brush.
Definition: CanvasGraph.cs:452
override bool TrySetDefaultColor(SKColor Color)
Tries to set the default color.
Definition: CanvasGraph.cs:117
void FillEllipse(double x1, double y1, double x2, double y2)
Fills a ellipse defined by two opposing corner points.
Definition: CanvasGraph.cs:444
override int GetHashCode()
Calculates a hash code of the element. Hash code.
Definition: CanvasGraph.cs:204
override bool Equals(object obj)
Compares the element to another. If elements are equal.
Definition: CanvasGraph.cs:164
override ISemiGroupElement AddLeft(ISemiGroupElement Element)
Tries to add an element to the current element, from the left.
Definition: CanvasGraph.cs:128
CanvasGraph(Variables Variables)
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:36
void Ellipse(double x1, double y1, double x2, double y2)
Draws a ellipse defined by two opposing corner points.
Definition: CanvasGraph.cs:432
void Line(double x1, double y1, double x2, double y2)
Draws a line between two points.
Definition: CanvasGraph.cs:359
void MoveTo(double x, double y)
Draws a line to a specific coordinate
Definition: CanvasGraph.cs:347
override PixelInformation CreatePixels(GraphSettings Settings, out object[] States)
Creates a bitmap of the graph.
Definition: CanvasGraph.cs:224
CanvasGraph()
Canvas graph. Permits custom drawing from scrupt.
Definition: CanvasGraph.cs:27
new? SKColor DefaultColor
Default color.
Definition: CanvasGraph.cs:95
override async Task ImportGraphAsync(XmlElement Xml)
Imports graph specifics from XML.
Definition: CanvasGraph.cs:278
void Plot(double x, double y)
Plots a pixel at a given coordinate.
Definition: CanvasGraph.cs:420
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:88
static readonly SKColor DefaultColor
Default color: Red
Definition: Graph.cs:127
GraphSettings Settings
Graph settings available during creation.
Definition: Graph.cs:205
static SKColor ToColor(object Object)
Converts an object to a color.
Definition: Graph.cs:844
static async Task< IElement > ParseAsync(string s, Variables Variables)
Parses an element expression string.
Definition: Graph.cs:1602
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.