Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/22/10 14:24:03 (14 years ago)
Author:
mkommend
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/PanTool.cs

    r2768 r3173  
    66using System.Diagnostics;
    77
    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     }
     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  }
    281247}
Note: See TracChangeset for help on using the changeset viewer.