diff --git a/SimpleStateMachineNodeEditor/Helpers/Extensions/PointExtensition.cs b/SimpleStateMachineNodeEditor/Helpers/Extensions/PointExtensition.cs index 069a08c..d482e30 100644 --- a/SimpleStateMachineNodeEditor/Helpers/Extensions/PointExtensition.cs +++ b/SimpleStateMachineNodeEditor/Helpers/Extensions/PointExtensition.cs @@ -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; @@ -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)); } + } } diff --git a/SimpleStateMachineNodeEditor/ViewModel/Node/NodeViewModel.cs b/SimpleStateMachineNodeEditor/ViewModel/Node/NodeViewModel.cs index ffd032c..0806a56 100644 --- a/SimpleStateMachineNodeEditor/ViewModel/Node/NodeViewModel.cs +++ b/SimpleStateMachineNodeEditor/ViewModel/Node/NodeViewModel.cs @@ -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; @@ -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 actionForCheck) { errorMessage = null; @@ -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; } diff --git a/SimpleStateMachineNodeEditor/ViewModel/NodesCanvas/NodesCanvasCommandsViewModel.cs b/SimpleStateMachineNodeEditor/ViewModel/NodesCanvas/NodesCanvasCommandsViewModel.cs index 63020c8..c252718 100644 --- a/SimpleStateMachineNodeEditor/ViewModel/NodesCanvas/NodesCanvasCommandsViewModel.cs +++ b/SimpleStateMachineNodeEditor/ViewModel/NodesCanvas/NodesCanvasCommandsViewModel.cs @@ -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); @@ -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;