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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Tango.Core.Commands;
using Tango.Editors;
namespace Tango.Editors
{
/// <summary>
/// Represents a <see cref="IElementEditor"/> collection container and editor. The editor supports Undo, Redo, Cut, Copy and Paste operations.
/// </summary>
/// <seealso cref="Tango.Editors.HybridControl" />
/// <seealso cref="Tango.Editors.IConfigurable" />
/// <seealso cref="Tango.Editors.ISupportEditingOperations" />
/// <seealso cref="Tango.Editors.ISupportUndoRedoOperations" />
public partial class ElementsEditor : HybridControl, IConfigurable, ISupportEditingOperations, ISupportUndoRedoOperations
{
private List<IElementEditor> _copiedElements;
private bool _isSelectionMouseDown; //Determines whether the mouse is down for selection by the selection rectangle.
private Point _selectionMouseDownPoint; //Holds the originating point to perform the selection.
private bool _selectionPerformed; //Determines whether the selection rectangle has moved at least 1 pixel.
#region Events
/// <summary>
/// Occurs when attempting to create a new element using the selection rectangle and holding the shift key.
/// </summary>
public event EventHandler<ElementCreationEventArgs> ElementCreation;
/// <summary>
/// Occurs before removing selected elements using the DELETE key.
/// </summary>
public event EventHandler<ElementsEventArgs> RemovingElements;
/// <summary>
/// Occurs when elements selection has changed;.
/// </summary>
public event EventHandler<ElementsEventArgs> SelectionChanged;
/// <summary>
/// Occurs after pasting the currently copied elements. The collection of elements represents the new cloned elements.
/// </summary>
public event EventHandler<ElementsEventArgs> AfterPaste;
/// <summary>
/// Occurs when one or many elements were added.
/// </summary>
public event EventHandler<ElementsEventArgs> ElementsAdded;
/// <summary>
/// Occurs when one or many elements were removed.
/// </summary>
public event EventHandler<ElementsEventArgs> ElementsRemoved;
public event EventHandler<IElementEditor> ElementDoubleClicked;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ElementsEditor"/> class.
/// </summary>
public ElementsEditor()
{
//Initialize Collections
//Elements = new ObservableCollection<IElementEditor>();
SelectedElements = new ObservableCollection<IElementEditor>();
InitializeComponent();
//Initialize Default Undo/Redo States Provider.
UndoRedoStatesProvider = new ElementsEditorUndoRedoStatesProvider(this);
//Register Events.
Loaded += ElementsEditor_Loaded;
//Initialize Commands
CopyCommand = new RelayCommand(Copy, (x) => GetSelectedElements().Count > 0);
PasteCommand = new RelayCommand(Paste, (x) => _copiedElements != null && _copiedElements.Count > 0);
UndoCommand = new RelayCommand(Undo, (x) => UndoRedoStatesProvider.CanUndo);
RedoCommand = new RelayCommand(Redo, (x) => UndoRedoStatesProvider.CanRedo);
DeleteCommand = new RelayCommand(() => { RemoveSelectedElements(true); }, (x) => GetSelectedElements().Count > 0);
CutCommand = new RelayCommand(Cut, (x) => GetSelectedElements().Count > 0);
ZoomCommand = new RelayCommand<String>((str) => { Zoom(Convert.ToDouble(str)); });
ResetZoomCommand = new RelayCommand(() => { ScaleFactor = 1; });
SelectAllCommand = new RelayCommand(SelectAll);
BringToFrontCommand = new RelayCommand(BringToFront, (x) => GetSelectedElements().Count > 0);
SendToBackCommand = new RelayCommand(SendToBack, (x) => GetSelectedElements().Count > 0);
}
#endregion
#region Properties
public BitmapSource PreviewImage
{
get { return (BitmapSource)GetValue(PreviewImageProperty); }
set { SetValue(PreviewImageProperty, value); }
}
public static readonly DependencyProperty PreviewImageProperty =
DependencyProperty.Register("PreviewImage", typeof(BitmapSource), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets a value indicating whether to enable undo and redo operations using the keyboard.
/// </summary>
public bool EnableKeyboardUndoRedoOperations
{
get { return (bool)GetValue(EnableKeyboardUndoRedoOperationsProperty); }
set { SetValue(EnableKeyboardUndoRedoOperationsProperty, value); }
}
public static readonly DependencyProperty EnableKeyboardUndoRedoOperationsProperty =
DependencyProperty.Register("EnableKeyboardUndoRedoOperations", typeof(bool), typeof(ElementsEditor), new PropertyMetadata(true));
/// <summary>
/// Gets or sets a value indicating whether to enable editing operations using the keyboard.
/// </summary>
public bool EnableKeyboardEditingOperations
{
get { return (bool)GetValue(EnableKeyboardEditingOperationsProperty); }
set { SetValue(EnableKeyboardEditingOperationsProperty, value); }
}
public static readonly DependencyProperty EnableKeyboardEditingOperationsProperty =
DependencyProperty.Register("EnableKeyboardEditingOperations", typeof(bool), typeof(ElementsEditor), new PropertyMetadata(true));
/// <summary>
/// Gets or sets the width of the editor.
/// </summary>
public double EditorWidth
{
get { return (double)GetValue(EditorWidthProperty); }
set { SetValue(EditorWidthProperty, value); }
}
public static readonly DependencyProperty EditorWidthProperty =
DependencyProperty.Register("EditorWidth", typeof(double), typeof(ElementsEditor), new PropertyMetadata(1280.0));
/// <summary>
/// Gets or sets the height of the editor.
/// </summary>
public double EditorHeight
{
get { return (double)GetValue(EditorHeightProperty); }
set { SetValue(EditorHeightProperty, value); }
}
public static readonly DependencyProperty EditorHeightProperty =
DependencyProperty.Register("EditorHeight", typeof(double), typeof(ElementsEditor), new PropertyMetadata(720.0));
/// <summary>
/// Gets or sets the editor scale factor.
/// </summary>
public double ScaleFactor
{
get { return (double)GetValue(ScaleFactorProperty); }
set { SetValue(ScaleFactorProperty, value); }
}
public static readonly DependencyProperty ScaleFactorProperty =
DependencyProperty.Register("ScaleFactor", typeof(double), typeof(ElementsEditor), new PropertyMetadata(1.0, null, (d, e) => { return (d as ElementsEditor).OnCoerceScaleFactor(e); }));
/// <summary>
/// Gets or sets the collection of <see cref="IElementEditor"/>.
/// </summary>
public ObservableCollection<IElementEditor> Elements
{
get { return (ObservableCollection<IElementEditor>)GetValue(ElementsProperty); }
set { SetValue(ElementsProperty, value); }
}
public static readonly DependencyProperty ElementsProperty =
DependencyProperty.Register("Elements", typeof(ObservableCollection<IElementEditor>), typeof(ElementsEditor), new PropertyMetadata(null, (d, e) => { (d as ElementsEditor).OnElementsChanged(); }));
/// <summary>
/// Gets or sets the selected element.
/// </summary>
public IElementEditor SelectedElement
{
get { return (IElementEditor)GetValue(SelectedElementProperty); }
set { SetValue(SelectedElementProperty, value); }
}
public static readonly DependencyProperty SelectedElementProperty =
DependencyProperty.Register("SelectedElement", typeof(IElementEditor), typeof(ElementsEditor), new PropertyMetadata(null, (d, e) => { (d as ElementsEditor).OnSelectedElementChanged(); }));
/// <summary>
/// Gets or sets the selected elements.
/// </summary>
public ObservableCollection<IElementEditor> SelectedElements
{
get { return (ObservableCollection<IElementEditor>)GetValue(SelectedElementsProperty); }
set { SetValue(SelectedElementsProperty, value); }
}
public static readonly DependencyProperty SelectedElementsProperty =
DependencyProperty.Register("SelectedElements", typeof(ObservableCollection<IElementEditor>), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the height of the ruler.
/// </summary>
public double RulerHeight
{
get { return (double)GetValue(RulerHeightProperty); }
set { SetValue(RulerHeightProperty, value); }
}
public static readonly DependencyProperty RulerHeightProperty =
DependencyProperty.Register("RulerHeight", typeof(double), typeof(ElementsEditor), new PropertyMetadata(22.0));
/// <summary>
/// Gets or sets the undo redo states provider.
/// </summary>
public IUndoRedoStatesProvider UndoRedoStatesProvider
{
get { return (IUndoRedoStatesProvider)GetValue(UndoRedoStatesProviderProperty); }
set { SetValue(UndoRedoStatesProviderProperty, value); }
}
public static readonly DependencyProperty UndoRedoStatesProviderProperty =
DependencyProperty.Register("UndoRedoStatesProvider", typeof(IUndoRedoStatesProvider), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets a value indicating whether to bring the selected element to the front z index.
/// </summary>
public bool BringToFrontOnSelect
{
get { return (bool)GetValue(BringToFrontOnSelectProperty); }
set { SetValue(BringToFrontOnSelectProperty, value); }
}
public static readonly DependencyProperty BringToFrontOnSelectProperty =
DependencyProperty.Register("BringToFrontOnSelect", typeof(bool), typeof(ElementsEditor), new PropertyMetadata(true));
/// <summary>
/// Gets or sets the editor mode.
/// </summary>
public ElementsEditorMode EditorMode
{
get { return (ElementsEditorMode)GetValue(EditorModeProperty); }
set { SetValue(EditorModeProperty, value); }
}
public static readonly DependencyProperty EditorModeProperty =
DependencyProperty.Register("EditorMode", typeof(ElementsEditorMode), typeof(ElementsEditor), new PropertyMetadata(ElementsEditorMode.Default));
/// <summary>
/// Gets or sets a value indicating whether to enable the creation of new elements using the mouse and Shift key.
/// </summary>
public bool EnableElementCreation
{
get { return (bool)GetValue(EnableElementCreationProperty); }
set { SetValue(EnableElementCreationProperty, value); }
}
public static readonly DependencyProperty EnableElementCreationProperty =
DependencyProperty.Register("EnableElementCreation", typeof(bool), typeof(ElementsEditor), new PropertyMetadata(true));
/// <summary>
/// Gets or sets an optional attached elements editor for editors mirroring mode.
/// </summary>
public ElementsEditor AttachedEditor
{
get { return (ElementsEditor)GetValue(AttachedEditorProperty); }
set { SetValue(AttachedEditorProperty, value); }
}
public static readonly DependencyProperty AttachedEditorProperty =
DependencyProperty.Register("AttachedEditor", typeof(ElementsEditor), typeof(ElementsEditor), new PropertyMetadata(null, (d, e) => (d as ElementsEditor).OnAttachedEditorChanged()));
/// <summary>
/// Gets or sets the preview visual source opacity.
/// </summary>
public double PreviewVisualSourceOpacity
{
get { return (double)GetValue(PreviewVisualSourceOpacityProperty); }
set { SetValue(PreviewVisualSourceOpacityProperty, value); }
}
public static readonly DependencyProperty PreviewVisualSourceOpacityProperty =
DependencyProperty.Register("PreviewVisualSourceOpacity", typeof(double), typeof(ElementsEditor), new PropertyMetadata(1.0));
/// <summary>
/// Gets or sets a value indicating whether this instance is editable.
/// </summary>
public bool IsEditable
{
get { return (bool)GetValue(IsEditableProperty); }
set { SetValue(IsEditableProperty, value); }
}
public static readonly DependencyProperty IsEditableProperty =
DependencyProperty.Register("IsEditable", typeof(bool), typeof(ElementsEditor), new PropertyMetadata(true));
#endregion
#region Attached Properties
#region IsSelected
/// <summary>
/// Determines whether the element is currently selected.
/// </summary>
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.RegisterAttached("IsSelected",
typeof(bool), typeof(ElementsEditor),
new FrameworkPropertyMetadata(false));
/// <summary>
/// Sets the IsSelected attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">if set to <c>true</c> selected.</param>
public static void SetIsSelected(IElementEditor element, bool value)
{
(element as DependencyObject).SetValue(IsSelectedProperty, value);
}
/// <summary>
/// Gets the is IsSelected attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static bool GetIsSelected(IElementEditor element)
{
return (bool)(element as DependencyObject).GetValue(IsSelectedProperty);
}
#endregion
#endregion
#region Theme Properties
/// <summary>
/// Gets or sets the ruler background.
/// </summary>
public Brush RulerBackground
{
get { return (Brush)GetValue(RulerBackgroundProperty); }
set { SetValue(RulerBackgroundProperty, value); }
}
public static readonly DependencyProperty RulerBackgroundProperty =
DependencyProperty.Register("RulerBackground", typeof(Brush), typeof(ElementsEditor), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// Gets or sets the editor background.
/// </summary>
public Brush EditorBackground
{
get { return (Brush)GetValue(EditorBackgroundProperty); }
set { SetValue(EditorBackgroundProperty, value); }
}
public static readonly DependencyProperty EditorBackgroundProperty =
DependencyProperty.Register("EditorBackground", typeof(Brush), typeof(ElementsEditor), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// Gets or sets the selection fill brush.
/// </summary>
public Brush SelectionFillBrush
{
get { return (Brush)GetValue(SelectionFillBrushProperty); }
set { SetValue(SelectionFillBrushProperty, value); }
}
public static readonly DependencyProperty SelectionFillBrushProperty =
DependencyProperty.Register("SelectionFillBrush", typeof(Brush), typeof(ElementsEditor), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
/// <summary>
/// Gets or sets the selection stroke brush.
/// </summary>
public Brush SelectionStrokeBrush
{
get { return (Brush)GetValue(SelectionStrokeBrushProperty); }
set { SetValue(SelectionStrokeBrushProperty, value); }
}
public static readonly DependencyProperty SelectionStrokeBrushProperty =
DependencyProperty.Register("SelectionStrokeBrush", typeof(Brush), typeof(ElementsEditor), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
#endregion
#region Virtual Methods
/// <summary>
/// Invoked when the attached editor has changed.
/// </summary>
protected virtual void OnAttachedEditorChanged()
{
if (AttachedEditor != null)
{
UndoRedoStatesProvider = new AttachedElementsEditorsUndoRedoStatesProvider(this, AttachedEditor);
AttachedEditor.UndoRedoStatesProvider = UndoRedoStatesProvider;
}
}
/// <summary>
/// Raises the <see cref="E:ElementsAdded" /> event.
/// </summary>
/// <param name="e">The <see cref="ElementsEventArgs"/> instance containing the event data.</param>
protected virtual void OnElementsAdded(ElementsEventArgs e)
{
if (ElementsAdded != null) ElementsAdded(this, e);
}
/// <summary>
/// Raises the <see cref="E:ElementsRemoved" /> event.
/// </summary>
/// <param name="e">The <see cref="ElementsEventArgs"/> instance containing the event data.</param>
protected virtual void OnElementsRemoved(ElementsEventArgs e)
{
if (ElementsRemoved != null) ElementsRemoved(this, e);
}
/// <summary>
/// Called when the reset scale factor button was clicked.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
protected virtual void OnResetScaleFactor(object sender, MouseButtonEventArgs e)
{
ScaleFactor = 1;
}
/// <summary>
/// Called when the selected element has changed.
/// </summary>
protected virtual void OnSelectedElementChanged()
{
if (SelectedElement != null)
{
if (BringToFrontOnSelect)
{
if (Elements.Count > 0)
{
Canvas.SetZIndex(SelectedElement as UIElement, Elements.Max(x => Canvas.GetZIndex(x as UIElement) + 1));
}
}
SetElementSelection(SelectedElement, true);
if (AttachedEditor != null && AttachedEditor.SelectedElement != SelectedElement.AttachedEditor)
{
AttachedEditor.SelectedElement = SelectedElement.AttachedEditor;
}
}
else
{
if (AttachedEditor != null)
{
AttachedEditor.SelectedElement = null;
}
}
if (!IsCtrlDown())
{
foreach (var element in Elements.Where(x => x != SelectedElement))
{
SetElementSelection(element, false);
}
}
OnSelectionChanged();
InvalidateRelayCommands();
}
/// <summary>
/// Raises the <see cref="SelectionChanged"/> event.
/// </summary>
protected void OnSelectionChanged()
{
if (SelectionChanged != null) SelectionChanged(this, new ElementsEventArgs(GetSelectedElements()));
}
/// <summary>
/// Called when the elements collection has changed.
/// </summary>
protected virtual void OnElementsChanged()
{
if (Elements != null)
{
RegisterElementsEvents();
Elements.CollectionChanged -= Elements_CollectionChanged;
Elements.CollectionChanged += Elements_CollectionChanged;
SetCanvasElements();
}
InvalidateRelayCommands();
}
/// <summary>
/// Called when the hosting canvas has captured a mouse down event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
protected virtual void OnCanvasMouseDown(object sender, MouseButtonEventArgs e)
{
DeselectElements();
_selectionMouseDownPoint = e.GetPosition(selectionCanvas);
_selectionPerformed = false;
_isSelectionMouseDown = true;
selectionRec.Width = 0;
selectionRec.Height = 0;
selectionCanvas.Visibility = System.Windows.Visibility.Visible;
}
/// <summary>
/// Handles the canvas mouse up event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
protected virtual void OnCanvasMouseUp(object sender, MouseButtonEventArgs e)
{
if (_selectionPerformed)
{
_selectionPerformed = false;
selectionCanvas.Visibility = System.Windows.Visibility.Hidden;
e.Handled = true;
if (IsShiftDown() && EnableElementCreation)
{
OnElementCreation();
}
}
_isSelectionMouseDown = false;
}
/// <summary>
/// Raises the <see cref="ElementCreation"/> event.
/// </summary>
protected virtual void OnElementCreation()
{
PrepareUndoState();
var args = new ElementCreationEventArgs(Canvas.GetLeft(selectionRec), Canvas.GetTop(selectionRec), selectionRec.Width, selectionRec.Height);
if (ElementCreation != null) ElementCreation(this, args);
if (args.AppendUndoState)
{
CommitUndoState();
}
}
/// <summary>
/// Handles the canvas mouse move event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
protected virtual void OnCanvasMouseMove(object sender, MouseEventArgs e)
{
if (_isSelectionMouseDown)
{
Point currentMousePoint = e.GetPosition(selectionCanvas);
_selectionPerformed = currentMousePoint.X != _selectionMouseDownPoint.X || currentMousePoint.Y != _selectionMouseDownPoint.Y;
Canvas.SetLeft(selectionRec, _selectionMouseDownPoint.X);
Canvas.SetTop(selectionRec, _selectionMouseDownPoint.Y);
if (currentMousePoint.X - _selectionMouseDownPoint.X > 1)
{
selectionRec.Width = currentMousePoint.X - _selectionMouseDownPoint.X;
}
if (currentMousePoint.Y - _selectionMouseDownPoint.Y > 1)
{
selectionRec.Height = currentMousePoint.Y - _selectionMouseDownPoint.Y;
}
if (currentMousePoint.X < _selectionMouseDownPoint.X)
{
Canvas.SetLeft(selectionRec, currentMousePoint.X);
selectionRec.Width = _selectionMouseDownPoint.X - currentMousePoint.X;
}
if (currentMousePoint.Y < _selectionMouseDownPoint.Y)
{
Canvas.SetTop(selectionRec, currentMousePoint.Y);
selectionRec.Height = _selectionMouseDownPoint.Y - currentMousePoint.Y;
}
if (!IsShiftDown())
{
//Select intersecting objects
Rect selectRect = new Rect(Canvas.GetLeft(selectionRec), Canvas.GetTop(selectionRec), selectionRec.Width, selectionRec.Height);
var allElements = GetAllElements();
foreach (var element in allElements)
{
SetElementSelection(element, GetElementBounds(element).IntersectsWith(selectRect));
}
var selectedElements = GetSelectedElements();
if (selectedElements.Count == 1)
{
SelectedElement = selectedElements.FirstOrDefault();
}
}
}
}
/// <summary>
/// Called when coercing the scale factor.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
protected virtual object OnCoerceScaleFactor(object value)
{
if ((double)value < 0.1)
{
return 0.1;
}
else
{
return value;
}
}
/// <summary>
/// Called when zooming with mouse wheel.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="MouseWheelEventArgs"/> instance containing the event data.</param>
protected virtual void OnMouseWheelZooming(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0) //Ticks up
{
ScaleFactor += 0.1;
}
else //Ticks Down
{
ScaleFactor -= 0.1;
}
Point pointAbsolute = Mouse.GetPosition(gridCanvas);
Point pointRelative = Mouse.GetPosition(scrollViewer);
scrollViewer.ScrollToHorizontalOffset((pointAbsolute.X * ScaleFactor) - pointRelative.X);
scrollViewer.ScrollToVerticalOffset((pointAbsolute.Y * ScaleFactor) - pointRelative.Y);
}
#endregion
#region Event Handlers
/// <summary>
/// Handles the Loaded event of the ElementsEditor control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void ElementsEditor_Loaded(object sender, RoutedEventArgs e)
{
SetCanvasElements();
}
/// <summary>
/// Handles the CollectionChanged event of the Elements control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
private void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
RegisterElementsEvents();
SetCanvasElements();
if (e.NewItems != null && e.NewItems.Count > 0)
{
OnElementsAdded(new ElementsEventArgs(e.NewItems.Cast<IElementEditor>().ToList()));
}
if (e.OldItems != null && e.OldItems.Count > 0)
{
OnElementsRemoved(new ElementsEventArgs(e.OldItems.Cast<IElementEditor>().ToList()));
}
}
/// <summary>
/// Handles the SelectionChanged event of the Element control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
/// <exception cref="System.NotImplementedException"></exception>
private void Element_SelectionChanged(object sender, EventArgs e)
{
IElementEditor element = sender as IElementEditor;
SelectedElement = element;
}
/// <summary>
/// Handles the Moving event of the Element control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Controls.Primitives.DragDeltaEventArgs"/> instance containing the event data.</param>
/// <exception cref="System.NotImplementedException"></exception>
private void Element_Moving(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
if (IsCtrlDown())
{
GetSelectedElements().Where(x => x != sender).ToList().ForEach(x => x.PushMove(e));
}
}
/// <summary>
/// Handles the AfterBoundsChange event of the Element control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void Element_AfterBoundsChange(object sender, EventArgs e)
{
CommitUndoState();
}
/// <summary>
/// Handles the BeforeBoundsChange event of the Element control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void Element_BeforeBoundsChange(object sender, EventArgs e)
{
PrepareUndoState();
}
/// <summary>
/// Handles the PreviewMouseDown event of the Element control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
private void Element_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left && !Keyboard.IsKeyDown(Key.LeftShift))
{
SelectedElement = sender as IElementEditor;
}
}
#endregion
#region Private Methods
private void SetCanvasElements()
{
if (!this.IsInDesignMode())
{
if (canvas != null && Elements != null)
{
foreach (UIElement element in Elements)
if (!canvas.Children.Contains(element))
canvas.Children.Add(element);
List<UIElement> removeList = new List<UIElement>();
foreach (UIElement element in canvas.Children)
if (!Elements.Contains(element as IElementEditor))
removeList.Add(element);
foreach (UIElement element in removeList)
canvas.Children.Remove(element);
}
}
}
/// <summary>
/// Determines whether the control key is down.
/// </summary>
private bool IsCtrlDown()
{
return Keyboard.IsKeyDown(Key.LeftCtrl);
}
/// <summary>
/// Determines whether the shift control is down.
/// </summary>
private bool IsShiftDown()
{
return Keyboard.IsKeyDown(Key.LeftShift);
}
/// <summary>
/// Registers the elements events.
/// </summary>
private void RegisterElementsEvents()
{
foreach (var element in Elements)
{
element.Moving -= Element_Moving;
element.Moving += Element_Moving;
element.BeforeBoundsChange -= Element_BeforeBoundsChange;
element.BeforeBoundsChange += Element_BeforeBoundsChange;
element.AfterBoundsChange -= Element_AfterBoundsChange;
element.AfterBoundsChange += Element_AfterBoundsChange;
if (element is FrameworkElement)
{
(element as FrameworkElement).PreviewMouseDown -= Element_PreviewMouseDown;
(element as FrameworkElement).PreviewMouseDown += Element_PreviewMouseDown;
(element as FrameworkElement).PreviewMouseDown -= ElementsEditor_PreviewMouseDown;
(element as FrameworkElement).PreviewMouseDown += ElementsEditor_PreviewMouseDown;
}
}
}
private void ElementsEditor_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
ElementDoubleClicked?.Invoke(this, sender as IElementEditor);
}
}
/// <summary>
/// Prepares the state of the undo.
/// </summary>
private void PrepareUndoState()
{
UndoRedoStatesProvider.PrepareUndoState();
}
/// <summary>
/// Commits the state of the undo.
/// </summary>
private void CommitUndoState()
{
UndoRedoStatesProvider.CommitUndoState();
}
/// <summary>
/// Executes the undo/redo state.
/// </summary>
/// <param name="state">The state.</param>
private void ExecuteUndoRedoState(List<AnimationSetup> state)
{
foreach (var setup in state)
{
setup.Animation.Completed += (x, y) =>
{
var aniValue = setup.DependencyObject.GetValue(setup.DependencyProperty);
(setup.DependencyObject as IAnimatable).BeginAnimation(setup.DependencyProperty, null);
setup.DependencyObject.SetCurrentValue(setup.DependencyProperty, aniValue);
};
(setup.DependencyObject as IAnimatable).BeginAnimation(setup.DependencyProperty, setup.Animation);
}
}
/// <summary>
/// Creates an undo/redo state.
/// </summary>
/// <returns></returns>
private List<AnimationSetup> CreateUndoRedoState()
{
List<AnimationSetup> all = new List<AnimationSetup>();
foreach (var element in Elements)
{
var setups = element.GetAnimationSetups(TimeSpan.FromSeconds(0), AnimationSetupMode.Discrete);
all.AddRange(setups);
}
return all;
}
/// <summary>
/// Gets the element bounds.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
private Rect GetElementBounds(IElementEditor element)
{
var visual = element as FrameworkElement;
if (visual != null)
{
var position = visual.TranslatePoint(new Point(0, 0), selectionCanvas);
return new Rect(position.X, position.Y, visual.ActualWidth, visual.ActualHeight);
}
else
{
return new Rect();
}
}
/// <summary>
/// Gets the visual child.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parent">The parent.</param>
/// <returns></returns>
private static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
#endregion
#region Public Methods
/// <summary>
/// De-selects all elements.
/// </summary>
public void DeselectElements()
{
Elements.ToList().ForEach(x => SetElementSelection(x, false));
SelectedElement = null;
OnSelectionChanged();
InvalidateRelayCommands();
}
/// <summary>
/// Selects all elements.
/// </summary>
public void SelectAll()
{
Elements.ToList().ForEach(x => SetElementSelection(x, true));
OnSelectionChanged();
InvalidateRelayCommands();
}
/// <summary>
/// Increase or decrease the <see cref="ScaleFactor"/> by the specified factor.
/// </summary>
/// <param name="factor">The factor (e.g 0.2 or -0.5).</param>
public void Zoom(double factor)
{
ScaleFactor += factor;
}
/// <summary>
/// Gets the selected elements.
/// </summary>
public List<IElementEditor> GetSelectedElements()
{
return GetAllElements().Where(x => GetIsSelected(x)).ToList();
}
/// <summary>
/// Gets all elements.
/// </summary>
/// <returns></returns>
public List<IElementEditor> GetAllElements()
{
return Elements.ToList();
}
/// <summary>
/// Removes the selected elements.
/// </summary>
public void RemoveSelectedElements(bool raiseRemoveEvent = false)
{
ElementsEventArgs args = new ElementsEventArgs();
if (raiseRemoveEvent)
{
args.Elements = GetSelectedElements();
if (RemovingElements != null) RemovingElements(this, args);
}
if (!raiseRemoveEvent || !args.Cancel)
{
PrepareUndoState();
var selectedElements = GetSelectedElements();
selectedElements.ForEach(x => RemoveElement(x));
if (AttachedEditor != null)
{
foreach (var element in selectedElements)
{
if (element.AttachedEditor != null)
{
AttachedEditor.RemoveElement(element.AttachedEditor);
}
}
}
CommitUndoState();
OnSelectionChanged();
InvalidateRelayCommands();
}
}
/// <summary>
/// Removes the element.
/// </summary>
/// <param name="element">The element.</param>
public void RemoveElement(IElementEditor element)
{
SetElementSelection(element, false);
Elements.Remove(element);
if (SelectedElement == element) SelectedElement = null;
OnSelectionChanged();
InvalidateRelayCommands();
}
/// <summary>
/// Gets the element by the hosted element.
/// </summary>
/// <param name="hostedElement">The hosted element.</param>
public IElementEditor GetElementByHostedElement(object hostedElement)
{
return Elements.SingleOrDefault(x => x.HostedElement == hostedElement);
}
/// <summary>
/// Undoes the current state of the elements collection.
/// </summary>
public void Undo()
{
UndoRedoStatesProvider.Undo();
InvalidateRelayCommands();
}
/// <summary>
/// Redoes the current state of the elements collection.
/// </summary>
public void Redo()
{
UndoRedoStatesProvider.Redo();
InvalidateRelayCommands();
}
/// <summary>
/// Performs copy operation on the selected elements.
/// </summary>
public void Copy()
{
Copy(false);
}
private void Copy(bool fromAttached)
{
_copiedElements = GetSelectedElements();
InvalidateRelayCommands();
if (AttachedEditor != null && !fromAttached)
{
AttachedEditor.Copy(true);
}
}
/// <summary>
/// Performs a Cut operation over the selected elements.
/// </summary>
public void Cut()
{
Copy();
RemoveSelectedElements();
InvalidateRelayCommands();
}
/// <summary>
/// Pastes the last copied elements.
/// </summary>
public void Paste()
{
Paste(false, Mouse.GetPosition(gridCanvas), null);
}
private void Paste(bool fromAttached, Point point, List<IElementEditor> fromAttachedElements)
{
if (_copiedElements != null)
{
PrepareUndoState();
List<IElementEditor> elementsToAdd = new List<IElementEditor>();
var clonedElements = _copiedElements.Select(x => x.Clone()).ToList();
if (clonedElements.Count == 0) return;
var mostLeft = clonedElements.OrderBy(x => x.Left).First().Left;
var mostTop = clonedElements.OrderBy(x => x.Top).First().Top;
int count = 0;
foreach (var element in clonedElements)
{
element.Left += point.X - mostLeft;
element.Top += point.Y - mostTop;
elementsToAdd.Add(element);
if (fromAttachedElements != null && fromAttachedElements.Count > 0)
{
fromAttachedElements[count].AttachedEditor = element;
element.AttachedEditor = fromAttachedElements[count++];
}
}
if (AttachedEditor != null && !fromAttached)
{
AttachedEditor.Paste(true, point, clonedElements);
}
if (!fromAttached)
{
if (AfterPaste != null) AfterPaste(this, new ElementsEventArgs(clonedElements));
}
foreach (var element in elementsToAdd)
{
Elements.Add(element);
}
CommitUndoState();
}
InvalidateRelayCommands();
}
/// <summary>
/// Brings the new element Z-index in front of the old element.
/// </summary>
/// <param name="oldElement">The old element.</param>
/// <param name="newElement">The new element.</param>
public void BringInFront(IElementEditor oldElement, IElementEditor newElement)
{
Canvas.SetZIndex(newElement as UIElement, Canvas.GetZIndex(oldElement as UIElement) + 1);
}
/// <summary>
/// Brings the specified element to the Z-index front.
/// </summary>
/// <param name="element">The element.</param>
public void BringToFront(IElementEditor element)
{
element.ZIndex = Elements.Max(x => Canvas.GetZIndex(x as UIElement) + 1);
}
/// <summary>
/// Brings all the selected items to the front.
/// </summary>
public void BringToFront()
{
foreach (var element in GetSelectedElements())
{
BringToFront(element);
}
}
/// <summary>
/// Sends all the selected items to the back.
/// </summary>
public void SendToBack()
{
foreach (var element in GetSelectedElements())
{
SendToBack(element);
}
}
/// <summary>
/// Sets the element Z-index.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="index">The index.</param>
public void SetZIndex(IElementEditor element, int index)
{
Canvas.SetZIndex(element as UIElement, index);
}
/// <summary>
/// Gets the element Z-index.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public int GetZIndex(IElementEditor element)
{
return Canvas.GetZIndex(element as UIElement);
}
/// <summary>
/// Sends the specified element to the Z-index back.
/// </summary>
/// <param name="element">The element.</param>
public void SendToBack(IElementEditor element)
{
element.ZIndex = Elements.Min(x => Canvas.GetZIndex(x as UIElement) - 1);
}
#endregion
#region Keyboard
/// <summary>
/// Invoked when an unhandled <see cref="E:System.Windows.Input.Keyboard.PreviewKeyDown" /> attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
/// </summary>
/// <param name="e">The <see cref="T:System.Windows.Input.KeyEventArgs" /> that contains the event data.</param>
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (!IsEditable) return;
if (e.Key == Key.Right)
{
GetSelectedElements().ForEach(x => x.PushMove(new DragDeltaEventArgs(1, 0)));
}
else if (e.Key == Key.Left)
{
GetSelectedElements().ForEach(x => x.PushMove(new DragDeltaEventArgs(-1, 0)));
}
else if (e.Key == Key.Up)
{
GetSelectedElements().ForEach(x => x.PushMove(new DragDeltaEventArgs(0, -1)));
}
else if (e.Key == Key.Down)
{
GetSelectedElements().ForEach(x => x.PushMove(new DragDeltaEventArgs(0, 1)));
}
else if (e.Key == Key.Z && IsCtrlDown() && EnableKeyboardUndoRedoOperations)
{
Undo();
}
else if (e.Key == Key.Y && IsCtrlDown() && EnableKeyboardUndoRedoOperations)
{
Redo();
}
else if (e.Key == Key.Delete)
{
RemoveSelectedElements(true);
}
else if (e.Key == Key.C && IsCtrlDown() && EnableKeyboardEditingOperations)
{
Copy();
}
else if (e.Key == Key.V && IsCtrlDown() && EnableKeyboardEditingOperations)
{
Paste();
}
else if (e.Key == Key.X && IsCtrlDown() && EnableKeyboardEditingOperations)
{
Cut();
}
else if (e.Key == Key.A && IsCtrlDown())
{
SelectAll();
}
else if (e.Key == Key.F && IsCtrlDown())
{
BringToFront();
}
else if (e.Key == Key.B && IsCtrlDown())
{
SendToBack();
}
}
#endregion
#region Override Methods
/// <summary>
/// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.MouseEnter" /> attached event is raised on this element. Implement this method to add class handling for this event.
/// </summary>
/// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs" /> that contains the event data.</param>
protected override void OnMouseEnter(MouseEventArgs e)
{
base.OnMouseEnter(e);
//scrollViewer.Focus();
}
#endregion
#region Commands
/// <summary>
/// Gets or sets the copy command.
/// </summary>
public RelayCommand CopyCommand
{
get { return (RelayCommand)GetValue(CopyCommandProperty); }
set { SetValue(CopyCommandProperty, value); }
}
public static readonly DependencyProperty CopyCommandProperty =
DependencyProperty.Register("CopyCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the paste command.
/// </summary>
public RelayCommand PasteCommand
{
get { return (RelayCommand)GetValue(PasteCommandProperty); }
set { SetValue(PasteCommandProperty, value); }
}
public static readonly DependencyProperty PasteCommandProperty =
DependencyProperty.Register("PasteCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the undo command.
/// </summary>
public RelayCommand UndoCommand
{
get { return (RelayCommand)GetValue(UndoCommandProperty); }
set { SetValue(UndoCommandProperty, value); }
}
public static readonly DependencyProperty UndoCommandProperty =
DependencyProperty.Register("UndoCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the redo command.
/// </summary>
public RelayCommand RedoCommand
{
get { return (RelayCommand)GetValue(RedoCommandProperty); }
set { SetValue(RedoCommandProperty, value); }
}
public static readonly DependencyProperty RedoCommandProperty =
DependencyProperty.Register("RedoCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the delete command.
/// </summary>
public RelayCommand DeleteCommand
{
get { return (RelayCommand)GetValue(DeleteCommandProperty); }
set { SetValue(DeleteCommandProperty, value); }
}
public static readonly DependencyProperty DeleteCommandProperty =
DependencyProperty.Register("DeleteCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the cut command.
/// </summary>
public RelayCommand CutCommand
{
get { return (RelayCommand)GetValue(CutCommandProperty); }
set { SetValue(CutCommandProperty, value); }
}
public static readonly DependencyProperty CutCommandProperty =
DependencyProperty.Register("CutCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the select all command.
/// </summary>
public RelayCommand SelectAllCommand
{
get { return (RelayCommand)GetValue(SelectAllCommandProperty); }
set { SetValue(SelectAllCommandProperty, value); }
}
public static readonly DependencyProperty SelectAllCommandProperty =
DependencyProperty.Register("SelectAllCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the zoom command.
/// </summary>
public RelayCommand<String> ZoomCommand
{
get { return (RelayCommand<String>)GetValue(ZoomCommandProperty); }
set { SetValue(ZoomCommandProperty, value); }
}
public static readonly DependencyProperty ZoomCommandProperty =
DependencyProperty.Register("ZoomCommand", typeof(RelayCommand<String>), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Gets or sets the reset zoom command.
/// </summary>
public RelayCommand ResetZoomCommand
{
get { return (RelayCommand)GetValue(ResetZoomCommandProperty); }
set { SetValue(ResetZoomCommandProperty, value); }
}
public static readonly DependencyProperty ResetZoomCommandProperty =
DependencyProperty.Register("ResetZoomCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Invokes bring to front on the selected item.
/// </summary>
public RelayCommand BringToFrontCommand
{
get { return (RelayCommand)GetValue(BringToFrontCommandProperty); }
set { SetValue(BringToFrontCommandProperty, value); }
}
public static readonly DependencyProperty BringToFrontCommandProperty =
DependencyProperty.Register("BringToFrontCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
/// <summary>
/// Invokes send to back on the selected item.
/// </summary>
public RelayCommand SendToBackCommand
{
get { return (RelayCommand)GetValue(SendToBackCommandProperty); }
set { SetValue(SendToBackCommandProperty, value); }
}
public static readonly DependencyProperty SendToBackCommandProperty =
DependencyProperty.Register("SendToBackCommand", typeof(RelayCommand), typeof(ElementsEditor), new PropertyMetadata(null));
#endregion
#region IConfigurable Members
/// <summary>
/// Gets a configuration object representing the configurable properties. This configuration can be serialized to a stream or file, and later be loaded and applied to the configurable object.
/// </summary>
/// <returns></returns>
public virtual IConfiguration GetConfiguration()
{
return GetConfiguration();
}
/// <summary>
/// Gets the configuration.
/// </summary>
/// <param name="configurationName">Name of the configuration.</param>
/// <returns></returns>
public virtual ElementsEditorConfiguration GetConfiguration(String configurationName)
{
ElementsEditorConfiguration config = new ElementsEditorConfiguration();
config.Name = configurationName;
config.Date = DateTime.Now;
config.ElementsConfigurations = new ObservableCollection<ElementEditorConfiguration>(Elements.Select(x => x.GetConfiguration() as ElementEditorConfiguration).ToList());
return config;
}
/// <summary>
/// Applies the specified configuration with an optional animation if supported by the configurable type.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <param name="animation">The animation.</param>
/// <exception cref="InvalidConfigurationException"></exception>
public virtual void SetConfiguration(IConfiguration configuration, ConfigurationAnimation animation)
{
if (!(configuration is ElementsEditorConfiguration))
{
throw new InvalidConfigurationException();
}
UndoRedoStatesProvider.Reset();
var config = configuration as ElementsEditorConfiguration;
foreach (var elementConfig in config.ElementsConfigurations)
{
var existingElement = Elements.SingleOrDefault(x => x.ID == elementConfig.ID);
if (existingElement != null)
{
existingElement.SetConfiguration(elementConfig, animation);
}
else
{
var newElement = elementConfig.CreateConfigurable<IElementEditor>();
Elements.Add(newElement);
}
}
var elementsToRemove = Elements.Where(x => !config.ElementsConfigurations.ToList().Exists(y => y.ID == x.ID)).ToList();
elementsToRemove.ForEach(x => RemoveElement(x));
InvalidateRelayCommands();
}
#endregion
private void SetElementSelection(IElementEditor element, bool selected)
{
SetIsSelected(element, selected);
if (element.AttachedEditor != null && GetIsSelected(element.AttachedEditor) != selected)
{
ElementsEditor.SetIsSelected(element.AttachedEditor, selected);
}
}
}
}
|