public bool onTouch(MotionEvent ev)
        {
            if (velocityTracker == null)
            {
                velocityTracker = VelocityTracker.Obtain();
            }

            velocityTracker.AddMovement(ev);

            if (ev.Action == MotionEventActions.Down)
            {
                if (!scroller.IsFinished)
                {
                    scroller.AbortAnimation();
                }

                isSmoothScrolling = false;
            }
            else if (ev.Action == MotionEventActions.Move)
            {
                velocityTracker.AddMovement(ev);
                velocityTracker.ComputeCurrentVelocity(500);
            }
            else if (ev.Action == MotionEventActions.Up)
            {
                handleHorizontalScrolling();
                velocityTracker.Recycle();
                velocityTracker.Clear();
                velocityTracker = null;
                isScrolling     = false;
            }

            return(false);
        }
Ejemplo n.º 2
0
        public override bool OnTouchEvent(MotionEvent ev)
        {
            if (ev.Action == MotionEventActions.Move)
            {
                tracker.AddMovement(ev);
            }
            if (ev.Action == MotionEventActions.Up)
            {
                tracker.ComputeCurrentVelocity(1000);
                var velocity = tracker.XVelocity;
                Console.WriteLine("Velocity: " + velocity);
                tracker.Clear();

                if (Math.Abs(velocity) > 1000)
                {
                    timer = new System.Timers.Timer(125);
                    timer.Start();
                    timer.Elapsed += delegate
                    {
                        CallHandler();
                        timer.Stop();
                        timer.Dispose();
                        timer = null;
                    };
                }
                else
                {
                    CallHandler();
                }
            }

            return(base.OnTouchEvent(ev));
        }
Ejemplo n.º 3
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            FindViewById(Android.Resource.Id.Content).SystemUiVisibility =
                (StatusBarVisibility)(SystemUiFlags.LayoutStable | SystemUiFlags.LayoutFullscreen | SystemUiFlags.HideNavigation);
            stiffness       = FindViewById <SeekBar>(Resource.Id.stiffness);
            damping         = FindViewById <SeekBar>(Resource.Id.damping);
            velocityTracker = VelocityTracker.Obtain();
            View box = FindViewById(Resource.Id.box);

            box.Touch += (sender, args) =>
            {
                switch (args.Event.Action)
                {
                case MotionEventActions.Down:
                    downX = args.Event.GetX();
                    downY = args.Event.GetY();
                    velocityTracker.AddMovement(args.Event);
                    break;

                case MotionEventActions.Move:
                    box.TranslationX = args.Event.GetX() - downX;
                    box.TranslationY = args.Event.GetY() - downY;
                    velocityTracker.AddMovement(args.Event);
                    break;

                case MotionEventActions.Up:
                case MotionEventActions.Cancel:
                    velocityTracker.ComputeCurrentVelocity(1000);
                    if (box.TranslationX != 0)
                    {
                        SpringAnimation animX = new SpringAnimation(box, DynamicAnimation.TranslationX, 0);
                        animX.Spring.SetStiffness(Stiffness);
                        animX.Spring.SetDampingRatio(Damping);
                        animX.SetStartVelocity(velocityTracker.XVelocity);
                        animX.Start();
                    }
                    if (box.TranslationY != 0)
                    {
                        SpringAnimation animY = new SpringAnimation(box, DynamicAnimation.TranslationY, 0);
                        animY.Spring.SetStiffness(Stiffness);
                        animY.Spring.SetDampingRatio(Damping);
                        animY.SetStartVelocity(velocityTracker.YVelocity);
                        animY.Start();
                    }
                    velocityTracker.Clear();
                    break;
                }
            };
        }
