void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) { SkeletonFrame skeletonFrame = e.SkeletonFrame; foreach (SkeletonData data in skeletonFrame.Skeletons) { if (SkeletonTrackingState.Tracked == data.TrackingState) { Joint hand = data.Joints[JointID.HandRight]; // Adjust the coordinates so we don't have to stretch to the // extremes of the Kinect's view in order to reach the extreme // menu items. int xpos = (int)(2.0 * ((hand.Position.X + 1.0) * 400.0 - 200.0)); if (xpos > 780) { xpos = 780; } else if (xpos < 20) { xpos = 20; } int ypos = (int)(600.0 - 2.0 * ((hand.Position.Y + 1.0) * 300.0 - 150.0)); if (ypos > 580) { ypos = 580; } else if (ypos < 20) { ypos = 20; } Canvas.SetLeft(pointer, xpos - 20); Canvas.SetTop(pointer, ypos - 20); Point handPoint = new Point(xpos, ypos); TouchButton hit = getHit(handPoint); if (hit != null) { TouchButton hitTB = (TouchButton)hit; if (!Object.ReferenceEquals(hitTB, currentlySelected)) { if (currentlySelected != null) { currentlySelected.RaiseEvent(new RoutedEventArgs(TouchButton.HandLeaveEvent)); } hitTB.RaiseEvent(new RoutedEventArgs(TouchButton.HandEnterEvent)); currentlySelected = hitTB; } } else { if (currentlySelected != null) { currentlySelected.RaiseEvent(new RoutedEventArgs(TouchButton.HandLeaveEvent)); } currentlySelected = null; } } } }
// Draw a menu given by menuName private void drawMenu(string menuName) { Menu menuLoaded = new Menu(menuName); if (menuLoaded.isLeaf()) { TextBlock successText = new TextBlock(); successText.Text = "Successfully selected " + menuName; successText.FontSize = 36; successText.MaxWidth = 800; Canvas.SetTop(successText, 250); canvas.Children.Add(successText); DispatcherTimer dispTimer = new DispatcherTimer(); dispTimer.Tick += new EventHandler(dispTimer_Tick); dispTimer.Interval = new TimeSpan(0, 0, 2); dispTimer.Start(); } else { foreach (MenuItem mi in menuLoaded.menuItems) { TouchButton tb = new TouchButton(mi); Canvas.SetLeft(tb, mi.getUpperLeft().X); Canvas.SetTop(tb, mi.getUpperLeft().Y); canvas.Children.Add(tb); } } return; }