Skip to content

Commit

Permalink
resolve #27
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanSoloweow committed Jul 8, 2020
1 parent 9793ef4 commit 2434568
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
Expand Down Expand Up @@ -321,13 +323,33 @@ public static Point Multiply(this Point point1, Size size)

public static string PointToString(Point point)
{
return string.Format("{0}, {1}", point.X.ToString(System.Globalization.CultureInfo.InvariantCulture), point.Y.ToString(System.Globalization.CultureInfo.InvariantCulture));
return string.Format("{0}, {1}", point.X.ToString(CultureInfo.InvariantCulture), point.Y.ToString(CultureInfo.InvariantCulture));
}

public static Point StringToPoint(string str)
public static bool TryParseFromString(string str, out Point point)
{
point = default(Point);
string[] parts = str.Split(",");
return new Point(double.Parse(parts[0], System.Globalization.CultureInfo.InvariantCulture), double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture));
if (parts.Length < 2)
return false;

double x, y;

if (!double.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out x))
{
return false;
}

if (!double.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out y))
{
return false;
}

point = new Point(x, y);

return true;
//return new Point(double.Parse(parts[0], System.Globalization.CultureInfo.InvariantCulture), double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture));
}

}
}
18 changes: 7 additions & 11 deletions SimpleStateMachineNodeEditor/ViewModel/Node/NodeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private NodeViewModel()
}


public NodeViewModel(NodesCanvasViewModel nodesCanvas, string name, Point point)
public NodeViewModel(NodesCanvasViewModel nodesCanvas, string name, Point point = default(Point))
{
NodesCanvas = nodesCanvas;
Name = name;
Expand Down Expand Up @@ -128,11 +128,15 @@ public XElement ToXElement()
{
XElement element = new XElement("State");
element.Add(new XAttribute("Name", Name));
return element;
}
public XElement ToVisualizationXElement()
{
XElement element = ToXElement();
element.Add(new XAttribute("Position", PointExtensition.PointToString(Point1)));
element.Add(new XAttribute("IsCollapse", IsCollapse.ToString()));
return element;
}

public static NodeViewModel FromXElement(NodesCanvasViewModel nodesCanvas, XElement node, out string errorMessage, Func<string, bool> actionForCheck)
{
errorMessage = null;
Expand All @@ -151,15 +155,7 @@ public static NodeViewModel FromXElement(NodesCanvasViewModel nodesCanvas, XElem
return viewModelNode;
}

var position = node.Attribute("Position")?.Value;
Point point = string.IsNullOrEmpty(position) ? new Point() : PointExtensition.StringToPoint(position);
viewModelNode = new NodeViewModel(nodesCanvas, name, point);
var isCollapse = node.Attribute("IsCollapse")?.Value;
if (isCollapse != null)
viewModelNode.IsCollapse = bool.Parse(isCollapse);



viewModelNode = new NodeViewModel(nodesCanvas, name);

return viewModelNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,61 @@ private void Open()
SchemePath = fileName;

#endregion setup Transitions/connects

#region setup Visualization
XElement Visualization = stateMachineXElement.Element("Visualization");


if (Visualization != null)
{
var visualizationStates = Visualization.Elements()?.ToList();
if(visualizationStates!=null)
{
var nodes = this.Nodes.Items.ToDictionary(x => x.Name, x => x);
Point point;
bool isCollapse;
string name;
string pointAttribute;
string isCollapseAttribute;
foreach (var visualization in visualizationStates)
{
name = visualization.Attribute("Name")?.Value;
if(nodes.TryGetValue(name, out NodeViewModel node))
{
pointAttribute = visualization.Attribute("Position")?.Value;
if (!PointExtensition.TryParseFromString(pointAttribute, out point))
{
Error(String.Format("Error parse attribute \'position\' for state with name \'{0}\'", name));
return;
}
isCollapseAttribute = visualization.Attribute("IsCollapse")?.Value;
if (!bool.TryParse(isCollapseAttribute, out isCollapse))
{
Error(String.Format("Error parse attribute \'isCollapse\' for state with name \'{0}\'", name));
return;
}
node.Point1 = point;
node.IsCollapse = isCollapse;
}
else
{
Error(String.Format("Visualization for state with name \'{0}\' that not exist", name));
return;
}
}
}


//NodeViewModel nodeViewModel = Nodes.w
//var position = node.Attribute("Position")?.Value;
//Point point = string.IsNullOrEmpty(position) ? new Point() : PointExtensition.StringToPoint(position);
//var isCollapse = node.Attribute("IsCollapse")?.Value;
//if (isCollapse != null)
// viewModelNode.IsCollapse = bool.Parse(isCollapse);

}
#endregion setup Visualization

Mouse.OverrideCursor = null;
WithoutMessages = false;
LogDebug("Scheme was loaded from file \"{0}\"", SchemePath);
Expand Down Expand Up @@ -471,6 +526,15 @@ private void Save(string fileName)
transitions.Add(transition.ToXElement());
}


XElement visualizationXElement = new XElement("Visualization");
stateMachineXElement.Add(visualizationXElement);
foreach (var state in Nodes.Items)
{
visualizationXElement.Add(state.ToVisualizationXElement());
}


xDocument.Save(fileName);
ItSaved = true;
SchemePath = fileName;
Expand Down

0 comments on commit 2434568

Please sign in to comment.