-
Notifications
You must be signed in to change notification settings - Fork 8
/
Connector.cs
174 lines (154 loc) · 5.71 KB
/
Connector.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace DiagramDesigner
{
public class Connector : Control, INotifyPropertyChanged
{
// drag start point, relative to the DesignerCanvas
private Point? dragStartPoint = null;
public ConnectorOrientation Orientation { get; set; }
// center position of this Connector relative to the DesignerCanvas
private Point position;
public Point Position
{
get { return position; }
set
{
if (position != value)
{
position = value;
OnPropertyChanged("Position");
}
}
}
// the DesignerItem this Connector belongs to;
// retrieved from DataContext, which is set in the
// DesignerItem template
private DesignerItem parentDesignerItem;
public DesignerItem ParentDesignerItem
{
get
{
if (parentDesignerItem == null)
parentDesignerItem = this.DataContext as DesignerItem;
return parentDesignerItem;
}
}
// keep track of connections that link to this connector
private List<Connection> connections;
public List<Connection> Connections
{
get
{
if (connections == null)
connections = new List<Connection>();
return connections;
}
}
public Connector()
{
// fired when layout changes
base.LayoutUpdated += new EventHandler(Connector_LayoutUpdated);
}
// when the layout changes we update the position property
void Connector_LayoutUpdated(object sender, EventArgs e)
{
DesignerCanvas designer = GetDesignerCanvas(this);
if (designer != null)
{
//get centre position of this Connector relative to the DesignerCanvas
this.Position = this.TransformToAncestor(designer).Transform(new Point(this.Width / 2, this.Height / 2));
}
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
DesignerCanvas canvas = GetDesignerCanvas(this);
if (canvas != null)
{
// position relative to DesignerCanvas
this.dragStartPoint = new Point?(e.GetPosition(canvas));
e.Handled = true;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// if mouse button is not pressed we have no drag operation, ...
if (e.LeftButton != MouseButtonState.Pressed)
this.dragStartPoint = null;
// but if mouse button is pressed and start point value is set we do have one
if (this.dragStartPoint.HasValue)
{
// create connection adorner
DesignerCanvas canvas = GetDesignerCanvas(this);
if (canvas != null)
{
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(canvas);
if (adornerLayer != null)
{
ConnectorAdorner adorner = new ConnectorAdorner(canvas, this);
if (adorner != null)
{
adornerLayer.Add(adorner);
e.Handled = true;
}
}
}
}
}
internal ConnectorInfo GetInfo()
{
ConnectorInfo info = new ConnectorInfo();
info.DesignerItemLeft = DesignerCanvas.GetLeft(this.ParentDesignerItem);
info.DesignerItemTop = DesignerCanvas.GetTop(this.ParentDesignerItem);
info.DesignerItemSize = new Size(this.ParentDesignerItem.ActualWidth, this.ParentDesignerItem.ActualHeight);
info.Orientation = this.Orientation;
info.Position = this.Position;
return info;
}
// iterate through visual tree to get parent DesignerCanvas
private DesignerCanvas GetDesignerCanvas(DependencyObject element)
{
while (element != null && !(element is DesignerCanvas))
element = VisualTreeHelper.GetParent(element);
return element as DesignerCanvas;
}
#region INotifyPropertyChanged Members
// we could use DependencyProperties as well to inform others of property changes
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
// provides compact info about a connector; used for the
// routing algorithm, instead of hand over a full fledged Connector
internal struct ConnectorInfo
{
public double DesignerItemLeft { get; set; }
public double DesignerItemTop { get; set; }
public Size DesignerItemSize { get; set; }
public Point Position { get; set; }
public ConnectorOrientation Orientation { get; set; }
}
public enum ConnectorOrientation
{
None,
Left,
Top,
Right,
Bottom
}
}