public void Reset()
 {
     using (var section = new EventQueueSection())
     {
         activityMeter.Clear();
         TrackedUserTrackingIds.Clear();
         PrimaryUserTrackingId = InvalidUserTrackingId;
         SetCandidateUserTrackingId(InvalidUserTrackingId, section);
         SetEngagedUserTrackingId(InvalidUserTrackingId, section);
         SendTrackedUsersChanged(section);
     }
 }
        private void SetCandidateUserTrackingId(int newId, EventQueueSection section)
        {
            int oldId = this.CandidateUserTrackingId;
            this.CandidateUserTrackingId = newId;

            if (oldId != newId)
            {
                section.Enqueue(
                    () =>
                    {
                        if (this.CandidateUserChanged != null)
                        {
                            var args = new UserTrackingIdChangedEventArgs(oldId, newId);
                            this.CandidateUserChanged(this, args);
                        }
                    });
            }
        }
 private void SendTrackedUsersChanged(EventQueueSection section)
 {
     section.Enqueue(
         () =>
         {
             if (this.TrackedUsersChanged != null)
             {
                 this.TrackedUsersChanged(this, EventArgs.Empty);
             }
         });
 }
        internal void SetPrimaryUserTrackingId(int newId, EventQueueSection section)
        {
            int oldId = this.PrimaryUserTrackingId;
            this.PrimaryUserTrackingId = newId;

            if (oldId != newId)
            {
                section.Enqueue(
                    () =>
                    {
                        if (this.PrimaryUserChanged != null)
                        {
                            var args = new UserTrackingIdChangedEventArgs(oldId, newId);
                            this.PrimaryUserChanged(this, args);
                        }
                    });

                // If the new primary user is the same as the engaged user, then there is no candidate user.
                // Otherwise, we have a new candidate user as long as the new primary user is a valid user.
                this.SetCandidateUserTrackingId(
                    (this.EngagedUserTrackingId != InvalidUserTrackingId) && (this.EngagedUserTrackingId == newId)
                        ? InvalidUserTrackingId
                        : newId,
                    section);
            }
        }
        public bool ConfirmCandidateEngagement(int candidateTrackingId)
        {
            bool isConfirmed = false;

            if ((candidateTrackingId != InvalidUserTrackingId) && (candidateTrackingId == this.CandidateUserTrackingId))
            {
                using (var section = new EventQueueSection())
                {
                    this.SetCandidateUserTrackingId(InvalidUserTrackingId, section);
                    this.SetEngagedUserTrackingId(candidateTrackingId, section);
                }

                isConfirmed = true;
            }

            return isConfirmed;
        }
        public void UpdateHandPointers(IEnumerable<HandPointer> trackedHandPointers)
        {
            bool foundEngagedUser = false;
            bool foundCandidateUser = false;
            int primaryUserTrackingId = InvalidUserTrackingId;

            using (var section = new EventQueueSection())
            {
                this.TrackedUserTrackingIds.Clear();

                foreach (var handPointer in trackedHandPointers)
                {
                    if (handPointer.IsTracked && (handPointer.TrackingId != InvalidUserTrackingId))
                    {
                        // Only consider valid user tracking ids
                        this.TrackedUserTrackingIds.Add(handPointer.TrackingId);

                        if (this.EngagedUserTrackingId == handPointer.TrackingId)
                        {
                            foundEngagedUser = true;
                        }

                        if (this.CandidateUserTrackingId == handPointer.TrackingId)
                        {
                            foundCandidateUser = true;
                        }

                        if (handPointer.IsPrimaryUser)
                        {
                            primaryUserTrackingId = handPointer.TrackingId;
                        }
                    }
                }

                this.SendTrackedUsersChanged(section);

                // If engaged user was not found in list of candidate users, engaged user has become invalid.
                if (!foundEngagedUser)
                {
                    this.SetEngagedUserTrackingId(InvalidUserTrackingId, section);
                }

                // If candidate user was not found in list of candidate users, candidate user has become invalid.
                if (!foundCandidateUser)
                {
                    this.SetCandidateUserTrackingId(InvalidUserTrackingId, section);
                }

                this.SetPrimaryUserTrackingId(primaryUserTrackingId, section);
            }
        }