Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/PanTool.cs @ 3173

Last change on this file since 3173 was 3173, checked in by mkommend, 14 years ago

added panning by pressing the space bar (ticket #867)

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