Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/ZoomListener.cs @ 1346

Last change on this file since 1346 was 1249, checked in by bspisic, 15 years ago

reimplemented panning and zooming (#424)

File size: 1.9 KB
Line 
1using System.Drawing;
2using System.Windows.Forms;
3
4namespace HeuristicLab.Visualization {
5  public class ZoomListener : IMouseEventListener {
6    private readonly Point startPoint;
7
8    public event RectangleHandler DrawRectangle;
9    public event RectangleHandler SetClippingArea;
10
11    public ZoomListener(Point startPoint) {
12      this.startPoint = startPoint;
13    }
14
15    #region IMouseEventListener Members
16
17    public void MouseMove(object sender, MouseEventArgs e) {
18      if(DrawRectangle != null) {
19        DrawRectangle(CalcRectangle(e.Location));
20      }
21    }
22
23    public void MouseUp(object sender, MouseEventArgs e) {
24     if(SetClippingArea != null) {
25       SetClippingArea(CalcRectangle(e.Location));
26     }
27    }
28
29    #endregion
30
31    private Rectangle CalcRectangle(Point actualPoint) {
32      Rectangle rectangle = new Rectangle();
33
34      if (startPoint.X < actualPoint.X) {
35        rectangle.X = startPoint.X;
36        rectangle.Width = actualPoint.X - startPoint.X;
37      } else {
38        rectangle.X = actualPoint.X;
39        rectangle.Width = startPoint.X - actualPoint.X;
40      }
41
42      if (startPoint.Y < actualPoint.Y) {
43        rectangle.Y = startPoint.Y;
44        rectangle.Height = actualPoint.Y - startPoint.Y;
45      } else {
46        rectangle.Y = actualPoint.Y;
47        rectangle.Height = startPoint.Y - actualPoint.Y;
48      }
49
50      return rectangle;
51    }
52
53    public static RectangleD ZoomClippingArea(RectangleD clippingArea, double zoomFactor) {
54      double x1, x2, y1, y2, width, height;
55
56      width = clippingArea.Width * zoomFactor;
57      height = clippingArea.Height * zoomFactor;
58
59      x1 = clippingArea.X1 - (width - clippingArea.Width) / 2;
60      y1 = clippingArea.Y1 - (height - clippingArea.Height) / 2;
61      x2 = width + x1;
62      y2 = height + y1;
63
64      return new RectangleD(x1, y1, x2, y2);
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.