-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathCalculatorViewModel.cs
159 lines (131 loc) · 5.81 KB
/
CalculatorViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System.Linq;
using System.Windows;
namespace Nodify.Calculator
{
public class CalculatorViewModel : ObservableObject
{
public CalculatorViewModel()
{
CreateConnectionCommand = new DelegateCommand<ConnectorViewModel>(
_ => CreateConnection(PendingConnection.Source, PendingConnection.Target),
_ => CanCreateConnection(PendingConnection.Source, PendingConnection.Target));
StartConnectionCommand = new DelegateCommand<ConnectorViewModel>(_ => PendingConnection.IsVisible = true, (c) => !(c.IsConnected && c.IsInput));
DisconnectConnectorCommand = new DelegateCommand<ConnectorViewModel>(DisconnectConnector);
DeleteSelectionCommand = new DelegateCommand(DeleteSelection);
GroupSelectionCommand = new DelegateCommand(GroupSelectedOperations, () => SelectedOperations.Count > 0);
Connections.WhenAdded(c =>
{
c.Input.IsConnected = true;
c.Output.IsConnected = true;
c.Input.Value = c.Output.Value;
c.Output.ValueObservers.Add(c.Input);
})
.WhenRemoved(c =>
{
var ic = Connections.Count(con => con.Input == c.Input || con.Output == c.Input);
var oc = Connections.Count(con => con.Input == c.Output || con.Output == c.Output);
if (ic == 0)
{
c.Input.IsConnected = false;
}
if (oc == 0)
{
c.Output.IsConnected = false;
}
c.Output.ValueObservers.Remove(c.Input);
});
Operations.WhenAdded(x =>
{
x.Input.WhenRemoved(RemoveConnection);
if (x is CalculatorInputOperationViewModel ci)
{
ci.Output.WhenRemoved(RemoveConnection);
}
void RemoveConnection(ConnectorViewModel i)
{
var c = Connections.Where(con => con.Input == i || con.Output == i).ToArray();
c.ForEach(con => Connections.Remove(con));
}
})
.WhenRemoved(x =>
{
foreach (var input in x.Input)
{
DisconnectConnector(input);
}
if (x.Output != null)
{
DisconnectConnector(x.Output);
}
});
OperationsMenu = new OperationsMenuViewModel(this);
}
private NodifyObservableCollection<OperationViewModel> _operations = new NodifyObservableCollection<OperationViewModel>();
public NodifyObservableCollection<OperationViewModel> Operations
{
get => _operations;
set => SetProperty(ref _operations, value);
}
private NodifyObservableCollection<OperationViewModel> _selectedOperations = new NodifyObservableCollection<OperationViewModel>();
public NodifyObservableCollection<OperationViewModel> SelectedOperations
{
get => _selectedOperations;
set => SetProperty(ref _selectedOperations, value);
}
public NodifyObservableCollection<ConnectionViewModel> Connections { get; } = new NodifyObservableCollection<ConnectionViewModel>();
public PendingConnectionViewModel PendingConnection { get; set; } = new PendingConnectionViewModel();
public OperationsMenuViewModel OperationsMenu { get; set; }
public INodifyCommand StartConnectionCommand { get; }
public INodifyCommand CreateConnectionCommand { get; }
public INodifyCommand DisconnectConnectorCommand { get; }
public INodifyCommand DeleteSelectionCommand { get; }
public INodifyCommand GroupSelectionCommand { get; }
private void DisconnectConnector(ConnectorViewModel connector)
{
var connections = Connections.Where(c => c.Input == connector || c.Output == connector).ToList();
connections.ForEach(c => Connections.Remove(c));
}
internal bool CanCreateConnection(ConnectorViewModel source, ConnectorViewModel? target)
=> target == null || (source != target && source.Operation != target.Operation && source.IsInput != target.IsInput);
internal void CreateConnection(ConnectorViewModel source, ConnectorViewModel? target)
{
if (target == null)
{
PendingConnection.IsVisible = true;
OperationsMenu.OpenAt(PendingConnection.TargetLocation);
OperationsMenu.Closed += OnOperationsMenuClosed;
return;
}
var input = source.IsInput ? source : target;
var output = target.IsInput ? source : target;
PendingConnection.IsVisible = false;
DisconnectConnector(input);
Connections.Add(new ConnectionViewModel
{
Input = input,
Output = output
});
}
private void OnOperationsMenuClosed()
{
PendingConnection.IsVisible = false;
OperationsMenu.Closed -= OnOperationsMenuClosed;
}
private void DeleteSelection()
{
var selected = SelectedOperations.ToList();
selected.ForEach(o => Operations.Remove(o));
}
private void GroupSelectedOperations()
{
var selected = SelectedOperations.ToList();
var bounding = selected.GetBoundingBox(50);
Operations.Add(new OperationGroupViewModel
{
Title = "Operations",
Location = bounding.Location,
GroupSize = new Size(bounding.Width, bounding.Height)
});
}
}
}