-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathMap_Drawing.cpp
More file actions
1483 lines (1259 loc) · 42 KB
/
Map_Drawing.cpp
File metadata and controls
1483 lines (1259 loc) · 42 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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "Map.h"
#include "Tiles.h"
#include "TilesDrawer.h"
#include "GeoProjection.h"
#include "ShapefileDrawing.h"
#include "ImageDrawing.h"
#include "LabelDrawing.h"
#include "ChartDrawing.h"
#include "Image.h"
#include "Measuring.h"
#include "GeometryHelper.h"
#include "LayerDrawer.h"
#include "ShapefileHelper.h"
#include "SelectionListHelper.h"
#include "ShapeStyleHelper.h"
#include "SelectionList.h"
#include "WmsHelper.h"
// ***************************************************************
// OnDraw()
// ***************************************************************
void CMapView::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
if (_isSizing)
{
// An option to clear the control surface with back color before redraw while sizing
// but to leave the previous bitmap and to paint only the new regions seems nicer
//m_layerDC->FillSolidRect(rcBounds,m_backColor);
//ShowRedrawTime(0.0f, "Drawing...");
//pdc->BitBlt( 0, 0, rcBounds.Width(), rcBounds.Height(), m_layerDC, 0, 0, SRCCOPY);
return; // redraw is prohibited before the sizing will be finished
}
//This line is intended to ensure proper function in MSAccess by verifying that the hWnd handle exists
//before trying to draw. Lailin Chen - 2005/10/17
if (this->m_hWnd == NULL)
return;
// no redraw is allowed when the rubber band is being dragged
if (_rectTrackerIsActive)
return;
// the map is locked
if (_lockCount > 0)
return;
m_drawMutex.Lock(); // TODO: perhaps use lighter CCriticalSection
if (!_canUseMainBuffer || !_canUseVolatileBuffer || !_canUseLayerBuffer)
{
bool hasMouseMoveData = HasDrawingData(tkDrawingDataAvailable::MeasuringData) ||
HasDrawingData(tkDrawingDataAvailable::Coordinates) ||
HasDrawingData(tkDrawingDataAvailable::ZoomBox) ||
HasDrawingData(tkDrawingDataAvailable::ShapeDigitizing) ||
HasDrawingData(tkDrawingDataAvailable::FocusRect);
// if there is no move data, draws to output canvas directly
this->HandleNewDrawing(pdc, rcBounds, rcInvalid, !hasMouseMoveData);
if (hasMouseMoveData)
{
// the main drawing will be taken from the buffer as it wasn't passed to output canvas yet
DrawDynamic(pdc, rcBounds, rcInvalid, true);
}
}
else
{
// always draw the main buffer, even if there is no measuring data
DrawDynamic(pdc, rcBounds, rcInvalid, true);
}
m_drawMutex.Unlock();
}
// ***************************************************************
// HandleNewDrawing()
// ***************************************************************
void CMapView::HandleNewDrawing(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid,
bool drawToOutputCanvas, float offsetX, float offsetY)
{
Gdiplus::Color backColor = Utility::OleColor2GdiPlus(m_backColor);
Gdiplus::Graphics* gBuffer = NULL; // for control rendering
Gdiplus::Graphics* gPrinting = NULL; // for snapshot drawing
Gdiplus::Graphics* g = NULL; // the right one to draw
// preparing graphics (for snapshot drawing to output canvas directly;
// for control rendering main buffer is used)
if (_isSnapshot)
{
gPrinting = Gdiplus::Graphics::FromHDC(pdc->GetSafeHdc());
gPrinting->TranslateTransform(offsetX, offsetY);
Gdiplus::RectF clip((Gdiplus::REAL)rcInvalid.left, (Gdiplus::REAL)rcInvalid.top, (Gdiplus::REAL)rcInvalid.Width(), (Gdiplus::REAL)rcInvalid.Height());
gPrinting->SetClip(clip);
Gdiplus::Color color(255, 255, 255, 255);
Gdiplus::SolidBrush brush(color);
gPrinting->Clear(color);
g = gPrinting;
}
else
{
gBuffer = Gdiplus::Graphics::FromImage(_bufferBitmap);
gBuffer->SetCompositingMode(Gdiplus::CompositingModeSourceOver);
gBuffer->Clear(backColor);
g = gBuffer;
}
// the main thing is to ensure that if new tile is coming it will trigger a new redraw,
// any tile after this moment will do it, otherwise a newcomer will be already in screen buffer
_canUseMainBuffer = true;
RedrawTiles(g, pdc);
RedrawWmsLayers(g);
DWORD startTick = ::GetTickCount();
bool layersRedraw = RedrawLayers(g, pdc, rcBounds);
RedrawVolatileData(g, pdc, rcBounds);
if (m_sendOnDrawBackBuffer)
{
// passing main buffer to client for custom drawing
FireOnDrawbackBufferCore(g, _isSnapshot ? NULL : _bufferBitmap);
}
RedrawTools(g, rcBounds);
// redraw time and logo
DWORD endTick = GetTickCount();
if (layersRedraw) {
_lastRedrawTime = (float)(endTick - startTick) / 1000.0f;
}
ShowRedrawTime(g, _lastRedrawTime, layersRedraw);
// uncomment to save screen buffers to the disk
//DumpBuffers();
// passing the main buffer to the screen if no other drawing will be needed
if (!_isSnapshot)
{
if (drawToOutputCanvas)
{
HDC hdc = pdc->GetSafeHdc();
Gdiplus::Graphics* g = Gdiplus::Graphics::FromHDC(hdc);
g->SetCompositingMode(Gdiplus::CompositingModeSourceCopy);
g->DrawImage(_bufferBitmap, 0.0f, 0.0f);
g->ReleaseHDC(pdc->GetSafeHdc());
delete g;
}
else {
// otherwise, there must be mouse move data and the buffer will be drawn there to avoid flickering
}
delete gBuffer;
}
else
{
gPrinting->ReleaseHDC(pdc->GetSafeHdc());
delete gPrinting;
}
}
// ***************************************************************
// DumpBuffers()
// ***************************************************************
void CMapView::DumpBuffers()
{
#ifndef RELEASE_MODE
CLSID clsid;
Utility::GetEncoderClsid(L"image/png", &clsid);
_bufferBitmap->Save(L"D:\\buffer.png", &clsid, NULL);
_drawingBitmap->Save(L"D:\\drawing.png", &clsid, NULL);
_tilesBitmap->Save(L"D:\\tiles.png", &clsid, NULL);
_layerBitmap->Save(L"D:\\layers.png", &clsid, NULL);
_volatileBitmap->Save(L"D:\\volatile.png", &clsid, NULL);
#endif
}
// ***************************************************************
// FireOnDrawbackBufferCore()
// ***************************************************************
void CMapView::FireOnDrawbackBufferCore(Gdiplus::Graphics* g, Gdiplus::Bitmap* bitmap)
{
// old way (doesn't support alpha channel)
if (g && (_customDrawingFlags & OnDrawBackBufferHdc))
{
HDC hdc = g->GetHDC();
FireOnDrawBackBuffer((long)hdc);
g->ReleaseHDC(hdc);
}
// new way (doesn't work for printing since there is no buffer bitmap in that case)
if (bitmap && (_customDrawingFlags & OnDrawBackBufferBitmapData))
{
Gdiplus::Rect r(0, 0, _bufferBitmap->GetWidth(), _bufferBitmap->GetHeight());
Gdiplus::BitmapData bmd;
_bufferBitmap->LockBits(&r, Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeWrite, _bufferBitmap->GetPixelFormat(), &bmd);
FireOnDrawBackBuffer2((long)bmd.Height, (long)bmd.Width, (long)bmd.Stride, (long)bmd.PixelFormat, (long)bmd.Scan0);
_bufferBitmap->UnlockBits(&bmd);
}
}
// ***************************************************************
// RedrawLayers()
// ***************************************************************
bool CMapView::RedrawLayers(Gdiplus::Graphics* g, CDC* dc, const CRect& rcBounds)
{
bool layersRedraw = false;
if (HasDrawingData(tkDrawingDataAvailable::LayersData))
{
if (_isSnapshot)
{
this->DrawLayers(rcBounds, g);
}
else
{
if (_canUseLayerBuffer)
{
bool dragging = _dragging.Operation == DragPanning &&
(_dragging.Start.x != 0 || _dragging.Start.y != 0 ||
_dragging.Move.x != 0 || _dragging.Move.y != 0);
int x = dragging ? _dragging.Move.x - _dragging.Start.x : 0;
int y = dragging ? _dragging.Move.y - _dragging.Start.y : 0;
// update from the layer buffer
g->DrawImage(_layerBitmap, (float)x, (float)y);
}
else
{
ClearHotTracking();
layersRedraw = true;
Gdiplus::Graphics* gLayers = Gdiplus::Graphics::FromImage(_layerBitmap);
gLayers->Clear(Gdiplus::Color::Transparent);
gLayers->SetCompositingMode(Gdiplus::CompositingModeSourceOver);
// fire external before layer drawing code
if (_customDrawingFlags & BeforeAfterLayers)
{
HDC hdc = g->GetHDC();
tkMwBoolean retVal = blnFalse;
this->FireBeforeLayers((long)hdc, rcBounds.left, rcBounds.right, rcBounds.top, rcBounds.bottom, &retVal);
g->ReleaseHDC(hdc);
}
bool useRotation = false; // not implemented
if (useRotation) {
DrawLayersRotated(dc, gLayers, rcBounds);
}
else {
DrawLayers(rcBounds, gLayers);
}
// passing layer buffer to the main buffer
g->DrawImage(_layerBitmap, 0.0f, 0.0f);
// clean up
delete gLayers;
}
// fire external after layer drawing code
// NOTE that we want to do this even if drawing from layer buffer (tkRedrawType::RedrawSkipDataLayers)
// since even that erases any custom drawing done through the hDC. We still should consider whether
// or not to allow After Layer drawing for the Snapshot (which is not done for the After Drawing draw)
if (_customDrawingFlags & BeforeAfterLayers)
{
HDC hdc = g->GetHDC();
tkMwBoolean retVal = blnFalse;
this->FireAfterLayers((long)hdc, rcBounds.left, rcBounds.right, rcBounds.top, rcBounds.bottom, &retVal);
g->ReleaseHDC(hdc);
}
}
}
_canUseLayerBuffer = true;
return layersRedraw;
}
// ***************************************************************
// RedrawWmsLayers()
// ***************************************************************
void CMapView::RedrawWmsLayers(Gdiplus::Graphics* g)
{
double scale = GetCurrentScale();
for (long i = 0; i < GetNumLayers(); i++)
{
Layer* layer = get_LayerByPosition(i);
if (!layer || layer->IsEmpty() || !layer->IsWmsLayer()) {
continue;
}
if (!layer->IsVisible(scale, _currentZoom)) {
continue;
}
CComPtr<IWmsLayer> wms = NULL;
layer->QueryWmsLayer(&wms);
TileManager* manager = WmsHelper::Cast(wms)->get_Manager();
if (!manager) return;
Gdiplus::Bitmap* wmsBuffer = WmsHelper::Cast(wms)->get_ScreenBuffer();
if (wmsBuffer)
{
if (manager->get_ScreenBufferChanged())
{
manager->MarkUndrawn();
}
bool hasUndrawn = manager->UndrawnTilesExist();
bool hasDrawn = manager->DrawnTilesExist();
if (!hasUndrawn && !hasDrawn)
{
return;
}
if (!hasUndrawn)
{
// take it from the buffer
g->DrawImage(wmsBuffer, 0.0f, 0.0f);
}
else
{
Gdiplus::Graphics* gWms = Gdiplus::Graphics::FromImage(wmsBuffer);
if (!hasDrawn)
{
gWms->Clear(Gdiplus::Color::Transparent);
}
TilesDrawer drawer(gWms, &_extents, _pixelPerProjectionX, _pixelPerProjectionY, PixelsPerMapUnit(), GetWgs84ToMapTransform());
drawer.DrawTiles(manager, GetMapProjection(), _isSnapshot, _projectionChangeCount);
g->DrawImage(wmsBuffer, 0.0f, 0.0f);
// clean up
delete gWms;
}
}
}
}
// ***************************************************************
// DrawTiles()
// ***************************************************************
void CMapView::RedrawTiles(Gdiplus::Graphics* g, CDC* dc)
{
if (HasDrawingData(tkDrawingDataAvailable::TilesData))
{
CTiles* tiles = (CTiles*)_tiles;
if (_isSnapshot)
{
get_TileManager()->MarkUndrawn();
DrawTiles(g);
get_TileManager()->MarkUndrawn();
}
else
{
bool zoomingAnimation = HasDrawingData(tkDrawingDataAvailable::ZoomingAnimation);
UpdateTileBuffer(dc, zoomingAnimation);
g->DrawImage(_tilesBitmap, 0.0f, 0.0f);
}
}
}
// ***************************************************************
// DrawVolatileData()
// ***************************************************************
void CMapView::RedrawVolatileData(Gdiplus::Graphics* g, CDC* dc, const CRect& rcBounds)
{
bool hasVolatile = HasVolatileShapefiles();
bool canUseBuffer = _canUseVolatileBuffer;
_canUseVolatileBuffer = true;
if (!hasVolatile && !HasDrawLists()) return;
ClearDrawingLabelFrames();
if (_isSnapshot)
{
if (hasVolatile)
this->DrawLayers(rcBounds, g, false);
this->DrawLists(rcBounds, g, dlSpatiallyReferencedList);
this->DrawLists(rcBounds, g, dlScreenReferencedList);
return;
}
if (canUseBuffer)
{
g->DrawImage(_volatileBitmap, 0.0f, 0.0f);
}
else {
// drawing layers
Gdiplus::Graphics* gDrawing = Gdiplus::Graphics::FromImage(_volatileBitmap);
gDrawing->Clear(Gdiplus::Color::Transparent);
// rendering of volatile shapefiles
if (HasVolatileShapefiles())
this->DrawLayers(rcBounds, gDrawing, false);
// fire external drawing
if (_customDrawingFlags & BeforeAfterDrawing)
{
HDC hdc = g->GetHDC();
tkMwBoolean retVal = blnFalse;
this->FireBeforeDrawing((long)hdc, rcBounds.left, rcBounds.right, rcBounds.top, rcBounds.bottom, &retVal);
g->ReleaseHDC(hdc);
}
// temp objects
this->DrawLists(rcBounds, gDrawing, dlSpatiallyReferencedList);
this->DrawLists(rcBounds, gDrawing, dlScreenReferencedList);
// passing layers to the main buffer
g->DrawImage(_volatileBitmap, 0.0f, 0.0f);
delete gDrawing;
// fire external drawing code
if (_customDrawingFlags & BeforeAfterDrawing)
{
HDC hdc = g->GetHDC();
tkMwBoolean retVal = blnFalse;
this->FireAfterDrawing((long)hdc, rcBounds.left, rcBounds.right, rcBounds.top, rcBounds.bottom, &retVal);
g->ReleaseHDC(hdc);
}
}
}
// ***************************************************************
// RedrawTools()
// ***************************************************************
void CMapView::RedrawTools(Gdiplus::Graphics* g, const CRect& rcBounds)
{
DrawShapeEditor(g, false);
if (!_isSnapshot)
{
DrawScaleBar(g);
DrawZoombar(g);
}
// distance measuring or persistent measuring
if (HasDrawingData(tkDrawingDataAvailable::MeasuringData))
{
GetMeasuringBase()->DrawData(g, false, DragNone);
}
DrawIdentified(g, rcBounds);
}
// ***************************************************************
// DrawIdentified()
// ***************************************************************
void CMapView::DrawIdentified(Gdiplus::Graphics* g, const CRect& rcBounds)
{
bool hotTracking = HasDrawingData(tkDrawingDataAvailable::HotTracking);
bool identified = HasDrawingData(tkDrawingDataAvailable::IdentifiedShapes);
if (!hotTracking && !identified)
return;
CShapefileDrawer drawer(g, &_extents, _pixelPerProjectionX, _pixelPerProjectionY, &_collisionList, GetCurrentScale(), GetCurrentZoom(), true);
if (hotTracking)
{
drawer.Draw(rcBounds, _hotTracking.Shapefile);
}
if (identified)
{
vector<long> handles;
if (SelectionListHelper::GetUniqueLayers(_identifiedShapes, handles))
{
vector<long> activeHandles;
for (size_t i = 0; i < handles.size(); i++)
{
Layer* layer = GetLayer(handles[i]);
if (layer && layer->wasRendered)
{
activeHandles.push_back(handles[i]);
}
}
if (activeHandles.size() > 0)
{
RenderSelectedPixels(activeHandles, drawer, rcBounds);
RenderIdentifiedShapes(activeHandles, drawer, rcBounds);
}
}
}
}
// ***************************************************************
// RenderIdentifiedShapes()
// ***************************************************************
void CMapView::RenderIdentifiedShapes(vector<long>& handles, CShapefileDrawer& drawer, const CRect& rcBounds)
{
VARIANT_BOOL vb;
for (size_t i = 0; i < handles.size(); i++)
{
Layer* layer = GetLayer(handles[i]);
if (!layer->wasRendered) continue;
if (layer->IsShapefile())
{
IShapefile* sf = GetShapefile(handles[i]);
if (!sf) continue;
// Shapefile.Create is called to change the ShapeType
SelectionListHelper::PopulateShapefile(_identifiedShapes, sf, _identifiedShapefile, handles[i]);
sf->Release();
// Shapefile.Create was called which cleared the style settings
ShapeStyleHelper::ApplyIdentifiedShapesStyle(_identifier, _identifiedShapefile, true);
drawer.Draw(rcBounds, _identifiedShapefile);
_identifiedShapefile->EditClear(&vb);
}
}
}
// ***************************************************************
// UpdateSelectedPixels()
// ***************************************************************
void CMapView::UpdateSelectedPixels(vector<long>& handles, bool& hasPolygons, bool& hasPoints)
{
hasPolygons = false;
hasPoints = false;
for (size_t i = 0; i < handles.size(); i++)
{
Layer* layer = GetLayer(handles[i]);
if (!layer->wasRendered) continue;
if (layer->IsImage())
{
CComPtr<IImage> img = NULL;
img.Attach(GetImage(handles[i]));
if (!img) continue;
// do we need polygon or point shapefile at this scale?
double dx, dy;
img->get_dX(&dx);
img->get_dY(&dy);
double size = (_pixelPerProjectionX * dx + _pixelPerProjectionY * dy) / 2.0;
bool polygon = size > 10;
if (polygon) {
hasPolygons = true;
}
if (!polygon) {
hasPoints = true;
}
// calculating bounds of pixels
((CSelectionList*)_identifiedShapes)->UpdatePixelBounds(handles[i], img, polygon);
}
}
}
// ***************************************************************
// RenderSelectedPixels()
// ***************************************************************
void CMapView::RenderSelectedPixels(vector<long>& handles, CShapefileDrawer& drawer, const CRect& rcBounds)
{
if (!SelectionListHelper::HasLayers(_identifiedShapes, ltRaster)) {
return;
}
bool hasPolygons, hasPoints;
UpdateSelectedPixels(handles, hasPolygons, hasPoints);
std::set<long> handlesSet(handles.begin(), handles.end());
if (hasPolygons)
{
SelectionListHelper::AddSelectedPixelsToShapefile(_identifiedShapes, _identifiedShapefile, handlesSet, true);
ShapeStyleHelper::ApplyIdentifiedShapesStyle(_identifier, _identifiedShapefile, true);
drawer.Draw(rcBounds, _identifiedShapefile);
}
if (hasPoints)
{
SelectionListHelper::AddSelectedPixelsToShapefile(_identifiedShapes, _identifiedShapefile, handlesSet, false);
ShapeStyleHelper::ApplyIdentifiedShapesStyle(_identifier, _identifiedShapefile, false);
_identifiedShapefile->put_CollisionMode(tkCollisionMode::AllowCollisions);
drawer.Draw(rcBounds, _identifiedShapefile);
}
VARIANT_BOOL vb;
_identifiedShapefile->EditClear(&vb);
}
// ***************************************************************
// UpdateTileBuffer
// ***************************************************************
void CMapView::UpdateTileBuffer( CDC* dc, bool zoomingAnimation )
{
CTiles* tiles = (CTiles*)_tiles;
Gdiplus::Graphics* gTiles = Gdiplus::Graphics::FromImage(_tilesBitmap);
int tileProvider = GetTileProvider();
bool initialization = !_tileBuffer.Initialized;
if (initialization)
{
_tileBuffer.Initialized = true;
// it's the first tile for current extents, we need to initialize the buffer
bool canReuseBuffer = /*ForceDiscreteZoom() &&*/
GetTileProvider() == _tileBuffer.Provider &&
_currentZoom != _tileBuffer.Zoom && abs(_currentZoom - _tileBuffer.Zoom) <= 4; // for larger difference it's not practical
bool wasReused = false;
if (canReuseBuffer && (_zoomAnimation || _reuseTileBuffer))
{
// reuse existing buffer
Gdiplus::Graphics* gTemp = Gdiplus::Graphics::FromImage(_tempBitmap);
Extent match;
if (_extents.getIntersection(_tileBuffer.Extents, match))
{
Gdiplus::RectF source, target;
DrawZoomingAnimation(match, gTemp, dc, source, target, zoomingAnimation);
if (_reuseTileBuffer)
{
Gdiplus::ImageAttributes attr;
gTemp->Clear(Gdiplus::Color::Transparent);
gTemp->Flush();
gTemp->DrawImage(_tilesBitmap, target, source.X, source.Y, source.Width, source.Height, Gdiplus::Unit::UnitPixel, &attr);
gTemp->Flush();
gTiles->Clear(Gdiplus::Color::Transparent);
gTiles->DrawImage(_tempBitmap, 0, 0 );
gTiles->Flush();
wasReused = true;
}
}
// clean up
delete gTemp;
}
if (!wasReused) {
gTiles->Clear(Gdiplus::Color::Transparent);
}
// save extents to reuse the buffer
_tileBuffer.Extents = _extents;
_tileBuffer.Zoom = _currentZoom;
_tileBuffer.Provider = tileProvider;
}
if (initialization) {
get_TileManager()->MarkUndrawn();
}
// drawing new tiles
if (initialization || get_TileManager()->UndrawnTilesExist()) {
DrawTiles(gTiles);
}
// clean up
delete gTiles;
}
// ***************************************************************
// DrawZoomingAnimation
// ***************************************************************
void CMapView::DrawZoomingAnimation( Extent match, Gdiplus::Graphics* gTemp, CDC* dc, Gdiplus::RectF& source, Gdiplus::RectF& target, bool zoomingAnimation )
{
// target rectangle (current screen buffer)
double tx = (match.left - _extents.left)/_extents.Width();
double tx2 = 1.0f - (_extents.right - match.right)/_extents.Width();
double ty = (_extents.top - match.top)/_extents.Height();
double ty2 = 1.0f - (match.bottom - _extents.bottom)/_extents.Height();
target.X = (float)(tx * _viewWidth);
target.Y = (float)(ty * _viewHeight);
target.Width = (float)((tx2 - tx) * _viewWidth);
target.Height = (float)((ty2 - ty) * _viewHeight);
// source rectangle (cached tile buffer)
Extent buffer = _tileBuffer.Extents;
double sx = (match.left - buffer.left)/buffer.Width();
double sx2 = 1.0f - (buffer.right - match.right)/buffer.Width();
double sy = (buffer.top - match.top)/buffer.Height();
double sy2 = 1.0f - (match.bottom - buffer.bottom)/buffer.Height();
source.X = (float)(sx * _tilesBitmap->GetWidth());
source.Y = (float)(sy * _tilesBitmap->GetHeight());
source.Width = (float)((sx2 - sx) * _tilesBitmap->GetWidth());
source.Height = (float)((sy2 - sy) * _tilesBitmap->GetHeight());
double x, x2, y, y2;
// -----------------------------------------------
// zooming animation
// -----------------------------------------------
if (zoomingAnimation)
{
HDC hdc = dc->GetSafeHdc();
Gdiplus::Graphics* g = Gdiplus::Graphics::FromHDC(hdc);
g->SetCompositingMode(Gdiplus::CompositingModeSourceCopy);
Gdiplus::Color backColor = Utility::OleColor2GdiPlus(m_backColor);
Gdiplus::ImageAttributes attr;
gTemp->Clear(backColor);
double steps = ((_viewHeight * _viewWidth > 1e6) ? 4 : 5) * abs(_currentZoom - _tileBuffer.Zoom);
DWORD lastTime, time;
DWORD redrawDelay = 40;
bool zoomingIn = _currentZoom > _tileBuffer.Zoom;
if (zoomingIn)
{
// portion of buffer is gradually scaled to the whole screen
for(int i = 1; i <= steps; i++)
{
x = i / steps * sx ;
x2 = sx2 + (1 - sx2) * (1 - i/steps);
y = i / steps * sy;
y2 = sy2 + (1 - sy2) * (1 - i/steps);
x *= _tilesBitmap->GetWidth();
x2 *= _tilesBitmap->GetWidth();
y *= _tilesBitmap->GetHeight();
y2 *= _tilesBitmap->GetHeight();
lastTime = GetTickCount();
// render to temp bitmap first as we need background color
gTemp->DrawImage(_tilesBitmap, target, (float)x, (float)y, (float)(x2 - x), (float)(y2 - y), Gdiplus::Unit::UnitPixel, &attr);
g->DrawImage(_tempBitmap, 0, 0 );
time = GetTickCount();
if (time - lastTime < redrawDelay )
Sleep(redrawDelay - time + lastTime);
}
}
else
{
// the whole buffer is gradually scale to the portion of the screen
Gdiplus::Color clr = Gdiplus::Color(backColor);
Gdiplus::SolidBrush brush(clr);
Gdiplus::RectF _lastTargetRect(target.X, target.Y, target.Width, target.Height);
Gdiplus::RectF tRect;
for(int i = 1; i <= steps ; i++)
{
x = i / steps * tx;
x2 = tx2 + (1 - tx2) * (1 - i/steps);
y = i / steps * ty;
y2 = ty2 + (1 - ty2) * (1 - i/steps);
tRect.X = (float)(x * _viewWidth);
tRect.Width = (float)(x2 * _viewWidth - tRect.X);
tRect.Y = (float)(y * _viewHeight);
tRect.Height =(float)(y2 * _viewHeight - tRect.Y);
lastTime = GetTickCount();
gTemp->FillRectangle(&brush, _lastTargetRect);
gTemp->DrawImage(_tilesBitmap, tRect, source.X, source.Y, source.Width, source.Height, Gdiplus::Unit::UnitPixel, &attr);
g->DrawImage(_tempBitmap, 0, 0 );
time = GetTickCount();
if (time - lastTime < redrawDelay )
Sleep(redrawDelay - time + lastTime);
_lastTargetRect.X = tRect.X;
_lastTargetRect.Y = tRect.Y;
_lastTargetRect.Width = tRect.Width;
_lastTargetRect.Height = tRect.Height;
}
}
g->ReleaseHDC(dc->GetSafeHdc());
delete g;
}
}
// ***************************************************************
// DrawTiles()
// ***************************************************************
void CMapView::DrawTiles(Gdiplus::Graphics* g)
{
int minZoom;
if ( GetTileMismatchMinZoom(minZoom) )
{
if (_currentZoom < minZoom) return;
}
TilesDrawer drawer(g, &_extents, _pixelPerProjectionX, _pixelPerProjectionY, PixelsPerMapUnit(), GetWgs84ToMapTransform());
drawer.DrawTiles(get_TileManager(), GetMapProjection(), _isSnapshot, _projectionChangeCount);
}
#pragma region Draw layers
// ****************************************************************
// DrawLayers()
// ****************************************************************
void CMapView::DrawLayers(const CRect & rcBounds, Gdiplus::Graphics* graphics, bool layerBuffer)
{
if (_lockCount > 0 && !_isSnapshot)
return;
// clear extents of drawn labels and charts
this->ClearLabelFrames();
register int i;
long startcondition = 0;
long endcondition = _activeLayers.size();
// nothing to draw
if (endcondition == 0)
return;
HCURSOR oldCursor;
if (layerBuffer) {
oldCursor = this->SetWaitCursor();
}
// ------------------------------------------------------------------
// Check whether some layers are completely concealed by images
// no need to draw them then
// ------------------------------------------------------------------
bool * isConcealed = new bool[endcondition];
memset(isConcealed,0,endcondition*sizeof(bool));
double scale = GetCurrentScale();
int zoom;
_tiles->get_CurrentZoom(&zoom);
if (layerBuffer)
CheckForConcealedImages(isConcealed, startcondition, endcondition, scale, zoom);
// drawing grouped images
if (layerBuffer)
DrawImageGroups();
// prepare for drawing
double currentScale = this->GetCurrentScale();
// collision avoidance
_collisionList.Clear();
CCollisionList collisionListLabels;
CCollisionList collisionListCharts;
CCollisionList* chosenListLabels = m_globalSettings.commonCollisionListForLabels ? (&_collisionList) : (&collisionListLabels);;
CCollisionList* chosenListCharts = m_globalSettings.commonCollisionListForCharts ? (&_collisionList) : (&collisionListCharts);;
// initializing classes for drawing
bool forceGdiplus = this->_rotateAngle != 0.0f || _isSnapshot;
CShapefileDrawer sfDrawer(graphics, &_extents, _pixelPerProjectionX, _pixelPerProjectionY, &_collisionList, this->GetCurrentScale(), this->GetCurrentZoom(), forceGdiplus);
CImageDrawer imgDrawer(graphics, &_extents, _pixelPerProjectionX, _pixelPerProjectionY, _viewWidth, _viewHeight);
CLabelDrawer lblDrawer(graphics, &_extents, _pixelPerProjectionX, _pixelPerProjectionY, currentScale, _currentZoom, chosenListLabels, _rotateAngle, _isSnapshot);
CChartDrawer chartDrawer(graphics, &_extents, _pixelPerProjectionX, _pixelPerProjectionY, currentScale, chosenListCharts, _isSnapshot);
// mark all shapes as not drawn
for (int i = startcondition; i < endcondition; i++)
{
Layer * l = _allLayers[_activeLayers[i]];
if (l->IsShapefile() && l->wasRendered) // if it's hidden don't clear every time
{
CComPtr<IShapefile> sf = NULL;
// don't mark as 'undrawn' if we're not going to redraw it
if (l->QueryShapefile(&sf) && ShapefileHelper::IsVolatile(sf) != layerBuffer)
{
ShapefileHelper::Cast(sf)->MarkUndrawn();
}
}
}
// run drawing
int shapeCount = 0;
for(int i = startcondition; i < endcondition; i++)
{
long layerHandle = _activeLayers[i];
Layer * l = _allLayers[layerHandle];
if (!l || !l->get_Object()) continue;
bool visible = l->IsVisible(scale, zoom) && !isConcealed[i];
l->wasRendered = visible;
if (visible)
{
if (l->IsImage())
{
if (!layerBuffer) continue;
DrawImageLayer(rcBounds, l, graphics, imgDrawer);
LayerDrawer::DrawLabels(l, lblDrawer, vpAboveParentLayer);
}
else if (l->IsShapefile() || l->IsDynamicOgrLayer())
{
CComPtr<IShapefile> sf = NULL;
if (l->IsDynamicOgrLayer())
{
// Try to get the data loaded so far & update labels & categories
l->UpdateShapefile();
// Get the shapefile
l->QueryShapefile(&sf);
}
else
{
// grab extents from shapefile in case they changed
l->UpdateExtentsFromDatasource();
if (!l->extents.Intersects(_extents))
continue;
// Update labels & categories
l->UpdateShapefile();
}
// layerBuffer == true indicates we're drawing the non-Volatile layers
if (l->QueryShapefile(&sf) && ShapefileHelper::IsVolatile(sf) == layerBuffer)
continue;
// Perform the draw:
sfDrawer.Draw(rcBounds, sf);
LayerDrawer::DrawLabels(l, lblDrawer, vpAboveParentLayer);
LayerDrawer::DrawCharts(l, chartDrawer, vpAboveParentLayer);
}
}
}
shapeCount = sfDrawer.GetShapeCount();
if (layerBuffer)
_shapeCountInView = shapeCount;
if (!layerBuffer && shapeCount > _shapeCountInView)
_shapeCountInView = shapeCount;
// drawing labels and charts above the layers
for (i = 0; i < (int)_activeLayers.size(); i++)
{
Layer * l = _allLayers[_activeLayers[i]];
if( !l || !l->get_Object()) continue;
if (!l->IsVisible(scale, zoom)) continue;
CComPtr<IShapefile> sf = NULL;
if (l->QueryShapefile(&sf) && ShapefileHelper::IsVolatile(sf) == layerBuffer)
continue;
LayerDrawer::DrawLabels(l, lblDrawer, vpAboveAllLayers);
LayerDrawer::DrawCharts(l, chartDrawer, vpAboveAllLayers);
}
if (layerBuffer && oldCursor != NULL) {
::SetCursor(oldCursor);
}
if (isConcealed)
delete[] isConcealed;
}
// ****************************************************************
// DrawImageLayer()
// ****************************************************************
void CMapView::DrawImageLayer(const CRect& rcBounds, Layer* l, Gdiplus::Graphics* graphics, CImageDrawer& imgDrawer)
{
if (!l->IsImage()) return;
IImage * iimg = NULL;
if (!l->QueryImage(&iimg)) return;
CImageClass* img = (CImageClass*)iimg;
if (_canUseImageGrouping && img->m_groupID != -1)
{
// this is grouped image, if this is the first image of group, we shall draw the whole group
if (!(*_imageGroups)[img->m_groupID]->wasDrawn)
{
this->DrawImageGroups(rcBounds, graphics, img->m_groupID);
(*_imageGroups)[img->m_groupID]->wasDrawn = true;