Ejemplo n.º 4
0
        private void OnSecondaryPointerUp(MotionEvent ev)
        {
#if DEBUG
            Log.Verbose(Tag, "OnSecondaryPointerUp called");
#endif
            var pointerIndex = MotionEventCompat.GetActionIndex(ev);
            var pointerId = MotionEventCompat.GetPointerId(ev, pointerIndex);
            if (pointerId == ActivePointerId)
            {
                var newPointerIndex = pointerIndex == 0 ? 1 : 0;
                _lastMotionX = MotionEventCompat.GetX(ev, newPointerIndex);
                ActivePointerId = MotionEventCompat.GetPointerId(ev, newPointerIndex);
                if (VelocityTracker != null)
                    VelocityTracker.Clear();
            }
        }
        private void OnSecondaryPointerUp(MotionEvent ev)
        {
            if (DEBUG)
            {
                Console.WriteLine(TAG + " OnSecondaryPointerUp called");
            }
            int pointerIndex = MotionEventCompat.GetActionIndex(ev);
            int pointerId    = MotionEventCompat.GetPointerId(ev, pointerIndex);

            if (pointerId == mActivePointerId)
            {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastMotionX     = MotionEventCompat.GetX(ev, newPointerIndex);
                mActivePointerId = MotionEventCompat.GetPointerId(ev, newPointerIndex);
                if (mVelocityTracker != null)
                {
                    mVelocityTracker.Clear();
                }
            }
        }
Ejemplo n.º 6
0
        void OnSecondaryPointerUp(MotionEvent ev)
        {
            var pointerIndex = ((int)ev.Action & MotionEventCompat.ActionPointerIndexMask) >> MotionEventCompat.ActionPointerIndexShift;

            var pointerId = MotionEventCompat.GetPointerId(ev, pointerIndex);

            if (pointerId == mActivePointerId)
            {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                // TODO: Make this decision more intelligent.
                int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mDownMotionX     = MotionEventCompat.GetX(ev, newPointerIndex);
                mDownMotionX     = MotionEventCompat.GetY(ev, newPointerIndex);
                mDownScrollX     = ScrollX;
                mActivePointerId = MotionEventCompat.GetPointerId(ev, newPointerIndex);
                if (mVelocityTracker != null)
                {
                    mVelocityTracker.Clear();
                }
            }
        }
