/// <summary>Begins the recognition of the current gesture.</summary>
 void BeginRecognition()
 {
     if (SingleTouch()) {
         if (Input.touches[0].phase == TouchPhase.Began) {
             currentGesture = new Tap(Input.touches[0].position, Time.time);
             if (gestureState == GestureState.NEUTRAL) { NotifyGestureRecognitionStart(); }
             gestureState = GestureState.TAP;
         }
     } else if (MultiTouch()) {
         currentGesture = new Sprinch(Input.touches[0].position, Input.touches[1].position);
         gestureState = GestureState.SPRINCH;
     }
 }
Exemple #2
0
 public void AddGesture(Gesture n)
 {
     //Check if exists;
     _GesturesList.Add(n);
 }
 /// <summary>Recognising a tap gesture.</summary>
 void Tap()
 {
     var tap = currentGesture as Tap;
     if (NoTouch()) {
         tap.EndTime = Time.time;
         NotifyGetsureRecognitionEnd();
         return;
     }
     if (SingleTouch() && Vector2.Distance(Input.touches[0].position, tap.Position) > MAX_DISTANCE_BEFORE_SWIPE) {
         currentGesture= new Swipe(tap.Position, tap.StartTime);
         ((Swipe)currentGesture).EndTime = tap.EndTime;
         if (gestureState == GestureState.TAP) { NotifyGestureRecognitionStart(); }
         gestureState = GestureState.SWIPE;
         //Il caso limite in cui lo swipe viene riconosciuto nello stesso momento in cui finisce è escluso
         return;
     }
     //TODO pinch&Spread
     //FIXME
     if (MultiTouch()) {
         currentGesture = new Sprinch(tap.Position, Input.touches[1].position);
         gestureState = GestureState.SPRINCH;
     }
 }
        /// <summary>Recognising the swipe gesture.</summary>
        void Swipe()
        {
            //FIXME
            Swipe swipe = (Swipe)currentGesture;
            if (MultiTouch()) {
                currentGesture = new Sprinch(swipe.Start, Input.touches[1].position);
                gestureState = GestureState.SPRINCH;
                return;
            }
            if (Input.touches[0].phase == TouchPhase.Ended ) {
                swipe.End = Input.touches[0].position;
                swipe.EndTime = Time.time;
                NotifyGetsureRecognitionEnd();
                return;
            }
            if (NoTouch()) {
                Debug.Log(swipe == null);
                swipe.EndTime = Time.time;
                NotifyGetsureRecognitionEnd();
                return;
            }

            if (Input.touches[0].phase == TouchPhase.Moved) {
                swipe.End = Input.touches[0].position;
                NotifyGestureRecognitionProgress();
                return;
            }
        }
        private void FixedUpdate()
        {
            if (!m_running)
            {
                return;
            }

            // Neutral device position is portrait or landscape, perpendicular to the ground, screen facing the m_user.
            // In this position, X runs left(-)/right(+), Y runs down(-)/up(+), and Z runs away(-)/towards(+).
            // Unity world space has the Z-axis running towards the back rather than the front, so that's why
            // some of the below code reverses the Z-axis.

            if (m_hasGyro)
            {
                m_gravity = oscAcceleration;                                       //Input.gyro.gravity;
                m_user    = oscAcceleration;                                       //Input.gyro.userAcceleration;

                Vector3 g = new Vector3(m_gravity.x, m_gravity.y, -m_gravity.z);
                m_attitudeXZ = Quaternion.FromToRotation(g, Vector3.down);

                Quaternion rot    = m_rotationBase * FlipHands(Input.gyro.attitude);
                Vector3    angles = rot.eulerAngles - m_calibrate;
                m_attitudeXYZ = Quaternion.Euler(angles.x, angles.y, angles.z);
            }
            else
            {
                m_gravity = Vector3.Lerp(m_gravity, oscAcceleration, c_gravityFilter);
                m_current = oscAcceleration;           //Input.acceleration;
                m_user    = m_current - m_gravity;

                Vector3 g = new Vector3(m_gravity.x, m_gravity.y, -m_gravity.z);
                m_attitudeXZ  = Quaternion.FromToRotation(g, Vector3.down);
                m_attitudeXYZ = m_attitudeXZ;
            }

            if (HandleGesture == null)
            {
                return;
            }

            bool aboveThreshold = m_user.magnitude >= IgnoreAccelBelow;

            if (!aboveThreshold)
            {
                m_user = Vector3.zero;
            }

            if (!m_cur.Started)
            {
                if (aboveThreshold)
                {
                    m_cur.Begin(m_user);
                }
                m_recording.Record(m_user, 0, m_user.magnitude, m_attitudeXYZ);
                return;
            }

            float dot      = Vector3.Dot(m_cur.m_dir.normalized, m_user.normalized);
            float spikeMag = 0f;

            if (aboveThreshold && (dot >= m_maxSpikeAngleDot))
            {
                // Continue current spike.
                m_cur.Update(m_user);
            }
            else
            {
                // End current spike and see if it completes a gesture.
                m_cur.End();

                if (m_cur.Magnitude < MinSpikeMagnitude)
                {
                    // Not big enough to complete a spike.  But if we have a gesture queued up, fire it off.
                    FireCandidateGesture();
                }
                else
                {
                    // Big enough to be a spike, so add it to the array.
                    spikeMag = m_cur.Magnitude;
                    m_spikes.Add(m_cur);

#if DEBUG
                    //Vector3 dir = new Vector3(m_cur.m_dir.x, m_cur.m_dir.y, -m_cur.m_dir.z);
                    //Debug.DrawRay(transform.position, dir * 2f, Color.magenta, 0.5f);
#endif

                    // Try to find a matching spike in the opposite direction to form a gesture.
                    Spike match;
                    bool  foundMatch = m_spikes.FindMatch(m_cur, out match);
                    if (!foundMatch)
                    {
                        // Didn't find one, but fire off any candidate that's queued up.
                        FireCandidateGesture();
                    }
                    else
                    {
                        // Found a matching spike, so create a gesture from the two spikes.
                        Gesture gest;
                        gest.m_dirDevice   = m_cur.m_dir - match.m_dir;
                        gest.m_dirDevice.z = -gest.m_dirDevice.z;                                               // Accel/gyro z-axis points towards m_user, which is backwards.
                        gest.m_dirWorldXZ  = m_attitudeXZ * gest.m_dirDevice;
                        gest.m_dirWorldXYZ = m_attitudeXYZ * gest.m_dirDevice;
                        gest.m_elapsed     = m_cur.m_endTime - match.m_endTime;

                        if (IgnoreWindUps)
                        {
                            // If we're ignoring wind-ups, we first save the gesture as a candidate (if we don't have one already).
                            if (!m_hasGestureCandidate)
                            {
                                if (Time.time - m_lastGestureTime <= PostGestureCooldown)
                                {
                                    // Ignore gesture since it's too soon since last gesture.
                                }
                                else
                                {
                                    m_hasGestureCandidate = true;
                                    m_gestureCandidate    = gest;
                                }
                            }
                            else
                            {
                                // If we do already have a candidate, fire off whichever one is bigger.
                                m_lastGestureTime     = Time.time;
                                m_hasGestureCandidate = false;

                                // Take whichever one is bigger.
                                if (gest.Magnitude >= m_gestureCandidate.Magnitude)
                                {
                                    HandleGesture(gest);
                                }
                                else
                                {
                                    HandleGesture(m_gestureCandidate);
                                }
                            }
                        }
                        else
                        {
                            m_lastGestureTime = Time.time;
                            HandleGesture(gest);
                        }
                    }
                }

                if (aboveThreshold)
                {
                    m_cur.Begin(m_user);
                }
                else
                {
                    m_cur.Clear();
                }
            }

            m_recording.Record(m_user, dot, spikeMag, m_attitudeXYZ);
            m_spikes.PurgeOld();
        }
 /// <summary>
 /// Add a new Gesture to be tracked
 /// </summary>
 /// <param name="name">The name to reference the Gesture by</param>
 /// <param name="g">The instance of a Gesture to track</param>
 public void AddGesture(string name, Gesture g)
 {
     gestureMap.Add(name, g);
 }