1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Diagnostics;
|
---|
7 |
|
---|
8 | namespace Netron.Diagramming.Core
|
---|
9 | {
|
---|
10 | // ----------------------------------------------------------------------
|
---|
11 | /// <summary>
|
---|
12 | /// A tool that "pans" the diagram. The 'Origin' of the diagram is
|
---|
13 | /// adjusted when the left mouse button is held down and dragged
|
---|
14 | /// across the canvas.
|
---|
15 | /// </summary>
|
---|
16 | // ----------------------------------------------------------------------
|
---|
17 | public class PanTool :
|
---|
18 | AbstractTool,
|
---|
19 | IMouseListener,
|
---|
20 | IKeyboardListener
|
---|
21 | {
|
---|
22 | // ------------------------------------------------------------------
|
---|
23 | /// <summary>
|
---|
24 | /// The number of times the mouse location has been updated. This is
|
---|
25 | /// used to determine if the cursor is updated. The cursor is only
|
---|
26 | /// updated every 5 times the mouse is moved and then this number is
|
---|
27 | /// reset to 0. The mouse is updated when this number is zero.
|
---|
28 | /// </summary>
|
---|
29 | // ------------------------------------------------------------------
|
---|
30 | int mouseMoveNumber = 0;
|
---|
31 |
|
---|
32 | // ------------------------------------------------------------------
|
---|
33 | /// <summary>
|
---|
34 | /// Set to true when the left mouse buttone is down, set to false
|
---|
35 | /// all other times.
|
---|
36 | /// </summary>
|
---|
37 | // ------------------------------------------------------------------
|
---|
38 | bool isLeftMouseButtonPressed = false;
|
---|
39 |
|
---|
40 | // ------------------------------------------------------------------
|
---|
41 | /// <summary>
|
---|
42 | /// The initial location of the mouse (when the mouse was clicked).
|
---|
43 | /// </summary>
|
---|
44 | // ------------------------------------------------------------------
|
---|
45 | Point initialLocation = Point.Empty;
|
---|
46 |
|
---|
47 | // ------------------------------------------------------------------
|
---|
48 | /// <summary>
|
---|
49 | /// The last location of the mouse.
|
---|
50 | /// </summary>
|
---|
51 | // ------------------------------------------------------------------
|
---|
52 | Point previousMouseLocation = Point.Empty;
|
---|
53 |
|
---|
54 | // ------------------------------------------------------------------
|
---|
55 | /// <summary>
|
---|
56 | /// Constructor.
|
---|
57 | /// </summary>
|
---|
58 | /// <param name="toolName">string: The name of this tool.</param>
|
---|
59 | // ------------------------------------------------------------------
|
---|
60 | public PanTool(string toolName)
|
---|
61 | : base(toolName)
|
---|
62 | {
|
---|
63 | }
|
---|
64 |
|
---|
65 | // ------------------------------------------------------------------
|
---|
66 | /// <summary>
|
---|
67 | /// Activates the tool and sets the cursor to a hand cursor to provide
|
---|
68 | /// visual feedback that panning is activated.
|
---|
69 | /// </summary>
|
---|
70 | // ------------------------------------------------------------------
|
---|
71 | protected override void OnActivateTool()
|
---|
72 | {
|
---|
73 | base.OnActivateTool();
|
---|
74 | Cursor = CursorPalette.Pan;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // ------------------------------------------------------------------
|
---|
78 | /// <summary>
|
---|
79 | /// Compares the location specified to the previous location and
|
---|
80 | /// sets the cursor as follows:
|
---|
81 | /// * current x < previous x and curreny y = previous y => pan W
|
---|
82 | /// * current x > previous x and curreny y = previous y => pan E
|
---|
83 | /// * current x = previous x and curreny y < previous y => pan N
|
---|
84 | /// * current x = previous x and curreny y > previous y => pan S
|
---|
85 | /// * current x < previous x and curreny y < previous y => pan NW
|
---|
86 | /// * current x > previous x and curreny y < previous y => pan NE
|
---|
87 | /// * current x < previous x and curreny y > previous y => pan SW
|
---|
88 | /// * current x > previous x and curreny y > previous y => pan SE
|
---|
89 | /// </summary>
|
---|
90 | /// <param name="location">Point: The current cursor location.</param>
|
---|
91 | // ------------------------------------------------------------------
|
---|
92 | protected void UpdateCursor(Point location)
|
---|
93 | {
|
---|
94 | if ((location.X < previousMouseLocation.X) &&
|
---|
95 | (location.Y == previousMouseLocation.Y))
|
---|
96 | {
|
---|
97 | Cursor = Cursors.PanWest;
|
---|
98 | }
|
---|
99 | else if ((location.X > previousMouseLocation.X) &&
|
---|
100 | (location.Y == previousMouseLocation.Y))
|
---|
101 | {
|
---|
102 | Cursor = Cursors.PanEast;
|
---|
103 | }
|
---|
104 | else if ((location.X == previousMouseLocation.X) &&
|
---|
105 | (location.Y < previousMouseLocation.Y))
|
---|
106 | {
|
---|
107 | Cursor = Cursors.PanNorth;
|
---|
108 | }
|
---|
109 | else if ((location.X == previousMouseLocation.X) &&
|
---|
110 | (location.Y > previousMouseLocation.Y))
|
---|
111 | {
|
---|
112 | Cursor = Cursors.PanSouth;
|
---|
113 | }
|
---|
114 | else if ((location.X < previousMouseLocation.X) &&
|
---|
115 | (location.Y < previousMouseLocation.Y))
|
---|
116 | {
|
---|
117 | Cursor = Cursors.PanNW;
|
---|
118 | }
|
---|
119 | else if ((location.X > previousMouseLocation.X) &&
|
---|
120 | (location.Y < previousMouseLocation.Y))
|
---|
121 | {
|
---|
122 | Cursor = Cursors.PanNE;
|
---|
123 | }
|
---|
124 | else if ((location.X < previousMouseLocation.X) &&
|
---|
125 | (location.Y > previousMouseLocation.Y))
|
---|
126 | {
|
---|
127 | Cursor = Cursors.PanSW;
|
---|
128 | }
|
---|
129 | else if ((location.X > previousMouseLocation.X) &&
|
---|
130 | (location.Y > previousMouseLocation.Y))
|
---|
131 | {
|
---|
132 | Cursor = Cursors.PanSE;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | #region IMouseListener Members
|
---|
137 |
|
---|
138 | // ------------------------------------------------------------------
|
---|
139 | /// <summary>
|
---|
140 | /// Starts the panning action if this tool is activated and is not
|
---|
141 | /// suspended.
|
---|
142 | /// </summary>
|
---|
143 | /// <param name="e">MouseEventArgs</param>
|
---|
144 | // ------------------------------------------------------------------
|
---|
145 | public bool MouseDown(MouseEventArgs e)
|
---|
146 | {
|
---|
147 | if ( (!IsActive) ||
|
---|
148 | (IsSuspended == true) )
|
---|
149 | {
|
---|
150 | this.isLeftMouseButtonPressed = false;
|
---|
151 | this.previousMouseLocation = Point.Empty;
|
---|
152 | return false;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (e.Button == MouseButtons.Left)
|
---|
156 | {
|
---|
157 | this.isLeftMouseButtonPressed = true;
|
---|
158 | this.previousMouseLocation =
|
---|
159 | Point.Round(Controller.View.WorldToView(e.Location));
|
---|
160 | this.initialLocation = previousMouseLocation;
|
---|
161 | return true;
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | this.isLeftMouseButtonPressed = false;
|
---|
166 | this.previousMouseLocation = Point.Empty;
|
---|
167 | return false;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | // ------------------------------------------------------------------
|
---|
172 | /// <summary>
|
---|
173 | /// Pans the diagram by the offset amount of the last mouse position
|
---|
174 | /// and the new mouse position divided by 2 (to slow things down a
|
---|
175 | /// bit; otherwise the "distance" panned is too much).
|
---|
176 | /// </summary>
|
---|
177 | /// <param name="e">MouseEventArgs</param>
|
---|
178 | // ------------------------------------------------------------------
|
---|
179 | public void MouseMove(MouseEventArgs e)
|
---|
180 | {
|
---|
181 | Point currentLocation =
|
---|
182 | Point.Round(Controller.View.WorldToView(e.Location));
|
---|
183 | if ((!IsSuspended) &&
|
---|
184 | (IsActive) &&
|
---|
185 | (this.isLeftMouseButtonPressed))
|
---|
186 | {
|
---|
187 | // Change the cursor to indicated which direction we're
|
---|
188 | // panning.
|
---|
189 | if (mouseMoveNumber == 0)
|
---|
190 | {
|
---|
191 | UpdateCursor(currentLocation);
|
---|
192 | }
|
---|
193 | mouseMoveNumber++;
|
---|
194 | if (mouseMoveNumber > 5)
|
---|
195 | {
|
---|
196 | mouseMoveNumber = 0;
|
---|
197 | }
|
---|
198 |
|
---|
199 | IDiagramControl control = Controller.ParentControl;
|
---|
200 | Point origin = Controller.View.Origin;
|
---|
201 |
|
---|
202 | Point offset = new Point(
|
---|
203 | (previousMouseLocation.X - currentLocation.X),
|
---|
204 | (previousMouseLocation.Y - currentLocation.Y));
|
---|
205 |
|
---|
206 | origin.Offset(offset);
|
---|
207 |
|
---|
208 | // 0,0 is the min scrolling point.
|
---|
209 | if (origin.X < 0)
|
---|
210 | {
|
---|
211 | origin.X = 0;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (origin.Y < 0)
|
---|
215 | {
|
---|
216 | origin.Y = 0;
|
---|
217 | }
|
---|
218 | control.AutoScrollPosition = origin;
|
---|
219 | Controller.View.Origin = origin;
|
---|
220 | previousMouseLocation = currentLocation;
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | // ------------------------------------------------------------------
|
---|
225 | /// <summary>
|
---|
226 | /// Stops the panning action (does not deactivate the tool).
|
---|
227 | /// </summary>
|
---|
228 | /// <param name="e">MouseEventArgs</param>
|
---|
229 | // ------------------------------------------------------------------
|
---|
230 | public void MouseUp(MouseEventArgs e)
|
---|
231 | {
|
---|
232 | if ((IsActive) && (!IsSuspended))
|
---|
233 | {
|
---|
234 | this.isLeftMouseButtonPressed = false;
|
---|
235 | this.previousMouseLocation = Point.Empty;
|
---|
236 | Cursor = CursorPalette.Pan;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | #endregion
|
---|
241 |
|
---|
242 | #region IKeyboardListener Members
|
---|
243 |
|
---|
244 | // ------------------------------------------------------------------
|
---|
245 | /// <summary>
|
---|
246 | /// Implementation of IKeyboardListener - deactivates this tool when
|
---|
247 | /// the 'escape' key is pressed.
|
---|
248 | /// </summary>
|
---|
249 | /// <param name="e">KeyEventArgs</param>
|
---|
250 | // ------------------------------------------------------------------
|
---|
251 | public void KeyDown(KeyEventArgs e)
|
---|
252 | {
|
---|
253 | if (e.KeyCode == Keys.Escape)
|
---|
254 | {
|
---|
255 | DeactivateTool();
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | // ------------------------------------------------------------------
|
---|
260 | /// <summary>
|
---|
261 | /// Implementation of IKeyboardListener - nothing performed here.
|
---|
262 | /// </summary>
|
---|
263 | /// <param name="e">KeyEventArgs</param>
|
---|
264 | // ------------------------------------------------------------------
|
---|
265 | public void KeyUp(KeyEventArgs e)
|
---|
266 | {
|
---|
267 | }
|
---|
268 |
|
---|
269 | // ------------------------------------------------------------------
|
---|
270 | /// <summary>
|
---|
271 | /// Implementation of IKeyboardListener - nothing performed here.
|
---|
272 | /// </summary>
|
---|
273 | /// <param name="e">KeyPressEventArgs</param>
|
---|
274 | // ------------------------------------------------------------------
|
---|
275 | public void KeyPress(KeyPressEventArgs e)
|
---|
276 | {
|
---|
277 | }
|
---|
278 |
|
---|
279 | #endregion
|
---|
280 | }
|
---|
281 | }
|
---|