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