Ejemplo n.º 7
0
        private void OnTouch(object sender, SKTouchEventArgs e)
        {
            // Save time, when the event occures
            long ticks = DateTime.Now.Ticks;

            var location = GetScreenPosition(e.Location);

            if (e.ActionType == SKTouchAction.Pressed)
            {
                _firstTouch = location;

                _touches[e.Id] = new TouchEvent(e.Id, location, ticks);

                _velocityTracker.Clear();

                // Do we have a doubleTapTestTimer running?
                // If yes, stop it and increment _numOfTaps
                if (_doubleTapTestTimer != null)
                {
                    _doubleTapTestTimer.Dispose();
                    _doubleTapTestTimer = null;
                    _numOfTaps++;
                }
                else
                {
                    _numOfTaps = 1;
                }

                e.Handled = OnTouchStart(_touches.Select(t => t.Value.Location).ToList());
            }
            if (e.ActionType == SKTouchAction.Released)
            {
                // Delete e.Id from _touches, because finger is released
                var releasedTouch = _touches[e.Id];
                _touches.Remove(e.Id);

                double velocityX;
                double velocityY;

                (velocityX, velocityY) = _velocityTracker.CalcVelocity(e.Id, ticks);

                // Is this a fling or swipe?
                if (_touches.Count == 0)
                {
                    if (velocityX > 10000 || velocityY > 10000)
                    {
                        // This was the last finger on screen, so this is a fling
                        e.Handled = OnFlinged(velocityX, velocityY);
                    }

                    // Do we have a tap event
                    if (releasedTouch == null)
                    {
                        e.Handled = false;
                        return;
                    }

                    // While tapping on screen, there could be a small movement of the finger
                    // (especially on Samsungs). So check, if touch start location isn't more
                    // than 2 pixels away from touch end location.
                    var isAround = Math.Abs(releasedTouch.Location.X - _firstTouch.X) < 2 && Math.Abs(releasedTouch.Location.Y - _firstTouch.Y) < 2;

                    // If touch start and end is in the same area and the touch time is shorter
                    // than longTap, than we have a tap.
                    if (isAround && ticks - releasedTouch.Tick < (e.DeviceType == SKTouchDeviceType.Mouse ? shortClick : longTap) * 10000)
                    {
                        // Start a timer with timeout delayTap ms. If than isn't arrived another tap, than it is a single
                        _doubleTapTestTimer = new System.Threading.Timer((l) =>
                        {
                            if (_numOfTaps > 1)
                            {
                                if (!e.Handled)
                                {
                                    e.Handled = OnDoubleTapped(location, _numOfTaps);
                                }
                            }
                            else
                            if (!e.Handled)
                            {
                                e.Handled = OnSingleTapped((Geometries.Point)l);
                            }
                            _numOfTaps = 1;
                            if (_doubleTapTestTimer != null)
                            {
                                _doubleTapTestTimer.Dispose();
                            }
                            _doubleTapTestTimer = null;
                        }, location, UseDoubleTap ? delayTap : 0, -1);
                    }
                    else if (releasedTouch.Location.Equals(_firstTouch) && ticks - releasedTouch.Tick >= longTap * 10000)
                    {
                        if (!e.Handled)
                        {
                            e.Handled = OnLongTapped(location);
                        }
                    }
                }

                if (_touches.Count == 1)
                {
                    e.Handled = OnTouchStart(_touches.Select(t => t.Value.Location).ToList());
                }

                if (!e.Handled)
                {
                    e.Handled = OnTouchEnd(_touches.Select(t => t.Value.Location).ToList(), releasedTouch.Location);
                }
            }
            if (e.ActionType == SKTouchAction.Moved)
            {
                _touches[e.Id] = new TouchEvent(e.Id, location, ticks);

                if (e.InContact)
                {
                    _velocityTracker.AddEvent(e.Id, location, ticks);
                }

                if (e.InContact && !e.Handled)
                {
                    e.Handled = OnTouchMove(_touches.Select(t => t.Value.Location).ToList());
                }
                else
                {
                    e.Handled = OnHovered(_touches.Select(t => t.Value.Location).FirstOrDefault());
                }
            }
            if (e.ActionType == SKTouchAction.Cancelled)
            {
                _touches.Remove(e.Id);
            }
            if (e.ActionType == SKTouchAction.Exited)
            {
            }
            if (e.ActionType == SKTouchAction.Entered)
            {
            }
        }
