Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Diagram elements/Shapes/MultiPointShape.cs @ 2768

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

added solution folders and sources for the netron library (ticket #867)

File size: 7.8 KB
Line 
1using System;
2using System.Drawing;
3using System.Drawing.Drawing2D;
4namespace Netron.Diagramming.Core
5{
6    /// <summary>
7    /// A mutli-point curve whose appearance depends on some predefined <see cref="MultiPointType"/> type.
8    /// </summary>
9    public partial class MultiPointShape : SimpleShapeBase
10    {
11        #region Fields
12
13        // ------------------------------------------------------------------
14        /// <summary>
15        /// Implementation of IVersion - the current version of
16        /// MultiPointShape.
17        /// </summary>
18        // ------------------------------------------------------------------
19        protected const double multiPointShapeVersion = 1.0;
20
21        private MultiPointType mCurveType = MultiPointType.Straight;
22        private Point[] mPoints;
23        #endregion
24
25        #region Properties
26
27        // ------------------------------------------------------------------
28        /// <summary>
29        /// Gets the current version.
30        /// </summary>
31        // ------------------------------------------------------------------
32        public override double Version
33        {
34            get
35            {
36                return multiPointShapeVersion;
37            }
38        }
39
40        /// <summary>
41        /// Gets or sets the points on which the curve is based.
42        /// </summary>
43        /// <value>The points.</value>
44        public Point[] Points
45        {
46            get
47            {
48                return mPoints;
49            }
50            set
51            {
52                mPoints = value;
53                CalculateRectangle();
54            }
55        }
56
57        /// <summary>
58        /// Gets or sets the type of the curve.
59        /// </summary>
60        /// <value>The type of the curve.</value>
61        public MultiPointType CurveType
62        {
63            get
64            {
65                return mCurveType;
66            }
67            set
68            {
69                mCurveType = value;
70            }
71        }
72        /// <summary>
73        /// Gets the friendly name of the entity to be displayed in the UI
74        /// </summary>
75        /// <value></value>
76        public override string EntityName
77        {
78            get { return "MultiPoint Curve"; }
79        }
80        #endregion
81
82        #region Constructor
83        /// <summary>
84        /// Default ctor
85        /// </summary>
86        /// <param name="s">The s.</param>
87        /// <param name="points">The points.</param>
88        /// <param name="curveType">Type of the curve.</param>
89        public MultiPointShape(IModel s, Point[] points, MultiPointType curveType)
90            : base(s)
91        {
92            this.mPaintStyle = new SolidPaintStyle(Color.Transparent);
93            this.mPenStyle = new PenStyle(Color.Black, DashStyle.Solid, 1);
94            mPoints = points;
95            mCurveType = curveType;
96            CalculateRectangle();
97        }
98        /// <summary>
99        /// Initializes a new instance of the <see cref="T:MultiPointShape"/> class.
100        /// </summary>
101        public MultiPointShape()
102            : base()
103        {
104            this.mPaintStyle = new SolidPaintStyle(Color.Transparent);
105            this.mPenStyle = new PenStyle(Color.Black, DashStyle.Solid, 1);
106        }
107
108
109
110        #endregion
111
112        #region Methods
113
114        /// <summary>
115        /// Calculates the bounding rectangle of the points
116        /// </summary>
117        private void CalculateRectangle()
118        {
119            //presume the extreme opposite
120            Point lt = new Point(int.MaxValue, int.MaxValue);
121            Point rb = Point.Empty;
122            Point p;
123            for (int k = 0; k < mPoints.Length; k++)
124            {
125                p = mPoints[k];
126                if (p.X < lt.X)
127                    lt.X = p.X;
128                if (p.Y < lt.Y)
129                    lt.Y = p.Y;
130                if (p.Y > rb.Y)
131                    rb.Y = p.Y;
132                if (p.X > rb.X)
133                    rb.X = p.X;
134            }
135            //note that calling the other overload causes problems.
136            //While usually the Rectangle dictates the shape,
137            //here the Rectangle is deduced from the shape.
138            //This is a bit tricky....
139            base.Transform(lt.X, lt.Y, rb.X - lt.X, rb.Y - lt.Y);
140        }
141
142        /// <summary>
143        /// Paints the bundle on the canvas
144        /// </summary>
145        /// <param name="g"></param>
146        public override void Paint(Graphics g)
147        {
148
149            GraphicsPath path = new GraphicsPath();
150            GraphicsPath shadow = new GraphicsPath();
151
152            g.SmoothingMode = SmoothingMode.HighQuality;
153            //the shadow
154
155            switch (mCurveType)
156            {
157                case MultiPointType.Straight:
158                    //g.DrawLines(ArtPalette.BlackPen, mPoints);
159                    path.AddLines(mPoints);
160                    break;
161                case MultiPointType.Polygon:
162                    //g.DrawPolygon(ArtPalette.BlackPen, mPoints);
163                    path.AddPolygon(mPoints);
164                    break;
165                case MultiPointType.Curve:
166                    //note that you can specify a tension of the curve here (greater than 0.0F)
167                    //g.DrawCurve(ArtPalette.BlackPen, mPoints);
168                    path.AddCurve(mPoints);
169                    break;
170            }
171
172            if (ArtPalette.EnableShadows)
173            {
174                shadow = (GraphicsPath) path.Clone();
175                Matrix m = new Matrix();
176                m.Translate(5, 5);
177                shadow.Transform(m);
178                g.FillPath(ArtPalette.ShadowBrush, shadow);
179            }
180
181            g.FillPath(this.mPaintStyle.GetBrush(Rectangle), path);
182
183            if (IsSelected || Hovered)
184            {
185                g.DrawPath(ArtPalette.HighlightPen, path);
186            }
187            else
188            {
189                g.DrawPath(this.mPenStyle.DrawingPen(), path);
190            }
191        }
192
193        /// <summary>
194        /// Maps the shape to another rectangle, including all its sub-entities and materials.
195        /// </summary>
196        /// <param name="x">The x.</param>
197        /// <param name="y">The y.</param>
198        /// <param name="width">The width.</param>
199        /// <param name="height">The height.</param>
200        public override void Transform(int x, int y, int width, int height)
201        {
202            //transform the curve points
203            double a, b;
204            Point p;
205            for (int k = 0; k < mPoints.Length; k++)
206            {
207                a = Math.Round(((double)mPoints[k].X - (double)Rectangle.X) / (double)Rectangle.Width, 1) * width + x - mPoints[k].X;
208                b = Math.Round(((double)mPoints[k].Y - (double)Rectangle.Y) / (double)Rectangle.Height, 1) * height + y - mPoints[k].Y;
209                p = new Point(Convert.ToInt32(a), Convert.ToInt32(b));
210                mPoints[k].Offset(p);
211            }
212
213
214            base.Transform(x, y, width, height);
215        }
216
217        /// <summary>
218        /// Moves the entity with the given shift vector
219        /// </summary>
220        /// <param name="p">Represent a shift-vector, not the absolute position!</param>
221        public override void MoveBy(Point p)
222        {
223
224            for (int k = 0; k < mPoints.Length; k++)
225            {
226                mPoints[k].Offset(p);
227            }
228            base.MoveBy(p);
229        }
230
231
232
233        #endregion
234    }
235
236    public enum MultiPointType
237    {
238        /// <summary>
239        /// Straight line interpolation.
240        /// </summary>
241        Straight,
242        /// <summary>
243        /// Plogonal interpolation.
244        /// </summary>
245        Polygon,
246        /// <summary>
247        /// Bezier-like interpolation.
248        /// </summary>
249        Curve
250    }
251}
Note: See TracBrowser for help on using the repository browser.