Example #1
0
        /// <summary>
        /// Function that takes the state of the core interaction components
        /// and translates it to RoutedEvents.  Also updates hand pointer states.
        /// </summary>
        /// <param name="data">Data directly from the core interaction components</param>
        public void HandleHandPointerData(InteractionFrameData data)
        {
            Debug.Assert(this.IsInInteractionFrame, "Call to HandleHandPointerData made without call to BeginInteractionFrame");

            if (this.isClearRequestPending)
            {
                // We don't care about new hand pointer data if client requested to clear
                // all hand pointers while in the middle of interaction frame processing.
                return;
            }

            var id = new Tuple <int, HandType>(data.TrackingId, data.HandType);

            HandPointer handPointer;

            if (!this.handPointers.TryGetValue(id, out handPointer))
            {
                handPointer = new HandPointer
                {
                    TrackingId  = data.TrackingId,
                    PlayerIndex = data.PlayerIndex,
                    HandType    = data.HandType,
                    Owner       = this,
                };
                this.handPointers[id] = handPointer;
            }

            handPointer.Updated = true;

            handPointer.TimestampOfLastUpdate = data.TimeStampOfLastUpdate;
            handPointer.HandEventType         = data.HandEventType;

            bool pressedChanged = handPointer.IsPressed != data.IsPressed;

            handPointer.IsPressed = data.IsPressed;

            handPointer.IsTracked     = data.IsTracked;
            handPointer.IsActive      = data.IsActive;
            handPointer.IsInteractive = data.IsInteractive;

            bool primaryHandOfPrimaryUserChanged = handPointer.IsPrimaryHandOfPrimaryUser != (data.IsPrimaryHandOfUser && data.IsPrimaryUser);

            handPointer.IsPrimaryHandOfUser = data.IsPrimaryHandOfUser;
            handPointer.IsPrimaryUser       = data.IsPrimaryUser;

            double newX;
            double newY;

            //InteractionZoneDefinition.InteractionZoneToUserInterface(data.X, data.Y, data.Z, this.InteractionRootElement.ActualWidth, this.InteractionRootElement.ActualHeight, mapperParameters, out newX, out newY);
            InteractionZoneDefinition.InteractionZoneToUserInterface(data.X, data.Y, this.InteractionRootElement.ActualWidth, this.InteractionRootElement.ActualHeight, out newX, out newY);
            bool positionChanged = !InteractionZoneDefinition.AreUserInterfaceValuesClose(newX, handPointer.X) ||
                                   !InteractionZoneDefinition.AreUserInterfaceValuesClose(newY, handPointer.Y) ||
                                   !InteractionZoneDefinition.AreUserInterfaceValuesClose(data.Z, handPointer.PressExtent);

            handPointer.X           = newX;
            handPointer.Y           = newY;
            handPointer.PressExtent = data.Z;

            this.HandleHandPointerChanges(handPointer, pressedChanged, positionChanged, primaryHandOfPrimaryUserChanged, false);
        }
Example #2
0
        public InteractionInfo GetInteractionInfoAtLocation(int skeletonTrackingId, InteractionHandType handType, double x, double y)
        {
            var interactionInfo = new InteractionInfo
            {
                IsPressTarget = false,
                IsGripTarget  = false,
            };

            var hitTestPosition = InteractionZoneDefinition.InteractionZoneToElement(x, y, this.InteractionRootElement);

            Func <HandPointer, bool> isTargetCapturedElement =
                handPointer =>
                (handPointer.TrackingId == skeletonTrackingId) &&
                (handPointer.HandType == EnumHelper.ConvertHandType(handType)) &&
                (handPointer.Captured != null);
            var targetHandPointer = this.publicHandPointers.FirstOrDefault(isTargetCapturedElement);
            var targetElement     = targetHandPointer != null ? targetHandPointer.Captured : this.HitTest(hitTestPosition);

            if (targetElement != null)
            {
                // Walk up the tree and try to find a grip target and/or a press target
                for (DependencyObject search = targetElement;
                     search != null && search != this.InteractionRootElement &&
                     (!interactionInfo.IsGripTarget || !interactionInfo.IsPressTarget);
                     search = VisualTreeHelper.GetParent(search))
                {
                    var searchElement = search as FrameworkElement;
                    if (searchElement == null)
                    {
                        // We need ActualWidth and Height which comes
                        // with FrameworkElement
                        continue;
                    }

                    if (!interactionInfo.IsPressTarget)
                    {
                        bool isPressTarget = KinectRegion.GetIsPressTarget(searchElement);
                        if (isPressTarget)
                        {
                            // We found a press target.
                            if (interactionInfo.PressTargetControlId == 0)
                            {
                                interactionInfo.PressTargetControlId = searchElement.GetHashCode();
                            }

                            interactionInfo.IsPressTarget = true;

                            // Apply the margin to the press target point
                            Point pressTargetPoint = ApplyControlPressPointMargin(KinectRegion.GetPressTargetPoint(searchElement));

                            // Convert from interaction zone space into actual control coordinates
                            var elementPressTargetPoint = InteractionZoneDefinition.InteractionZoneToElement(
                                pressTargetPoint.X, pressTargetPoint.Y, searchElement);

                            // Get it into the space of the KinectRegion
                            var regionPressTargetPoint = searchElement.TranslatePoint(elementPressTargetPoint, this.InteractionRootElement);

                            // Now put it into the interaction zone space but now relative to the KinectRegion
                            var interactionPressTargetPoint = InteractionZoneDefinition.ElementToInteractionZone(
                                regionPressTargetPoint.X, regionPressTargetPoint.Y, this.InteractionRootElement);

                            interactionInfo.PressAttractionPointX = interactionPressTargetPoint.X;
                            interactionInfo.PressAttractionPointY = interactionPressTargetPoint.Y;
                        }
                    }

                    if (!interactionInfo.IsGripTarget)
                    {
                        bool isGripTarget = KinectRegion.GetIsGripTarget(searchElement);

                        if (isGripTarget)
                        {
                            // We found a grip target.
                            interactionInfo.IsGripTarget = true;
                        }
                    }
                }
            }

            return(interactionInfo);
        }