1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Drawing.Drawing2D;
|
---|
5 | using System.Drawing;
|
---|
6 |
|
---|
7 | namespace Netron.Diagramming.Core
|
---|
8 | {
|
---|
9 | public class Shape : ShapeBase
|
---|
10 | {
|
---|
11 | // ------------------------------------------------------------------
|
---|
12 | /// <summary>
|
---|
13 | /// The collection of shapes.
|
---|
14 | /// </summary>
|
---|
15 | // ------------------------------------------------------------------
|
---|
16 | CollectionBase<IDiagramEntity> myEntities;
|
---|
17 |
|
---|
18 | // ------------------------------------------------------------------
|
---|
19 | /// <summary>
|
---|
20 | /// Gets the friendly name of this shape.
|
---|
21 | /// </summary>
|
---|
22 | // ------------------------------------------------------------------
|
---|
23 | public override string EntityName
|
---|
24 | {
|
---|
25 | get
|
---|
26 | {
|
---|
27 | return "Shape";
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | // ------------------------------------------------------------------
|
---|
32 | /// <summary>
|
---|
33 | /// Gets or set the owned shapes.
|
---|
34 | /// </summary>
|
---|
35 | // ------------------------------------------------------------------
|
---|
36 | public CollectionBase<IDiagramEntity> Entities
|
---|
37 | {
|
---|
38 | get
|
---|
39 | {
|
---|
40 | return myEntities;
|
---|
41 | }
|
---|
42 | set
|
---|
43 | {
|
---|
44 | myEntities = value;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public virtual GraphicsPath GraphicsPath
|
---|
49 | {
|
---|
50 | get
|
---|
51 | {
|
---|
52 | GraphicsPath path = new GraphicsPath();
|
---|
53 | path.AddRectangle(mRectangle);
|
---|
54 | return path;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | // ------------------------------------------------------------------
|
---|
59 | /// <summary>
|
---|
60 | /// Default constructor.
|
---|
61 | /// </summary>
|
---|
62 | // ------------------------------------------------------------------
|
---|
63 | public Shape()
|
---|
64 | : base()
|
---|
65 | {
|
---|
66 | }
|
---|
67 |
|
---|
68 | // ------------------------------------------------------------------
|
---|
69 | /// <summary>
|
---|
70 | /// Constructor that receives the model this shape belongs to.
|
---|
71 | /// </summary>
|
---|
72 | /// <param name="model">IModel</param>
|
---|
73 | // ------------------------------------------------------------------
|
---|
74 | public Shape(IModel model)
|
---|
75 | : base(model)
|
---|
76 | {
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected override void Initialize()
|
---|
80 | {
|
---|
81 | base.Initialize();
|
---|
82 | myEntities = new CollectionBase<IDiagramEntity>();
|
---|
83 | AttachEventsToShapeCollection();
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected virtual void AttachEventsToShapeCollection()
|
---|
87 | {
|
---|
88 | myEntities.OnClear += new EventHandler(OnEntitiesClear);
|
---|
89 | myEntities.OnItemAdded +=
|
---|
90 | new EventHandler<CollectionEventArgs<IDiagramEntity>>(
|
---|
91 | OnEntityAdded);
|
---|
92 | myEntities.OnItemRemoved +=
|
---|
93 | new EventHandler<CollectionEventArgs<IDiagramEntity>>(
|
---|
94 | OnEntityRemoved);
|
---|
95 | }
|
---|
96 |
|
---|
97 | void OnEntityRemoved(object sender, CollectionEventArgs<IDiagramEntity> e)
|
---|
98 | {
|
---|
99 | CalculateRectangle();
|
---|
100 | }
|
---|
101 |
|
---|
102 | void OnEntityAdded(object sender, CollectionEventArgs<IDiagramEntity> e)
|
---|
103 | {
|
---|
104 | if (myEntities.Count == 1)
|
---|
105 | {
|
---|
106 | mRectangle = e.Item.Rectangle;
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | mRectangle = Rectangle.Union(
|
---|
111 | (Rectangle)mRectangle,
|
---|
112 | e.Item.Rectangle);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | void OnEntitiesClear(object sender, EventArgs e)
|
---|
117 | {
|
---|
118 | mRectangle = Rectangle.Empty;
|
---|
119 | }
|
---|
120 |
|
---|
121 | // ------------------------------------------------------------------
|
---|
122 | /// <summary>
|
---|
123 | /// Calculates the bounding rectangle of this shape from all children.
|
---|
124 | /// </summary>
|
---|
125 | // ------------------------------------------------------------------
|
---|
126 | public void CalculateRectangle()
|
---|
127 | {
|
---|
128 | if (myEntities == null || myEntities.Count == 0)
|
---|
129 | return;
|
---|
130 | Rectangle rec = myEntities[0].Rectangle;
|
---|
131 | foreach (IDiagramEntity entity in Entities)
|
---|
132 | {
|
---|
133 | //cascade the calculation if necessary
|
---|
134 | if (entity is IGroup) (entity as IGroup).CalculateRectangle();
|
---|
135 |
|
---|
136 | rec = Rectangle.Union(rec, entity.Rectangle);
|
---|
137 | }
|
---|
138 | this.mRectangle = rec;
|
---|
139 | this.mRectangle.Inflate(20, 20);
|
---|
140 | }
|
---|
141 |
|
---|
142 | // ------------------------------------------------------------------
|
---|
143 | /// <summary>
|
---|
144 | /// Tests whether the group is hit by the mouse
|
---|
145 | /// </summary>
|
---|
146 | /// <param name="p">Point</param>
|
---|
147 | /// <returns>bool</returns>
|
---|
148 | // ------------------------------------------------------------------
|
---|
149 | public override bool Hit(Point p)
|
---|
150 | {
|
---|
151 | foreach (IDiagramEntity entity in myEntities)
|
---|
152 | {
|
---|
153 | if (entity.Hit(p))
|
---|
154 | {
|
---|
155 | return true;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // ------------------------------------------------------------------
|
---|
162 | /// <summary>
|
---|
163 | /// Invalidates the entity
|
---|
164 | /// </summary>
|
---|
165 | // ------------------------------------------------------------------
|
---|
166 | public override void Invalidate()
|
---|
167 | {
|
---|
168 |
|
---|
169 | if (mRectangle == null)
|
---|
170 | return;
|
---|
171 |
|
---|
172 | Rectangle rec = mRectangle;
|
---|
173 | rec.Inflate(20, 20);
|
---|
174 | Model.RaiseOnInvalidateRectangle(rec);
|
---|
175 | }
|
---|
176 |
|
---|
177 | // ------------------------------------------------------------------
|
---|
178 | /// <summary>
|
---|
179 | /// Moves the entity on the canvas
|
---|
180 | /// </summary>
|
---|
181 | /// <param name="p">Point</param>
|
---|
182 | // ------------------------------------------------------------------
|
---|
183 | public override void MoveBy(Point p)
|
---|
184 | {
|
---|
185 |
|
---|
186 | Rectangle recBefore = mRectangle;
|
---|
187 | recBefore.Inflate(20, 20);
|
---|
188 |
|
---|
189 | // No need to invalidate since it'll be done by the individual
|
---|
190 | // move actions.
|
---|
191 | foreach (IDiagramEntity entity in myEntities)
|
---|
192 | {
|
---|
193 | entity.MoveBy(p);
|
---|
194 | }
|
---|
195 | mRectangle.X += p.X;
|
---|
196 | mRectangle.Y += p.Y;
|
---|
197 |
|
---|
198 | //refresh things
|
---|
199 | this.Invalidate(recBefore);//position before the move
|
---|
200 | this.Invalidate();//current position
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | public override void Paint(Graphics g)
|
---|
205 | {
|
---|
206 | base.Paint(g);
|
---|
207 |
|
---|
208 | GraphicsPath path = GraphicsPath;
|
---|
209 | if (PaintStyle != null)
|
---|
210 | {
|
---|
211 | g.FillPath(mPaintStyle.GetBrush(mRectangle), path);
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (Hovered)
|
---|
215 | {
|
---|
216 | g.DrawPath(ArtPalette.HighlightPen, path);
|
---|
217 | }
|
---|
218 | else if (PenStyle != null)
|
---|
219 | {
|
---|
220 | g.DrawPath(mPenStyle.DrawingPen(), path);
|
---|
221 | }
|
---|
222 |
|
---|
223 | foreach (IDiagramEntity entity in myEntities)
|
---|
224 | {
|
---|
225 | entity.Paint(g);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | public override bool MouseDown(System.Windows.Forms.MouseEventArgs e)
|
---|
230 | {
|
---|
231 | bool result = base.MouseDown(e);
|
---|
232 |
|
---|
233 | foreach (IDiagramEntity entity in myEntities)
|
---|
234 | {
|
---|
235 | if (entity.Hit(e.Location))
|
---|
236 | {
|
---|
237 | this.Model.Selection.SelectedItems.Add(entity);
|
---|
238 | if (entity.MouseDown(e))
|
---|
239 | {
|
---|
240 | result = true;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | return result;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|