public static IEnumerable <MouseButton> ToMouseButtons(this MotionEventButtonState motionEventMouseButton) { if (motionEventMouseButton.HasFlagFast(MotionEventButtonState.Primary)) { yield return(MouseButton.Left); } if (motionEventMouseButton.HasFlagFast(MotionEventButtonState.Secondary)) { yield return(MouseButton.Right); } if (motionEventMouseButton.HasFlagFast(MotionEventButtonState.Tertiary)) { yield return(MouseButton.Middle); } if (motionEventMouseButton.HasFlagFast(MotionEventButtonState.Back)) { yield return(MouseButton.Button1); } if (motionEventMouseButton.HasFlagFast(MotionEventButtonState.Forward)) { yield return(MouseButton.Button2); } }
/// <summary> /// Returns the corresponding <see cref="MouseButton"/> for a mouse button given as a <see cref="MotionEventButtonState"/>. /// </summary> /// <param name="motionEventMouseButton">The given button. Must not be a raw state or a non-mouse button.</param> /// <returns>The corresponding <see cref="MouseButton"/>.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if the provided button <paramref name="motionEventMouseButton"/> is not a </exception> public static MouseButton ToMouseButton(this MotionEventButtonState motionEventMouseButton) { switch (motionEventMouseButton) { case MotionEventButtonState.Primary: return(MouseButton.Left); case MotionEventButtonState.Secondary: return(MouseButton.Right); case MotionEventButtonState.Tertiary: return(MouseButton.Middle); case MotionEventButtonState.Back: return(MouseButton.Button1); case MotionEventButtonState.Forward: return(MouseButton.Button2); default: throw new ArgumentOutOfRangeException(nameof(motionEventMouseButton), motionEventMouseButton, "Given button is not a mouse button."); } }
private PointerPointProperties GetProperties(MotionEventToolType type, MotionEventActions action, MotionEventButtonState buttons) { var props = new PointerPointProperties { IsPrimary = true, IsInRange = Pointer.IsInRange }; var isDown = action == /* 0 = */ MotionEventActions.Down || action.HasFlag(MotionEventActions.PointerDown); var isUp = action.HasFlag(MotionEventActions.Up) || action.HasFlag(MotionEventActions.PointerUp); var updates = _none; switch (type) { case MotionEventToolType.Finger: case MotionEventToolType.Unknown: // used by Xamarin.UITest props.IsLeftButtonPressed = Pointer.IsInContact; updates = isDown ? _fingerDownUpdates : isUp ? _fingerUpUpdates : _none; // Pressure = .5f => Keeps default as UWP returns .5 for fingers. break; case MotionEventToolType.Mouse: props.IsLeftButtonPressed = buttons.HasFlag(MotionEventButtonState.Primary); props.IsMiddleButtonPressed = buttons.HasFlag(MotionEventButtonState.Tertiary); props.IsRightButtonPressed = buttons.HasFlag(MotionEventButtonState.Secondary); updates = isDown ? _mouseDownUpdates : isUp ? _mouseUpUpdates : _none; // Pressure = .5f => Keeps default as UWP returns .5 for Mouse no matter is button is pressed or not (Android return 1.0 while pressing a button, but 0 otherwise). break; // Note: On UWP, if you touch screen while already holding the barrel button, you will get a right + barrel, // ** BUT ** if you touch screen and THEN press the barrel button props will be left + barrel until released. // On Android this distinction seems to be flagged by the "1101 ****" action flag (i.e. "StylusWithBarrel***" actions), // so here we set the Is<Left|Right>ButtonPressed based on the action and we don't try to link it to the barrel button state. case MotionEventToolType.Stylus when action == StylusWithBarrelDown: case MotionEventToolType.Stylus when action == StylusWithBarrelMove: case MotionEventToolType.Stylus when action == StylusWithBarrelUp: // Note: We still validate the "IsButtonPressed(StylusPrimary)" as the user might release the button while pressed. // In that case we will still receive moves and up with the "StylusWithBarrel***" actions. props.IsBarrelButtonPressed = buttons.HasFlag(MotionEventButtonState.StylusPrimary); props.IsRightButtonPressed = Pointer.IsInContact; props.Pressure = Math.Min(1f, _nativeEvent.GetPressure(_pointerIndex)); // Might exceed 1.0 on Android break; case MotionEventToolType.Stylus: props.IsBarrelButtonPressed = buttons.HasFlag(MotionEventButtonState.StylusPrimary); props.IsLeftButtonPressed = Pointer.IsInContact; props.Pressure = Math.Min(1f, _nativeEvent.GetPressure(_pointerIndex)); // Might exceed 1.0 on Android break; case MotionEventToolType.Eraser: props.IsEraser = true; props.Pressure = Math.Min(1f, _nativeEvent.GetPressure(_pointerIndex)); // Might exceed 1.0 on Android break; default: break; } if (updates.TryGetValue(_nativeEvent.ActionButton, out var update)) { props.PointerUpdateKind = update; } return(props); }
private static bool IsInContact(MotionEvent nativeEvent, PointerDeviceType pointerType, MotionEventActions action, MotionEventButtonState buttons) { switch (pointerType) { case PointerDeviceType.Mouse: // For mouse, we cannot only rely on action: We will get a "HoverExit" when we press the left button. return(buttons != 0); case PointerDeviceType.Pen: return(nativeEvent.GetAxisValue(Axis.Distance, nativeEvent.ActionIndex) == 0); default: case PointerDeviceType.Touch: // WARNING: MotionEventActions.Down == 0, so action.HasFlag(MotionEventActions.Up) is always true! return(!action.HasFlag(MotionEventActions.Up) && !action.HasFlag(MotionEventActions.PointerUp) && !action.HasFlag(MotionEventActions.Cancel)); } }
bool OnTouchOrHoverEvent(MotionEvent e, bool isTouch) { MotionEventButtonState buttonState = e.ButtonState; MotionEventButtonState pressedButtons = buttonState & ~mOldButtonState; mOldButtonState = buttonState; if ((pressedButtons & MotionEventButtonState.Secondary) != 0) { // Advance color when the right mouse button or first stylus button // is pressed. AdvanceColor(); } PaintMode mode; if ((buttonState & MotionEventButtonState.Tertiary) != 0) { // Splat paint when the middle mouse button or second stylus button is pressed. mode = PaintMode.Splat; } else if (isTouch || (buttonState & MotionEventButtonState.Primary) != 0) { // Draw paint when touching or if the primary button is pressed. mode = PaintMode.Draw; } else { // Otherwise, do not paint anything. return(false); } MotionEventActions action = e.ActionMasked; if (action == MotionEventActions.Down || action == MotionEventActions.Move || action == MotionEventActions.HoverMove) { int N = e.HistorySize; int P = e.PointerCount; for (int i = 0; i < N; i++) { for (int j = 0; j < P; j++) { Paint(GetPaintModeForTool(e.GetToolType(j), mode), e.GetHistoricalX(j, i), e.GetHistoricalY(j, i), e.GetHistoricalPressure(j, i), e.GetHistoricalTouchMajor(j, i), e.GetHistoricalTouchMinor(j, i), e.GetHistoricalOrientation(j, i), e.GetHistoricalAxisValue(Axis.Distance, j, i), e.GetHistoricalAxisValue(Axis.Tilt, j, i)); } } for (int j = 0; j < P; j++) { Paint(GetPaintModeForTool(e.GetToolType(j), mode), e.GetX(j), e.GetY(j), e.GetPressure(j), e.GetTouchMajor(j), e.GetTouchMinor(j), e.GetOrientation(j), e.GetAxisValue(Axis.Distance, j), e.GetAxisValue(Axis.Tilt, j)); } mCurX = e.GetX(); mCurY = e.GetY(); } return(true); }
bool OnTouchOrHoverEvent (MotionEvent e, bool isTouch) { MotionEventButtonState buttonState = e.ButtonState; MotionEventButtonState pressedButtons = buttonState & ~mOldButtonState; mOldButtonState = buttonState; if ((pressedButtons & MotionEventButtonState.Secondary) != 0) { // Advance color when the right mouse button or first stylus button // is pressed. AdvanceColor (); } PaintMode mode; if ((buttonState & MotionEventButtonState.Tertiary) != 0) { // Splat paint when the middle mouse button or second stylus button is pressed. mode = PaintMode.Splat; } else if (isTouch || (buttonState & MotionEventButtonState.Primary) != 0) { // Draw paint when touching or if the primary button is pressed. mode = PaintMode.Draw; } else { // Otherwise, do not paint anything. return false; } MotionEventActions action = e.ActionMasked; if (action == MotionEventActions.Down || action == MotionEventActions.Move || action == MotionEventActions.HoverMove) { int N = e.HistorySize; int P = e.PointerCount; for (int i = 0; i < N; i++) { for (int j = 0; j < P; j++) { Paint (GetPaintModeForTool (e.GetToolType (j), mode), e.GetHistoricalX (j, i), e.GetHistoricalY(j, i), e.GetHistoricalPressure (j, i), e.GetHistoricalTouchMajor (j, i), e.GetHistoricalTouchMinor (j, i), e.GetHistoricalOrientation (j, i), e.GetHistoricalAxisValue (Axis.Distance, j, i), e.GetHistoricalAxisValue (Axis.Tilt, j, i)); } } for (int j = 0; j < P; j++) { Paint (GetPaintModeForTool (e.GetToolType (j), mode), e.GetX (j), e.GetY (j), e.GetPressure (j), e.GetTouchMajor (j), e.GetTouchMinor (j), e.GetOrientation (j), e.GetAxisValue (Axis.Distance, j), e.GetAxisValue (Axis.Tilt, j)); } mCurX = e.GetX (); mCurY = e.GetY (); } return true; }