Example #1
0
        protected virtual void Update()
        {
            if (provider == null)
            {
                Debug.LogError("No provider set for " + this.GetType().Name
                               + " in " + this.name + "; you should sure that there is a LeapProvider in "
                               + "your scene and that this component has a LeapProvider reference.", this);
                this.enabled = false;
            }

            _wasActivated   = false;
            _wasDeactivated = false;
            _wasCancelled   = false;
            _wasFinished    = false;

            var hand = provider.Get(whichHand);

            // Determine the tracked state of hands and fire appropriate methods.
            _isHandTracked = hand != null;

            if (isHandTracked != _wasHandTracked)
            {
                if (isHandTracked)
                {
                    WhenHandBecomesTracked(hand);
                }
                else
                {
                    WhenHandLosesTracking(whichHand == Chirality.Left);
                }
            }

            if (isHandTracked)
            {
                WhileHandTracked(hand);
            }
            else
            {
                WhileHandUntracked(whichHand == Chirality.Left);
            }

            _wasHandTracked = isHandTracked;

            // Determine whether or not the gesture should be active or inactive.
            bool shouldGestureBeActive;
            DeactivationReason?deactivationReason = null;

            if (!isHandTracked)
            {
                shouldGestureBeActive = false;
            }
            else
            {
                if (!_isActive)
                {
                    if (ShouldGestureActivate(hand))
                    {
                        shouldGestureBeActive = true;
                    }
                    else
                    {
                        shouldGestureBeActive = false;
                    }
                }
                else
                {
                    if (ShouldGestureDeactivate(hand, out deactivationReason))
                    {
                        shouldGestureBeActive = false;
                    }
                    else
                    {
                        shouldGestureBeActive = true;
                    }
                }
            }

            // If the deactivation reason is not set, we assume the gesture completed
            // successfully.
            bool wasGestureSuccessful = deactivationReason.GetValueOrDefault()
                                        == DeactivationReason.FinishedGesture;

            // Fire gesture state change events.
            if (shouldGestureBeActive != _isActive)
            {
                if (shouldGestureBeActive)
                {
                    _wasActivated = true;
                    _isActive     = true;

                    WhenGestureActivated(hand);
                    OnGestureActivated();
                    OnOneHandedGestureActivated(hand);
                }
                else
                {
                    _wasDeactivated = true;
                    if (wasGestureSuccessful)
                    {
                        _wasFinished = true;
                    }
                    else
                    {
                        _wasCancelled = true;
                    }
                    _isActive = false;

                    WhenGestureDeactivated(hand, deactivationReason.GetValueOrDefault());
                    OnGestureDeactivated();
                    OnOneHandedGestureDeactivated(hand);
                }
            }

            // Fire per-update events.
            if (_isActive)
            {
                WhileGestureActive(hand);
            }
            else
            {
                WhileGestureInactive(hand);
            }
        }