Ejemplo n.º 8
0
        private void OnTouch(object sender, SKTouchEventArgs e)
        {
            // Save time, when the event occures
            long ticks = DateTime.Now.Ticks;

            var location = GetScreenPosition(e.Location);

            // Get finger/handler for this event
            if (!_fingers.Keys.Contains(e.Id))
            {
                if (_fingers.Count < 10)
                {
                    _fingers.Add(e.Id, _fingers.Count);
                }
            }

            var id = _fingers[e.Id];

            if (e.ActionType == SKTouchAction.Pressed)
            {
                _firstTouch = location;

                _touches[id] = new TouchEvent(id, location, ticks);

                _velocityTracker.Clear();

                // Do we have a doubleTapTestTimer running?
                // If yes, stop it and increment _numOfTaps
                if (_doubleTapTestTimer != null)
                {
                    _doubleTapTestTimer.Dispose();
                    _doubleTapTestTimer = null;
                    _numOfTaps++;
                }
                else
                {
                    _numOfTaps = 1;
                }

                e.Handled = OnTouchStart(_touches.Select(t => t.Value.Location).ToList());
            }
            if (e.ActionType == SKTouchAction.Released)
            {
                // Delete e.Id from _fingers, because finger is released

                _fingers.Remove(e.Id);

                double velocityX;
                double velocityY;

                (velocityX, velocityY) = _velocityTracker.CalcVelocity(id, ticks);

                // Is this a fling or swipe?
                if (velocityX > 10000 || velocityY > 10000)
                {
                    System.Diagnostics.Debug.WriteLine($"Velocity X = {velocityX}, Velocity Y = {velocityY}");

                    e.Handled = OnFlinged(velocityX, velocityY);
                }

                // Do we have a tap event
                if (_touches.Count == 0 || _touches[id] == null)
                {
                    e.Handled = false;
                    return;
                }

                if (_touches[id].Location.Equals(_firstTouch) && ticks - _touches[id].Tick < (e.DeviceType == SKTouchDeviceType.Mouse ? shortClick : longTap) * 10000)
                {
                    // Start a timer with timeout delayTap ms. If than isn't arrived another tap, than it is a single
                    _doubleTapTestTimer = new System.Threading.Timer((l) =>
                    {
                        if (_numOfTaps > 1)
                        {
                            if (!e.Handled)
                            {
                                e.Handled = OnDoubleTapped(location, _numOfTaps);
                            }
                        }
                        else
                        if (!e.Handled)
                        {
                            e.Handled = OnSingleTapped((Geometries.Point)l);
                        }
                        _numOfTaps = 1;
                        if (_doubleTapTestTimer != null)
                        {
                            _doubleTapTestTimer.Dispose();
                        }
                        _doubleTapTestTimer = null;
                    }, location, UseDoubleTap ? delayTap : 0, -1);
                }
                else if (_touches[id].Location.Equals(_firstTouch) && ticks - _touches[id].Tick >= longTap * 10000)
                {
                    if (!e.Handled)
                    {
                        e.Handled = OnLongTapped(location);
                    }
                }
                var releasedTouch = _touches[id];
                _touches.Remove(id);

                if (!e.Handled)
                {
                    e.Handled = OnTouchEnd(_touches.Select(t => t.Value.Location).ToList(), releasedTouch.Location);
                }
            }
            if (e.ActionType == SKTouchAction.Moved)
            {
                _touches[id] = new TouchEvent(id, location, ticks);

                if (e.InContact)
                {
                    _velocityTracker.AddEvent(id, location, ticks);
                }

                if (e.InContact && !e.Handled)
                {
                    e.Handled = OnTouchMove(_touches.Select(t => t.Value.Location).ToList());
                }
                else
                {
                    e.Handled = OnHovered(_touches.Select(t => t.Value.Location).FirstOrDefault());
                }
            }
        }
Ejemplo n.º 9
0
        public override bool DispatchTouchEvent(MotionEvent e)
        {
            var index     = e.ActionIndex;
            var action    = e.ActionMasked;
            var pointerId = e.GetPointerId(index);

            switch (action & MotionEventActions.Mask)
            {
            case MotionEventActions.PointerDown:
                MultiTouchGesture?.Invoke(this, EventArgs.Empty);
                break;

            case MotionEventActions.Up:
                if ((DateTime.Now - _downTime).TotalMilliseconds < 200)
                {
                    SingleTapGesture?.Invoke(this, EventArgs.Empty);
                }

                break;
            }

            switch (action)
            {
            case MotionEventActions.Down:
                _downTime = DateTime.Now;

                if (_velocityTracker == null)
                {
                    _velocityTracker = VelocityTracker.Obtain();
                }
                else
                {
                    // Reset the velocity tracker back to its initial state.
                    _velocityTracker.Clear();
                }

                if (IfVelocityTrackerIsNull())
                {
                    return(true);
                }

                _velocityTracker.AddMovement(e);
                break;

            case MotionEventActions.Move:
                if (IfVelocityTrackerIsNull())
                {
                    return(true);
                }

                _velocityTracker.AddMovement(e);
                _velocityTracker.ComputeCurrentVelocity(Sensitivity);
                TryExportVelocity(_velocityTracker.GetXVelocity(pointerId), _velocityTracker.GetYVelocity(pointerId));

                break;

            case MotionEventActions.Up:
            case MotionEventActions.Cancel:
                if (IfVelocityTrackerIsNull())
                {
                    return(true);
                }

                _velocityTracker.Recycle();
                _velocityTracker = null;
                break;
            }

            return(true);
        }