Skip to content

Commit 2434568

Browse files
committed
resolve #27
1 parent 9793ef4 commit 2434568

File tree

3 files changed

+97
-15
lines changed

3 files changed

+97
-15
lines changed

SimpleStateMachineNodeEditor/Helpers/Extensions/PointExtensition.cs

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System;
1+
using ReactiveUI;
2+
using System;
23
using System.Collections.Generic;
4+
using System.Globalization;
35
using System.Runtime.CompilerServices;
46
using System.Text;
57
using System.Windows;
@@ -321,13 +323,33 @@ public static Point Multiply(this Point point1, Size size)
321323

322324
public static string PointToString(Point point)
323325
{
324-
return string.Format("{0}, {1}", point.X.ToString(System.Globalization.CultureInfo.InvariantCulture), point.Y.ToString(System.Globalization.CultureInfo.InvariantCulture));
326+
return string.Format("{0}, {1}", point.X.ToString(CultureInfo.InvariantCulture), point.Y.ToString(CultureInfo.InvariantCulture));
325327
}
326328

327-
public static Point StringToPoint(string str)
329+
public static bool TryParseFromString(string str, out Point point)
328330
{
331+
point = default(Point);
329332
string[] parts = str.Split(",");
330-
return new Point(double.Parse(parts[0], System.Globalization.CultureInfo.InvariantCulture), double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture));
333+
if (parts.Length < 2)
334+
return false;
335+
336+
double x, y;
337+
338+
if (!double.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out x))
339+
{
340+
return false;
341+
}
342+
343+
if (!double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out y))
344+
{
345+
return false;
346+
}
347+
348+
point = new Point(x, y);
349+
350+
return true;
351+
//return new Point(double.Parse(parts[0], System.Globalization.CultureInfo.InvariantCulture), double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture));
331352
}
353+
332354
}
333355
}

SimpleStateMachineNodeEditor/ViewModel/Node/NodeViewModel.cs

+7-11
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private NodeViewModel()
4848
}
4949

5050

51-
public NodeViewModel(NodesCanvasViewModel nodesCanvas, string name, Point point)
51+
public NodeViewModel(NodesCanvasViewModel nodesCanvas, string name, Point point = default(Point))
5252
{
5353
NodesCanvas = nodesCanvas;
5454
Name = name;
@@ -128,11 +128,15 @@ public XElement ToXElement()
128128
{
129129
XElement element = new XElement("State");
130130
element.Add(new XAttribute("Name", Name));
131+
return element;
132+
}
133+
public XElement ToVisualizationXElement()
134+
{
135+
XElement element = ToXElement();
131136
element.Add(new XAttribute("Position", PointExtensition.PointToString(Point1)));
132137
element.Add(new XAttribute("IsCollapse", IsCollapse.ToString()));
133138
return element;
134139
}
135-
136140
public static NodeViewModel FromXElement(NodesCanvasViewModel nodesCanvas, XElement node, out string errorMessage, Func<string, bool> actionForCheck)
137141
{
138142
errorMessage = null;
@@ -151,15 +155,7 @@ public static NodeViewModel FromXElement(NodesCanvasViewModel nodesCanvas, XElem
151155
return viewModelNode;
152156
}
153157

154-
var position = node.Attribute("Position")?.Value;
155-
Point point = string.IsNullOrEmpty(position) ? new Point() : PointExtensition.StringToPoint(position);
156-
viewModelNode = new NodeViewModel(nodesCanvas, name, point);
157-
var isCollapse = node.Attribute("IsCollapse")?.Value;
158-
if (isCollapse != null)
159-
viewModelNode.IsCollapse = bool.Parse(isCollapse);
160-
161-
162-
158+
viewModelNode = new NodeViewModel(nodesCanvas, name);
163159

164160
return viewModelNode;
165161
}

SimpleStateMachineNodeEditor/ViewModel/NodesCanvas/NodesCanvasCommandsViewModel.cs

+64
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,61 @@ private void Open()
389389
SchemePath = fileName;
390390

391391
#endregion setup Transitions/connects
392+
393+
#region setup Visualization
394+
XElement Visualization = stateMachineXElement.Element("Visualization");
395+
396+
397+
if (Visualization != null)
398+
{
399+
var visualizationStates = Visualization.Elements()?.ToList();
400+
if(visualizationStates!=null)
401+
{
402+
var nodes = this.Nodes.Items.ToDictionary(x => x.Name, x => x);
403+
Point point;
404+
bool isCollapse;
405+
string name;
406+
string pointAttribute;
407+
string isCollapseAttribute;
408+
foreach (var visualization in visualizationStates)
409+
{
410+
name = visualization.Attribute("Name")?.Value;
411+
if(nodes.TryGetValue(name, out NodeViewModel node))
412+
{
413+
pointAttribute = visualization.Attribute("Position")?.Value;
414+
if (!PointExtensition.TryParseFromString(pointAttribute, out point))
415+
{
416+
Error(String.Format("Error parse attribute \'position\' for state with name \'{0}\'", name));
417+
return;
418+
}
419+
isCollapseAttribute = visualization.Attribute("IsCollapse")?.Value;
420+
if (!bool.TryParse(isCollapseAttribute, out isCollapse))
421+
{
422+
Error(String.Format("Error parse attribute \'isCollapse\' for state with name \'{0}\'", name));
423+
return;
424+
}
425+
node.Point1 = point;
426+
node.IsCollapse = isCollapse;
427+
}
428+
else
429+
{
430+
Error(String.Format("Visualization for state with name \'{0}\' that not exist", name));
431+
return;
432+
}
433+
}
434+
}
435+
436+
437+
//NodeViewModel nodeViewModel = Nodes.w
438+
//var position = node.Attribute("Position")?.Value;
439+
//Point point = string.IsNullOrEmpty(position) ? new Point() : PointExtensition.StringToPoint(position);
440+
//var isCollapse = node.Attribute("IsCollapse")?.Value;
441+
//if (isCollapse != null)
442+
// viewModelNode.IsCollapse = bool.Parse(isCollapse);
443+
444+
}
445+
#endregion setup Visualization
446+
392447
Mouse.OverrideCursor = null;
393448
WithoutMessages = false;
394449
LogDebug("Scheme was loaded from file \"{0}\"", SchemePath);
@@ -471,6 +526,15 @@ private void Save(string fileName)
471526
transitions.Add(transition.ToXElement());
472527
}
473528

529+
530+
XElement visualizationXElement = new XElement("Visualization");
531+
stateMachineXElement.Add(visualizationXElement);
532+
foreach (var state in Nodes.Items)
533+
{
534+
visualizationXElement.Add(state.ToVisualizationXElement());
535+
}
536+
537+
474538
xDocument.Save(fileName);
475539
ItSaved = true;
476540
SchemePath = fileName;

0 commit comments

Comments
 (0)