-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPanAndZoomCustom.cs
447 lines (372 loc) · 18.6 KB
/
PanAndZoomCustom.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Ptv.XServer.Controls.Map.Tools;
using Ptv.XServer.Controls.Map.Gadgets;
using Ptv.XServer.Controls.Map;
namespace VdiPerformance
{
/// <summary><para> User control for the <see cref="MapView"/>-object, translating user interactions into modification
/// of the visible map section. </para>
/// <para> See the <conceptualLink target="eb8e522c-5ed2-4481-820f-bfd74ee2aeb8"/> topic for an example. </para></summary>
public class PanAndZoom : MapGadget
{
#region private variables
/// <summary> Start point of the interaction in world coordinates. </summary>
private Point WorldStartPoint = new Point(0, 0);
/// <summary> Start point of the interaction in screen coordinates (pixel). </summary>
private Point ScreenStartPoint = new Point(0, 0);
/// <summary> Map on which the interaction is to be executed. </summary>
private MapView mapView;
/// <summary> Interaction mode defining whether a panning or a zoom selection is to be executed. </summary>
private DragMode dragMode;
/// <summary> Rectangle used for zoom selection. The map will be zoomed to this section. </summary>
private Rectangle dragRectangle;
/// <summary> Flag showing if the map has lately been panned. </summary>
private bool wasPanned;
#if PINCHZOOM
/// <summary>Stores the map zoom level at the beginning of a ManipulationDelta event.</summary>
private double manipulationDeltaInitialZoom;
#endif
#endregion
#region public variables
/// <summary> Gets or sets a value indicating whether a zooming or panning action is in progress. </summary>
/// <value> Flag indicating whether a zooming or panning action is active. </value>
public bool IsActive { get; set; }
#endregion
#region private methods
/// <summary> Initializes the handlers for key and mouse events. </summary>
private void Setup()
{
mapView.Focusable = true;
#if PINCHZOOM
mapView.IsManipulationEnabled = true;
#endif
mapView.KeyDown += map_KeyDown;
mapView.MouseMove += control_MouseMove;
mapView.MouseDown += source_MouseDown;
mapView.MouseUp += source_MouseUp;
mapView.MouseWheel += source_MouseWheel;
#if PINCHZOOM
mapView.StylusDown += mapView_StylusDown;
mapView.StylusButtonUp += mapView_StylusButtonUp;
mapView.ManipulationStarting += mapView_ManipulationStarting;
mapView.ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs>(map_ManipulationDelta);
#endif
}
#if PINCHZOOM
/// <summary>Gets the current position of the map in screen coordinates.</summary>
private Point CurrentScreenPosition => mapView.PtvMercatorToCanvas(this, new Point(mapView.CurrentX, mapView.CurrentY));
/// <summary>
/// Helper; moves the map to a new position while keeping the map zoom.
/// </summary>
/// <param name="moveTo">Position to move the map top.</param>
/// <param name="screen">Set to false if moveTo is given in world coordinates.</param>
private void MoveMap(Point moveTo, bool screenCoordinates = true)
{
if (screenCoordinates)
moveTo = mapView.CanvasToPtvMercator(this, moveTo);
mapView.SetXYZ(moveTo.X, moveTo.Y, mapView.CurrentZoom, false);
}
#endif
#endregion
// *VdiPerformance* use a simple, non-filed shape for pan/select
public bool UseSimpleSelectShape { get; set; }
// *MoveWhileDragging* move the map while dragging the mouse
public bool MoveWhileDragging { get; set; }
#region event handling
/// <summary> Event handler for pressing a key. Scrolls or zooms the map depending on the pressed key. </summary>
/// <param name="sender"> Sender of the KeyDown event. </param>
/// <param name="e"> Event parameters. </param>
private void map_KeyDown(object sender, KeyEventArgs e)
{
if (!mapView.IsFocused)
return;
const double panOffset = .25;
var mapRectangle = mapView.FinalEnvelope;
double dX = mapRectangle.Width * panOffset;
double dY = mapRectangle.Height * panOffset;
switch (e.Key)
{
case Key.Up:
case Key.W:
case Key.NumPad8:
mapView.SetXYZ(mapView.FinalX, mapView.FinalY + dY, mapView.FinalZoom, Map.UseAnimation);
e.Handled = true;
break;
case Key.Right:
case Key.D:
case Key.NumPad6:
mapView.SetXYZ(mapView.FinalX + dX, mapView.FinalY, mapView.FinalZoom, Map.UseAnimation);
e.Handled = true;
break;
case Key.Down:
case Key.S:
case Key.NumPad2:
mapView.SetXYZ(mapView.FinalX, mapView.FinalY - dY, mapView.FinalZoom, Map.UseAnimation);
e.Handled = true;
break;
case Key.Left:
case Key.A:
case Key.NumPad4:
mapView.SetXYZ(mapView.FinalX - dX, mapView.FinalY, mapView.FinalZoom, Map.UseAnimation);
e.Handled = true;
break;
case Key.Add:
case Key.OemPlus:
if (mapView.FinalZoom < mapView.MaxZoom)
mapView.SetZoom(mapView.FinalZoom + 1, Map.UseAnimation);
e.Handled = true;
break;
case Key.OemMinus:
case Key.Subtract:
if (mapView.FinalZoom > mapView.MinZoom)
mapView.SetZoom(mapView.FinalZoom - 1, Map.UseAnimation);
e.Handled = true;
break;
}
}
/// <summary> Event handler for scrolling the mouse wheel. Zooms in or out the map depending on the scroll
/// direction of the mouse wheel. </summary>
/// <param name="sender"> Sender of the MouseWheel event. </param>
/// <param name="e"> Event parameters. </param>
private void source_MouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
double oldZoom = mapView.FinalZoom;
double delta = e.Delta * Map.MouseWheelSpeed / 120;
if (Map.InvertMouseWheel)
delta = -delta;
double newZoom = oldZoom + delta;
Point p = mapView.CanvasToPtvMercator(mapView.GeoCanvas, e.GetPosition(mapView.GeoCanvas));
mapView.ZoomAround(p, newZoom, Map.UseAnimation);
}
/// <summary> Event handler for releasing the mouse button. The map is zoomed to the selected section. </summary>
/// <param name="sender"> Sender of the MouseUp event. </param>
/// <param name="e"> Event parameters. </param>
private void source_MouseUp(object sender, MouseButtonEventArgs e)
{
if (!IsActive)
return;
if (mapView.IsMouseCaptured)
{
// we're done. reset the cursor and release the mouse pointer
mapView.Cursor = Cursors.Arrow;
mapView.ReleaseMouseCapture();
}
if (dragMode == DragMode.Select)
{
mapView.ForePaneCanvas.Children.Remove(dragRectangle);
dragRectangle = null;
dragMode = DragMode.None;
var physicalPoint = e.GetPosition(mapView);
double minx = physicalPoint.X < ScreenStartPoint.X ? physicalPoint.X : ScreenStartPoint.X;
double miny = physicalPoint.Y < ScreenStartPoint.Y ? physicalPoint.Y : ScreenStartPoint.Y;
double maxx = physicalPoint.X > ScreenStartPoint.X ? physicalPoint.X : ScreenStartPoint.X;
double maxy = physicalPoint.Y > ScreenStartPoint.Y ? physicalPoint.Y : ScreenStartPoint.Y;
if (Math.Abs(maxx - minx) < 32 && Math.Abs(maxy - miny) < 32)
return;
Point p1 = mapView.TranslatePoint(new Point(minx, miny), mapView.GeoCanvas);
Point p2 = mapView.TranslatePoint(new Point(maxx, maxy), mapView.GeoCanvas);
mapView.SetEnvelope(new MapRectangle(
p1.X / MapView.ZoomAdjust * MapView.LogicalSize / MapView.ReferenceSize - 1.0 / MapView.ZoomAdjust * MapView.LogicalSize / 2 - mapView.OriginOffset.X,
p2.X / MapView.ZoomAdjust * MapView.LogicalSize / MapView.ReferenceSize - 1.0 / MapView.ZoomAdjust * MapView.LogicalSize / 2 - mapView.OriginOffset.X,
-(p2.Y / MapView.ZoomAdjust * MapView.LogicalSize / MapView.ReferenceSize) + 1.0 / MapView.ZoomAdjust * MapView.LogicalSize / 2 + mapView.OriginOffset.Y,
-(p1.Y / MapView.ZoomAdjust * MapView.LogicalSize / MapView.ReferenceSize) + 1.0 / MapView.ZoomAdjust * MapView.LogicalSize / 2 + mapView.OriginOffset.Y),
Map.UseAnimation);
e.Handled = true;
}
else if (!MoveWhileDragging)
{
mapView.ForePaneCanvas.Children.Remove(dragRectangle);
dragRectangle = null;
var physicalPoint = MapView.CanvasToPtvMercator(MapView, e.GetPosition(MapView));
if (WorldStartPoint.X == physicalPoint.X && WorldStartPoint.Y == physicalPoint.Y)
return;
double x = MapView.CurrentX + WorldStartPoint.X - physicalPoint.X;
double y = MapView.CurrentY + WorldStartPoint.Y - physicalPoint.Y;
MapView.SetXYZ(x, y, MapView.CurrentZoom, Map.UseAnimation);
}
if (!wasPanned) return;
wasPanned = false;
e.Handled = true;
}
/// <summary> Event handler for pressing the mouse button. A double click with the left mouse button results in
/// zooming in the map. A double click with the right mouse button results in zooming out the map. Pressing the
/// left button and the shift key, the zooming rectangle is shown on the map. </summary>
/// <param name="sender"> Sender of the MouseDown event. </param>
/// <param name="e"> Event parameters. </param>
private void source_MouseDown(object sender, MouseButtonEventArgs e)
{
if (!IsActive)
return;
mapView.Focus();
// NEW: Skip MouseDragMode == None
if (Map.MouseDragMode == DragMode.None)
{
e.Handled = true;
return;
}
if (Map.MouseDoubleClickZoom && e.ClickCount == 2)
{
var point = MapView.CanvasToPtvMercator(MapView.GeoCanvas, e.GetPosition(MapView.GeoCanvas));
if (e.RightButton == MouseButtonState.Pressed)
{
MapView.ZoomAround(point, MapView.FinalZoom + -1, Map.UseAnimation);
e.Handled = true;
return;
}
if (e.LeftButton == MouseButtonState.Pressed)
{
MapView.ZoomAround(point, MapView.FinalZoom + 1, Map.UseAnimation);
e.Handled = true;
return;
}
}
// Save starting point, used later when determining how much to scroll.
ScreenStartPoint = e.GetPosition(mapView);
WorldStartPoint = mapView.CanvasToPtvMercator(mapView, e.GetPosition(mapView));
bool select = e.LeftButton == MouseButtonState.Pressed && (Map.MouseDragMode == DragMode.Select ||
Map.MouseDragMode == DragMode.SelectOnShift && (Keyboard.Modifiers & ModifierKeys.Shift) > 0);
// NEW: check for MouseDragMode
if (!MoveWhileDragging || select)
{
mapView.Cursor = Cursors.Arrow;
if (dragRectangle != null)
mapView.ForePaneCanvas.Children.Remove(dragRectangle);
dragRectangle = new Rectangle
{
IsHitTestVisible = false,
Fill = UseSimpleSelectShape ? null : new SolidColorBrush(Color.FromArgb(0x3e, 0x11, 0x57, 0xdc)),
Stroke = UseSimpleSelectShape ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Color.FromArgb(0x55, 0x07, 0x81, 0xf7)),
StrokeDashArray = new DoubleCollection(new double[] { 20, 8 }),
StrokeEndLineCap = PenLineCap.Round,
StrokeDashCap = PenLineCap.Round,
StrokeThickness = UseSimpleSelectShape ? 3 : 1.5,
RadiusX = 8,
RadiusY = 8
};
Panel.SetZIndex(dragRectangle, 266);
Canvas.SetLeft(dragRectangle, ScreenStartPoint.X);
Canvas.SetTop(dragRectangle, ScreenStartPoint.Y);
mapView.ForePaneCanvas.Children.Add(dragRectangle);
dragMode = select ? DragMode.Select : DragMode.Pan;
mapView.CaptureMouse();
}
else if (e.LeftButton == MouseButtonState.Pressed && mapView.CaptureMouse())
{
mapView.Cursor = Cursors.Hand;
dragMode = DragMode.Pan;
wasPanned = false;
}
}
/// <summary> Event handler for moving the mouse. If you are currently panning, the map is moved together with
/// the mouse. If you are currently in zoom mode, the Drag rectangle is shown and changed when moving the mouse. </summary>
/// <param name="sender"> Sender of the MouseMove event. </param>
/// <param name="e"> Event parameters. </param>
private void control_MouseMove(object sender, MouseEventArgs e)
{
if (!IsActive || !mapView.IsMouseCaptured)
return;
if (dragMode == DragMode.Pan)
{
if(!MoveWhileDragging)
{
var physicalPoint = e.GetPosition(MapView);
Canvas.SetLeft(dragRectangle, physicalPoint.X - ScreenStartPoint.X);
Canvas.SetTop(dragRectangle, physicalPoint.Y - ScreenStartPoint.Y);
dragRectangle.Width = MapView.ActualWidth;
dragRectangle.Height = MapView.ActualHeight;
}
else
{
var physicalPoint = mapView.CanvasToPtvMercator(mapView, e.GetPosition(mapView));
if ((Math.Abs(WorldStartPoint.X - physicalPoint.X) < 1e-4) && (Math.Abs(WorldStartPoint.Y - physicalPoint.Y) < 1e-4))
return;
double x = mapView.CurrentX + WorldStartPoint.X - physicalPoint.X;
double y = mapView.CurrentY + WorldStartPoint.Y - physicalPoint.Y;
wasPanned = true;
mapView.SetXYZ(x, y, mapView.CurrentZoom, Map.UseAnimation);
}
}
else if (dragMode == DragMode.Select)
{
var physicalPoint = e.GetPosition(mapView);
Canvas.SetLeft(dragRectangle, physicalPoint.X < ScreenStartPoint.X ? physicalPoint.X : ScreenStartPoint.X);
Canvas.SetTop(dragRectangle, physicalPoint.Y < ScreenStartPoint.Y ? physicalPoint.Y : ScreenStartPoint.Y);
dragRectangle.Width = Math.Abs(physicalPoint.X - ScreenStartPoint.X);
dragRectangle.Height = Math.Abs(physicalPoint.Y - ScreenStartPoint.Y);
}
e.Handled = true;
}
#if PINCHZOOM
/// <summary>
/// Event handler for stylus up; needs to be handled when map was previously panned.
/// </summary>
/// <param name="sender">Sender of the StylusButtonUp event.</param>
/// <param name="e">Event parameters.</param>
void mapView_StylusButtonUp(object sender, StylusButtonEventArgs e)
{
if (wasPanned)
e.Handled = true;
}
/// <summary>
/// Event
/// </summary>
/// <param name="sender">Sender of the ManipulationStarting event.</param>
/// <param name="e">Event parameters.</param>
void mapView_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
// reset panned flag
wasPanned = false;
// store initial zoom on for follow up ManipulationDelta events.
manipulationDeltaInitialZoom = mapView.CurrentZoom;
}
/// <summary>
/// Event handler for the StylusDown event. Implements a zoom operation for a double tap.
/// </summary>
/// <param name="sender">Sender of the StylusDown event.</param>
/// <param name="e">Event parameters.</param>
void mapView_StylusDown(object sender, StylusDownEventArgs e)
{
if (e.TapCount == 2)
{
var p = MapView.CanvasToPtvMercator(MapView.GeoCanvas, e.GetPosition(MapView.GeoCanvas));
MapView.ZoomAround(p, MapView.FinalZoom + 1, Map.UseAnimation);
e.Handled = true;
}
}
/// <summary>
/// Event handler for the ManipulationDelta event; implements the pinch zoom and pan.
/// </summary>
/// <param name="sender">Sender of the ManipulationDelta event.</param>
/// <param name="e">Event parameters.</param>
void map_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
// Use the delta information for adjusting the map position (while keeping the map zoom)
MoveMap(CurrentScreenPosition - e.DeltaManipulation.Translation);
// SECOND, use the cumulative scale and set zoom at current ManipulationOrigin
var scale = Math.Max(Math.Abs(e.CumulativeManipulation.Scale.X), Math.Abs(e.CumulativeManipulation.Scale.Y));
var zoom = Math.Log(Math.Pow(2, manipulationDeltaInitialZoom) * scale, 2);
mapView.ZoomAround(mapView.CanvasToPtvMercator(this, e.ManipulationOrigin), zoom, false);
// done, handled
wasPanned = true;
e.Handled = true;
}
#endif
#endregion
#region protected methods
/// <inheritdoc/>
protected override void Initialize()
{
base.Initialize();
IsActive = true;
mapView = MapView;
Setup();
}
#endregion
}
}