[2768] | 1 | using System;
|
---|
| 2 | using System.Drawing;
|
---|
| 3 | using System.Windows.Forms;
|
---|
| 4 |
|
---|
[4068] | 5 | namespace Netron.Diagramming.Core {
|
---|
| 6 | /// <summary>
|
---|
| 7 | /// This tool implement the action of moving shape connectors on the canvas.
|
---|
| 8 | /// </summary>
|
---|
| 9 | class ConnectorMoverTool : AbstractTool, IMouseListener {
|
---|
| 10 |
|
---|
| 11 | #region Fields
|
---|
[2768] | 12 | /// <summary>
|
---|
[4068] | 13 | /// the location of the mouse when the motion starts
|
---|
[2768] | 14 | /// </summary>
|
---|
[4068] | 15 | private Point initialPoint;
|
---|
| 16 | /// <summary>
|
---|
| 17 | /// the intermediate location of the mouse during the motion
|
---|
| 18 | /// </summary>
|
---|
| 19 | private Point lastPoint;
|
---|
| 20 | /// <summary>
|
---|
| 21 | /// the connector being moved
|
---|
| 22 | /// </summary>
|
---|
| 23 | private IConnector fetchedConnector;
|
---|
| 24 | /// <summary>
|
---|
| 25 | /// whether we are currently moving something
|
---|
| 26 | /// </summary>
|
---|
| 27 | private bool motionStarted;
|
---|
[2768] | 28 |
|
---|
[4068] | 29 | #endregion
|
---|
[2768] | 30 |
|
---|
[4068] | 31 | #region Constructor
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Initializes a new instance of the <see cref="T:ConnectorMoverTool"/> class.
|
---|
| 34 | /// </summary>
|
---|
| 35 | /// <param name="name">The name of the tool.</param>
|
---|
| 36 | public ConnectorMoverTool(string name)
|
---|
| 37 | : base(name) {
|
---|
| 38 | }
|
---|
| 39 | #endregion
|
---|
[2768] | 40 |
|
---|
[4068] | 41 | #region Methods
|
---|
[2768] | 42 |
|
---|
[4068] | 43 | /// <summary>
|
---|
| 44 | /// Called when the tool is activated.
|
---|
| 45 | /// </summary>
|
---|
| 46 | protected override void OnActivateTool() {
|
---|
| 47 | Controller.View.CurrentCursor = CursorPalette.Select;
|
---|
| 48 | motionStarted = false;
|
---|
| 49 | fetchedConnector = null;
|
---|
| 50 | }
|
---|
[2768] | 51 |
|
---|
[4068] | 52 | /// <summary>
|
---|
| 53 | /// Handles the mouse down event
|
---|
| 54 | /// </summary>
|
---|
| 55 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 56 | public bool MouseDown(MouseEventArgs e) {
|
---|
| 57 | if (e == null)
|
---|
| 58 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
| 59 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended) {
|
---|
| 60 | fetchedConnector = this.Controller.Model.Selection.FindShapeConnector(e.Location);
|
---|
| 61 | if (fetchedConnector != null) {
|
---|
[2768] | 62 |
|
---|
[4068] | 63 | initialPoint = e.Location;
|
---|
| 64 | lastPoint = initialPoint;
|
---|
| 65 | motionStarted = true;
|
---|
| 66 | return true;
|
---|
[2768] | 67 | }
|
---|
[4068] | 68 | }
|
---|
| 69 | return false;
|
---|
| 70 | }
|
---|
[2768] | 71 |
|
---|
[4068] | 72 | /// <summary>
|
---|
| 73 | /// Handles the mouse move event
|
---|
| 74 | /// </summary>
|
---|
| 75 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 76 | public void MouseMove(MouseEventArgs e) {
|
---|
| 77 | if (e == null)
|
---|
| 78 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
| 79 | Point point = e.Location;
|
---|
| 80 | if (IsActive && motionStarted) {
|
---|
[2768] | 81 |
|
---|
[4068] | 82 | fetchedConnector.MoveBy(new Point(point.X - lastPoint.X, point.Y - lastPoint.Y));
|
---|
| 83 |
|
---|
| 84 | lastPoint = point;
|
---|
| 85 | }
|
---|
[2768] | 86 | }
|
---|
[4068] | 87 | /// <summary>
|
---|
| 88 | /// Handles the mouse up event
|
---|
| 89 | /// </summary>
|
---|
| 90 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 91 | public void MouseUp(MouseEventArgs e) {
|
---|
| 92 | if (IsActive) {
|
---|
| 93 | DeactivateTool();
|
---|
| 94 | if (fetchedConnector == null)
|
---|
| 95 | return;
|
---|
| 96 | Bundle bundle = new Bundle(Controller.Model);
|
---|
| 97 | bundle.Entities.Add(fetchedConnector);
|
---|
| 98 | MoveCommand cmd = new MoveCommand(this.Controller, bundle, new Point(lastPoint.X - initialPoint.X, lastPoint.Y - initialPoint.Y));
|
---|
| 99 | Controller.UndoManager.AddUndoCommand(cmd);
|
---|
| 100 | //not necessary to perform the Redo action of the command since the mouse-move already moved the bundle!
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | #endregion
|
---|
| 104 | }
|
---|
[2768] | 105 |
|
---|
| 106 | }
|
---|