summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/VisualElementTracker.cs
blob: 8211ec63b49129b623bfda2d10e8bdc16eebb90a (plain)
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
´╗┐using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace Xamarin.Forms.Platform.WinPhone
{
	public abstract class VisualElementTracker : IDisposable
	{
		public abstract FrameworkElement Child { get; set; }

		public abstract void Dispose();

		public event EventHandler Updated;

		protected void OnUpdated()
		{
			if (Updated != null)
				Updated(this, EventArgs.Empty);
		}
	}

	public class VisualElementTracker<TModel, TElement> : VisualElementTracker where TModel : VisualElement where TElement : FrameworkElement
	{
		FrameworkElement _child;
		bool _disposed;
		TElement _element;

		bool _invalidateArrangeNeeded;
		bool _isPanning;
		bool _isPinching;

		TModel _model;
		bool _touchFrameReportedEventSet;
		int _touchPoints = 1;

		public override FrameworkElement Child
		{
			get { return _child; }
			set
			{
				if (_child == value)
					return;
				_child = value;
				UpdateNativeControl();
			}
		}

		public TElement Element
		{
			get { return _element; }
			set
			{
				if (_element == value)
					return;

				if (_element != null)
				{
					_element.Tap -= ElementOnTap;
					_element.DoubleTap -= ElementOnDoubleTap;
					_element.ManipulationDelta -= OnManipulationDelta;
					_element.ManipulationCompleted -= OnManipulationCompleted;
				}

				_element = value;

				if (_element != null)
				{
					_element.Tap += ElementOnTap;
					_element.DoubleTap += ElementOnDoubleTap;
					_element.ManipulationDelta += OnManipulationDelta;
					_element.ManipulationCompleted += OnManipulationCompleted;
				}

				UpdateNativeControl();
			}
		}

		public TModel Model
		{
			get { return _model; }
			set
			{
				if (_model == value)
					return;

				if (_model != null)
				{
					_model.BatchCommitted -= HandleRedrawNeeded;
					_model.PropertyChanged -= HandlePropertyChanged;
				}

				_model = value;

				if (_model != null)
				{
					_model.BatchCommitted += HandleRedrawNeeded;
					_model.PropertyChanged += HandlePropertyChanged;
				}

				UpdateNativeControl();
			}
		}

		public override void Dispose()
		{
			if (_disposed)
				return;
			_disposed = true;

			if (_element != null)
			{
				_element.Tap -= ElementOnTap;
				_element.DoubleTap -= ElementOnDoubleTap;
				_element.ManipulationDelta -= OnManipulationDelta;
				_element.ManipulationCompleted -= OnManipulationCompleted;
			}

			if (_model != null)
			{
				_model.BatchCommitted -= HandleRedrawNeeded;
				_model.PropertyChanged -= HandlePropertyChanged;
			}

			Child = null;
			Model = null;
			Element = null;
		}

		protected virtual void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (Model.Batched)
			{
				if (e.PropertyName == VisualElement.XProperty.PropertyName || e.PropertyName == VisualElement.YProperty.PropertyName || e.PropertyName == VisualElement.WidthProperty.PropertyName ||
					e.PropertyName == VisualElement.HeightProperty.PropertyName)
					_invalidateArrangeNeeded = true;
				return;
			}

			if (e.PropertyName == VisualElement.XProperty.PropertyName || e.PropertyName == VisualElement.YProperty.PropertyName || e.PropertyName == VisualElement.WidthProperty.PropertyName ||
				e.PropertyName == VisualElement.HeightProperty.PropertyName)
				MaybeInvalidate();
			else if (e.PropertyName == VisualElement.AnchorXProperty.PropertyName || e.PropertyName == VisualElement.AnchorYProperty.PropertyName)
				UpdateScaleAndRotation(Model, Element);
			else if (e.PropertyName == VisualElement.ScaleProperty.PropertyName)
				UpdateScaleAndRotation(Model, Element);
			else if (e.PropertyName == VisualElement.TranslationXProperty.PropertyName || e.PropertyName == VisualElement.TranslationYProperty.PropertyName ||
					 e.PropertyName == VisualElement.RotationProperty.PropertyName || e.PropertyName == VisualElement.RotationXProperty.PropertyName || e.PropertyName == VisualElement.RotationYProperty.PropertyName)
				UpdateRotation(Model, Element);
			else if (e.PropertyName == VisualElement.IsVisibleProperty.PropertyName)
				UpdateVisibility(Model, Element);
			else if (e.PropertyName == VisualElement.OpacityProperty.PropertyName)
				UpdateOpacity(Model, Element);
			else if (e.PropertyName == VisualElement.InputTransparentProperty.PropertyName)
				UpdateInputTransparent(Model, Element);
		}

		protected virtual void UpdateNativeControl()
		{
			if (Model == null || Element == null)
				return;

			UpdateOpacity(_model, _element);
			UpdateScaleAndRotation(_model, _element);
			UpdateInputTransparent(_model, _element);

			if (_invalidateArrangeNeeded)
				MaybeInvalidate();
			_invalidateArrangeNeeded = false;

			UpdateTouchFrameReportedEvent(_model);

			OnUpdated();
		}

		void ElementOnDoubleTap(object sender, GestureEventArgs gestureEventArgs)
		{
			var view = Model as View;
			if (view == null)
				return;

			foreach (TapGestureRecognizer gestureRecognizer in
				view.GestureRecognizers.OfType<TapGestureRecognizer>().Where(g => g.NumberOfTapsRequired == 2))
			{
				gestureRecognizer.SendTapped(view);
				gestureEventArgs.Handled = true;
			}
		}

		void ElementOnTap(object sender, GestureEventArgs gestureEventArgs)
		{
			var view = Model as View;
			if (view == null)
				return;

			foreach (TapGestureRecognizer gestureRecognizer in
				view.GestureRecognizers.OfType<TapGestureRecognizer>().Where(g => g.NumberOfTapsRequired == 1))
			{
				gestureRecognizer.SendTapped(view);
				gestureEventArgs.Handled = true;
			}
		}

		void HandlePan(ManipulationDeltaEventArgs e, View view)
		{
			foreach (PanGestureRecognizer recognizer in
				view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _touchPoints))
			{
				if (!_isPanning)
					((IPanGestureController)recognizer).SendPanStarted(view, Application.Current.PanGestureId);

				double totalX = 0;
				double totalY = 0;

				// Translation and CumulativeManipulation will be 0 if we have more than one touch point because it thinks we're pinching,
				// so we'll just go ahead and use the center point of the pinch gesture to figure out how much we're panning.
				if (_touchPoints > 1 && e.PinchManipulation != null)
				{
					totalX = e.PinchManipulation.Current.Center.X - e.PinchManipulation.Original.Center.X;
					totalY = e.PinchManipulation.Current.Center.Y - e.PinchManipulation.Original.Center.Y;
				}
				else
				{
					totalX = e.DeltaManipulation.Translation.X + e.CumulativeManipulation.Translation.X;
					totalY = e.DeltaManipulation.Translation.Y + e.CumulativeManipulation.Translation.Y;
				}

				((IPanGestureController)recognizer).SendPan(view, totalX, totalY, Application.Current.PanGestureId);
				_isPanning = true;
			}
		}

		void HandlePinch(ManipulationDeltaEventArgs e, View view)
		{
			if (e.PinchManipulation == null)
				return;

			IEnumerable<PinchGestureRecognizer> pinchGestures = view.GestureRecognizers.GetGesturesFor<PinchGestureRecognizer>();
			System.Windows.Point translationPoint = e.ManipulationContainer.TransformToVisual(Element).Transform(e.PinchManipulation.Current.Center);
			var scaleOriginPoint = new Point(translationPoint.X / view.Width, translationPoint.Y / view.Height);
			foreach (var recognizer in pinchGestures)
			{
				if (!_isPinching)
					((IPinchGestureController)recognizer).SendPinchStarted(view, scaleOriginPoint);
				((IPinchGestureController)recognizer).SendPinch(view, e.PinchManipulation.DeltaScale, scaleOriginPoint);
			}
			_isPinching = true;
		}

		void HandleRedrawNeeded(object sender, EventArgs e)
		{
			UpdateNativeControl();
		}

		void MaybeInvalidate()
		{
			if (Model.IsInNativeLayout)
				return;
			var parent = (FrameworkElement)Element.Parent;
			parent?.InvalidateMeasure();
			Element.InvalidateMeasure();
		}

		void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			var view = Model as View;
			if (view == null)
				return;

			IEnumerable pinchGestures = view.GestureRecognizers.GetGesturesFor<PinchGestureRecognizer>();
			foreach (var recognizer in pinchGestures)
				((IPinchGestureController)recognizer).SendPinchEnded(view);
			_isPinching = false;

			IEnumerable<PanGestureRecognizer> panGestures = view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _touchPoints);
			foreach (PanGestureRecognizer recognizer in panGestures)
				((IPanGestureController)recognizer).SendPanCompleted(view, Application.Current.PanGestureId);
			Application.Current.PanGestureId++;
			_isPanning = false;
		}

		void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
		{
			var view = Model as View;
			if (view == null)
				return;

			HandlePinch(e, view);

			HandlePan(e, view);
		}

		void Touch_FrameReported(object sender, TouchFrameEventArgs e)
		{
			_touchPoints = e.GetTouchPoints(Child).Count;
		}

		static void UpdateInputTransparent(VisualElement view, FrameworkElement frameworkElement)
		{
			frameworkElement.IsHitTestVisible = !view.InputTransparent;
		}

		static void UpdateOpacity(VisualElement view, FrameworkElement frameworkElement)
		{
			frameworkElement.Opacity = view.Opacity;
		}

		static void UpdateRotation(VisualElement view, FrameworkElement frameworkElement)
		{
			double anchorX = view.AnchorX;
			double anchorY = view.AnchorY;
			double rotationX = view.RotationX;
			double rotationY = view.RotationY;
			double rotation = view.Rotation;
			double translationX = view.TranslationX;
			double translationY = view.TranslationY;
			double scale = view.Scale;

			frameworkElement.Projection = new PlaneProjection
			{
				CenterOfRotationX = anchorX,
				CenterOfRotationY = anchorY,
				GlobalOffsetX = translationX / scale,
				GlobalOffsetY = translationY / scale,
				RotationX = -rotationX,
				RotationY = -rotationY,
				RotationZ = -rotation
			};
		}

		static void UpdateScaleAndRotation(VisualElement view, FrameworkElement frameworkElement)
		{
			double anchorX = view.AnchorX;
			double anchorY = view.AnchorY;
			double scale = view.Scale;
			frameworkElement.RenderTransformOrigin = new System.Windows.Point(anchorX, anchorY);
			frameworkElement.RenderTransform = new ScaleTransform { ScaleX = scale, ScaleY = scale };

			UpdateRotation(view, frameworkElement);
		}

		void UpdateTouchFrameReportedEvent(VisualElement model)
		{
			if (_touchFrameReportedEventSet)
				return;

			Touch.FrameReported -= Touch_FrameReported;
			_touchFrameReportedEventSet = false;

			var view = model as View;
			if (view == null)
				return;

			if (!view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Any(g => g.TouchPoints > 1))
				return;

			Touch.FrameReported += Touch_FrameReported;
			_touchFrameReportedEventSet = true;
		}

		static void UpdateVisibility(VisualElement view, FrameworkElement frameworkElement)
		{
			frameworkElement.Visibility = view.IsVisible ? Visibility.Visible : Visibility.Collapsed;
		}
	}
}