-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModelBase.cs
More file actions
164 lines (143 loc) · 4.82 KB
/
ViewModelBase.cs
File metadata and controls
164 lines (143 loc) · 4.82 KB
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
159
160
161
162
163
164
using ReactiveUI;
using System.Collections.Generic;
using System;
using Avalonia.Media;
using System.Reactive;
using System.Windows.Input; // Added for ICommand
namespace vector_editor
{
public enum MouseButton
{
Left,
Right
}
public class ViewModelBase : ReactiveObject
{
private Canvas _canvas;
private Elements _selectedElement;
private Effects _selectedEffect;
public ViewModelBase (Canvas canvas){
_canvas = canvas;
_selectedElement = canvas.selectedElement;
_selectedEffect = canvas.selectedEffect;
NewLineCommand = ReactiveCommand.Create(NewLine);
DeleteLineCommand = ReactiveCommand.Create(DeleteLine);
HandleColorClickCommand = ReactiveCommand.Create<(Color color, MouseButton button)>(HandleColorClick);
SaveCommand = ReactiveCommand.Create<string>(SaveCanvas);
LoadCommand = ReactiveCommand.Create<string>(LoadCanvas);
ClearCanvasCommand = ReactiveCommand.Create(ClearCanvas);
this.WhenAnyValue(x => x._canvas.selectedElement)
.Subscribe(value => {
_selectedElement = value;
this.RaisePropertyChanged(nameof(SelectedElement));
});
}
public ReactiveCommand<Unit, Unit> NewLineCommand { get; }
public ReactiveCommand<Unit, Unit> DeleteLineCommand { get; }
public ReactiveCommand<(Color color, MouseButton button), Unit> HandleColorClickCommand { get; }
public ICommand SaveCommand { get; }
public ICommand LoadCommand { get; }
public ReactiveCommand<Unit, Unit> ClearCanvasCommand { get; }
private void NewLine()
{
_canvas.CreateNewLine();
NotifyDrawnElementsChanged();
}
private void DeleteLine()
{
_canvas.drawnElements.RemoveAt(SelectedIndex);
NotifyDrawnElementsChanged();
}
private void SaveCanvas(string filePath)
{
_canvas.SaveDrawnElements(filePath);
}
private void LoadCanvas(string filePath)
{
_canvas.LoadDrawnElements(filePath);
NotifyDrawnElementsChanged();
}
private void ClearCanvas()
{
_canvas.ClearCanvas();
NotifyDrawnElementsChanged();
}
public int SelectedIndex
{
get => _canvas.activeElement;
set {
_canvas.activeElement = value;
this.RaisePropertyChanged(nameof(SelectedIndex));
}
}
public Elements SelectedElement
{
get => _selectedElement;
set {
Console.WriteLine(value);
_selectedElement = value;
_canvas.selectedElement = value;
this.RaisePropertyChanged(nameof(SelectedElement));
NotifyDrawnElementsChanged();
}
}
public Effects SelectedEffect
{
get => _selectedEffect;
set {
Console.WriteLine(value);
_selectedEffect = value;
_canvas.selectedEffect = value;
this.RaisePropertyChanged(nameof(SelectedEffect));
NotifyDrawnElementsChanged();
}
}
public int RegularPolygonSides {
get { return _canvas.RegularPolygonSides; }
set {
if (_canvas.RegularPolygonSides != value) {
_canvas.RegularPolygonSides = value;
}
}
}
public List<string> DrawnElements
{
get => _canvas.DrawnElementsList();
}
public Color Color
{
get => _canvas.color;
set {
_canvas.color = value;
this.RaisePropertyChanged(nameof(Color));
NotifyDrawnElementsChanged();
}
}
public Color FillColor
{
get => _canvas.fillColor;
set {
_canvas.fillColor = value;
this.RaisePropertyChanged(nameof(FillColor));
NotifyDrawnElementsChanged();
}
}
private void HandleColorClick((Color color, MouseButton button) info)
{
if (info.button == MouseButton.Left)
{
Color = info.color;
}
else if (info.button == MouseButton.Right)
{
FillColor = info.color;
}
}
public void NotifyDrawnElementsChanged()
{
SelectedIndex = _canvas.DrawnElementsList().Count - 1;
this.RaisePropertyChanged(nameof(DrawnElements));
this.RaisePropertyChanged(nameof(SelectedIndex));
}
}
}