private void OnCapturedTouchReported(object sender, TouchReportedEventArgs e) { UIElement parent = this.AssociatedObject.Parent as UIElement; if (parent == null) { return; } //Find the root element UIElement root = TouchHelper.GetRootElement(parent); if (root == null) { return; } // get transformation to convert positions to the parent's coordinate system GeneralTransform transform = root.TransformToVisual(parent); List <Manipulator2D> manipulators = null; foreach (TouchPoint touchPoint in e.TouchPoints) { Point position = touchPoint.Position; // convert to the parent's coordinate system position = transform.Transform(position); // create a manipulator Manipulator2D manipulator = new Manipulator2D( touchPoint.TouchDevice.Id, (float)(position.X), (float)(position.Y)); if (manipulators == null) { // lazy initialization manipulators = new List <Manipulator2D>(); } manipulators.Add(manipulator); } // process manipulations this.manipulationProcessor.ProcessManipulators( Timestamp, manipulators); }
/// <summary> /// Initialize the behavior /// </summary> protected override void OnAttached() { base.OnAttached(); this.manipulationProcessor = new ManipulationProcessor2D(SupportedManipulations); this.manipulationProcessor.Started += OnManipulationStarted; this.manipulationProcessor.Delta += OnManipulationDelta; this.manipulationProcessor.Completed += OnManipulationCompleted; this.inertiaProcessor = new InertiaProcessor2D(); this.inertiaProcessor.TranslationBehavior.DesiredDeceleration = Deceleration; this.inertiaProcessor.RotationBehavior.DesiredDeceleration = AngularDeceleration; this.inertiaProcessor.ExpansionBehavior.DesiredDeceleration = ExpansionDeceleration; this.inertiaProcessor.Delta += OnManipulationDelta; this.inertiaProcessor.Completed += OnInertiaCompleted; this.inertiaTimer = new DispatcherTimer(); this.inertiaTimer.Interval = TimeSpan.FromMilliseconds(30); this.inertiaTimer.Tick += OnTimerTick; this.AssociatedObject.RenderTransformOrigin = new Point(0.5, 0.5); Move(new Point(0, 0), 0, 100); IsPivotActive = true; this.AssociatedObject.MouseLeftButtonUp += OnMouseUp; this.AssociatedObject.MouseLeftButtonDown += OnMouseDown; this.AssociatedObject.MouseMove += OnMouseMove; this.AssociatedObject.LostMouseCapture += OnLostMouseCapture; TouchHelper.AddHandlers(this.AssociatedObject, new TouchHandlers { TouchDown = OnTouchDown, CapturedTouchReported = OnCapturedTouchReported, }); TouchHelper.EnableInput(true); this.AssociatedObject.Loaded += (s1, e1) => { TouchHelper.SetRootElement(TouchHelper.GetRootElement(this.AssociatedObject)); }; }
/// <summary> /// Occurs when Touch points are reported: handles manipulations /// </summary> private void OnCapturedTouchReported(object sender, TouchReportedEventArgs e) { var parent = AssociatedObject.Parent as UIElement; if (parent == null) { return; } //Find the root element var root = TouchHelper.GetRootElement(parent); if (root == null) { return; } //Multi-Page support: verify if the collection of Touch points is null var touchPoints = e.TouchPoints; List <Manipulator2D> manipulators = null; if (touchPoints.FirstOrDefault() != null) { // get transformation to convert positions to the parent's coordinate system var transform = root.TransformToVisual(parent); foreach (var touchPoint in touchPoints) { var position = touchPoint.Position; // convert to the parent's coordinate system position = transform.Transform(position); // create a manipulator var manipulator = new Manipulator2D( touchPoint.TouchDevice.Id, (float)(position.X), (float)(position.Y)); if (manipulators == null) { // lazy initialization manipulators = new List <Manipulator2D>(); } manipulators.Add(manipulator); //Change the visualization of the touchPoint if (_touchPointsMarkers.ContainsKey(touchPoint.TouchDevice.Id)) { if (AreFingersVisible) { _touchPointsMarkers[touchPoint.TouchDevice.Id].HorizontalOffset = touchPoint.Position.X - (EllipseWidth / 2); _touchPointsMarkers[touchPoint.TouchDevice.Id].VerticalOffset = touchPoint.Position.Y - (EllipseWidth / 2); } #if DEBUG Debug.WriteLine("TouchPoint Reported: Id {0} at ({1} - Total Touch Points: {2})", touchPoint.TouchDevice.Id, touchPoint.Position, touchPoints.Count()); #endif } } } // process manipulations _manipulationProcessor.ProcessManipulators(Timestamp, manipulators); }
/// <summary> /// Initialize the TouchHelper /// </summary> void AssociatedObjectLoaded(object sender, RoutedEventArgs e) { TouchHelper.SetRootElement(TouchHelper.GetRootElement(AssociatedObject)); }