Exemple #1
0
        private bool OnTouchMove(global::Android.Views.MotionEvent e)
        {
            if (base.ViewGroup != null)
            {
                int width  = base.ViewGroup.Width;
                int height = base.ViewGroup.Height;
                if ((e.GetX() < 0) || (e.GetY() < 0) || (e.GetX() >= width) || (e.GetY() >= height))
                {
                    this.cancelTouch = true;
                    if (base.Element != null)
                    {
                        ((PressedView)base.Element).SetPressed(false);
                    }
                }
            }

            return(true);
        }
        public void OnTouchEvent(global::Android.Views.MotionEvent e, CanvasContent content)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            var began     = new List <CanvasTouch> ();
            var moved     = new List <CanvasTouch> ();
            var ended     = new List <CanvasTouch> ();
            var cancelled = new List <CanvasTouch> ();

            var pointerCount = e.PointerCount;
            var actionMasked = e.Action & global::Android.Views.MotionEventActions.Mask;
            var actionIndex  = (int)(e.Action & global::Android.Views.MotionEventActions.PointerIdMask) >> (int)global::Android.Views.MotionEventActions.PointerIdShift;
            var actionId     = e.GetPointerId(actionIndex);

            //Log.WriteLine("a = {0}, index = {1}, id = {2}, c = {3}", actionMasked, actionIndex, actionId, pointerCount);

            switch (actionMasked)
            {
            case global::Android.Views.MotionEventActions.Move:
                for (var index = 0; index < pointerCount; index++)
                {
                    var id = e.GetPointerId(index);
                    if (id < MaxTouchId)
                    {
                        var t = _activeTouches[id];
                        if (t != null)
                        {
                            var curSuperLoc = t.SuperCanvasLocation;
                            var newSuperLoc = new System.Drawing.PointF(e.GetX(index), e.GetY(index));
                            var dx          = curSuperLoc.X - newSuperLoc.X;
                            var dy          = curSuperLoc.Y - newSuperLoc.Y;
                            if (t.IsMoving ||
                                (Math.Abs(dx) > _initialMoveResolution ||
                                 Math.Abs(dy) > _initialMoveResolution) ||
                                NumActiveTouches > 1)
                            {
                                t.SuperCanvasPreviousLocation = t.SuperCanvasLocation;
                                t.CanvasPreviousLocation      = t.CanvasLocation;
                                t.PreviousTime = t.Time;
                                t.IsMoving     = true;

                                t.SuperCanvasLocation = newSuperLoc;
                                t.CanvasLocation      = LocationFromView(t.SuperCanvasLocation);
                                t.Time = DateTime.UtcNow;

                                moved.Add(t);

                                _longClickToken = -1;
                            }
                        }
                    }
                }
                break;

            case global::Android.Views.MotionEventActions.Down:
            case global::Android.Views.MotionEventActions.PointerDown:
                if (actionId < MaxTouchId)
                {
                    var t = new AndroidTouch {
                        Handle = new IntPtr(actionId + 1),                          // +1 because IntPtr=0 is special
                        SuperCanvasLocation = new System.Drawing.PointF(e.GetX(actionIndex), e.GetY(actionIndex)),
                        Time     = DateTime.UtcNow,
                        TapCount = 1,
                    };
                    t.CanvasLocation              = LocationFromView(t.SuperCanvasLocation);
                    t.CanvasPreviousLocation      = t.CanvasLocation;
                    t.SuperCanvasPreviousLocation = t.SuperCanvasLocation;
                    t.PreviousTime = t.Time;
                    began.Add(t);
                    _activeTouches[actionId] = t;

                    //
                    // Detect double tap
                    //
                    if ((t.Time - _lastBeganTime).TotalMilliseconds <= _doubleTapTimeoutMillis &&
                        Math.Abs(_lastBeganLocation.X - t.SuperCanvasLocation.X) <= _doubleTapResolution &&
                        Math.Abs(_lastBeganLocation.Y - t.SuperCanvasLocation.Y) <= _doubleTapResolution)
                    {
                        t.TapCount = 2;
                    }
                    else
                    {
                        //
                        // Detect long press
                        //
                        if (NumActiveTouches == 1)
                        {
                            var tok = _longClickTokenGenerator.Next();
                            _handler.PostDelayed(() => { HandleLongClick(tok, content); }, _longClickTimeoutMillis);
                            _longClickToken = tok;
                        }
                        else
                        {
                            _longClickToken = -1;
                        }
                    }

                    _lastBeganTime     = t.Time;
                    _lastBeganLocation = t.SuperCanvasLocation;
                }
                break;

            case global::Android.Views.MotionEventActions.Up:
            case global::Android.Views.MotionEventActions.PointerUp:
                if (actionId < MaxTouchId)
                {
                    var t = _activeTouches[actionId];
                    if (t != null)
                    {
                        t.Time = DateTime.UtcNow;
                        _activeTouches[actionId] = null;
                        ended.Add(t);
                    }
                    _longClickToken = -1;
                }
                break;
            }

            if (began.Count > 0)
            {
                content.TouchesBegan(began.ToArray(), CanvasKeys.None);
            }
            if (moved.Count > 0)
            {
                content.TouchesMoved(moved.ToArray());
            }
            if (ended.Count > 0)
            {
                content.TouchesEnded(ended.ToArray());
            }
            if (cancelled.Count > 0)
            {
                content.TouchesCancelled(cancelled.ToArray());
            }
        }