Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/MultiLineTool.cs

Last change on this file was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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