-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathOperationViewModel.cs
100 lines (88 loc) · 2.52 KB
/
OperationViewModel.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
using System.ComponentModel;
using System.Linq;
using System.Windows;
namespace Nodify.Calculator
{
public class OperationViewModel : ObservableObject
{
public OperationViewModel()
{
Input.WhenAdded(x =>
{
x.Operation = this;
x.IsInput = true;
x.PropertyChanged += OnInputValueChanged;
})
.WhenRemoved(x =>
{
x.PropertyChanged -= OnInputValueChanged;
});
}
private void OnInputValueChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ConnectorViewModel.Value))
{
OnInputValueChanged();
}
}
private Point _location;
public Point Location
{
get => _location;
set => SetProperty(ref _location, value);
}
private Size _size;
public Size Size
{
get => _size;
set => SetProperty(ref _size, value);
}
private string? _title;
public string? Title
{
get => _title;
set => SetProperty(ref _title, value);
}
private bool _isSelected;
public bool IsSelected
{
get => _isSelected;
set => SetProperty(ref _isSelected, value);
}
public bool IsReadOnly { get; set; }
private IOperation? _operation;
public IOperation? Operation
{
get => _operation;
set => SetProperty(ref _operation, value)
.Then(OnInputValueChanged);
}
public NodifyObservableCollection<ConnectorViewModel> Input { get; } = new NodifyObservableCollection<ConnectorViewModel>();
private ConnectorViewModel? _output;
public ConnectorViewModel? Output
{
get => _output;
set
{
if (SetProperty(ref _output, value) && _output != null)
{
_output.Operation = this;
}
}
}
protected virtual void OnInputValueChanged()
{
if (Output != null && Operation != null)
{
try
{
var input = Input.Select(i => i.Value).ToArray();
Output.Value = Operation?.Execute(input) ?? 0;
}
catch
{
}
}
}
}
}