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