Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/HeuristicLab.Netron-3.0.2672.12446/View.cs @ 2781

Last change on this file since 2781 was 2781, checked in by mkommend, 14 years ago

added netron specific extensions (ticket #867)

File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Netron.Diagramming.Core;
6using System.Drawing;
7using System.Drawing.Drawing2D;
8
9namespace HeuristicLab.Netron {
10  public class View : ViewBase {
11
12    public View(IDiagramControl control)
13      : base(control) {
14      Selection.OnNewSelection += new EventHandler(Selection_OnNewSelection);
15      // this.Model.OnEntityAdded += new EventHandler<EntityEventArgs>(Model_OnEntityAdded);
16      this.HorizontalRuler.Visible = true;
17      this.VerticalRuler.Visible = true;
18      GhostsFactory.View = this;
19    }
20
21    void Model_OnEntityAdded(object sender, EntityEventArgs e) {
22
23    }
24
25    void Selection_OnNewSelection(object sender, EventArgs e) {
26      ShowTracker();
27    }
28
29    public override void Paint(Graphics g) {
30      base.Paint(g);
31      //Rectangle rectangle = WorkArea;
32      //g.SetClip(WorkArea);
33      g.Transform = ViewMatrix;
34      //draw the ghost and ants on top of the diagram
35      if (Ants != null)
36        Ants.Paint(g);
37      if (Ghost != null)
38        Ghost.Paint(g);
39      if (Tracker != null)
40        Tracker.Paint(g);
41
42      g.Transform.Reset();
43      //g.PageUnit = GraphicsUnit.Pixel;
44      //g.PageScale = 1.0F;
45    }
46
47    protected virtual void InvalidateGhostArea() {
48      if (Ghost != null) {
49        Rectangle area = Ghost.Rectangle;
50        // Inflate it a little so we make sure to get everything.
51        area.Inflate(20, 20);
52        this.Invalidate(area);
53      }
54    }
55
56    protected virtual void InvalidateTrackerArea() {
57      if (Tracker != null) {
58        Rectangle area = Tracker.Rectangle;
59        // Inflate it a little so we make sure to get everything.
60        area.Inflate(20, 20);
61        this.Invalidate(area);
62      }
63    }
64
65    protected virtual void InvalidateAntsArea() {
66      if (Ants != null) {
67        Rectangle area = Ants.Rectangle;
68        // Inflate it a little so we make sure to get everything.
69        area.Inflate(20, 20);
70        this.Invalidate(area);
71      }
72    }
73
74    public override void PaintGhostEllipse(
75        Point ltPoint,
76        Point rbPoint) {
77      // Refresh the old ghost area if needed.
78      this.InvalidateGhostArea();
79
80      Ghost = GhostsFactory.GetGhost(
81          new Point[] { ltPoint, rbPoint },
82              GhostTypes.Ellipse);
83    }
84    public override void PaintGhostRectangle(
85        Point ltPoint,
86        Point rbPoint) {
87      // Refresh the old ghost area if needed.
88      this.InvalidateGhostArea();
89
90      Ghost = GhostsFactory.GetGhost(
91          new Point[] { ltPoint, rbPoint },
92          GhostTypes.Rectangle);
93    }
94    public override void PaintAntsRectangle(
95        Point ltPoint,
96        Point rbPoint) {
97      // Refresh the old area if needed.
98      this.InvalidateAntsArea();
99      Ants = AntsFactory.GetAnts(
100          new Point[] { ltPoint, rbPoint },
101          AntTypes.Rectangle);
102    }
103    public override void PaintGhostLine(Point ltPoint, Point rbPoint) {
104      // Refresh the old ghost area if needed.
105      this.InvalidateGhostArea();
106
107      Ghost = GhostsFactory.GetGhost(
108          new Point[] { ltPoint, rbPoint },
109          GhostTypes.Line);
110    }
111    public override void PaintGhostLine(
112        MultiPointType curveType,
113        Point[] points) {
114      // Refresh the old ghost area if needed.
115      this.InvalidateGhostArea();
116
117      switch (curveType) {
118        case MultiPointType.Straight:
119          Ghost = GhostsFactory.GetGhost(points, GhostTypes.MultiLine);
120          break;
121        case MultiPointType.Polygon:
122          Ghost = GhostsFactory.GetGhost(points, GhostTypes.Polygon);
123          break;
124        case MultiPointType.Curve:
125          Ghost = GhostsFactory.GetGhost(points, GhostTypes.CurvedLine);
126          break;
127
128      }
129    }
130
131    public override void PaintTracker(Rectangle rectangle, bool showHandles) {
132      // Refresh the old area if needed.
133      this.InvalidateTrackerArea();
134      Tracker = TrackerFactory.GetTracker(rectangle, TrackerTypes.Default, showHandles);
135      rectangle.Inflate(20, 20);
136      this.Invalidate(rectangle);
137    }
138
139    #region Tracker
140
141    private enum TrackerTypes {
142      Default
143    }
144
145    private class TrackerFactory {
146
147      private static ITracker defTracker;
148
149      public static ITracker GetTracker(Rectangle rectangle, TrackerTypes type, bool showHandles) {
150        switch (type) {
151          case TrackerTypes.Default:
152            if (defTracker == null) defTracker = new DefaultTracker();
153            defTracker.Transform(rectangle);
154            defTracker.ShowHandles = showHandles;
155            return defTracker;
156          default:
157            return null;
158        }
159
160      }
161    }
162
163    private class DefaultTracker : TrackerBase {
164      private const int gripSize = 4;
165      private const int hitSize = 6;
166      float mx, my, sx, sy;
167
168      public DefaultTracker(Rectangle rectangle)
169        : base(rectangle) {
170      }
171
172      public DefaultTracker()
173        : base() { }
174
175      public override void Transform(Rectangle rectangle) {
176        this.Rectangle = rectangle;
177      }
178
179      public override void Paint(Graphics g) {
180        //the main rectangle
181        g.DrawRectangle(ArtPalette.TrackerPen, Rectangle);
182        #region Recalculate the size and location of the grips
183        mx = Rectangle.X + Rectangle.Width / 2;
184        my = Rectangle.Y + Rectangle.Height / 2;
185        sx = Rectangle.Width / 2;
186        sy = Rectangle.Height / 2;
187        #endregion
188        #region draw the grips
189        if (!ShowHandles) return;
190
191        for (int x = -1; x <= 1; x++) {
192          for (int y = -1; y <= 1; y++) {
193            if (x != 0 || y != 0) //not the middle one
194                        {
195              g.FillRectangle(ArtPalette.GripBrush, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
196              g.DrawRectangle(ArtPalette.BlackPen, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
197            }
198          }
199        }
200        #endregion
201      }
202
203      public override Point Hit(Point p) {
204        //no need to test if the handles are not shown
205        if (!ShowHandles) return Point.Empty;
206
207        for (int x = -1; x <= +1; x++)
208          for (int y = -1; y <= +1; y++)
209            if ((x != 0) || (y != 0)) {
210              if (new RectangleF(mx + x * sx - hitSize / 2, my + y * sy - hitSize / 2, hitSize, hitSize).Contains(p))
211                return new Point(x, y);
212            }
213        return Point.Empty;
214      }
215    }
216    #endregion
217  }
218}
Note: See TracBrowser for help on using the repository browser.