-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathOperationsMenuViewModel.cs
95 lines (84 loc) · 2.88 KB
/
OperationsMenuViewModel.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace Nodify.Calculator
{
public class OperationsMenuViewModel : ObservableObject
{
private bool _isVisible;
public bool IsVisible
{
get => _isVisible;
set
{
SetProperty(ref _isVisible, value);
if (!value)
{
Closed?.Invoke();
}
}
}
private Point _location;
public Point Location
{
get => _location;
set => SetProperty(ref _location, value);
}
public event Action? Closed;
public void OpenAt(Point targetLocation)
{
Close();
Location = targetLocation;
IsVisible = true;
}
public void Close()
{
IsVisible = false;
}
public NodifyObservableCollection<OperationInfoViewModel> AvailableOperations { get; }
public INodifyCommand CreateOperationCommand { get; }
private readonly CalculatorViewModel _calculator;
public OperationsMenuViewModel(CalculatorViewModel calculator)
{
_calculator = calculator;
List<OperationInfoViewModel> operations = new List<OperationInfoViewModel>
{
new OperationInfoViewModel
{
Type = OperationType.Graph,
Title = "Operation Graph",
},
new OperationInfoViewModel
{
Type = OperationType.Calculator,
Title = "Calculator"
},
new OperationInfoViewModel
{
Type = OperationType.Expression,
Title = "Custom",
}
};
operations.AddRange(OperationFactory.GetOperationsInfo(typeof(OperationsContainer)));
AvailableOperations = new NodifyObservableCollection<OperationInfoViewModel>(operations);
CreateOperationCommand = new DelegateCommand<OperationInfoViewModel>(CreateOperation);
}
private void CreateOperation(OperationInfoViewModel operationInfo)
{
OperationViewModel op = OperationFactory.GetOperation(operationInfo);
op.Location = Location;
_calculator.Operations.Add(op);
var pending = _calculator.PendingConnection;
if (pending.IsVisible)
{
var connector = pending.Source.IsInput ? op.Output : op.Input.FirstOrDefault();
if (connector != null && _calculator.CanCreateConnection(pending.Source, connector))
{
_calculator.CreateConnection(pending.Source, connector);
}
}
Close();
}
}
}