forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapDecoration.cs
More file actions
368 lines (313 loc) · 13.3 KB
/
Copy pathMapDecoration.cs
File metadata and controls
368 lines (313 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace SharpMap.Rendering.Decoration
{
// ReSharper disable InconsistentNaming
/// <summary>
/// Abstract base class for all map decorations.
/// <para>
/// Handles framing and positioning of the decoration
/// </para>
/// </summary>
[Serializable]
public abstract class MapDecoration : /* Component, */IMapDecoration
{
/// <summary>
/// The size of this map decoration as computed/assigned previously
/// </summary>
protected Size _cachedSize;
/// <summary>
/// The bounding rectangle around the map decoration
/// </summary>
protected Rectangle _boundingRectangle;
/// <summary>
/// Creates an instance of this class.
/// </summary>
protected MapDecoration()
{
BackgroundColor = Color.Transparent;
Enabled = true;
Opacity = 1;
Location = new Point(5, 5);
Padding = new Size(3, 3);
Anchor = MapDecorationAnchor.RightBottom;
}
/// <summary>
/// Gets or sets enabled status
/// </summary>
public bool Enabled { get; set; }
#region Position
/// <summary>
/// The anchor of the map Decoration
/// </summary>
public MapDecorationAnchor Anchor { get; set; }
/// <summary>
/// The point that defines the location
/// </summary>
public Point Location { get; set; }
/// <summary>
/// Returns the left-top location of the Map decoration
/// </summary>
/// <param name="map"></param>
/// <returns></returns>
private Point GetLocation(MapViewport map)
{
var clipRect = map.Size;
var objectSize = Size;
var offsetX = Location.X;
var offsetY = Location.Y;
var anchors = (MapDecorationAnchorFlags)Anchor;
switch (anchors & MapDecorationAnchorFlags.Horizontal)
{
case MapDecorationAnchorFlags.Right:
offsetX = clipRect.Width - (Location.X + objectSize.Width);
break;
case MapDecorationAnchorFlags.HorizontalCenter:
offsetX = (clipRect.Width - (Location.X + objectSize.Width)) / 2;
break;
}
switch (anchors & MapDecorationAnchorFlags.Vertical)
{
case MapDecorationAnchorFlags.Bottom:
offsetY = clipRect.Height - (Location.Y + objectSize.Height);
break;
case MapDecorationAnchorFlags.VerticalCenter:
offsetY = (clipRect.Height - (Location.Y + objectSize.Height)) / 2;
break;
}
return new Point(offsetX, offsetY);
}
/// <summary>
/// Gets or sets the Padding of the map decoration
/// </summary>
public Size Padding { get; set; }
#endregion
/// <summary>
/// Function to compute an transparent color by combining <see cref="Opacity"/> with <paramref name="color"/>.
/// </summary>
/// <param name="color">The base color</param>
/// <returns>The (semi) transparent color</returns>
protected Color OpacityColor(Color color)
{
if (color.A != 255)
return color;
return Color.FromArgb((int)(_opacity * 255), color);
}
/// <summary>
/// Gets or sets the background color for the map decoration
/// </summary>
public Color BackgroundColor { get; set; }
private float _opacity;
/// <summary>
/// Gets or sets the opacity of map decoration
/// </summary>
public float Opacity
{
get { return _opacity; }
set
{
if (value < 0) value = 0;
if (value > 1) value = 1;
_opacity = value;
}
}
#region Appearance Border
/// <summary>
/// The margin between decoration and border
/// </summary>
public Size BorderMargin { get; set; }
/// <summary>
/// Gets or sets the color of the border
/// </summary>
public Color BorderColor { get; set; }
/// <summary>
/// Gets or sets the width of the border
/// </summary>
public int BorderWidth { get; set; }
/// <summary>
/// Gets or sets whether the border should be rendered with rounded edges
/// </summary>
public bool RoundedEdges { get; set; }
#endregion
/// <summary>
/// Function to compute the required size for rendering the map decoration object
/// <para>This is just the size of the decoration object, border settings are excluded</para>
/// </summary>
/// <param name="g">The graphics object</param>
/// <param name="mvp">The map's viewport</param>
/// <returns>The size of the map decoration</returns>
protected abstract Size InternalSize(Graphics g, MapViewport mvp);
/// <summary>
/// Function to compute the required size for rendering the map decoration object
/// <para>This is just the size of the decoration object, border settings are excluded</para>
/// </summary>
/// <param name="g">The graphics object</param>
/// <param name="map">The map</param>
/// <returns>The size of the map decoration</returns>
[Obsolete("Use InternalSize(Graphics, MapViewport")]
protected virtual Size InternalSize(Graphics g, Map map)
{
return InternalSize(g, (MapViewport) map);
}
private void CalcMapDecorationMetrics(Graphics g, MapViewport map)
{
_cachedSize = InternalSize(g, map);
var rect = new Rectangle(Point.Add(GetLocation(map), BorderMargin), _cachedSize);
_boundingRectangle = Rectangle.Inflate(rect, 2 * BorderMargin.Width, 2 * BorderMargin.Height);
}
private Size Size
{
get { return Size.Add(_cachedSize, Size.Add(BorderMargin, BorderMargin)); }
}
private Region GetClipRegion(MapViewport map)
{
return new Region(new Rectangle(Point.Add(GetLocation(map), BorderMargin), _cachedSize));
}
private static GraphicsPath CreateRoundedRectangle(Rectangle rectangle, Size margin)
{
var gp = new GraphicsPath();
//metrics
int x1 = rectangle.Left + margin.Width;
int x2 = rectangle.Right - margin.Width;
int y1 = rectangle.Top + margin.Height;
int y2 = rectangle.Bottom - margin.Height;
int arcWidth = 2*margin.Width;
int arcHeight = 2*margin.Height;
if (arcWidth > 0 && arcHeight > 0)
{
gp.AddLine(x1, rectangle.Top, x2, rectangle.Top);
gp.AddArc(x2 - margin.Width, rectangle.Top, arcWidth, arcHeight, 270, 90);
gp.AddLine(rectangle.Right, y1, rectangle.Right, y2);
gp.AddArc(x2 - margin.Width, y2 - margin.Height, arcWidth, arcHeight, 0, 90);
gp.AddLine(x2, rectangle.Bottom, x1, rectangle.Bottom);
gp.AddArc(rectangle.Left, y2 - margin.Height, arcWidth, arcHeight, 90, 90);
gp.AddLine(rectangle.Left, y2, rectangle.Left, y1);
gp.AddArc(rectangle.Left, rectangle.Top, arcWidth, arcWidth, 180, 90);
gp.CloseFigure();
}
else
gp.AddRectangle(rectangle);
return gp;
}
/// <summary>
/// Draw the map decoration.
/// <para>Note that this base class' implementation resets <paramref name="g"/>.Transform
/// prior to raising event OnRendering, and restore the <paramref name="g"/>.Transform prior to
/// raising event OnRendered.</para>
/// Likewise, <paramref name="g"/>.Clip is reset prior to rendering map decoration, and restored
/// immediately after rendering.
/// </summary>
/// <param name="g">A graphics object to use for rendering</param>
/// <param name="mvp">The viewport for rendering</param>
public void Render(Graphics g, MapViewport mvp)
{
//Is this map decoration enabled?
if (!Enabled)
return;
// remove any image to world rotation
var oldTransform = g.Transform;
g.ResetTransform();
//Preparing rendering
OnRendering(g, mvp);
//Draw border
GraphicsPath gp;
if (RoundedEdges && !BorderMargin.IsEmpty)
gp = CreateRoundedRectangle(_boundingRectangle, BorderMargin);
else
{
gp = new GraphicsPath();
gp.AddRectangle(_boundingRectangle);
}
g.DrawPath(new Pen(OpacityColor(BorderColor), BorderWidth), gp);
g.FillPath(new SolidBrush(OpacityColor(BackgroundColor)), gp);
//Clip region
var oldClip = g.Clip;
g.Clip = GetClipRegion(mvp);
//Actually render the Decoration
OnRender(g, mvp);
//Restore old clip region
g.Clip = oldClip;
// restore any image to world rotation
g.Transform = oldTransform;
//Finished rendering
OnRendered(g, mvp);
}
/// <summary>
/// Draw the map decoration.
/// <para>Note that this base class' implementation resets <paramref name="g"/>.Transform
/// prior to raising event OnRendering, and restore the <paramref name="g"/>.Transform prior to
/// raising event OnRendered.</para>
/// Likewise, <paramref name="g"/>.Clip is reset prior to rendering map decoration, and restored
/// immediately after rendering.
/// </summary>
/// <param name="g">A graphics object to use for rendering</param>
/// <param name="map">The viewport for rendering</param>
[Obsolete("Use Render(Graphics, Map")]
public void Render(Graphics g, Map map)
{
Render(g, (MapViewport)map);
}
/// <summary>
/// Render the actual map decoration.
/// <para>Refer to MapDecoration.<see cref="Render(System.Drawing.Graphics,SharpMap.MapViewport)"/> for underlying management of <paramref name="g"/>.Transform</para>
/// </summary>
/// <param name="g">A graphics object</param>
/// <param name="mvp">A map viewport</param>
protected virtual void OnRender(Graphics g, MapViewport mvp)
{
}
/// <summary>
/// Render the actual map decoration.
/// <para>Refer to MapDecoration.<see cref="Render(System.Drawing.Graphics,SharpMap.MapViewport)"/> for underlying management of <paramref name="g"/>.Transform</para>
/// </summary>
/// <param name="g">A graphics object</param>
/// <param name="map">A map</param>
[Obsolete("Use OnRender(Graphics, MapViewport")]
protected virtual void OnRender(Graphics g, Map map)
{
OnRender(g, (MapViewport) map);
}
/// <summary>
/// Signal commencing rendering
/// <para>Refer to MapDecoration.<see cref="Render(System.Drawing.Graphics,SharpMap.MapViewport)"/> for underlying management of <paramref name="g"/>.Transform</para>
/// </summary>
/// <param name="g">A graphics object</param>
/// <param name="mvp">A map viewport</param>
protected virtual void OnRendering(Graphics g, MapViewport mvp)
{
CalcMapDecorationMetrics(g, mvp);
}
/// <summary>
/// Signal commencing rendering
/// <para>Refer to MapDecoration. <see cref="Render(System.Drawing.Graphics,SharpMap.MapViewport)"/> for underlying management of <paramref name="g"/>.Transform</para>
/// </summary>
/// <param name="g">A graphics object</param>
/// <param name="map">A map viewport</param>
[Obsolete("Use OnRendering(Graphics, MapViewport")]
protected virtual void OnRendering(Graphics g, Map map)
{
OnRendering(g, (MapViewport)map);
}
/// <summary>
/// Signal completion of rendering
/// <para>Refer to MapDecoration.<see cref="Render(System.Drawing.Graphics,SharpMap.MapViewport)"/> for underlying management of <paramref name="g"/>.Transform</para>
/// </summary>
/// <param name="g">A graphics object</param>
/// <param name="mvp">A map viewport</param>
protected virtual void OnRendered(Graphics g, MapViewport mvp)
{
}
/// <summary>
/// Signal completion of rendering
/// <para>Refer to MapDecoration.<see cref="Render(System.Drawing.Graphics,SharpMap.Map)"/> for underlying management of <paramref name="g"/>.Transform</para>
/// </summary>
/// <param name="g">A graphics object</param>
/// <param name="map">A map</param>
[Obsolete("Use OnRendered(Graphics, MapViewport")]
protected virtual void OnRendered(Graphics g, Map map)
{
OnRendered(g, (MapViewport) map);
}
}
}