Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1265_HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/LinearPrimitiveBase.cs @ 16317

Last change on this file since 16317 was 13717, checked in by bburlacu, 9 years ago

#1265: Updated license year to 2016.

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using System.Drawing.Drawing2D;
25using System.Windows.Forms;
26
27namespace HeuristicLab.Visualization {
28  public abstract class LinearPrimitiveBase : PrimitiveBase {
29    protected IGroup SelectionRectangles;
30
31    private PointD myStart;
32    public virtual PointD Start {
33      get { return myStart; }
34    }
35    private PointD myEnd;
36    public virtual PointD End {
37      get { return myEnd; }
38    }
39    public virtual SizeD Size {
40      get { return new SizeD(End.X - Start.X, End.Y - Start.Y); }
41    }
42    public virtual double Length {
43      get { return (Start - End).Length; }
44    }
45
46    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end)
47      : base(chart) {
48      SelectionRectangles = new Group(chart);
49      myStart = start;
50      myEnd = end;
51    }
52    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end, Pen pen, Brush brush)
53      : base(chart, pen, brush) {
54      SelectionRectangles = new Group(chart);
55      myStart = start;
56      myEnd = end;
57    }
58
59    public virtual void SetPosition(PointD start, PointD end) {
60      myStart = start;
61      myEnd = end;
62      OnRedrawRequired();
63    }
64
65    public override void Move(Offset delta) {
66      SetPosition(Start + delta, End + delta);
67    }
68
69    public override void Move(PointD point, Offset delta) {
70      if (Selected && SelectionRectangles.ContainsPoint(point)) {
71        var rect = (SelectionRectangle)SelectionRectangles.GetPrimitive(point);
72        if (rect.Point == Start) {
73          SetPosition(Start + delta, End);
74        } else if (rect.Point == End) {
75          SetPosition(Start, End + delta);
76        }
77      } else SetPosition(Start + delta, End + delta);
78    }
79
80    public override void SnapToGrid(IGrid grid) {
81      var bl = grid.GetBottomLeftGridPoint(Start);
82      var ur = grid.GetUpperRightGridPoint(Start);
83      var sx = Math.Abs(bl.X - Start.X) <= Math.Abs(ur.X - Start.X) ? bl.X : ur.X;
84      var sy = Math.Abs(bl.Y - Start.Y) <= Math.Abs(ur.Y - Start.Y) ? bl.Y : ur.Y;
85
86      var end = new PointD(End.X + (sx - Start.X), End.Y + (sy - Start.Y));
87
88      bl = grid.GetBottomLeftGridPoint(end);
89      ur = grid.GetUpperRightGridPoint(end);
90      var ex = Math.Abs(bl.X - end.X) <= Math.Abs(ur.X - end.X) ? bl.X : ur.X;
91      var ey = Math.Abs(bl.Y - end.Y) <= Math.Abs(ur.Y - end.Y) ? bl.Y : ur.Y;
92
93      SetPosition(new PointD(sx, sy), new PointD(ex, ey));
94    }
95
96    public override void SnapToGrid(PointD point, IGrid grid) {
97      if (Selected && SelectionRectangles.ContainsPoint(point)) {
98        var rect = (SelectionRectangle)SelectionRectangles.GetPrimitive(point);
99        if (rect.Point == Start) {
100          var bl = grid.GetBottomLeftGridPoint(Start);
101          var ur = grid.GetUpperRightGridPoint(Start);
102          var sx = Math.Abs(bl.X - Start.X) <= Math.Abs(ur.X - Start.X) ? bl.X : ur.X;
103          var sy = Math.Abs(bl.Y - Start.Y) <= Math.Abs(ur.Y - Start.Y) ? bl.Y : ur.Y;
104          SetPosition(new PointD(sx, sy), End);
105        } else if (rect.Point == End) {
106          var bl = grid.GetBottomLeftGridPoint(End);
107          var ur = grid.GetUpperRightGridPoint(End);
108          var ex = Math.Abs(bl.X - End.X) <= Math.Abs(ur.X - End.X) ? bl.X : ur.X;
109          var ey = Math.Abs(bl.Y - End.Y) <= Math.Abs(ur.Y - End.Y) ? bl.Y : ur.Y;
110          SetPosition(Start, new PointD(ex, ey));
111        }
112      } else SnapToGrid(grid);
113    }
114
115    public override bool ContainsPoint(PointD point) {
116      if (Selected && SelectionRectangles.GetPrimitive(point) != null) return true;
117      return false;
118    }
119
120    public override Cursor GetCursor(PointD point) {
121      if (Selected) {
122        var cursor = SelectionRectangles.GetCursor(point);
123        if (cursor != null) return cursor;
124      }
125      return base.GetCursor(point);
126    }
127
128    public override void PostDraw(Graphics graphics) {
129      SelectionRectangles.Clear();
130      if (Selected) {
131        var pen = new Pen(Color.LightGray, 3) { DashStyle = DashStyle.Dash };
132        graphics.DrawLine(pen,
133                          Chart.TransformWorldToPixel(Start),
134                          Chart.TransformWorldToPixel(End));
135        SelectionRectangles.Add(new SelectionRectangle(Chart, Start, Cursors.SizeAll));
136        SelectionRectangles.Add(new SelectionRectangle(Chart, End, Cursors.SizeAll));
137
138        SelectionRectangles.Draw(graphics);
139      }
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.