forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniMapControl.cs
More file actions
849 lines (728 loc) · 29.3 KB
/
MiniMapControl.cs
File metadata and controls
849 lines (728 loc) · 29.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
// Copyright 2014 - Spartaco Giubbolini (spartaco@sgsoftware.it), portions by Felix Obermaier (www.ivv-aachen.de)
//
// This file is part of SharpMap.UI.
// SharpMap.UI is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap.UI is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Threading.Tasks;
using System.Windows.Forms;
using GeoAPI.Geometries;
namespace SharpMap.Forms
{
/// <summary>
/// This control displays a minimap of the whole extension of a map, and let the user drag the viewport.
/// </summary>
[Serializable]
public partial class MiniMapControl : Control
{
/// <summary>
/// Enumeration of possible hit results
/// </summary>
protected enum HitResult
{
/// <summary>
/// Not hit anything at all
/// </summary>
None,
/// <summary>
/// Hit inside the frame
/// </summary>
InsideFrame,
/// <summary>
/// Hit resize north-west corner
/// </summary>
SizeNW,
/// <summary>
/// Hit resize north-east corner
/// </summary>
SizeNE,
/// <summary>
/// Hit resize south-west corner
/// </summary>
SizeSW,
/// <summary>
/// Hit resize south-east corner
/// </summary>
SizeSE
}
#region fields
private const int CornerMouseLength = 8;
private const int CornerMouseSensibility = 1;
private MapBox _mapBoxControl;
[NonSerialized]
private Timer _resizeTimer;
private int _resizeInterval = 500;
private volatile int _generation;
private volatile Map _currentMap;
private Rectangle _frame;
private bool _mouseDown;
private BorderStyle _borderStyle = BorderStyle.Fixed3D;
private DashStyle _framePenDashStyle = DashStyle.Solid;
private PenAlignment _framePenAlignment = PenAlignment.Center;
private int _frameHalo;
private Color _frameBrushColor = Color.Red;
private Color _framePenColor = Color.Red;
private int _framePenWidth = 2;
private float _opacity;
private Color _frameHaloColor;
private Point _translate;
private Cursor _oldCursor;
private HitResult _hitResult;
private Guid _mapId = Guid.NewGuid();
[ThreadStatic]
private static bool _isRenderingThread;
#endregion
#region ctor
/// <summary>
/// Creates an instance of this class
/// </summary>
public MiniMapControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.UserPaint, true);
base.DoubleBuffered = true;
_resizeTimer = new Timer { Interval = ResizeInterval };
_resizeTimer.Tick += HandleResizeTimerTick;
}
#endregion
#region properties
/// <summary>
/// Gets a value indicating if the current thread is rendering the map.
/// </summary>
/// <remarks>A layer could use this value to customize the rendering into the minimap.</remarks>
public static bool IsRenderingThread
{
get { return _isRenderingThread; }
}
//public event EventHandler ResizeIntervalChanged;
//protected virtual void OnResizeIntervalChanged(EventArgs e)
//{
// _resizeTimer.Stop();
// _resizeTimer.Interval = _resizeInterval;
// _resizeTimer.Start();
// var handler = ResizeIntervalChanged;
// if (handler != null) handler(this, e);
//}
/// <summary>
/// Gets or sets a value indicating the interval between two MapControl.Resize events
/// </summary>
public int ResizeInterval
{
get { return _resizeInterval; }
set
{
if (value == _resizeInterval)
return;
if (value < 1)
throw new ArgumentException("The resize interval must be a positive value");
_resizeInterval = value;
_resizeTimer.Stop();
_resizeTimer.Interval = _resizeInterval;
_resizeTimer.Start();
//OnResizeIntervalChanged(EventArgs.Empty);
}
}
/// <summary>
/// Gets or sets a value indicating if the viewport frame should have a halo
/// </summary>
[Description("If set, a halo effect is drawn around the viewport frame")]
[Category("Frame Appearance")]
[DefaultValue(0)]
public int FrameHalo
{
get { return _frameHalo; }
set
{
if (value == _frameHalo)
return;
_frameHalo = value;
Refresh();
}
}
/// <summary>
/// Gets or sets a value indicating the color of the the viewport frame halo
/// </summary>
[Description("If set, a halo effect is drawn around the viewport frame")]
[Category("Frame Appearance")]
[DefaultValue(typeof(Color), "System.Drawing.Color.White")]
public Color FrameHaloColor
{
get { return _frameHaloColor; }
set
{
if (_frameHaloColor == value)
return;
_frameHaloColor = value;
Refresh();
}
}
/// <summary>
/// Gets or sets a value indicating the pen alignment used when drawing the frame
/// </summary>
[Description("The pen alignment used when drawing the frame")]
[Category("Frame Appearance")]
[DefaultValue(typeof(PenAlignment), "System.Drawing.PenAlignment.Center")]
public PenAlignment FramePenAlignment
{
get { return _framePenAlignment; }
set
{
if (value == _framePenAlignment)
return;
_framePenAlignment = value;
Refresh();
//OnFramePenAlignmentChanged(EventArgs.Empty);
}
}
///// <summary>
///// Event raised when the <see cref="FramePenAlignment"/> has changed
///// </summary>
//public event EventHandler FramePenAlignmentChanged;
///// <summary>
///// Event invoker for the <see cref="FramePenAlignmentChanged"/> event.
///// </summary>
///// <param name="e">The events arguments</param>
//protected virtual void OnFramePenAlignmentChanged(EventArgs e)
//{
// Refresh();
// var h = FramePenAlignmentChanged;
// if (h != null) h(this, e);
//}
/// <summary>
/// Gets or sets a value indicating the pen alignment used when drawing the frame
/// </summary>
[Description("The dash style used when drawing the frame")]
[Category("Frame Appearance")]
[DefaultValue(typeof(DashStyle), "System.Drawing.DashStyle.Solid")]
public DashStyle FramePenDashStyle
{
get { return _framePenDashStyle; }
set
{
if (value == _framePenDashStyle)
return;
if (value == DashStyle.Custom)
throw new NotSupportedException("DashStyle.Custom is not supported");
_framePenDashStyle = value;
Refresh();
//OnFramePenDashStyleChanged(EventArgs.Empty);
}
}
///// <summary>
///// Event raised when the <see cref="FramePenDashStyle"/> has changed
///// </summary>
//public event EventHandler FramePenDashStyleChanged;
///// <summary>
///// Event invoker for the <see cref="FramePenDashStyleChanged"/> event.
///// </summary>
///// <param name="e">The events arguments</param>
//protected virtual void OnFramePenDashStyleChanged(EventArgs e)
//{
// Refresh();
// var h = FramePenDashStyleChanged;
// if (h != null) h(this, e);
//}
/// <summary>
/// Gets or sets a value indicating the <see cref="MapBox"/> control linked to the mini map.
/// </summary>
[DefaultValue(null)]
public MapBox MapControl
{
get { return _mapBoxControl; }
set
{
UnhookEvents();
_mapBoxControl = value;
HookEvents();
Cursor = value == null ? Cursors.Default : Cursors.Hand;
}
}
/// <summary>
/// Gets or sets a value indicating the border style
/// </summary>
[Category("Appearance")]
[Description("Defines the border style")]
[DefaultValue(BorderStyle.Fixed3D)]
public BorderStyle BorderStyle
{
get { return _borderStyle; }
set
{
if (_borderStyle == value)
return;
_borderStyle = value;
UpdateStyles();
//OnBorderStyleChanged(EventArgs.Empty);
}
}
///// <summary>
///// Event raised when <see cref="BorderStyle"/> has changed
///// </summary>
//public event EventHandler BorderStyleChanged;
///// <summary>
///// Event invoker for the <see cref="BorderStyleChanged"/> event.
///// </summary>
///// <param name="e">The event's arguments</param>
//protected virtual void OnBorderStyleChanged(EventArgs e)
//{
// var h = BorderStyleChanged;
// if (h != null) h(this, e);
//}
/// <summary>
/// Gets or sets a value indicating the color of the pen that draws the frame
/// </summary>
[Description("The color of the pen that draws the frame")]
[Category("Frame Appearance")]
[DefaultValue(typeof(Color), "Red")]
public Color FramePenColor
{
get { return _framePenColor; }
set
{
if (value == _framePenColor)
return;
_framePenColor = value;
Refresh();
}
}
/// <summary>
/// Gets or sets a value indicating the color of the brush that fills the frame
/// </summary>
[Description("The color of the brush that fills the frame")]
[Category("Frame Appearance")]
[DefaultValue(typeof(Color), "Transparent")]
public Color FrameBrushColor
{
get { return _frameBrushColor; }
set
{
if (value == _frameBrushColor)
return;
_frameBrushColor = value;
Refresh();
}
}
/// <summary>
/// Gets or sets a value indicating the width of the pen that draws the frame
/// </summary>
[Description("The width of the pen that draws the frame")]
[Category("Frame Appearance")]
[DefaultValue(2)]
public int FramePenWidth
{
get { return _framePenWidth; }
set
{
if (value < 1) value = 1;
if (value == _framePenWidth)
return;
_framePenWidth = value;
Refresh();
}
}
/// <summary>
/// Gets or sets a value indicating the opacity of the brush used to fill the frame
/// </summary>
[Description("The opacity of the brush used to fill the frame")]
[Category("Frame Appearance")]
[DefaultValue(0f)]
public float Opacity
{
get { return _opacity; }
set
{
if (value < 0f) value = 0f;
if (value > 1f) value = 1f;
if (value == _opacity)
return;
_opacity = value;
Refresh();
}
}
#endregion
#region protected members
/// <summary>
/// Returns a value that determines what the provided location hits
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
protected HitResult HitTest(int x, int y)
{
if ((x >= _frame.X - CornerMouseSensibility &&
x <= _frame.X + CornerMouseLength &&
y >= _frame.Y - CornerMouseSensibility && y <= _frame.Y + CornerMouseSensibility) ||
(x >= _frame.X - CornerMouseSensibility && x <= _frame.X + CornerMouseSensibility &&
y >= _frame.Y - CornerMouseSensibility &&
y <= _frame.Y + CornerMouseLength))
return HitResult.SizeNW;
if ((x >= _frame.Right - CornerMouseLength && x < _frame.Right + CornerMouseSensibility &&
y >= _frame.Y - CornerMouseSensibility && y <= _frame.Y + CornerMouseSensibility) ||
(x >= _frame.Right - CornerMouseSensibility && x <= _frame.Right + CornerMouseSensibility &&
y >= _frame.Y - CornerMouseSensibility && y <= _frame.Y + CornerMouseLength))
return HitResult.SizeNE;
if ((x >= _frame.X - CornerMouseSensibility && x <= _frame.X + CornerMouseLength &&
y >= _frame.Bottom - CornerMouseLength && y <= _frame.Bottom + CornerMouseSensibility) ||
(x >= _frame.X - CornerMouseSensibility && x <= _frame.X + CornerMouseLength &&
y >= _frame.Bottom - CornerMouseLength && y <= _frame.Bottom + CornerMouseSensibility))
return HitResult.SizeSW;
if ((x >= _frame.Right - CornerMouseLength && x < _frame.Right + CornerMouseSensibility &&
y >= _frame.Bottom - CornerMouseSensibility && y <= _frame.Bottom + CornerMouseSensibility) ||
(x >= _frame.Right - CornerMouseSensibility && x <= _frame.Right + CornerMouseSensibility &&
y >= _frame.Bottom - CornerMouseLength && y <= _frame.Bottom + CornerMouseSensibility))
return HitResult.SizeSE;
if (x >= _frame.X && x <= _frame.Right && y >= _frame.Y && y <= _frame.Bottom)
return HitResult.InsideFrame;
return HitResult.None;
}
/// <inheritdoc cref="Control.Refresh"/>
public override void Refresh()
{
CreateMiniMap();
base.Refresh();
}
/// <inheritdoc cref="Control.CreateParams"/>
protected override CreateParams CreateParams
{
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= 65536;
createParams.ExStyle &= -513;
createParams.Style &= -8388609;
switch (_borderStyle)
{
case BorderStyle.FixedSingle:
createParams.Style |= 8388608;
break;
case BorderStyle.Fixed3D:
createParams.ExStyle |= 512;
break;
}
return createParams;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (_resizeTimer != null))
{
_resizeTimer.Dispose();
}
base.Dispose(disposing);
}
/// <inheritdoc cref="Control.OnMouseDown"/>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button != MouseButtons.Left)
return;
if (_hitResult == HitResult.None)
{
_translate = Point.Empty;
_frame = CalculateNewFrame(e.X, e.Y);
}
else
{
_translate = new Point(Convert.ToInt32(e.X - (_frame.X + _frame.Width * 0.5)),
Convert.ToInt32(e.Y - (_frame.Y + _frame.Height * 0.5)));
}
Rectangle clippingRect;
switch (_hitResult)
{
case HitResult.None:
case HitResult.InsideFrame:
clippingRect = ClientRectangle;
break;
case HitResult.SizeNW:
clippingRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y, _frame.Right, _frame.Bottom);
break;
case HitResult.SizeNE:
clippingRect = new Rectangle(_frame.X, ClientRectangle.Y, ClientRectangle.Width - _frame.X, _frame.Bottom);
break;
case HitResult.SizeSW:
clippingRect = new Rectangle(ClientRectangle.X, _frame.Y, _frame.Right - ClientRectangle.X,
ClientRectangle.Height - _frame.Y);
break;
case HitResult.SizeSE:
clippingRect = new Rectangle(_frame.X, _frame.Y, ClientRectangle.Width - _frame.X,
ClientRectangle.Height - _frame.Y);
break;
default:
clippingRect = ClientRectangle;
break;
}
Cursor.Clip = RectangleToScreen(clippingRect);
_mouseDown = true;
}
/// <inheritdoc cref="Control.OnMouseMove"/>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_mouseDown)
{
switch (_hitResult)
{
case HitResult.InsideFrame:
_frame = CalculateNewFrame(e.X - _translate.X, e.Y - _translate.Y);
break;
case HitResult.None:
_frame = CalculateNewFrame(e.X, e.Y);
break;
case HitResult.SizeNW:
_frame = new Rectangle(e.X, e.Y, _frame.Right - e.X, _frame.Bottom - e.Y);
break;
case HitResult.SizeNE:
_frame = new Rectangle(_frame.X, e.Y, e.X - _frame.X, _frame.Bottom - e.Y);
break;
case HitResult.SizeSW:
_frame = new Rectangle(e.X, _frame.Y, _frame.Right - e.X, e.Y - _frame.Y);
break;
case HitResult.SizeSE:
_frame = new Rectangle(_frame.X, _frame.Y, e.X - _frame.X, e.Y - _frame.Y);
break;
}
Invalidate();
}
else
{
_hitResult = HitTest(e.X, e.Y);
if (_hitResult == HitResult.SizeNW || _hitResult == HitResult.SizeSE)
{
if (_oldCursor == null)
_oldCursor = Cursor;
Cursor = Cursors.SizeNWSE;
}
else if (_hitResult == HitResult.SizeNE || _hitResult == HitResult.SizeSW)
{
if (_oldCursor == null)
_oldCursor = Cursor;
Cursor = Cursors.SizeNESW;
}
else if (_oldCursor != null)
{
Cursor = _oldCursor;
_oldCursor = null;
}
}
}
/// <inheritdoc cref="Control.OnMouseUp"/>
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (MapControl != null && _currentMap != null && _mouseDown)
{
var newCenter =
_currentMap.ImageToWorld(new PointF(
Convert.ToInt32(_frame.X + _frame.Width * 0.5),
Convert.ToInt32(_frame.Y + _frame.Height * 0.5)));
MapControl.Map.Center = newCenter;
if (_hitResult != HitResult.None && _hitResult != HitResult.InsideFrame)
{
var p1 = _currentMap.ImageToWorld(_frame.Location);
var p2 = _currentMap.ImageToWorld(new PointF(_frame.X + _frame.Width, _frame.Y + _frame.Height));
MapControl.Map.ZoomToBox(new Envelope(p1.X, p2.X, p1.Y, p2.Y));
}
MapControl.Refresh();
Invalidate();
Cursor.Clip = Rectangle.Empty;
}
_mouseDown = false;
}
/// <inheritdoc cref="Control.OnPaint"/>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
try
{
if (DesignMode)
{
var width = Convert.ToInt32(ClientSize.Width * 0.5);
var height = width * (MapControl != null
? (double)MapControl.ClientSize.Height / MapControl.ClientSize.Width
: (double)ClientSize.Height / ClientSize.Width);
DrawFrame(e.Graphics, new Rectangle(
Convert.ToInt32(ClientSize.Width * 0.2), Convert.ToInt32(ClientSize.Height * 0.3),
width, Convert.ToInt32(height)));
//DrawFrame(e.Graphics, new Rectangle(
// Convert.ToInt32(ClientSize.Width * 0.2), Convert.ToInt32(ClientSize.Height * 0.3),
// Convert.ToInt32(ClientSize.Width * 0.5), Convert.ToInt32(ClientSize.Height * 0.7)));
}
else
{
DrawFrame(e.Graphics, _frame);
}
}
catch (OverflowException)
{
// sometimes can happen this exception if the frame rectangle is too big, we can simply skip it
}
}
#endregion
#region private members
private Rectangle CalculateNewFrame(int x, int y)
{
return new Rectangle(
x - Convert.ToInt32(_frame.Width * 0.5),
y - Convert.ToInt32(_frame.Height * 0.5),
Convert.ToInt32(_frame.Width),
Convert.ToInt32(_frame.Height));
}
private void HookEvents()
{
if (_mapBoxControl == null)
return;
_mapBoxControl.MapRefreshed += OnMapBoxRendered;
SizeChanged += OnSizeChanged;
}
private void OnSizeChanged(object sender, EventArgs eventArgs)
{
_resizeTimer.Stop();
_resizeTimer.Start();
}
private void OnMapBoxRendered(object sender, EventArgs e)
{
if (InvokeRequired)
BeginInvoke(new Action(CreateMiniMap));
else
CreateMiniMap();
}
private void CreateMiniMap()
{
if (_mapBoxControl == null)
return;
var clonedMap = _mapBoxControl.Map.Clone();
clonedMap.ID = _mapId;
clonedMap.Decorations.Clear();
clonedMap.EnforceMaximumExtents = false;
clonedMap.MaximumExtents = null;
clonedMap.MinimumZoom = Double.Epsilon;
clonedMap.MaximumZoom = Double.MaxValue;
var tsk = Task.Factory.StartNew(new Func<object, Tuple<Image, int, Rectangle>>(GenerateMap), new object[] { ++_generation, clonedMap });
tsk.ContinueWith(t =>
{
if (t.Result != null)
{
if (t.Result.Item2 == _generation)
{
_currentMap = clonedMap;
BackgroundImage = t.Result.Item1;
_frame = t.Result.Item3;
}
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
private void UnhookEvents()
{
if (_mapBoxControl == null)
return;
_mapBoxControl.MapRefreshed -= OnMapBoxRendered;
SizeChanged -= OnMapBoxRendered;
}
private void DrawFrame(Graphics g, Rectangle rect)
{
if (rect == Rectangle.Empty)
return;
var oldSmoothingMode = g.SmoothingMode;
g.SmoothingMode = SmoothingMode.AntiAlias;
if (FrameBrushColor != Color.Transparent && Opacity != 0)
{
using (var b = new SolidBrush(Color.FromArgb(Convert.ToInt32(Opacity * 255), FrameBrushColor)))
g.FillRectangle(b, rect);
}
if (FrameHalo > 0)
{
using (var newPen = new Pen(FrameHaloColor, FramePenWidth + _frameHalo))
{
g.DrawRectangle(newPen, rect);
}
}
using (var newPen = new Pen(FramePenColor, FramePenWidth))
{
newPen.DashStyle = FramePenDashStyle;
newPen.Alignment = FramePenAlignment;
g.DrawRectangle(newPen, rect);
}
g.SmoothingMode = oldSmoothingMode;
}
private Tuple<Image, int, Rectangle> GenerateMap(object state)
{
var args = (object[])state;
var currentGeneration = (int)args[0];
try
{
_isRenderingThread = true;
var map = (Map) args[1];
Image img = null;
Rectangle frame;
if (map.Layers.Count > 0 && Height > 0 && Width > 0)
{
var originalCenter = map.Center;
var originalWidth = map.Zoom;
var originalHeight = map.MapHeight;
var wx1 = originalCenter.X - originalWidth*0.5;
var wy1 = originalCenter.Y - originalHeight*0.5;
var wx2 = originalCenter.X + originalWidth*0.5;
var wy2 = originalCenter.Y + originalHeight*0.5;
map.Size = Size;
map.ZoomToExtents();
img = map.GetMap();
var np1 = map.WorldToImage(new Coordinate(wx1, wy1));
var np2 = map.WorldToImage(new Coordinate(wx2, wy2));
frame = new Rectangle(
Convert.ToInt32(np1.X),
Convert.ToInt32(np2.Y),
Convert.ToInt32(Math.Abs(np2.X - np1.X)),
Convert.ToInt32(Math.Abs(np1.Y - np2.Y)));
}
else
{
frame = Rectangle.Empty;
}
return new Tuple<Image, int, Rectangle>(img, currentGeneration, frame);
}
catch (Exception)
{
return new Tuple<Image, int, Rectangle>(new Bitmap(1, 1), currentGeneration, Rectangle.Empty);
}
finally
{
_isRenderingThread = false;
}
}
private void HandleResizeTimerTick(object sender, EventArgs e)
{
_resizeTimer.Stop();
OnMapBoxRendered(sender, e);
}
[OnDeserialized]
internal void OnDeserialized(StreamingContext context)
{
_resizeTimer = new Timer { Interval = _resizeInterval };
_resizeTimer.Tick += HandleResizeTimerTick;
_resizeTimer.Start();
}
#endregion
}
}