-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathMap_Scalebar.cpp
More file actions
732 lines (630 loc) · 24.1 KB
/
Map_Scalebar.cpp
File metadata and controls
732 lines (630 loc) · 24.1 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
// ReSharper disable CppTooWideScopeInitStatement
// ReSharper disable CppTooWideScope
#include <StdAfx.h>
#include "Map.h"
#include "TileProviders.h"
#include "GraphicsStateHelper.h"
// ****************************************************************
// DrawStringWithShade()
// ****************************************************************
void CMapView::DrawStringWithShade(Gdiplus::Graphics* g, CStringW s, Gdiplus::Font* font, Gdiplus::PointF& point,
Gdiplus::Brush* brush, Gdiplus::Brush* brushOutline)
{
g->DrawString(s.GetString(), s.GetLength(), font, point, brushOutline);
point.X -= 1.0f;
point.Y -= 1.0f;
g->DrawString(s.GetString(), s.GetLength(), font, point, brush);
}
// ****************************************************************
// FormatUnits()
// ****************************************************************
CStringW FormatUnits(CStringW& s, double step, double power, int count)
{
if (power >= 1)
{
s.Format(L"%d", static_cast<int>(step * count));
}
else
{
CStringW sFormat;
if (power == 0.0) power = -1;
sFormat.Format(L"%%.%df", static_cast<int>(-power));
s.Format(sFormat, static_cast<float>(step) * count);
}
return s;
}
// ****************************************************************
// ChooseScalebarUnits()
// ****************************************************************
void ChooseScalebarUnits(tkUnitsOfMeasure sourceUnits, tkUnitsOfMeasure& targetUnits, double distance,
double& unitsPerPixel, double& step, int& count, double& power)
{
if (targetUnits == umMiles)
{
if (distance < 1)
{
targetUnits = umFeets;
unitsPerPixel *= 5280.0; // feet in mile
distance *= 5280.0;
}
}
else // meters
{
if (distance > 1000)
{
targetUnits = umKilometers;
unitsPerPixel /= 1000.0;
distance /= 1000.0;
}
if (distance < 1)
{
targetUnits = umCentimeters;
unitsPerPixel *= 100.0;
distance *= 100.0;
}
}
// ----------------------------------------------------------
// Choosing a step
// ----------------------------------------------------------
power = floor(log10(distance));
step = pow(10, floor(log10(distance)));
count = static_cast<int>(floor(distance / step));
if (count == 1)
{
step /= 4; // steps like 25-50-75
count = static_cast<int>(floor(distance / step));
}
if (count == 2)
{
step /= 2; // steps like 0-50-100
count = static_cast<int>(floor(distance / step));
}
if (count > 5)
{
step *= 2.5;
count = static_cast<int>(floor(distance / step));
}
}
// ****************************************************************
// DrawScaleBar()
// ****************************************************************
void CMapView::DrawScaleBar(Gdiplus::Graphics* g)
{
if (!_scalebarVisible) return;
constexpr int barWidth = 140;
constexpr int barHeight = 30;
constexpr int yPadding = 10;
constexpr int xPadding = 10;
constexpr int xOffset = 20;
constexpr int yOffset = 10;
constexpr int segmHeight = 5;
if (_viewWidth <= barWidth + xOffset || _viewHeight <= barHeight + yOffset) // control must be big enough
return;
if (_transformationMode != tmNotDefined)
{
const int zoom = GetCurrentZoom();
if (zoom >= 0 && zoom < 3) {
// lsu: there are some problems with displaying scale bar at such zoom levels:
// - there are areas outside the globe where coordinate transformations may fail;
// - the points at the left and right sides of the screen may lie on the same meridian
// so geodesic distance across the screen will be 0;
// - finally projection distortions change drastically by Y axis across map so
// the scalebar will be virtually meaningless;
// The easy solution will be simply not to show scale bar at such small scales
return;
}
}
double minX, maxX, minY, maxY; // size of map control in pixels
PROJECTION_TO_PIXEL(_extents.left, _extents.bottom, minX, minY);
PROJECTION_TO_PIXEL(_extents.right, _extents.top, maxX, maxY);
double xMin = _extents.left;
double yMin = _extents.top;
double xMax = _extents.right;
double yMax = _extents.bottom;
double width = _extents.right - _extents.left;
tkUnitsOfMeasure units = _unitsOfMeasure;
// ----------------------------------------------------------
// Geodesic distance calculation
// ----------------------------------------------------------
if (_transformationMode != tkTransformationMode::tmNotDefined)
{
// skip calculations when extents haven't changed
if (this->_lastWidthMeters == 0.0)
{
bool skipTransform = false;
if (_transformationMode == tkTransformationMode::tmDoTransformation)
{
VARIANT_BOOL vb1, vb2;
IGeoProjection* projTemp = GetMapToWgs84Transform();
if (projTemp)
{
projTemp->Transform(&xMin, &yMin, &vb1);
projTemp->Transform(&xMax, &yMax, &vb2);
if (!vb1 || !vb2)
{
skipTransform = true;
}
}
else {
skipTransform = true;
}
}
if (!skipTransform)
{
GetUtils()->GeodesicDistance((yMax + yMin) / 2, xMin, (yMax + yMin) / 2, xMax, &width);
_lastWidthMeters = width;
units = tkUnitsOfMeasure::umMeters;
}
}
else
{
width = _lastWidthMeters;
units = tkUnitsOfMeasure::umMeters;
}
}
if (width <= 0.0) return;
// ----------------------------------------------------------
// Choosing units
// ----------------------------------------------------------
double unitsPerPixel2{};
double step, power; int count;
double step2{}, power2{}; int count2{};
double newWidth = width;
// metric calculations are performed for mixed mode as well
tkUnitsOfMeasure targetUnits = _scalebarUnits == American ? umMiles : umMeters;
Utility::ConvertDistance(units, targetUnits, newWidth);
double unitsPerPixel = newWidth / (maxX - minX); // target units on screen size
double distance = (barWidth - xPadding * 2) * unitsPerPixel;
ChooseScalebarUnits(units, targetUnits, distance, unitsPerPixel, step, count, power);
tkUnitsOfMeasure targetUnits2 = umMiles;
if (_scalebarUnits == GoogleStyle)
{
// now do calculation for miles
newWidth = width;
Utility::ConvertDistance(units, umMiles, newWidth);
unitsPerPixel2 = newWidth / (maxX - minX); // target units on screen size
distance = (barWidth - xPadding * 2) * unitsPerPixel2;
ChooseScalebarUnits(units, targetUnits2, distance, unitsPerPixel2, step2, count2, power2);
}
// ----------------------------------------------------------
// Initialize drawing
// ----------------------------------------------------------
Gdiplus::Matrix mtx;
mtx.Translate(static_cast<float>(5), static_cast<float>(_viewHeight) - barHeight - yOffset);
g->SetTransform(&mtx);
const Gdiplus::PixelOffsetMode pixelOffsetMode = g->GetPixelOffsetMode();
g->SetPixelOffsetMode(Gdiplus::PixelOffsetMode::PixelOffsetModeHighQuality);
Gdiplus::RectF rect(0.0f, 0.0f, barWidth, barHeight);
// ----------------------------------------------------------
// Drawing of segments
// ----------------------------------------------------------
std::vector<Gdiplus::Rect*> parts;
int length = static_cast<int>(step * count / unitsPerPixel + xPadding);
if (_scalebarUnits != GoogleStyle)
{
// horizontal line
parts.push_back(new Gdiplus::Rect(xPadding, barHeight - yPadding, length - xPadding, 0));
// inner measures (shorter)
for (int i = 0; i <= count; i++)
{
length = static_cast<int>(step * i / unitsPerPixel + xPadding);
const int valHeight = i == 0 || i == count ? segmHeight * 2 : segmHeight; // the height of the mark; side marks are longer
parts.push_back(new Gdiplus::Rect(length, barHeight - yPadding - valHeight, 0, valHeight));
}
}
else
{
const int length2 = static_cast<int>(step2 * count2 / unitsPerPixel2 + xPadding);
// horizontal line
parts.push_back(new Gdiplus::Rect(xPadding, barHeight / 2 - yPadding, length - xPadding, 0));
parts.push_back(new Gdiplus::Rect(xPadding, barHeight / 2 - yPadding, length2 - xPadding, 0));
// vertical lines for metric
const int valHeight = segmHeight * 2;
parts.push_back(new Gdiplus::Rect(xPadding, barHeight / 2 - yPadding - valHeight, 0, valHeight));
parts.push_back(new Gdiplus::Rect(length, barHeight / 2 - yPadding - valHeight, 0, valHeight));
// vertical lines for USA
parts.push_back(new Gdiplus::Rect(xPadding, barHeight / 2 - yPadding, 0, valHeight));
parts.push_back(new Gdiplus::Rect(length2, barHeight / 2 - yPadding, 0, valHeight));
}
for (size_t i = 0; i < parts.size(); i++)
{
g->FillRectangle(_brushWhite, parts[i]->X - 2, parts[i]->Y - 2, parts[i]->Width + 4, parts[i]->Height + 4);
}
for (size_t i = 0; i < parts.size(); i++)
{
g->FillRectangle(_brushBlack, parts[i]->X - 1, parts[i]->Y - 1, parts[i]->Width + 2, parts[i]->Height + 2);
}
for (size_t i = 0; i < parts.size(); i++)
{
delete parts[i];
}
parts.clear();
g->SetPixelOffsetMode(pixelOffsetMode);
// ----------------------------------------------------------
// Drawing of text
// ----------------------------------------------------------
const Gdiplus::TextRenderingHint hint = g->GetTextRenderingHint();
g->SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAliasGridFit);
Gdiplus::FontFamily family(L"Arial");
const Gdiplus::REAL fontSize = _scalebarUnits != GoogleStyle ? 10.0f : 8.0f;
Gdiplus::Font font(&family, fontSize, Gdiplus::FontStyleRegular, Gdiplus::UnitPoint);
CStringW s;
Gdiplus::StringFormat format;
if (_scalebarUnits != GoogleStyle)
{
s.Format(L"0");
Gdiplus::PointF point(xPadding + 4.0f, -4.0f);
DrawStringWithShade(g, s, &font, point, _brushBlack, _brushWhite);
// max
FormatUnits(s, step, power, count);
point.X = static_cast<Gdiplus::REAL>(step * count / unitsPerPixel + xPadding + 3 + 1.0f);
point.Y = -4.0f;
DrawStringWithShade(g, s, &font, point, _brushBlack, _brushWhite);
// units
s = Utility::GetLocalizedUnitsText(targetUnits);
point.X = static_cast<Gdiplus::REAL>(step * count / unitsPerPixel + xPadding + 3 + 1.0f);
point.Y = barHeight - yPadding - 12 + 1.0f;
DrawStringWithShade(g, s, &font, point, _brushBlack, _brushWhite);
}
else
{
// metric
USES_CONVERSION;
s.Format(L"%s %s", FormatUnits(s, step, power, count), Utility::GetLocalizedUnitsText(targetUnits));
Gdiplus::PointF point(xPadding + 8.0f, -10.0f);
DrawStringWithShade(g, s, &font, point, _brushBlack, _brushWhite);
// miles
s.Format(L"%s %s", FormatUnits(s, step2, power2, count2), Utility::GetLocalizedUnitsText(targetUnits2));
point.Y = 8.0f;
DrawStringWithShade(g, s, &font, point, _brushBlack, _brushWhite);
}
g->SetTextRenderingHint(hint);
g->ResetTransform();
}
// ****************************************************************
// ShowRedrawTime()
// ****************************************************************
// Displays redraw time in the bottom right corner
void CMapView::ShowRedrawTime(Gdiplus::Graphics* g, float time, bool layerRedraw, CStringW message)
{
_copyrightRect = Gdiplus::RectF(0.0f, 0.0f, 0.0f, 0.0f);
bool showRedrawTime = _showRedrawTime && time > 0.01 && !_isSnapshot;
bool showVersionNumber = _showVersionNumber && !_isSnapshot;
CStringW s;
tkTileProvider provider = GetTileProvider();
if (provider != tkTileProvider::ProviderNone && _transformationMode != tmNotDefined)
{
CComPtr<ITileProviders> providers = NULL;
_tiles->get_Providers(&providers);
s = ((CTileProviders*)&(*providers))->get_CopyrightNotice(provider);
}
if (!showRedrawTime && !showVersionNumber && s.GetLength() == 0) return;
POINT mousePnt;
if (GetCursorPos(&mousePnt))
ScreenToClient(&mousePnt);
Gdiplus::GraphicsStateHelper gstate;
gstate.SetTextRenderingHint(g, Gdiplus::TextRenderingHintAntiAliasGridFit);
Gdiplus::PointF point(0.0f, 0.0f);
Gdiplus::StringFormat format;
if (s.GetLength() > 0)
{
g->MeasureString(s, s.GetLength(), _fontCourierLink, point, &format, &_copyrightRect);
_copyrightRect.Height += 5;
_copyrightRect.Width += 5;
if (_copyrightRect.Width < _viewWidth) // control must be big enough to host the string
{
point.X = (float)(_viewWidth - _copyrightRect.Width);
point.Y = (float)(_viewHeight - _copyrightRect.Height);
_copyrightRect.X += point.X;
_copyrightRect.Y += point.Y;
//bool active = _copyrightRect.Contains((Gdiplus::REAL)mousePnt.x, (Gdiplus::REAL)mousePnt.y);
Gdiplus::SolidBrush* textBrush = _copyrightLinkActive ? _brushBlue : _brushBlack;
Gdiplus::Font* font = _copyrightLinkActive ? _fontCourierLink : _fontCourierSmall;
format.SetAlignment(Gdiplus::StringAlignmentCenter);
format.SetLineAlignment(Gdiplus::StringAlignmentCenter);
g->FillRectangle(_brushLightGray, _copyrightRect);
g->DrawString(s.GetString(), s.GetLength(), font, _copyrightRect, &format, textBrush);
}
}
if (showVersionNumber)
{
USES_CONVERSION;
Gdiplus::RectF rect;
s.Format(L"MapWinGIS %s", OLE2W(GetVersionNumber()));
g->MeasureString(s, s.GetLength(), _fontCourierSmall, point, &format, &rect);
if (rect.Width < _viewWidth) // control must be big enough to host the string
{
point.X = (float)(_viewWidth - rect.Width);
point.Y = (float)(_viewHeight - rect.Height - _copyrightRect.Height - 3);
DrawStringWithShade(g, s, _fontCourierSmall, point, _brushBlack, _brushWhite);
}
}
if (showRedrawTime)
{
if (message.GetLength() != 0)
{
s = message;
}
else
{
s.Format(L"Redraw time: %.3fs", time);
}
Gdiplus::RectF rect;
g->MeasureString(s, s.GetLength(), _fontCourier, point, &format, &rect);
if (rect.Width + 15 < _viewWidth) // control must be big enough to host the string
{
point.X = (float)(_viewWidth - rect.Width - 10);
point.Y = _showCoordinates != cdmNone ? 10.0f + rect.Height : 10.0f;
DrawStringWithShade(g, s, _fontCourier, point, _brushBlack, _brushWhite);
}
}
gstate.RestoreTextRenderingHint(g);
}
#pragma region Zoombar
// ****************************************************************
// ZoombarHitTest()
// ****************************************************************
ZoombarPart CMapView::ZoombarHitTest(int x, int y)
{
if (!_zoombarVisible || _transformationMode == tmNotDefined)
return ZoombarPart::ZoombarNone;
POINT pnt;
pnt.x = x; pnt.y = y;
// plus button
if (_zoombarParts.PlusButton.PtInRect(pnt))
return ZoombarPart::ZoombarPlus;
// minus button
if (_zoombarParts.MinusButton.PtInRect(pnt))
return ZoombarPart::ZoombarMinus;
// handle
if (_zoombarParts.Handle.PtInRect(pnt))
return ZoombarPart::ZoombarHandle;
// bar
if (_zoombarParts.Bar.PtInRect(pnt))
return ZoombarPart::ZoombarBar;
return ZoombarPart::ZoombarNone;
}
// ****************************************************************
// DrawGradientShadowForPath()
// ****************************************************************
void DrawGradientShadowForPath(Gdiplus::GraphicsPath& path, Gdiplus::Graphics* g)
{
bool enableShadow = false; // doesn't look well; skip it for now
if (enableShadow)
{
float shadowOffset = 2.0f;
g->TranslateTransform(0.0f, shadowOffset);
// drawing shadow
Gdiplus::GraphicsPath* outline = path.Clone();
Gdiplus::Pen p2(Gdiplus::Color::Gray, 4.0f);
outline->Widen(&p2);
Gdiplus::PathGradientBrush pathBrush(outline);
pathBrush.SetWrapMode(Gdiplus::WrapMode::WrapModeClamp);
Gdiplus::Color colors[3];
colors[0] = Gdiplus::Color::Transparent;
colors[1] = Gdiplus::Color(180, 0, 0, 0);
colors[2] = Gdiplus::Color(180, 0, 0, 0);
float positions[3];
positions[0] = 0.0f;
positions[1] = 0.7f;
positions[2] = 1.0f;
pathBrush.SetInterpolationColors(&(colors[0]), &(positions[0]), 3);
g->FillPath(&pathBrush, outline);
g->TranslateTransform(0, -shadowOffset);
}
}
// ****************************************************************
// DrawZoombar()
// ****************************************************************
void CMapView::DrawZoombar(Gdiplus::Graphics* g)
{
if (!_zoombarVisible || _transformationMode == tmNotDefined)
return;
// mouse position
POINT pnt;
if (GetCursorPos(&pnt))
ScreenToClient(&pnt);
float boxSize = 17.0f;
float cornerRadius = 5.0f;
Gdiplus::GraphicsPath path;
Gdiplus::RectF bounds(0.0f, 0.0f, boxSize, boxSize);
path.AddArc(bounds.X + bounds.Width - cornerRadius, bounds.Y, cornerRadius, cornerRadius, 270.0f, 90.0f);
path.AddArc(bounds.X + bounds.Width - cornerRadius, bounds.Y + bounds.Height - cornerRadius, cornerRadius, cornerRadius, 0.0f, 90.0f);
path.AddArc(bounds.X, bounds.Y + bounds.Height - cornerRadius, cornerRadius, cornerRadius, 90.0f, 90.0f);
path.AddArc(bounds.X, bounds.Y, cornerRadius, cornerRadius, 180.0f, 90.0f);
path.CloseAllFigures();
int maxZoom, minZoom;
GetMinMaxZoom(minZoom, maxZoom);
float notchStep = 7;
float lineOffset = 8.0f;
float lineHeight = (maxZoom - minZoom) * notchStep;
float x = 15.0f, y = 15.0f;
float height = lineHeight + boxSize + 2 * lineOffset;
Gdiplus::Matrix m;
g->GetTransform(&m);
// ---------------------------------------------------
// Plus/minus sings
// ---------------------------------------------------
Gdiplus::PixelOffsetMode pixelOffsetMode = g->GetPixelOffsetMode();
// upper plus button
_zoombarParts.PlusButton.SetRect((int)x, (int)y, (int)(x + boxSize), (int)(y + boxSize));
BOOL highlight = _zoombarParts.PlusButton.PtInRect(pnt);
g->TranslateTransform(x, y);
DrawGradientShadowForPath(path, g);
g->FillPath(_brushWhite, &path);
g->DrawPath(highlight ? _penDarkGray : _penGray, &path);
g->SetTransform(&m);
// lower minus button
_zoombarParts.MinusButton.SetRect((int)x, (int)(y + height), (int)(x + boxSize), (int)(y + height + boxSize));
highlight = _zoombarParts.MinusButton.PtInRect(pnt);
g->TranslateTransform(x, y + height);
DrawGradientShadowForPath(path, g);
g->FillPath(_brushWhite, &path);
g->DrawPath(highlight ? _penDarkGray : _penGray, &path);
g->SetTransform(&m);
// plus sign
g->SetPixelOffsetMode(Gdiplus::PixelOffsetMode::PixelOffsetModeHighQuality);
g->FillRectangle(_brushGray, x + 5.0f, y + 8.0f, 8.0f, 2.0f);
g->FillRectangle(_brushGray, x + 8.0f, y + 5.0f, 2.0f, 8.0f);
// minus sign
g->FillRectangle(_brushGray, x + 5.0f, y + height + 8.0f, 8.0f, 2.0f);
g->SetPixelOffsetMode(pixelOffsetMode);
// ---------------------------------------------------
// Handle position
// ---------------------------------------------------
float lineWidth = 5.0f;
float handleY = 0.0f;
bool handleHighlight = false;
int zoom = this->GetCurrentZoom();
int targetZoom = -1; // for handle dragging
bool zooming = _dragging.Operation == DragZoombarHandle;
if (zooming)
{
// take y from current mouse position
handleY = (float)_dragging.Move.y;
// make sure that we are within bar
float minY = y + boxSize + lineOffset;
float maxY = minY + lineHeight;
if (handleY < minY) handleY = minY;
if (handleY > maxY) handleY = maxY;
// calculate current zoom
float ratio = (handleY - minY) / (maxY - minY);
targetZoom = (int)Utility::Rint(maxZoom - (maxZoom - minZoom) * ratio);
_zoombarTargetZoom = targetZoom;
// backward calculation to snap to the level
float position = (float)((maxZoom - targetZoom) / ((double)(maxZoom - minZoom)) * lineHeight);
handleY = (float)(int)(y + boxSize + lineOffset + position - boxSize / 4.0f + 0.5);
// adjust for the height of handle
//handleY -= boxSize/4.0f;
handleY = (float)Utility::Rint(handleY);
handleHighlight = true;
_zoombarParts.Handle.SetRect((int)x, (int)handleY, (int)(x + boxSize), (int)(handleY + bounds.Height));
}
else
{
// display current zoom
if (zoom > maxZoom) zoom = maxZoom;
if (zoom < minZoom) zoom = minZoom;
float position = (float)((maxZoom - zoom) / ((double)(maxZoom - minZoom)) * lineHeight);
handleY = (float)(int)(y + boxSize + lineOffset + position - boxSize / 4.0f + 0.5);
_zoombarParts.Handle.SetRect((int)x, (int)handleY, (int)(x + boxSize), (int)(handleY + bounds.Height));
handleHighlight = _zoombarParts.Handle.PtInRect(pnt) ? true : false;
}
// ---------------------------------------------------
// Tooltip drawing
// ---------------------------------------------------
// if handle is highlighted (i.e. mouse cursor is within it, draw a tooltip)
if (handleHighlight)
{
if (_zoomBarVerbosity != zbvNone)
{
double scale = GetCurrentScale();
if (zooming)
{
// it's troublesome to calculate target scale directly, therefore use simple proportion
double ratio = pow(2.0, (double)(zoom - targetZoom));
scale = scale * ratio;
}
double resoultion = scale / 96.0 * 0.0254; // meters per pixel
CStringW s;
switch (_zoomBarVerbosity)
{
case zbvFull:
s.Format(L"Zoom: %d\nScale: 1:%.0f\nResolution: %.2f", zooming ? targetZoom : zoom, scale, resoultion);
break;
case zbvZoomOnly:
s.Format(L"Zoom: %d", zooming ? targetZoom : zoom);
break;
}
float tooltipOffset = 15.0f;
Gdiplus::PointF tooltipOrigin(x + boxSize + tooltipOffset, handleY);
Gdiplus::RectF tooltipBox;
g->MeasureString(s, s.GetLength(), _fontArial, tooltipOrigin, &tooltipBox);
Gdiplus::SolidBrush tooltipBrush(Gdiplus::Color(255, 255, 255)/*Gdiplus::Color(246, 212, 178 )*/);
tooltipBox.Inflate(9.0f, 5.0f);
g->FillRectangle(&tooltipBrush, tooltipBox);
g->DrawRectangle(_penGray, tooltipBox);
tooltipBox.Inflate(-9.0f, -5.0f);
g->DrawString(s, s.GetLength(), _fontArial, tooltipOrigin, _brushBlack);
}
}
// ---------------------------------------------------
// Bar with notches
// ---------------------------------------------------
Gdiplus::RectF line(x + boxSize / 2.0f - lineWidth / 2.0f, y + boxSize + lineOffset, lineWidth, lineHeight);
_zoombarParts.Bar.SetRect((int)x, (int)line.Y, (int)(x + boxSize), (int)(line.Y + line.Height)); // include the width of handle; not the actual line only
highlight = _zoombarParts.Bar.PtInRect(pnt);
Gdiplus::GraphicsPath path3;
path3.AddRectangle(line);
DrawGradientShadowForPath(path3, g);
g->FillRectangle(_brushWhite, line);
g->DrawRectangle(highlight && !handleHighlight ? _penDarkGray : _penGray, line);
// notches for zoom levels
g->SetPixelOffsetMode(Gdiplus::PixelOffsetMode::PixelOffsetModeHighQuality);
float step = lineHeight / (maxZoom - minZoom);
float tempX = x + boxSize / 2.0f - lineWidth / 2.0f + 1;
for (int i = 1; i < maxZoom - minZoom; i++)
{
float tempY = (float)(int)(y + boxSize + lineOffset + step * i + 0.5);
g->FillRectangle(_brushGray, tempX, tempY, 4.0f, 1.0f);
}
g->SetPixelOffsetMode(pixelOffsetMode);
// ---------------------------------------------------
// Drawing of handle
// ---------------------------------------------------
// shape of handle
Gdiplus::GraphicsPath path2;
bounds.Height = 9.0f;
path2.AddArc(bounds.X, bounds.Y, cornerRadius, cornerRadius, 180, 90);
path2.AddArc(bounds.X + bounds.Width - cornerRadius, bounds.Y, cornerRadius, cornerRadius, 270, 90);
path2.AddArc(bounds.X + bounds.Width - cornerRadius, bounds.Y + bounds.Height - cornerRadius, cornerRadius, cornerRadius, 0, 90);
path2.AddArc(bounds.X, bounds.Y + bounds.Height - cornerRadius, cornerRadius, cornerRadius, 90, 90);
path2.CloseAllFigures();
// drawing the handle
g->TranslateTransform(x, handleY); // boxSize/4.0 = center of handle is the position
DrawGradientShadowForPath(path2, g);
g->FillPath(_brushWhite, &path2);
g->DrawPath(handleHighlight ? _penDarkGray : _penGray, &path2);
g->SetTransform(&m);
// minus sign
g->SetPixelOffsetMode(Gdiplus::PixelOffsetMode::PixelOffsetModeHighQuality);
g->FillRectangle(_brushGray, x + 5.0f, handleY + 4.0f, 8.0f, 2.0f);
g->SetPixelOffsetMode(pixelOffsetMode);
_penDarkGray->SetWidth(1.0f);
}
// ****************************************************************
// GetMinMaxZoom()
// ****************************************************************
bool CMapView::GetMinMaxZoom(int& minZoom, int& maxZoom)
{
_tiles->get_MinZoom(&minZoom);
_tiles->get_MaxZoom(&maxZoom);
if (_zoomBarMinZoom + 1 <= _zoomBarMaxZoom)
{
if (_zoomBarMinZoom != -1) minZoom = _zoomBarMinZoom;
if (_zoomBarMaxZoom != -1) maxZoom = _zoomBarMaxZoom;
}
int min;
if (GetTileMismatchMinZoom(min)) {
minZoom = min;
}
return true;
}
// ****************************************************************
// GetTileMismatchMinZoom()
// ****************************************************************
bool CMapView::GetTileMismatchMinZoom(int& minZoom)
{
VARIANT_BOOL sphericalMercator;
_tiles->get_ProjectionIsSphericalMercator(&sphericalMercator);
if (_tileProjectionState == ProjectionDoTransform && sphericalMercator)
{
minZoom = m_globalSettings.tilesMaxZoomOnProjectionMismatch;
return true;
}
else
{
return false;
}
}
#pragma endregion