void SetContext(MotionEvent curr) { if (mCurrEvent != null) { mCurrEvent.Recycle(); } mCurrEvent = MotionEvent.Obtain(curr); mCurrLen = -1; mPrevLen = -1; mScaleFactor = -1; MotionEvent prev = mPrevEvent; int prevIndex0 = prev.FindPointerIndex(mActiveId0); int prevIndex1 = prev.FindPointerIndex(mActiveId1); int currIndex0 = curr.FindPointerIndex(mActiveId0); int currIndex1 = curr.FindPointerIndex(mActiveId1); if (prevIndex0 < 0 || prevIndex1 < 0 || currIndex0 < 0 || currIndex1 < 0) { mInvalidGesture = true; if (mGestureInProgress) { mListener.OnScaleEnd(this); } return; } float px0 = prev.GetX(prevIndex0); float py0 = prev.GetY(prevIndex0); float px1 = prev.GetX(prevIndex1); float py1 = prev.GetY(prevIndex1); float cx0 = curr.GetX(currIndex0); float cy0 = curr.GetY(currIndex0); float cx1 = curr.GetX(currIndex1); float cy1 = curr.GetY(currIndex1); float pvx = px1 - px0; float pvy = py1 - py0; float cvx = cx1 - cx0; float cvy = cy1 - cy0; mPrevFingerDiffX = pvx; mPrevFingerDiffY = pvy; mCurrFingerDiffX = cvx; mCurrFingerDiffY = cvy; mFocusX = cx0 + cvx * 0.5f; mFocusY = cy0 + cvy * 0.5f; mTimeDelta = curr.EventTime - prev.EventTime; mCurrPressure = curr.GetPressure(currIndex0) + curr.GetPressure(currIndex1); mPrevPressure = prev.GetPressure(prevIndex0) + prev.GetPressure(prevIndex1); }
// convert the touches data for the use of recognition public void TouchStart(MotionEvent e) { lastBegin = DateTimeOffset.Now.ToUnixTimeMilliseconds(); if (startTime == null) { startTime = lastBegin; var index = GetIndexForTimestamp(lastBegin); AddEntryToArrayAtIndex(lastOrientation, ref orientationOverTime, index); AddEntryToArrayAtIndex(lastAcceleration, ref accelerationOverTime, index); } numStrokes++; // obtain the location and pressure of the touch var x = e.GetX(); var y = e.GetY(); var force = e.GetPressure(0); var touchPoint = new JSONObject(); touchPoint.Put("timestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds()); touchPoint.Put("x", x); touchPoint.Put("y", y); touchPoint.Put("pressure", force); AddTouchPoint(touchPoint, DateTimeOffset.Now.ToUnixTimeMilliseconds()); }
public void TouchMove(MotionEvent e) { // obtain the location and pressure of the touch var x = e.GetX(); var y = e.GetY(); var force = e.GetPressure(0); var touchPoint = new JSONObject(); touchPoint.Put("timestamp", DateTimeOffset.Now.ToUnixTimeMilliseconds()); touchPoint.Put("x", x); touchPoint.Put("y", y); touchPoint.Put("pressure", force); AddTouchPoint(touchPoint, DateTimeOffset.Now.ToUnixTimeMilliseconds()); }
protected virtual void UpdateStateByEvent(MotionEvent curr) { var prev = this.PrevEvent; // Reset mCurrEvent if (this.mCurrEvent != null) { this.mCurrEvent.Recycle(); this.mCurrEvent = null; } this.mCurrEvent = MotionEvent.Obtain(curr); // Delta time this.TimeDelta = curr.EventTime - prev.EventTime; // Pressure this.CurrPressure = curr.GetPressure(curr.ActionIndex); this.PrevPressure = prev.GetPressure(prev.ActionIndex); }
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); }
public void SetContext(View view, MotionEvent curr) { try { if (MCurrEvent != null) { MCurrEvent.Recycle(); } MCurrEvent = MotionEvent.Obtain(curr); MCurrLen = -1; MPrevLen = -1; MScaleFactor = -1; MCurrSpanVector.Set(0.0f, 0.0f); MotionEvent prev = MPrevEvent; int prevIndex0 = prev.FindPointerIndex(MActiveId0); int prevIndex1 = prev.FindPointerIndex(MActiveId1); int currIndex0 = curr.FindPointerIndex(MActiveId0); int currIndex1 = curr.FindPointerIndex(MActiveId1); if (prevIndex0 < 0 || prevIndex1 < 0 || currIndex0 < 0 || currIndex1 < 0) { MInvalidGesture = true; Log.Error(Tag, "Invalid MotionEvent stream detected.", new Throwable()); if (MGestureInProgress) { MListener.OnScaleEnd(view, this); } return; } float px0 = prev.GetX(prevIndex0); float py0 = prev.GetY(prevIndex0); float px1 = prev.GetX(prevIndex1); float py1 = prev.GetY(prevIndex1); float cx0 = curr.GetX(currIndex0); float cy0 = curr.GetY(currIndex0); float cx1 = curr.GetX(currIndex1); float cy1 = curr.GetY(currIndex1); float pvx = px1 - px0; float pvy = py1 - py0; float cvx = cx1 - cx0; float cvy = cy1 - cy0; MCurrSpanVector.Set(cvx, cvy); MPrevFingerDiffX = pvx; MPrevFingerDiffY = pvy; MCurrFingerDiffX = cvx; MCurrFingerDiffY = cvy; MFocusX = cx0 + cvx * 0.5f; MFocusY = cy0 + cvy * 0.5f; MTimeDelta = curr.EventTime - prev.EventTime; MCurrPressure = curr.GetPressure(currIndex0) + curr.GetPressure(currIndex1); MPrevPressure = prev.GetPressure(prevIndex0) + prev.GetPressure(prevIndex1); } catch (Exception e) { Console.WriteLine(e); } }
private void UpdateSecondPoint(MotionEvent e, int index) { newSecondPoint = new PVector(e.GetX(index), e.GetY(index)); newSecondPressure = e.GetPressure(index); newSecondTime = (ulong)e.EventTime; }
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); }