-
Notifications
You must be signed in to change notification settings - Fork 88
/
EditorToolbarController.cs
235 lines (210 loc) · 8.86 KB
/
EditorToolbarController.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using CommunityToolkit.Mvvm.ComponentModel;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Editing;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EditorDemo
{
internal partial class EditorToolbarController : ObservableObject
{
private readonly MyGeometryEditor editor = new MyGeometryEditor();
private readonly GeometryEditor lineInputEditor = new GeometryEditor();
private readonly GraphicsOverlay EditorOverlay = new GraphicsOverlay();
public EditorToolbarController()
{
lineInputEditor.PropertyChanged += ReshapeEditor_PropertyChanged;
editor.PropertyChanged += Editor_PropertyChanged;
}
private void ReshapeEditor_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(GeometryEditor.Geometry))
LineInputAcceptCommand.NotifyCanExecuteChanged();
}
private void Editor_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(GeometryEditor.CanUndo): UndoCommand.NotifyCanExecuteChanged(); break;
case nameof(GeometryEditor.CanRedo): RedoCommand.NotifyCanExecuteChanged(); break;
case nameof(MyGeometryEditor.Geometry):
{
ReshapeCommand.NotifyCanExecuteChanged();
CutCommand.NotifyCanExecuteChanged();
ApplyCommand.NotifyCanExecuteChanged();
break;
}
case nameof(MyGeometryEditor.Tool):
{
ClearSelection();
RaiseActiveINPC();
break;
}
case nameof(MyGeometryEditor.SelectedElement):
{
DeleteSelectionCommand.NotifyCanExecuteChanged();
ClearSelectionCommand.NotifyCanExecuteChanged();
break;
}
}
}
string lineInputMode = "";
private void RaiseActiveINPC()
{
OnPropertyChanged(nameof(IsMoveActive));
OnPropertyChanged(nameof(IsRotateActive));
OnPropertyChanged(nameof(IsEditVerticesActive));
OnPropertyChanged(nameof(IsReshapeActive));
OnPropertyChanged(nameof(IsCutActive));
OnPropertyChanged(nameof(IsLineInputActive));
OnPropertyChanged(nameof(IsEditingActive));
}
public bool IsMoveActive => GeometryEditor == editor && editor.IsMoveActive;
public bool IsRotateActive => GeometryEditor == editor && editor.IsRotateActive;
public bool IsEditVerticesActive => GeometryEditor == editor && editor.IsEditVerticesActive;
public bool IsReshapeActive => IsLineInputActive && lineInputMode == "Reshape";
public bool IsCutActive => IsLineInputActive && lineInputMode == "Cut";
public bool IsLineInputActive => GeometryEditor == lineInputEditor;
public bool IsEditingActive => GeometryEditor is not null && GeometryEditor.IsStarted;
[ObservableProperty]
private GeometryEditor? _geometryEditor;
partial void OnGeometryEditorChanged(GeometryEditor? oldValue, GeometryEditor? newValue)
{
if(newValue != editor)
{
// Add current state of geometry to temporary overlay while using a different editor
EditorOverlay.Graphics.Add(new Graphic(editor.Geometry, editor.Symbol) { IsSelected = true });
}
else
{
EditorOverlay.Graphics.Clear();
}
if (newValue == editor)
editor.SetInactive();
RaiseActiveINPC();
CopySnapSettings(oldValue, newValue);
}
public void CopySnapSettings(GeometryEditor? source, GeometryEditor? target)
{
if (source is not null && target is not null)
{
target.SnapSettings = source.SnapSettings;
}
}
private GraphicsOverlayCollection? _graphicsOverlays;
public GraphicsOverlayCollection? GraphicsOverlays
{
get { return _graphicsOverlays; }
set
{
var oldOverlays = _graphicsOverlays;
if (_graphicsOverlays != value)
{
if (oldOverlays is not null && oldOverlays.Contains(EditorOverlay))
oldOverlays.Remove(EditorOverlay);
_graphicsOverlays = value;
if (value is not null) // TODO: Perhaps do this delayed / on-demand instead
value.Add(EditorOverlay);
}
}
}
[ObservableProperty]
private GeoElement? _geoElement;
partial void OnGeoElementChanged(GeoElement? oldValue, GeoElement? newValue)
{
GeometryEditor = null;
EditorOverlay.Graphics.Clear();
editor.Stop();
if (newValue is not null && !CanEditGeometry(newValue))
{
Trace.WriteLine("Geometry doesn't not support editing");
}
else
{
SetElementVisibility(true, oldValue);
if (newValue?.Geometry is not null)
{
Symbol? symbol = null;
if (GeoElement is Graphic g)
{
symbol = g.Symbol ?? GetGraphicsOwner(g)?.Renderer?.GetSymbol(g);
}
else if (GeoElement is Feature f)
{
symbol = (f.FeatureTable?.Layer as FeatureLayer)?.Renderer?.GetSymbol(f, true) ??
(f.FeatureTable as ArcGISFeatureTable)?.LayerInfo?.DrawingInfo?.Renderer?.GetSymbol(f, true);
}
SetElementVisibility(false, newValue);
editor.Initialize(newValue.Geometry, symbol);
editor.SetInactive();
GeometryEditor = editor;
}
}
EditVerticesCommand.NotifyCanExecuteChanged();
RotateCommand.NotifyCanExecuteChanged();
MoveCommand.NotifyCanExecuteChanged();
UndoCommand.NotifyCanExecuteChanged();
RedoCommand.NotifyCanExecuteChanged();
ReshapeCommand.NotifyCanExecuteChanged();
CutCommand.NotifyCanExecuteChanged();
LineInputAcceptCommand.NotifyCanExecuteChanged();
DiscardCommand.NotifyCanExecuteChanged();
ApplyCommand.NotifyCanExecuteChanged();
DeleteSelectionCommand.NotifyCanExecuteChanged();
ClearSelectionCommand.NotifyCanExecuteChanged();
}
[ObservableProperty]
private bool isSettingsPanelVisible;
[ObservableProperty]
private bool isSnappingEnabled;
partial void OnIsSnappingEnabledChanged(bool value)
{
SnapSourceSettings.Clear();
if (GeometryEditor is not null)
{
if (value)
GeometryEditor.SnapSettings.SyncSourceSettings();
GeometryEditor.SnapSettings.IsEnabled = value;
foreach (var sourceSetting in GeometryEditor.SnapSettings.SourceSettings)
{
SnapSourceSettings.Add(sourceSetting);
}
}
}
[ObservableProperty]
private ObservableCollection<SnapSourceSettings> snapSourceSettings = [];
private GraphicsOverlay? GetGraphicsOwner(Graphic g)
{
if (g is not null && GraphicsOverlays is not null)
{
foreach (var overlay in GraphicsOverlays)
{
if (overlay == EditorOverlay) continue;
if (overlay.Graphics.Contains(g)) return overlay;
}
}
return null;
}
public static bool CanEditGeometry(GeoElement? geoElement)
{
if (geoElement?.Geometry is null)
return false;
if (geoElement is Graphic)
return true;
if (geoElement is Feature feature && feature.FeatureTable != null)
{
return feature.FeatureTable.CanEditGeometry();
}
return false;
}
public event EventHandler<Esri.ArcGISRuntime.Geometry.Geometry>? EditingCompleted;
public event EventHandler? EditingCancelled;
}
}