Skip to content

Commit d0a8f8f

Browse files
committed
bix optimaze
1 parent a584fec commit d0a8f8f

File tree

11 files changed

+83
-71
lines changed

11 files changed

+83
-71
lines changed

SimpleStateMachineNodeEditor/Helpers/MyPoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void Mirror(bool onX = true, bool onY = true)
5656

5757
public MyPoint Add(MyPoint point)
5858
{
59-
Value = new Point(point.X, point.Y);
59+
Value = new Point(this.X + point.X, this.Y + point.Y);
6060
return this;
6161
}
6262

SimpleStateMachineNodeEditor/View/ViewConnect.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void SetupBinding()
6161

6262
this.OneWayBind(this.ViewModel, x => x.StrokeDashArray, x => x.PathElement.StrokeDashArray).DisposeWith(disposable);
6363

64-
this.OneWayBind(this.ViewModel, x => x.StrokeThickness, x => x.PathElement.StrokeThickness).DisposeWith(disposable);
64+
this.OneWayBind(this.ViewModel, x => x.FromConnector.NodesCanvas.Scale.Value, x => x.PathElement.StrokeThickness).DisposeWith(disposable);
6565

6666
this.WhenAnyValue(x => x.ViewModel.ToConnector).Where(x=>x!=null).Subscribe(_ => UpdateZindex()).DisposeWith(disposable);
6767

SimpleStateMachineNodeEditor/View/ViewLeftConnector.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ private void SetupBinding()
7171
// Отображается ли переход
7272
this.OneWayBind(this.ViewModel, x => x.Visible, x => x.LeftConnector.Visibility).DisposeWith(disposable);
7373

74-
// При изменении размера, позиции или zoom узла
75-
this.WhenAnyValue(x => x.ViewModel.Node.Size, x => x.ViewModel.Node.Point1.Value, x => x.ViewModel.Node.NodesCanvas.Scale.Scales.Value)
76-
.Subscribe(_ => UpdatePosition()).DisposeWith(disposable);
74+
//// При изменении размера, позиции или zoom узла
75+
//this.WhenAnyValue(x => x.ViewModel.Node.Size, x => x.ViewModel.Node.Point1.Value, x => x.ViewModel.Node.NodesCanvas.Scale.Scales.Value)
76+
//.Subscribe(_ => UpdatePosition()).DisposeWith(disposable);
7777

7878

7979
});

SimpleStateMachineNodeEditor/View/ViewNode.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,11 @@ private void SetupBinding()
8080
this.OneWayBind(this.ViewModel, x => x.Output, x => x.Output.ViewModel).DisposeWith(disposable);
8181

8282
this.OneWayBind(this.ViewModel, x => x.Transitions, x => x.ItemsControlTransitions.ItemsSource).DisposeWith(disposable);
83-
84-
8583

8684
});
8785
}
8886
#endregion Setup Binding
87+
8988
#region Setup Commands
9089
private void SetupCommands()
9190
{
@@ -95,6 +94,7 @@ private void SetupCommands()
9594
});
9695
}
9796
#endregion Setup Commands
97+
9898
#region Setup Events
9999
private void SetupEvents()
100100
{

SimpleStateMachineNodeEditor/View/ViewRightConnector.xaml.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ public ViewRightConnector()
4848
SetupBinding();
4949
SetupEvents();
5050
SetupSubcriptions();
51+
52+
5153
}
5254

