[2768] | 1 | using System;
|
---|
| 2 | using System.Diagnostics;
|
---|
| 3 | using System.Collections;
|
---|
| 4 | using System.Collections.Generic;
|
---|
| 5 | using System.Drawing;
|
---|
| 6 | using System.Windows.Forms;
|
---|
| 7 |
|
---|
| 8 | namespace Netron.Diagramming.Core
|
---|
| 9 | {
|
---|
| 10 |
|
---|
| 11 | class MultiLineTool : AbstractTool, IMouseListener, IKeyboardListener
|
---|
| 12 | {
|
---|
| 13 |
|
---|
| 14 | #region Fields
|
---|
| 15 | /// <summary>
|
---|
| 16 | /// the location of the mouse when the motion starts
|
---|
| 17 | /// </summary>
|
---|
| 18 |
|
---|
| 19 | private bool doDraw;
|
---|
| 20 | private Point[] points;
|
---|
| 21 | #endregion
|
---|
| 22 |
|
---|
| 23 | #region Constructor
|
---|
| 24 | /// <summary>
|
---|
| 25 | /// Initializes a new instance of the <see cref="T:MultiLineTool"/> class.
|
---|
| 26 | /// </summary>
|
---|
| 27 | /// <param name="name">The name of the tool.</param>
|
---|
| 28 | public MultiLineTool(string name)
|
---|
| 29 | : base(name)
|
---|
| 30 | {
|
---|
| 31 | }
|
---|
| 32 | #endregion
|
---|
| 33 |
|
---|
| 34 | #region Methods
|
---|
| 35 |
|
---|
| 36 | /// <summary>
|
---|
| 37 | /// Called when the tool is activated.
|
---|
| 38 | /// </summary>
|
---|
| 39 | protected override void OnActivateTool()
|
---|
| 40 | {
|
---|
| 41 | Controller.View.CurrentCursor = CursorPalette.Cross;
|
---|
| 42 | this.SuspendOtherTools();
|
---|
| 43 | doDraw = false;
|
---|
| 44 | points = new Point[1];
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /// <summary>
|
---|
| 48 | /// Handles the mouse down event
|
---|
| 49 | /// </summary>
|
---|
| 50 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 51 | /// <returns>Returns 'true' if the event was handled, otherwise 'false'.</returns>
|
---|
| 52 | public bool MouseDown(MouseEventArgs e)
|
---|
| 53 | {
|
---|
| 54 | if (e == null)
|
---|
| 55 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
| 56 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended)
|
---|
| 57 | {
|
---|
| 58 | Point p = e.Location;
|
---|
| 59 | if (Closure(p))
|
---|
| 60 | {
|
---|
| 61 | points[points.Length - 1] = points[0];
|
---|
| 62 | doDraw = false;
|
---|
| 63 | DeactivateTool();
|
---|
| 64 | Package();
|
---|
| 65 |
|
---|
| 66 | return true;
|
---|
| 67 | }
|
---|
| 68 | Array.Resize<Point>(ref points, points.Length + 1);
|
---|
| 69 | points[points.Length - 2] = p;
|
---|
| 70 | doDraw = true;
|
---|
| 71 | return true;
|
---|
| 72 | }
|
---|
| 73 | return false;
|
---|
| 74 | }
|
---|
| 75 | private bool Closure(Point p)
|
---|
| 76 | {
|
---|
| 77 | for (int k = 0; k < points.Length - 1; k++)
|
---|
| 78 | {
|
---|
| 79 | if (new Rectangle(points[k].X - 6, points[k].Y - 6, 12, 12).Contains(p))
|
---|
| 80 | {
|
---|
| 81 | DialogResult res = MessageBox.Show("Do you wish to close the curve?", "Closure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
---|
| 82 | if (res == DialogResult.Yes)
|
---|
| 83 | return true;
|
---|
| 84 |
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | return false;
|
---|
| 88 | }
|
---|
| 89 | /// <summary>
|
---|
| 90 | /// Handles the mouse move event
|
---|
| 91 | /// </summary>
|
---|
| 92 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 93 | public void MouseMove(MouseEventArgs e)
|
---|
| 94 | {
|
---|
| 95 | if (e == null)
|
---|
| 96 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
| 97 | Point point = e.Location;
|
---|
| 98 | if (IsActive && doDraw)
|
---|
| 99 | {
|
---|
| 100 | points[points.Length - 1] = e.Location;
|
---|
| 101 | Controller.View.PaintGhostLine(MultiPointType.Straight, points);
|
---|
| 102 | //Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(Controller.View.Ghost.Rectangle, 20, 20));
|
---|
| 103 | //TODO: find a more performant way to invalidate the area
|
---|
| 104 | Controller.View.Invalidate();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | /// <summary>
|
---|
| 108 | ///
|
---|
| 109 | /// </summary>
|
---|
| 110 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
| 111 | public void MouseUp(MouseEventArgs e)
|
---|
| 112 | {
|
---|
| 113 | if (IsActive)
|
---|
| 114 | {
|
---|
| 115 |
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | private void Package()
|
---|
| 119 | {
|
---|
| 120 | if ((points == null) ||
|
---|
| 121 | (points.Length < 1) ||
|
---|
| 122 | (points[0] == Point.Empty))
|
---|
| 123 | {
|
---|
| 124 | return;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | MultiPointShape shape = new MultiPointShape(
|
---|
| 128 | this.Controller.Model,
|
---|
| 129 | points,
|
---|
| 130 | MultiPointType.Straight);
|
---|
| 131 |
|
---|
| 132 | AddMultiPointShapeCommand cmd = new AddMultiPointShapeCommand(this.Controller, shape);
|
---|
| 133 | this.Controller.UndoManager.AddUndoCommand(cmd);
|
---|
| 134 | this.Controller.View.ResetGhost();
|
---|
| 135 | cmd.Redo();
|
---|
| 136 | }
|
---|
| 137 | #endregion
|
---|
| 138 |
|
---|
| 139 | #region IKeyboardListener Members
|
---|
| 140 |
|
---|
| 141 | public void KeyDown(KeyEventArgs e)
|
---|
| 142 | {
|
---|
| 143 | if (e.KeyData == Keys.Escape && IsActive)
|
---|
| 144 | {
|
---|
| 145 | DeactivateTool();
|
---|
| 146 | Package();
|
---|
| 147 | e.Handled = true;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | public void KeyUp(KeyEventArgs e)
|
---|
| 152 | {
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public void KeyPress(KeyPressEventArgs e)
|
---|
| 156 | {
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | #endregion
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | }
|
---|