Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PolyLine.cs
1using System.Threading.Tasks;
2using System.Xml;
3using SkiaSharp;
6
8{
13 {
14 private StringAttribute head;
15 private StringAttribute tail;
16
22 public PolyLine(Layout2DDocument Document, ILayoutElement Parent)
23 : base(Document, Parent)
24 {
25 }
26
30 public override string LocalName => "PolyLine";
31
36 {
37 get => this.head;
38 set => this.head = value;
39 }
40
45 {
46 get => this.tail;
47 set => this.tail = value;
48 }
49
54 public override Task FromXml(XmlElement Input)
55 {
56 this.head = new StringAttribute(Input, "head", this.Document);
57 this.tail = new StringAttribute(Input, "tail", this.Document);
58
59 return base.FromXml(Input);
60 }
61
66 public override void ExportAttributes(XmlWriter Output)
67 {
68 base.ExportAttributes(Output);
69
70 this.head?.Export(Output);
71 this.tail?.Export(Output);
72 }
73
80 public override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
81 {
82 return new PolyLine(Document, Parent);
83 }
84
89 public override void CopyContents(ILayoutElement Destination)
90 {
91 base.CopyContents(Destination);
92
93 if (Destination is PolyLine Dest)
94 {
95 Dest.head = this.head?.CopyIfNotPreset(Destination.Document);
96 Dest.tail = this.tail?.CopyIfNotPreset(Destination.Document);
97 }
98 }
99
105 public override async Task DoMeasureDimensions(DrawingState State)
106 {
107 await base.DoMeasureDimensions(State);
108
109 EvaluationResult<string> RefId = await this.head.TryEvaluate(State.Session);
110 if (RefId.Ok &&
111 this.Document.TryGetElement(RefId.Result, out ILayoutElement Element) &&
112 Element is Shape Shape)
113 {
114 this.headElement = Shape;
115 }
116
117 RefId = await this.tail.TryEvaluate(State.Session);
118 if (RefId.Ok &&
119 this.Document.TryGetElement(RefId.Result, out Element) &&
120 Element is Shape Shape2)
121 {
122 this.tailElement = Shape2;
123 }
124 }
125
126 private Shape headElement;
127 private Shape tailElement;
128
133 public override async Task Draw(DrawingState State)
134 {
135 if (this.defined)
136 {
137 SKPaint Pen = await this.GetPen(State);
138
139 using (SKPath Path = new SKPath())
140 {
141 bool First = true;
142
143 foreach (Vertex V in this.points)
144 {
145 if (First)
146 {
147 Path.MoveTo(V.XCoordinate, V.YCoordinate);
148 First = false;
149 }
150 else
151 Path.LineTo(V.XCoordinate, V.YCoordinate);
152 }
153
154 State.Canvas.DrawPath(Path, Pen);
155 }
156
157 if (!(this.tailElement is null) || !(this.headElement is null))
158 {
159 SKPaint Fill = await this.TryGetFill(State);
160 if (!(Fill is null))
161 Fill = null;
162
163 this.tailElement?.DrawTail(State, this, Pen, Fill);
164 this.headElement?.DrawHead(State, this, Pen, Fill);
165 }
166 }
167
168 await base.Draw(State);
169 }
170
178 public bool TryGetStart(out float X, out float Y, out float Direction)
179 {
180 if (!this.defined || this.points is null || this.points.Length < 2)
181 {
182 X = Y = Direction = 0;
183 return false;
184 }
185
186 Vertex P1 = this.points[0];
187 Vertex P2 = this.points[1];
188
189 X = P1.XCoordinate;
190 Y = P1.YCoordinate;
191 Direction = CalcDirection(P1, P2);
192
193 return true;
194 }
195
203 public bool TryGetEnd(out float X, out float Y, out float Direction)
204 {
205 int c;
206
207 if (!this.defined || this.points is null || (c = this.points.Length) < 2)
208 {
209 X = Y = Direction = 0;
210 return false;
211 }
212
213 Vertex P1 = this.points[c - 2];
214 Vertex P2 = this.points[c - 1];
215
216 X = P2.XCoordinate;
217 Y = P2.YCoordinate;
218 Direction = CalcDirection(P1, P2);
219
220 return true;
221 }
222
227 public override void ExportStateAttributes(XmlWriter Output)
228 {
229 base.ExportStateAttributes(Output);
230
231 this.head?.ExportState(Output);
232 this.tail?.ExportState(Output);
233 }
234
235 }
236}
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
StringAttribute CopyIfNotPreset(Layout2DDocument ForDocument)
Copies the attribute object if undefined, or defined by an expression. Returns a reference to itself,...
SKCanvas Canvas
Current drawing canvas.
Variables Session
Current session.
Definition: DrawingState.cs:91
async Task< SKPaint > TryGetFill(DrawingState State)
Tries to get the filling of the figure, if one is defined.
Definition: Figure.cs:130
async Task< SKPaint > GetPen(DrawingState State)
Gets the pen associated with the element. If not found, the default pen is returned.
Definition: Figure.cs:90
override void ExportStateAttributes(XmlWriter Output)
Exports the local attributes of the current element.
Definition: PolyLine.cs:227
bool TryGetEnd(out float X, out float Y, out float Direction)
Tries to get end position and terminating direction.
Definition: PolyLine.cs:203
override string LocalName
Local name of type of element.
Definition: PolyLine.cs:30
override async Task Draw(DrawingState State)
Draws layout entities.
Definition: PolyLine.cs:133
bool TryGetStart(out float X, out float Y, out float Direction)
Tries to get start position and initial direction.
Definition: PolyLine.cs:178
override void ExportAttributes(XmlWriter Output)
Exports attributes to XML.
Definition: PolyLine.cs:66
override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
Creates a new instance of the layout element.
Definition: PolyLine.cs:80
PolyLine(Layout2DDocument Document, ILayoutElement Parent)
A poly-line
Definition: PolyLine.cs:22
override async Task DoMeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions.
Definition: PolyLine.cs:105
override Task FromXml(XmlElement Input)
Populates the element (including children) with information from its XML definition.
Definition: PolyLine.cs:54
override void CopyContents(ILayoutElement Destination)
Copies contents (attributes and children) to the destination element.
Definition: PolyLine.cs:89
Layout2DDocument Document
Layout document.
bool defined
If element is well-defined.
static float CalcDirection(float x1, float y1, float x2, float y2)
Calculates the direction of the pen when drawing from (x1,y1) to (x2,y2).
Defines a shape for use elsewhere in the layout.
Definition: Shape.cs:11
Task DrawTail(DrawingState State, IDirectedElement DirectedElement, SKPaint DefaultPen, SKPaint DefaultFill)
Draws shape as a tail
Definition: Shape.cs:98
Task DrawHead(DrawingState State, IDirectedElement DirectedElement, SKPaint DefaultPen, SKPaint DefaultFill)
Draws shape as a head
Definition: Shape.cs:114
Defines a vertex in the graf
Definition: Vertex.cs:7
float YCoordinate
Measured Y-coordinate
Definition: Vertex.cs:42
float XCoordinate
Measured X-coordinate
Definition: Vertex.cs:37
Interface for directed elements.
Base interface for all layout elements.
Layout2DDocument Document
Layout document.