5355
#region SetupBinding
5456
private void SetupBinding()
5557
{
5658
this.WhenActivated(disposable =>
5759
{
60+
5861
Canvas.SetZIndex((UIElement)this.VisualParent, this.ViewModel.Node.Zindex+2);
5962

6063
this.OneWayBind(this.ViewModel, x => x.Visible, x => x.RightConnector.Visibility).DisposeWith(disposable);
@@ -83,8 +86,9 @@ private void SetupSubcriptions()
8386
{
8487
this.WhenActivated(disposable =>
8588
{
86-
this.WhenAnyValue(x => x.ViewModel.Node.Size, x => x.ViewModel.Node.Point1.Value, x => x.ViewModel.Node.NodesCanvas.Scale.Scales.Value)
87-
.Subscribe(_ => UpdatePositionConnectPoin()).DisposeWith(disposable);
89+
90+
//this.WhenAnyValue(x => x.ViewModel.Node.Size, x => x.ViewModel.Node.Point1.Value, x => x.ViewModel.Node.NodesCanvas.Scale.Scales.Value)
91+
// .Subscribe(_ => UpdatePositionConnectPoin()).DisposeWith(disposable);
8892
this.WhenAnyValue(x => x.EllipseElement.IsMouseOver).Subscribe(value => OnEventMouseOver(value)).DisposeWith(disposable);
8993
});
9094
}
@@ -186,7 +190,7 @@ private void ConnectorDrop(DragEventArgs e)
186190
#endregion SetupEvents
187191

188192

189-
private void UpdatePositionConnectPoin()
193+
private void UpdatePosition()
190194
{
191195
Point positionConnectPoint;
192196
MyPoint Position;

SimpleStateMachineNodeEditor/ViewModel/Connect/ViewModelConnect.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,42 @@ public class ViewModelConnect : ReactiveObject
3333

3434
[Reactive] public double StrokeThickness { get; set; } = 1;
3535

36+
private IDisposable subscriptionOnConnectorPositionChange;
37+
private IDisposable subscriptionOnOutputPositionChange;
38+
3639
public ViewModelConnect(ViewModelNodesCanvas viewModelNodesCanvas, ViewModelConnector fromConnector)
3740
{
38-
SetupSubscriptions();
3941
Initial(viewModelNodesCanvas, fromConnector);
40-
42+
SetupSubscriptions();
4143
}
4244
#region Setup Subscriptions
4345

4446
private void SetupSubscriptions()
4547
{
4648
this.WhenAnyValue(x => x.StartPoint.Value, x => x.EndPoint.Value).Subscribe(_ => UpdateMedium());
47-
this.WhenAnyValue(x => x.FromConnector.PositionConnectPoint.Value).Subscribe(value => StartPointUpdate(value));
48-
this.WhenAnyValue(x => x.ToConnector.PositionConnectPoint.Value).Subscribe(value => EndPointUpdate(value));
49-
this.WhenAnyValue(x => x.ToConnector).Where(x => x != null).Subscribe(_ => ToConnectChanged());
50-
this.WhenAnyValue(x => x.FromConnector.Node.NodesCanvas.Scale.Value).Subscribe(value => StrokeThickness = value);
49+
this.WhenAnyValue(x => x.FromConnector.Node.IsCollapse).Subscribe(value => UpdateSubscriptionForPosition(value));
50+
this.WhenAnyValue(x => x.ToConnector.PositionConnectPoint.Value).Subscribe(value => EndPointUpdate(value));
51+
this.WhenAnyValue(x => x.FromConnector.Selected).Subscribe(value => Select(value));
52+
}
53+
private void UpdateSubscriptionForPosition(bool nodeIsCollapse)
54+
{
55+
if(!nodeIsCollapse)
56+
{
57+
subscriptionOnOutputPositionChange?.Dispose();
58+
subscriptionOnConnectorPositionChange = this.WhenAnyValue(x => x.FromConnector.PositionConnectPoint.Value).Subscribe(value => StartPointUpdate(value));
59+
60+
}
61+
else
62+
{
63+
subscriptionOnConnectorPositionChange?.Dispose();
64+
subscriptionOnOutputPositionChange = this.WhenAnyValue(x => x.FromConnector.Node.Output.PositionConnectPoint.Value).Subscribe(value => StartPointUpdate(value));
65+
}
5166
}
5267
private void Initial(ViewModelNodesCanvas viewModelNodesCanvas, ViewModelConnector fromConnector)
5368
{
5469
NodesCanvas = viewModelNodesCanvas;
5570
FromConnector = fromConnector;
56-
FromConnector.Connect = this;
57-
58-
StartPointUpdate((FromConnector.Node.IsCollapse ? FromConnector.Node.Output : FromConnector).PositionConnectPoint.ToPoint());
59-
this.WhenAnyValue(x => x.FromConnector.Selected).Subscribe(value => Select(value));
71+
FromConnector.Connect = this;
6072
}
6173
private void Select(bool value)
6274
{

SimpleStateMachineNodeEditor/ViewModel/Connector/ViewModelConnector.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
using SimpleStateMachineNodeEditor.Helpers.Extensions;
1212
using SimpleStateMachineNodeEditor.ViewModel.NodesCanvas;
1313
using SimpleStateMachineNodeEditor.ViewModel.Connect;
14+
using System.Reactive.Linq;
1415

1516
namespace SimpleStateMachineNodeEditor.ViewModel.Connector
1617
{
1718
public partial class ViewModelConnector : ReactiveObject
1819
{
19-
[Reactive] public MyPoint PositionConnectPoint { get; set; } = new MyPoint();
20+
[Reactive] public MyPoint PositionConnectPoint { get; set; }
2021
[Reactive] public string Name { get; set; }
2122
[Reactive] public bool TextEnable { get; set; } = false;
2223
[Reactive] public bool? Visible { get; set; } = true;
@@ -30,22 +31,24 @@ public partial class ViewModelConnector : ReactiveObject
3031
[Reactive] public bool ItsLoop { get; set; } = false;
3132
[Reactive] public ViewModelNodesCanvas NodesCanvas { get; set; }
3233
[Reactive] public bool Selected { get; set; }
33-
34-
public ViewModelConnector(ViewModelNodesCanvas nodesCanvas, ViewModelNode viewModelNode)
34+
public ViewModelConnector(ViewModelNodesCanvas nodesCanvas, ViewModelNode viewModelNode, string name, MyPoint myPoint)
3535
{
3636
Node = viewModelNode;
3737
NodesCanvas = nodesCanvas;
38+
Name = name;
39+
PositionConnectPoint = new MyPoint(myPoint);
3840
SetupCommands();
39-
SetupBinding();
41+
SetupSubscriptions();
4042
}
41-
42-
#region SetupBinding
43-
private void SetupBinding()
43+
#region Setup Subscriptions
44+
private void SetupSubscriptions()
4445
{
4546
this.WhenAnyValue(x => x.Selected).Subscribe(value => Select(value));
47+
this.WhenAnyValue(x => x.Node.Point1).Buffer(2, 1).Subscribe(value => PositionConnectPoint.Add(value[1] - value[0]));
48+
4649
}
47-
#endregion SetupBinding
4850

51+
#endregion Setup Subscriptions
4952
public XElement ToXElement()
5053
{
5154
XElement element = new XElement("Transition");

SimpleStateMachineNodeEditor/ViewModel/Connector/ViewModelConnectorCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ private void ConnectorDragEnter()
108108

109109
if ((indexFrom > -1) && (indexTo > -1) && (indexFrom < count) && (indexTo < count))
110110
{
111-
MyPoint positionTo = this.Node.Transitions[indexTo].PositionConnectPoint;
111+
MyPoint positionTo = this.Node.Transitions[indexTo].PositionConnectPoint.Copy();
112112
MyPoint position;
113113
//shift down
114114
if (indexTo > indexFrom)
115115
{
116116
for (int i = indexTo; i >= indexFrom + 1; i--)
117117
{
118-
position = this.Node.Transitions[i - 1].PositionConnectPoint;
118+
position = this.Node.Transitions[i - 1].PositionConnectPoint.Copy();
119119
this.Node.Transitions[i].PositionConnectPoint.Set(position);
120120
}
121121
}
@@ -124,7 +124,7 @@ private void ConnectorDragEnter()
124124
{
125125
for (int i = indexTo; i <= indexFrom - 1; i++)
126126
{
127-
position = this.Node.Transitions[i + 1].PositionConnectPoint;
127+
position = this.Node.Transitions[i + 1].PositionConnectPoint.Copy();
128128
this.Node.Transitions[i].PositionConnectPoint.Set(position);
129129
}
130130
}

SimpleStateMachineNodeEditor/ViewModel/Node/ViewModelNode.cs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
using SimpleStateMachineNodeEditor.Helpers.Extensions;
1919
using SimpleStateMachineNodeEditor.ViewModel.NodesCanvas;
2020
using SimpleStateMachineNodeEditor.ViewModel.Connector;
21+
using System.Windows.Documents;
22+
using System.Collections.Generic;
2123

2224
namespace SimpleStateMachineNodeEditor.ViewModel
2325
{
2426
public class ViewModelNode : ReactiveValidationObject<ViewModelNode>
2527
{
26-
[Reactive] public MyPoint Point1 { get; set; } = new MyPoint();
28+
[Reactive] public MyPoint Point1 { get; set; }
2729
[Reactive] public MyPoint Point2 { get; set; } = new MyPoint();
2830
[Reactive] public Size Size { get; set; }
2931
[Reactive] public string Name { get; set; }
@@ -50,11 +52,12 @@ private ViewModelNode()
5052
}
5153

5254

53-
public ViewModelNode(ViewModelNodesCanvas nodesCanvas)
55+
public ViewModelNode(ViewModelNodesCanvas nodesCanvas, string name, MyPoint point)
5456
{
5557
NodesCanvas = nodesCanvas;
58+
Name = name;
5659
Zindex = nodesCanvas.Nodes.Count;
57-
60+
Point1 = point;
5861
SetupConnectors();
5962
SetupCommands();
6063
SetupBinding();
@@ -73,21 +76,16 @@ private void SetupSubscriptions()
7376
{
7477
this.WhenAnyValue(x => x.Selected).Subscribe(value => { this.BorderBrush = value ? Application.Current.Resources["ColorSelectedElement"] as SolidColorBrush : Brushes.LightGray; });
7578
this.WhenAnyValue(x => x.Transitions.Count).Buffer(2, 1).Select(x => (Previous: x[0], Current: x[1])).Subscribe(x => UpdateCount(x.Previous, x.Current));
76-
7779
this.WhenAnyValue(x => x.Point1.Value, x => x.Size).Subscribe(_ => UpdatePoint2());
7880
this.WhenAnyValue(x => x.IsCollapse).Subscribe(value => Collapse(value));
7981
}
8082
#endregion Setup Subscriptions
8183
#region Connectors
8284
private void SetupConnectors()
8385
{
84-
Input = new ViewModelConnector(NodesCanvas, this)
85-
{
86-
Name = "Input"
87-
};
88-
Output = new ViewModelConnector(NodesCanvas, this)
86+
Input = new ViewModelConnector(NodesCanvas, this, "Input", new MyPoint(Point1.X, Point1.Y + 30));
87+
Output = new ViewModelConnector(NodesCanvas, this, "Output", new MyPoint(Point1.X + 80, Point1.Y + 54))
8988
{
90-
Name = "Output",
9189
Visible = null
9290
};
9391
AddEmptyConnector();
@@ -132,6 +130,7 @@ private void NotSaved()
132130
{
133131
NodesCanvas.ItSaved = false;
134132
}
133+
135134
private void UpdateCount(int oldValue, int newValue)
136135
{
137136
if (newValue > oldValue)
@@ -192,7 +191,8 @@ private void Select(SelectMode selectMode)
192191
}
193192
private void Move(MyPoint delta)
194193
{
195-
Point1 += delta / NodesCanvas.Scale.Value;
194+
MyPoint moveValue = delta / NodesCanvas.Scale.Value;
195+
Point1 += moveValue;
196196
}
197197
private void ValidateName(string newName)
198198
{
@@ -210,15 +210,23 @@ private void AddEmptyConnector()
210210
{
211211
CurrentConnector.TextEnable = true;
212212
CurrentConnector.FormEnable = false;
213+
ShiftConnectors();
213214
if (string.IsNullOrEmpty(CurrentConnector.Name))
214215
CurrentConnector.Name = "Transition " + NodesCanvas.TransitionsCount.ToString();
215216
}
216-
CurrentConnector = new ViewModelConnector(NodesCanvas, this)
217+
CurrentConnector = new ViewModelConnector(NodesCanvas, this,"", new MyPoint(Point1.X + 80 , Point1.Y + 54))
217218
{
218219
TextEnable = false
219220
};
220221
Transitions.Insert(0, CurrentConnector);
221222
}
223+
private void ShiftConnectors()
224+
{
225+
foreach(var transition in Transitions)
226+
{
227+
transition.PositionConnectPoint.Add(0, 19);
228+
}
229+
}
222230
private void UnSelectedAllConnectors()
223231
{
224232
foreach(var transition in Transitions)
@@ -275,17 +283,16 @@ public static ViewModelNode FromXElement(ViewModelNodesCanvas nodesCanvas, XElem
275283
return viewModelNode;
276284
}
277285

278-
viewModelNode = new ViewModelNode(nodesCanvas);
279-
viewModelNode.Name = name;
280-
281-
var position = node.Attribute("Position")?.Value;
282-
if (position != null)
283-
viewModelNode.Point1 = MyPoint.Parse(position);
284-
286+
var position = node.Attribute("Position")?.Value;
287+
MyPoint point = string.IsNullOrEmpty(position) ? new MyPoint() : MyPoint.Parse(position);
288+
viewModelNode = new ViewModelNode(nodesCanvas, name, point);
285289
var isCollapse = node.Attribute("IsCollapse")?.Value;
286290
if (isCollapse != null)
287291
viewModelNode.IsCollapse = bool.Parse(isCollapse);
288292

293+
294+
295+
289296
return viewModelNode;
290297
}
291298
}

SimpleStateMachineNodeEditor/ViewModel/NodesCanvas/ViewModelNodesCanvas.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Splat;
1212
using SimpleStateMachineNodeEditor.ViewModel.Connector;
1313
using SimpleStateMachineNodeEditor.ViewModel.Connect;
14+
using SimpleStateMachineNodeEditor.Helpers;
1415

1516
namespace SimpleStateMachineNodeEditor.ViewModel.NodesCanvas
1617
{
@@ -60,29 +61,18 @@ private void SetupSubscriptions()
6061
{
6162
this.WhenAnyValue(x => x.Nodes.Count).Buffer(2, 1).Select(x => (Previous: x[0], Current: x[1])).Subscribe(x => UpdateCount(x.Previous, x.Current));
6263
}
64+
6365
#endregion Setup Subscriptions
66+
6467
#region Setup Nodes
6568

6669
private void SetupStartState()
6770
{
6871
string name = Nodes.Any(x => x.Name == "Start") ? GetNameForNewNode() : "Start";
69-
StartState = new ViewModelNode(this)
70-
{
71-
Name = name
72-
};
72+
StartState = new ViewModelNode(this, name, new MyPoint());
7373
SetAsStart(StartState);
7474
Nodes.Add(StartState);
7575
this.ItSaved = true;
76-
//ViewModelNode end = new ViewModelNode(this)
77-
//{
78-
// Name = "End",
79-
// NameEnable = false,
80-
// CanBeDelete = false,
81-
// Point1 = new MyPoint(100, 100)
82-
//};
83-
//end.TransitionsVisible = null;
84-
//end.RollUpVisible = null;
85-
//Nodes.Add(end);
8676
}
8777
private void SetAsStart(ViewModelNode node)
8878
{

0 commit comments

Comments
 (0)