protected virtual void SkeletonDataChange(SkeletonDataChangeEventArgs e) { if (SkeletonDataChanged != null) { SkeletonDataChanged(this, e); } }
// Event Handler for SkeletonFrameReady events // (A new frame of SkeletonStream data is available) private void KinectSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e) { // Initialize/empty the skeletonStreamData array this.skeletonStreamData = new Skeleton[0]; // Get the current SkeletonFrame and copy out the data using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) { if (skeletonFrame != null) { try { // Get the Skeleton data from the SkeletonFrame skeletonStreamData = new Skeleton[skeletonFrame.SkeletonArrayLength]; skeletonFrame.CopySkeletonDataTo(skeletonStreamData); // Dispatch the SkeletonDataChange event SkeletonDataChangeEventArgs s = new SkeletonDataChangeEventArgs(skeletonStreamData); SkeletonDataChange(s); } catch (NullReferenceException ex) { Console.WriteLine(ex.TargetSite + " - " + ex.Message); } } } // Create the Skeleton image output using (DrawingContext dc = this.drawingGroup.Open()) { // Draw a black background the size of our render dc.DrawRectangle(backgroundBrush, null, new Rect(0, 0, RenderWidth, RenderHeight)); //Draw each Skeleton if (skeletonStreamData.Length != 0) { foreach (Skeleton skeleton in skeletonStreamData) { // TO-DO: Render clipped edges if (skeleton.TrackingState == SkeletonTrackingState.Tracked) { DrawSkeletonBonesAndJoints(dc, skeleton.Joints); } else if (skeleton.TrackingState == SkeletonTrackingState.PositionOnly) { DrawSkeletonPosition(dc, skeleton.Position); } } } // Prevent any drawing outside the render area this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0, 0, RenderWidth, RenderHeight)); } }
// Event handler for changes in Skeleton stream data // Used as a secondary "update" method for setting the position of kinect-controlled paddles private void SkeletonDataChanged(object sender, SkeletonDataChangeEventArgs e) { // Don't update if the game hasn't started if (gameThread == null) return; // Determine which skeletons are on which side Skeleton right = null; Skeleton left = null; // Loop through all available skeletons for (int i = 0; i < e.skeletons.Length; i++) { // Grab the current skeleton Skeleton skel = e.skeletons[i]; // If we're tracked figure out what side of the screen we're on if (skel.TrackingState == SkeletonTrackingState.Tracked) { Point position = helper.SkeletonPointToScreen(skel.Joints[JointType.ShoulderCenter].Position); // If the skeleton is the first on the left side of the screen, it is the left skeleton if ((position.X > 0 && position.X <= GameCanvas.Width / 2) && left == null) left = skel; // If the skeleton is the first on the right side of the screen, it is the right skeleton else if ((position.X > GameCanvas.Width / 2 && position.X < GameCanvas.Width) && right == null) right = skel; } // If both skeletons have been found, no need to keep looking if (left != null & right != null) break; } // If the left skeleton wasn't found, hide the marker if (left == null) markOne.Opacity = 0; // If the left skeleton was found, update some values else { // Get the locations of the skeleton's head and hand Point playerOneHand = helper.SkeletonPointToScreen(left.Joints[playerOneHandedness].Position); Point playerOneHead = helper.SkeletonPointToScreen(left.Joints[JointType.Head].Position); // Save the last position of player one's paddle playerOnePosPast = playerOnePos; // Update the position of player one's paddle playerOnePos.Y = playerOneHand.Y - playerOne.Height / 2; // Show and move the player's marker markOne.Opacity = 1; markOnePos.X = playerOneHead.X - markOne.Width / 2; markOnePos.Y = playerOneHead.Y - markOne.Height / 2; } // If the right skeleton wasn't found, hide the marker if (right == null) markTwo.Opacity = 0; // If the right skeleton was found, update some values else { // Get the locations of the skeleton's head and hand Point playerTwoHand = helper.SkeletonPointToScreen(right.Joints[playerTwoHandedness].Position); Point playerTwoHead = helper.SkeletonPointToScreen(right.Joints[JointType.Head].Position); // Save the last position of player two's paddle playerTwoPosPast = playerTwoPos; // Update the position of player one's paddle playerTwoPos.Y = playerTwoHand.Y - playerTwo.Height / 2; // Show and move the player's marker markTwo.Opacity = 1; markTwoPos.X = playerTwoHead.X - markTwo.Width / 2; markTwoPos.Y = playerTwoHead.Y - markTwo.Height / 2; } // Draw the markers separately from the rest of the game // TO-DO: Move this somewhere better this.Dispatcher.Invoke((Action)(() => { // Move the markers to their new positions Canvas.SetLeft(markOne, markOnePos.X); Canvas.SetTop(markOne, markOnePos.Y); Canvas.SetLeft(markTwo, markTwoPos.X); Canvas.SetTop(markTwo, markTwoPos.Y); })); }