forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphControl.cs
244 lines (192 loc) · 9.63 KB
/
GraphControl.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
236
237
238
239
240
241
242
243
244
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace GeminiGraphEditor
{
// Inspired by studying http://www.codeproject.com/Articles/182683/NetworkView-A-WPF-custom-control-for-visualizing-a
// Thank you Ashley Davis!
public class GraphControl : Control
{
private ConnectionItemsControl connectionItemsControl;
private ElementItemsControl elementItemsControl;
static GraphControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(GraphControl), new FrameworkPropertyMetadata(typeof(GraphControl)));
}
public GraphControl()
{
AddHandler(ConnectorItem.ConnectorDragStartedEvent, new ConnectorItemDragStartedEventHandler(OnConnectorItemDragStarted));
AddHandler(ConnectorItem.ConnectorDraggingEvent, new ConnectorItemDraggingEventHandler(OnConnectorItemDragging));
AddHandler(ConnectorItem.ConnectorDragCompletedEvent,
new ConnectorItemDragCompletedEventHandler(OnConnectorItemDragCompleted));
}
public IList SelectedElements => elementItemsControl.SelectedItems;
public IList ElementsView => elementItemsControl.Items;
public IList SelectedConnections => connectionItemsControl.SelectedItems;
public event SelectionChangedEventHandler SelectionChanged;
public override void OnApplyTemplate()
{
elementItemsControl = (ElementItemsControl) Template.FindName("PART_ElementItemsControl", this);
elementItemsControl.SelectionChanged += OnElementItemsControlSelectChanged;
connectionItemsControl = (ConnectionItemsControl) Template.FindName("PART_ConnectionItemsControl", this);
//_connectionItemsControl.SelectionChanged += OnConnectionItemsControlSelectChanged;
base.OnApplyTemplate();
}
private void OnElementItemsControlSelectChanged(object sender, SelectionChangedEventArgs e)
{
SelectionChangedEventHandler handler = SelectionChanged;
if (handler != null)
handler(this, new SelectionChangedEventArgs(Selector.SelectionChangedEvent, e.RemovedItems, e.AddedItems));
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
elementItemsControl.SelectedItems.Clear();
connectionItemsControl.SelectedItems.Clear();
base.OnMouseLeftButtonDown(e);
}
public int GetMaxZIndex()
{
return elementItemsControl.Items.Cast<object>()
.Select(item => (ElementItem) elementItemsControl.ItemContainerGenerator.ContainerFromItem(item))
.Select(elementItem => elementItem.ZIndex)
.Concat(new[] {0})
.Max();
}
#region Dependency properties
public static readonly DependencyProperty ElementsSourceProperty =
DependencyProperty.Register("ElementsSource", typeof(IEnumerable), typeof(GraphControl));
public IEnumerable ElementsSource
{
get => (IEnumerable) GetValue(ElementsSourceProperty);
set => SetValue(ElementsSourceProperty, value);
}
public static readonly DependencyProperty ElementItemContainerStyleProperty =
DependencyProperty.Register("ElementItemContainerStyle", typeof(Style), typeof(GraphControl));
public Style ElementItemContainerStyle
{
get => (Style) GetValue(ElementItemContainerStyleProperty);
set => SetValue(ElementItemContainerStyleProperty, value);
}
public static readonly DependencyProperty ElementItemTemplateProperty =
DependencyProperty.Register("ElementItemTemplate", typeof(DataTemplate), typeof(GraphControl));
public DataTemplate ElementItemTemplate
{
get => (DataTemplate) GetValue(ElementItemTemplateProperty);
set => SetValue(ElementItemTemplateProperty, value);
}
public static readonly DependencyProperty ElementItemDataTemplateSelectorProperty = DependencyProperty.Register(
"ElementItemDataTemplateSelector",
typeof(DataTemplateSelector),
typeof(GraphControl),
new PropertyMetadata(null));
public DataTemplateSelector ElementItemDataTemplateSelector
{
get => (DataTemplateSelector) GetValue(ElementItemDataTemplateSelectorProperty);
set => SetValue(ElementItemDataTemplateSelectorProperty, value);
}
public static readonly DependencyProperty ConnectionItemDataTemplateSelectorProperty = DependencyProperty.Register(
"ConnectionItemDataTemplateSelector",
typeof(DataTemplateSelector),
typeof(GraphControl),
new PropertyMetadata(null));
public DataTemplateSelector ConnectionItemDataTemplateSelector
{
get => (DataTemplateSelector) GetValue(ConnectionItemDataTemplateSelectorProperty);
set => SetValue(ConnectionItemDataTemplateSelectorProperty, value);
}
public static readonly DependencyProperty ConnectionsSourceProperty =
DependencyProperty.Register("ConnectionsSource", typeof(IEnumerable), typeof(GraphControl));
public IEnumerable ConnectionsSource
{
get => (IEnumerable) GetValue(ConnectionsSourceProperty);
set => SetValue(ConnectionsSourceProperty, value);
}
public static readonly DependencyProperty ConnectionItemContainerStyleProperty =
DependencyProperty.Register("ConnectionItemContainerStyle", typeof(Style), typeof(GraphControl));
public Style ConnectionItemContainerStyle
{
get => (Style) GetValue(ConnectionItemContainerStyleProperty);
set => SetValue(ConnectionItemContainerStyleProperty, value);
}
public static readonly DependencyProperty ConnectionItemTemplateProperty = DependencyProperty.Register(
"ConnectionItemTemplate",
typeof(DataTemplate),
typeof(GraphControl));
public DataTemplate ConnectionItemTemplate
{
get => (DataTemplate) GetValue(ConnectionItemTemplateProperty);
set => SetValue(ConnectionItemTemplateProperty, value);
}
#endregion
#region Routed events
public static readonly RoutedEvent ConnectionDragStartedEvent = EventManager.RegisterRoutedEvent("ConnectionDragStarted",
RoutingStrategy.Bubble,
typeof(ConnectionDragStartedEventHandler),
typeof(GraphControl));
public static readonly RoutedEvent ConnectionDraggingEvent = EventManager.RegisterRoutedEvent("ConnectionDragging",
RoutingStrategy.Bubble,
typeof(ConnectionDraggingEventHandler),
typeof(GraphControl));
public static readonly RoutedEvent ConnectionDragCompletedEvent = EventManager.RegisterRoutedEvent("ConnectionDragCompleted",
RoutingStrategy.Bubble,
typeof(ConnectionDragCompletedEventHandler),
typeof(GraphControl));
public event ConnectionDragStartedEventHandler ConnectionDragStarted
{
add => AddHandler(ConnectionDragStartedEvent, value);
remove => RemoveHandler(ConnectionDragStartedEvent, value);
}
public event ConnectionDraggingEventHandler ConnectionDragging
{
add => AddHandler(ConnectionDraggingEvent, value);
remove => RemoveHandler(ConnectionDraggingEvent, value);
}
public event ConnectionDragCompletedEventHandler ConnectionDragCompleted
{
add => AddHandler(ConnectionDragCompletedEvent, value);
remove => RemoveHandler(ConnectionDragCompletedEvent, value);
}
#endregion
#region Connection dragging
private ConnectorItem draggingSourceConnector;
private object draggingConnectionDataContext;
private void OnConnectorItemDragStarted(object sender, ConnectorItemDragStartedEventArgs e)
{
e.Handled = true;
draggingSourceConnector = (ConnectorItem) e.OriginalSource;
ConnectionDragStartedEventArgs eventArgs = new ConnectionDragStartedEventArgs(ConnectionDragStartedEvent,
this,
draggingSourceConnector.ParentElementItem,
draggingSourceConnector);
RaiseEvent(eventArgs);
draggingConnectionDataContext = eventArgs.Connection;
if (draggingConnectionDataContext == null)
e.Cancel = true;
}
private void OnConnectorItemDragging(object sender, ConnectorItemDraggingEventArgs e)
{
e.Handled = true;
ConnectionDraggingEventArgs connectionDraggingEventArgs = new ConnectionDraggingEventArgs(ConnectionDraggingEvent,
this,
draggingSourceConnector.ParentElementItem,
draggingConnectionDataContext,
draggingSourceConnector);
RaiseEvent(connectionDraggingEventArgs);
}
private void OnConnectorItemDragCompleted(object sender, ConnectorItemDragCompletedEventArgs e)
{
e.Handled = true;
RaiseEvent(new ConnectionDragCompletedEventArgs(ConnectionDragCompletedEvent,
this,
draggingSourceConnector.ParentElementItem,
draggingConnectionDataContext,
draggingSourceConnector));
draggingSourceConnector = null;
draggingConnectionDataContext = null;
}
#endregion
}
}