/
MapGestureBase.cs
85 lines (72 loc) · 2.3 KB
/
MapGestureBase.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
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using JetBrains.Annotations;
using Microsoft.Phone.Maps.Controls;
namespace JaguarRoutePlanner.Sources.Utils.Gestures
{
/// <summary>
/// A base class for map gestures, which allows them to suppress the built-in map gestures.
/// source https://github.com/ColinEberhardt/WP8-MapGestures
/// </summary>
public abstract class MapGestureBase
{
private Map _map;
protected MapGestureBase()
{
Touch.FrameReported += OnFrameReported;
}
/// <summary>
/// Gets or sets whether to suppress the existing gestures/
/// </summary>
public bool SuppressMapGestures { get; set; }
public Map Map
{
get { return _map; }
set
{
UnregisterMapCrawler();
_map = value;
if (_map != null)
_map.Loaded += OnMapLoaded;
}
}
/// <summary>
/// unregister crawler on previous map
/// </summary>
private void UnregisterMapCrawler()
{
if (_map != null)
_map.Loaded -= OnMapLoaded;
}
private void OnMapLoaded(object sender, RoutedEventArgs e)
{
CrawlTree(Map);
}
~MapGestureBase()
{
UnregisterMapCrawler();
Touch.FrameReported -= OnFrameReported;
}
private void CrawlTree([NotNull] UIElement el)
{
el.ManipulationDelta += MapElement_ManipulationDelta;
for (var c = 0; c < VisualTreeHelper.GetChildrenCount(el); c++)
{
CrawlTree((FrameworkElement) VisualTreeHelper.GetChild(el, c));
}
}
private void MapElement_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
if (SuppressMapGestures)
e.Handled = true;
}
private void OnFrameReported(object sender, TouchFrameEventArgs e)
{
var touchPoints = e.GetTouchPoints(null);
if (touchPoints != null)
OnTouch(touchPoints);
}
protected abstract void OnTouch([NotNull] TouchPointCollection touchPoints);
}
}