-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMapViewer.cs
748 lines (603 loc) · 25.8 KB
/
frmMapViewer.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DayTripper;
using ImageMagick;
using System.Security;
using DayTripperUI.Enums;
using DTPathfinder.HelperClasses;
using DTPathfinder.Enums;
namespace DayTripperUI
{
public partial class frmMapViewer : Form
{
#region Search State
private SearchableMap _map;
private readonly Dictionary<MapType, MagickImage> _mapImages = new Dictionary<MapType, MagickImage>
{
{ MapType.Elevation, null },
{ MapType.Passibility, null },
{ MapType.Water, null },
{ MapType.Road, null }
};
private Size imageSize;
private bool mapReady = false;
private List<SearchTile> foundPath = null;
private MagickImage pathImage;
private Point pathStart => new Point((int)numStartX.Value, (int)numStartY.Value);
private Point pathGoal => new Point((int)numGoalX.Value, (int)numGoalY.Value);
#endregion
#region UI State
private readonly MagickImage reticleIcon;
private float zoomFactor = 1f;
private float invZoomFactor => 1 / zoomFactor;
private readonly float zoomDelta = .05f;
private InteractionMode interactionMode;
private PathPickerTarget currentTarget;
private bool showPath = true;
private bool showWater = true;
private bool showRoads = true;
private bool showPassibility = true;
private bool showReticles = true;
static readonly Dictionary<PathPickerTarget, Cursor> CursorFor = new Dictionary<PathPickerTarget, Cursor>()
{
{ PathPickerTarget.None, Cursors.Default },
{ PathPickerTarget.Goal, Cursors.Cross },
{ PathPickerTarget.Start, Cursors.Cross },
};
bool _pendingInitialize
{
get => btnInitializeMap.Enabled;
set => btnInitializeMap.Enabled = value;
}
readonly Dictionary<MapType, bool> _loadedAndPendingInitialize = new Dictionary<MapType, bool>
{
{ MapType.Elevation, false },
{ MapType.Passibility, false },
{ MapType.Water, false },
{ MapType.Road, false },
};
readonly Dictionary<MapType, Control> _mapLoadingButtons;
#endregion
public frmMapViewer()
{
InitializeComponent();
_mapLoadingButtons = new Dictionary<MapType, Control>
{
{ MapType.Elevation, btnLoadElevation },
{ MapType.Passibility, btnLoadPassibility },
{ MapType.Water, btnLoadWater },
{ MapType.Road, btnLoadRoads },
};
SetAllButtonCaptions();
workerMapLoad.DoWork += WorkerMapLoad_DoWork;
workerMapLoad.ProgressChanged += WorkerMapLoad_ProgressChanged;
workerMapLoad.RunWorkerCompleted += WorkerMapLoad_RunWorkerCompleted;
workerPathfind.DoWork += WorkerPathfind_DoWork;
workerPathfind.RunWorkerCompleted += WorkerPathfind_RunWorkerCompleted;
txtCoords.Text = "";
pictureBoxMap.MouseMove += PictureBoxMap_MouseMove;
pictureBoxMap.MouseLeave += PictureBoxMap_MouseLeave;
pictureBoxMap.MouseClick += PictureBoxMap_MouseClick;
this.KeyDown += FrmMapViewer_KeyDown;
this.KeyUp += FrmMapViewer_KeyUp;
pictureBoxMap.MouseWheel += PictureBoxMap_MouseWheel;
var ss = new SearchSettings();
numDistanceWeight.Value = (decimal)ss.CrowDistanceWeight;
numTerrainStepCost.Value = (decimal)ss.RoughTerrainStepCost;
numUphillPenalty.Value = (decimal)ss.UphillPenalty;
numDownhillPenalty.Value = (decimal)ss.DownhillPenalty;
numWaterStepCost.Value = (decimal)ss.WaterStepCost;
numWaterEnterPenalty.Value = (decimal)ss.WaterEnterPenalty;
numWaterExitPenalty.Value = (decimal)ss.WaterExitPenalty;
numRoadStepCost.Value = (decimal)ss.RoadStepCost;
numRoadExitPenalty.Value = (decimal)ss.RoadExitPenalty;
toolTip.SetToolTip(numDistanceWeight, "How much to prefer short paths.\nHigher values take less time to compute, but often result in uglier paths.");
toolTip.SetToolTip(numTerrainStepCost, "Cost of each step while traveling rough terrain");
toolTip.SetToolTip(numWaterStepCost, "Cost of each step while traveling on water");
toolTip.SetToolTip(numRoadStepCost, "Cost of each step while traveling on a road");
toolTip.SetToolTip(numUphillPenalty, "How much to avoid going up hill\nHigher values avoid upward slopes more.\nValues less than 1 will reduce the penalty");
toolTip.SetToolTip(numDownhillPenalty, "How much to avoid going down hill\nHigher values avoid downward slopes low ground more.\nValues less than 1 will reduce the penalty");
toolTip.SetToolTip(numWaterEnterPenalty, "How much to avoid entering water while on land");
toolTip.SetToolTip(numWaterExitPenalty, "How much to avoid exiting water while already on water");
//toolTip.SetToolTip(numRoadEnterPenalty, "How much to avoid entering a road while on land");
toolTip.SetToolTip(numRoadExitPenalty, "How much to avoid exiting a road");
btnFindPath.Enabled = false;
dlgOpenMap.Filter = "PNG|*.png|BMP|*.bmp|JPG|*.jpg|GIF|*.gif";
dlgSave.Filter = "PNG|*.png";
dlgSave.AddExtension = true;
dlgSave.DefaultExt = "png";
reticleIcon = new MagickImage(new MagickFactory().Image.Create(Properties.Resources.reticle));
reticleIcon.Scale(new Percentage(50));
#region Debugging
//static void DebugMap(AStarMap map, SearchTile current, SearchCandidateCollection candidateTiles, int closedCount, Point targetPoint, DebugSettings debugSettings)
//{
// using var img = new MagickImage(MagickColors.Transparent, map.Width, map.Height);
// using (var pixels = img.GetPixels())
// {
// //foreach (var loc in visitedLocations)
// //{
// // pixels.SetPixel(loc.X, loc.Y, DebugSettings.Orange);
// //}
// foreach (var tile in candidateTiles)
// {
// pixels.SetPixel(tile.Location.X, tile.Location.Y, DebugSettings.Yellow);
// }
// foreach (var tile in current.PathBackToStart())
// {
// pixels.SetPixel(tile.Location.X, tile.Location.Y, DebugSettings.Red);
// }
// if (debugSettings.OutputPassable)
// {
// var colors = new Dictionary<bool, byte[]>
// {
// { true, DebugSettings.PassableColor },
// { false, DebugSettings.ImpassableColor },
// };
// for (var x = 0; x < map.Width; x++)
// {
// for (var y = 0; y < map.Height; y++)
// {
// pixels.SetPixel(x, y, colors[map[x, y].Passable]);
// }
// }
// }
// pixels.SetPixel(targetPoint.X, targetPoint.Y, DebugSettings.Magenta);
// }
// img.Write(Path.Join(debugSettings.DebugOutputDirectory, $"debug_{closedCount}.png"));
//}
//var debugSettings = new DebugSettings()
//{
// report = DebugMap
//};
#endregion
}
#region PictureBox Controls
private void PictureBoxMap_MouseWheel(object sender, MouseEventArgs e)
{
if (interactionMode == InteractionMode.Zoom)
{
zoomFactor += e.Delta > 1 ? zoomDelta : -zoomDelta;
SetDisplayImage();
((HandledMouseEventArgs)e).Handled = true;
}
else
{
base.OnMouseWheel(e);
}
}
private void FrmMapViewer_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
interactionMode = InteractionMode.None;
SetCursor();
}
else if (e.KeyCode == Keys.Menu)
{
interactionMode = InteractionMode.None;
SetCursor();
}
}
private void FrmMapViewer_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
interactionMode = InteractionMode.Pan;
pictureBoxMap.Cursor = Cursors.NoMove2D;
}
else if (e.KeyCode == Keys.Menu)
{
interactionMode = InteractionMode.Zoom;
pictureBoxMap.Cursor = Cursors.SizeAll;
}
}
private void PictureBoxMap_MouseClick(object sender, MouseEventArgs e)
{
if (currentTarget == PathPickerTarget.None) return;
if (currentTarget == PathPickerTarget.Start)
{
numStartX.Value = (int)Math.Round(e.X * (1 / zoomFactor));
numStartY.Value = (int)Math.Round(e.Y * (1 / zoomFactor));
}
else
{
numGoalX.Value = (int)Math.Round(e.X * (1 / zoomFactor));
numGoalY.Value = (int)Math.Round(e.Y * (1 / zoomFactor));
}
SetDisplayImage();
SetCursor();
currentTarget = PathPickerTarget.None;
}
private void PictureBoxMap_MouseLeave(object sender, EventArgs e)
{
txtCoords.Text = "";
}
private void PictureBoxMap_MouseMove(object sender, MouseEventArgs e)
{
txtCoords.Text = $"({(int)Math.Round(e.X * invZoomFactor)},{(int)Math.Round(e.Y * invZoomFactor)})";
}
private void SetCursor()
{
pictureBoxMap.Cursor = CursorFor[currentTarget];
}
#endregion
#region Initialize Map
private void InitializeMap()
{
if (workerMapLoad.IsBusy) return;
mapReady = false;
btnFindPath.Enabled = false;
statusProgressBar.Visible = true;
statusProgressBar.Maximum = imageSize.Width * imageSize.Height;
txtProgressStatus.Text = "Initializing Map for Search";
_pendingInitialize = false;
btnInitializeMap.Text = "Initializing Map...";
foreach (var type in _mapLoadingButtons.Keys)
{
_mapLoadingButtons[type].Enabled = false;
}
workerMapLoad.RunWorkerAsync();
}
private void WorkerMapLoad_DoWork(object sender, DoWorkEventArgs e)
{
var elevationMap = new MapMatrix(_mapImages[MapType.Elevation]);
var passibilityMap = new MapMatrix(_mapImages[MapType.Passibility], defaultValue: true);
var waterMap = new MapMatrix(_mapImages[MapType.Water], defaultValue: false);
var roadMap = new MapMatrix(_mapImages[MapType.Road], defaultValue: false);
BackgroundWorker worker = sender as BackgroundWorker;
_map = new SearchableMap(
elevationMap,
passibilityMap,
waterMap,
roadMap,
imageSize,
worker.ReportProgress);
}
private void WorkerMapLoad_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
statusProgressBar.Visible = false;
txtProgressStatus.Text = "";
statusProgressBar.Value = 0;
mapReady = true;
btnFindPath.Text = "Find Path";
btnFindPath.Enabled = true;
btnFindPath.BackColor = Color.GreenYellow;
_pendingInitialize = false;
_loadedAndPendingInitialize[MapType.Elevation] = false;
_loadedAndPendingInitialize[MapType.Passibility] = false;
_loadedAndPendingInitialize[MapType.Water] = false;
_loadedAndPendingInitialize[MapType.Road] = false;
foreach (var type in _mapLoadingButtons.Keys)
{
_mapLoadingButtons[type].Enabled = true;
}
btnInitializeMap.Text = "Initialize Loaded Map";
SetAllButtonCaptions();
}
private void WorkerMapLoad_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
statusProgressBar.Value = e.ProgressPercentage;
}
#endregion
private void btnFindPath_Click(object sender, EventArgs e)
{
if (!mapReady)
{
MessageBox.Show("Map Not Yet Ready");
return;
}
foundPath = null;
pathImage = null;
txtProgressStatus.Text = "Finding Path...";
workerPathfind.RunWorkerAsync();
}
private void WorkerPathfind_DoWork(object sender, DoWorkEventArgs e)
{
void progress(int crowDistance, int candidateCount, int closedCount)
{
txtProgressStatus.Text = $"Distance: {crowDistance,-10:#,###} Active: {candidateCount,-10:#,###} Visited: {closedCount,-10:#,###}";
}
var searchSettings = new SearchSettings()
{
CrowDistanceWeight = (float)numDistanceWeight.Value,
RoughTerrainStepCost = (float)numTerrainStepCost.Value,
UphillPenalty = (float)numUphillPenalty.Value,
DownhillPenalty = (float)numDownhillPenalty.Value,
WaterStepCost = (float)numWaterStepCost.Value,
WaterEnterPenalty = (float)numWaterEnterPenalty.Value,
WaterExitPenalty = (float)numWaterExitPenalty.Value,
RoadStepCost = (float)numRoadStepCost.Value,
RoadExitPenalty = (float)numRoadExitPenalty.Value,
};
foundPath = AStar.FindPath(_map, pathStart, pathGoal, searchSettings, progress, new DebugSettings());
}
private void WorkerPathfind_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (foundPath != null)
{
txtProgressStatus.Text = "Path Found";
GeneratatePathImage(foundPath);
SetDisplayImage(forceRefresh: true);
}
else
{
txtProgressStatus.Text = "No Path Found";
pathImage = null;
SetDisplayImage(forceRefresh: true);
}
}
private void WorkerPathfind_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
throw new NotImplementedException();
}
private void GeneratatePathImage(List<SearchTile> path)
{
var img = new MagickImage(MagickColors.Transparent, imageSize.Width, imageSize.Height);
using (var pixels = img.GetPixels())
{
foreach (var tile in path)
{
pixels.SetPixel(tile.Location.X, tile.Location.Y, DebugSettings.Red);
}
}
pathImage = img;
}
void SetDisplayImage(bool forceRefresh = false)
{
if (forceRefresh) _cachedComposite = null;
var display = GenerateCompositeImage();
pictureBoxMap.Image = display.ToBitmap();
pictureBoxMap.Width = (int)Math.Round(imageSize.Width * zoomFactor);
pictureBoxMap.Height = (int)Math.Round(imageSize.Height * zoomFactor);
txtZoom.Text = $"{zoomFactor * 100:###}%";
numStartX.Maximum = display.Width;
numStartY.Maximum = display.Height;
numGoalX.Maximum = display.Width;
numGoalY.Maximum = display.Height;
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dlgSave.ShowDialog() == DialogResult.OK)
{
if (!CheckExtension(dlgSave.FileName)) return;
var comp = GenerateCompositeImage(showReticles: false);
comp.Write(dlgSave.FileName);
}
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
if (pathImage != null && dlgSave.ShowDialog() == DialogResult.OK)
{
if (!CheckExtension(dlgSave.FileName)) return;
pathImage.Write(dlgSave.FileName);
}
}
bool CheckExtension(string fileName)
{
if (!fileName.EndsWith(".png"))
{
MessageBox.Show("Please only save as .png file", "Error Saving File", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
private MagickImage GenerateCompositeImage()
{
return GenerateCompositeImage(showReticles);
}
readonly Dictionary<MapType, MagickColor> _layerColors = new Dictionary<MapType, MagickColor> {
{ MapType.Passibility, MagickColors.PaleVioletRed },
{ MapType.Water, MagickColors.LightBlue },
{ MapType.Road, MagickColors.GreenYellow },
};
Dictionary<MapType, IMagickImage> _cachedColoredLayers = new Dictionary<MapType, IMagickImage> {
{ MapType.Passibility, null },
{ MapType.Water, null },
{ MapType.Road, null },
};
private IMagickImage GetColoredLayer(MapType mapType)
{
if(_cachedColoredLayers[mapType] == null)
{
using (var color = new MagickImage(_layerColors[mapType], imageSize.Width, imageSize.Height))
{
var compositeOp = mapType == MapType.Passibility ? CompositeOperator.Lighten : CompositeOperator.Darken;
var coloredMap = _mapImages[mapType].Clone();
coloredMap.Composite(color, compositeOp);
_cachedColoredLayers[mapType] = coloredMap;
}
}
return _cachedColoredLayers[mapType];
}
MagickImage _cachedComposite;
MagickImage GetLayeredImage()
{
if(_cachedComposite == null)
{
var composite = new MagickImage(_mapImages[MapType.Elevation]);
if (_mapImages[MapType.Water] != null && showWater)
{
composite.Composite(GetColoredLayer(MapType.Water), CompositeOperator.Screen);
}
if (_mapImages[MapType.Road] != null && showRoads)
{
composite.Composite(GetColoredLayer(MapType.Road), CompositeOperator.Screen);
}
if (pathImage != null && showPath)
{
composite.Composite(pathImage, CompositeOperator.Atop);
}
if (_mapImages[MapType.Passibility] != null && showPassibility)
{
composite.Composite(GetColoredLayer(MapType.Passibility), CompositeOperator.Multiply);
}
_cachedComposite = composite;
}
return _cachedComposite;
}
private MagickImage GenerateCompositeImage(bool showReticles = true)
{
var composite = new MagickImage(GetLayeredImage());
if (showReticles)
{
var sizedReticle = reticleIcon.Clone();
sizedReticle.Scale((int)Math.Round(reticleIcon.Width * invZoomFactor), (int)Math.Round(reticleIcon.Height * invZoomFactor));
composite.Composite(sizedReticle, pathStart.X - sizedReticle.Width / 2, pathStart.Y - sizedReticle.Height / 2, CompositeOperator.Atop);
composite.Composite(sizedReticle, pathGoal.X - sizedReticle.Width / 2, pathGoal.Y - sizedReticle.Height / 2, CompositeOperator.Atop);
}
return composite;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
//TODO: Check if Saved
Environment.Exit(0);
}
private void btnSelectStart_Click(object sender, EventArgs e)
{
currentTarget = PathPickerTarget.Start;
SetCursor();
}
private void btnSelectGoal_Click(object sender, EventArgs e)
{
currentTarget = PathPickerTarget.Goal;
SetCursor();
}
private void zoomOutToolStripMenuItem1_Click(object sender, EventArgs e)
{
zoomFactor /= 2;
SetDisplayImage();
}
private void zoomInToolStripMenuItem1_Click(object sender, EventArgs e)
{
zoomFactor *= 2;
SetDisplayImage();
}
private void zoomResetToolStripMenuItem_Click(object sender, EventArgs e)
{
zoomFactor = 1f;
SetDisplayImage();
}
private void showPathToolStripMenuItem_Click(object sender, EventArgs e)
{
showPath = showPathToolStripMenuItem.Checked;
SetDisplayImage(forceRefresh: true);
}
private void showPassibilityToolStripMenuItem_Click(object sender, EventArgs e)
{
showPassibility = showPassibilityToolStripMenuItem.Checked;
SetDisplayImage(forceRefresh: true);
}
private void showWaterToolStripMenuItem_Click(object sender, EventArgs e)
{
showWater = showWaterToolStripMenuItem.Checked;
SetDisplayImage(forceRefresh: true);
}
private void showRoadToolStripMenuItem_Click(object sender, EventArgs e)
{
showRoads = showRoadToolStripMenuItem.Checked;
SetDisplayImage(forceRefresh: true);
}
private void showStartGoalToolStripMenuItem_Click(object sender, EventArgs e)
{
showReticles = showStartGoalToolStripMenuItem.Checked;
SetDisplayImage();
}
private void btnLoadElevation_Click(object sender, EventArgs e)
{
if (!LoadMap(MapType.Elevation)) return;
grpGoalCoords.Enabled = true;
grpStartCoords.Enabled = true;
foreach (var type in _mapLoadingButtons.Keys)
_mapLoadingButtons[type].Enabled = true;
}
private void btnLoadPassibility_Click(object sender, EventArgs e)
{
LoadMap(MapType.Passibility);
}
private void btnLoadWater_Click(object sender, EventArgs e)
{
LoadMap(MapType.Water);
}
private void btnLoadRoads_Click(object sender, EventArgs e)
{
LoadMap(MapType.Road);
}
private bool LoadMap(MapType mapType)
{
dlgOpenMap.Title = $"Open {mapType} Map";
if (dlgOpenMap.ShowDialog() != DialogResult.OK) return false;
MagickImage potentialMap;
try
{
var mapPath = dlgOpenMap.FileName;
potentialMap = new MagickImage(mapPath);
}
catch (SecurityException ex)
{
MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
$"Details:\n\n{ex.StackTrace}");
return false;
}
if(mapType != MapType.Elevation)
{
if (_mapImages[MapType.Elevation] == null)
{
MessageBox.Show("Load Failed: load elevation map first");
return false;
}
if (!SizeMatchesElevation(potentialMap))
{
MessageBox.Show("Load Failed: all maps must be the same pixel dimensions as the elevation map");
return false;
}
_cachedColoredLayers[mapType] = null;
}
_mapImages[mapType] = potentialMap;
if (mapType == MapType.Elevation)
{
imageSize = new Size(potentialMap.Width, potentialMap.Height);
EnsureAllMapsSameSize();
}
SetDisplayImage(forceRefresh: true);
_pendingInitialize = true;
_loadedAndPendingInitialize[mapType] = true;
SetAllButtonCaptions();
return true;
}
bool SizeMatchesElevation(MagickImage img)
{
return imageSize.Width == img.Width && imageSize.Height == img.Height;
}
private void EnsureAllMapsSameSize()
{
foreach (var type in new MapType[]{MapType.Passibility, MapType.Water, MapType.Road})
{
if (_mapImages[type] == null) continue;
if(!SizeMatchesElevation(_mapImages[type])) {
_mapImages[type] = null;
_pendingInitialize = true;
_loadedAndPendingInitialize[type] = false;
}
}
}
private void btnInitializeMap_Click(object sender, EventArgs e)
{
InitializeMap();
}
private void SetAllButtonCaptions()
{
foreach(var mapType in _mapLoadingButtons.Keys)
{
var state = _mapImages[mapType] == null
? "None"
: _loadedAndPendingInitialize[mapType]
? "Loaded"
: "Initialized";
_mapLoadingButtons[mapType].Text = $"Load {mapType} Map ({state})";
}
}
}
}