// </Snippet_GamePiece_ProcessInertia> // <Snippet_GamePiece_UpdateFromMouse> #region UpdateFromMouse public bool UpdateFromMouse(MouseState mouseState) { if (mouseState.LeftButton == ButtonState.Released) { if (isMouseCaptured) { manipulationProcessor.CompleteManipulation(Timestamp); } isMouseCaptured = false; } if (isMouseCaptured || (mouseState.LeftButton == ButtonState.Pressed && bounds.Contains(mouseState.X, mouseState.Y))) { isMouseCaptured = true; Manipulator2D[] manipulators = new Manipulator2D[] { new Manipulator2D(0, mouseState.X, mouseState.Y) }; dragPoint.X = mouseState.X; dragPoint.Y = mouseState.Y; manipulationProcessor.ProcessManipulators(Timestamp, manipulators); } // If the right button is pressed, stop the piece and move it to the center. if (mouseState.RightButton == ButtonState.Pressed) { processInertia = false; X = viewport.Width / 2; Y = viewport.Height / 2; rotation = 0; } return(isMouseCaptured); }
/// <summary> /// Handles the Affine2DManipulationDelta event of the ManipulationProcessor. Attempts to distinguish scroll gestures /// from those intended to manipulate the parent ScatterViewItem. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Input.Manipulation2DDeltaEventArgs"/> instance containing the event data.</param> private void ManipulationProcessor_Delta(object sender, Manipulation2DDeltaEventArgs e) { if (_scatterViewItem == null) { // Hook up an event to the parent SVI. When it loses a contact, the ScrollViewer should capture them again. _scatterViewItem = AssociatedObject.FindVisualParent <ScatterViewItem>(); _scatterViewItem.PreviewTouchUp += ScatterViewItem_PreviewContactUp; } if (_scatterViewItem == null) { // There's no SVI, so don't do anything. return; } if (e.Delta.ExpansionX != 0 || e.Delta.ExpansionY != 0 || e.Delta.Rotation != 0) { // If any scale or rotation is detected, pass that to the SVI. GiveControlToScatterViewItem(); return; } Vector translation = new Vector((double)e.Cumulative.TranslationX, (double)e.Cumulative.TranslationY); if (Math.Abs(translation.Length) < 10) { // There's no translation, so don't do anything. return; } if (AssociatedObject.ComputedHorizontalScrollBarVisibility == Visibility.Collapsed && AssociatedObject.ComputedVerticalScrollBarVisibility == Visibility.Collapsed) { // There are no scrollbars, so pass all translations to the SVI. GiveControlToScatterViewItem(); return; } if (translation.Length >= SensitivityDistance) { // The scroll has gone for some distance, so assume the gesture is intended as a scroll and never do a drag. _manipulationProcessor.CompleteManipulation(DateTime.UtcNow.Ticks); return; } double translationAngle = Math.Abs(Math.Atan2(e.Cumulative.TranslationX, e.Cumulative.TranslationY) * (180 / Math.PI)); if (AssociatedObject.ComputedVerticalScrollBarVisibility == Visibility.Visible && AssociatedObject.ComputedHorizontalScrollBarVisibility == Visibility.Collapsed) { // The ScrollViewer is scrolling vertically. if (translationAngle >= 90 - SensitivityAngle && translationAngle <= 90 + SensitivityAngle) { // The gesture scrolled horizontally, so pass that to the SVI. GiveControlToScatterViewItem(); } } else if (AssociatedObject.ComputedVerticalScrollBarVisibility == Visibility.Collapsed && AssociatedObject.ComputedHorizontalScrollBarVisibility == Visibility.Visible) { // The ScrollViewer is scrolling horizontally. if (e.Cumulative.TranslationX < 0) { // The gesture is scrolling to the left. if (180 - translationAngle < SensitivityAngle) { // But it's close enough to vertical that it should go to the SVI. GiveControlToScatterViewItem(); } } else if (e.Cumulative.TranslationX > 0) { // The gesture is scrolling to the right. if (translationAngle <= SensitivityAngle) { // But it's close enough to vertical that it should go to the SVI. GiveControlToScatterViewItem(); } } } }