[28897] | 1 | // -----------------------------------------------------------------------
|
---|
| 2 | // <copyright file="InteractionStreamMessage.cs" company="Microsoft">
|
---|
| 3 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
| 4 | // </copyright>
|
---|
| 5 | // -----------------------------------------------------------------------
|
---|
| 6 |
|
---|
| 7 | namespace Microsoft.Samples.Kinect.Webserver.Sensor.Serialization
|
---|
| 8 | {
|
---|
| 9 | using System;
|
---|
| 10 | using System.Collections.Generic;
|
---|
| 11 | using System.Diagnostics.CodeAnalysis;
|
---|
| 12 |
|
---|
| 13 | using Microsoft.Kinect.Toolkit.Interaction;
|
---|
| 14 |
|
---|
| 15 | /// <summary>
|
---|
| 16 | /// Serializable representation of an interaction stream message to send to client.
|
---|
| 17 | /// </summary>
|
---|
| 18 | [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter",
|
---|
| 19 | Justification = "Lower case names allowed for JSON serialization.")]
|
---|
| 20 | public class InteractionStreamMessage : StreamMessage
|
---|
| 21 | {
|
---|
| 22 | /// <summary>
|
---|
| 23 | /// Maximum number of hand pointers we can track.
|
---|
| 24 | /// </summary>
|
---|
| 25 | private const int MaximumHandPointers = 4; // 2 hands for 2 tracked users
|
---|
| 26 |
|
---|
| 27 | /// <summary>
|
---|
| 28 | /// Internal array of MessageHandPointer objects, allocated once and then reused.
|
---|
| 29 | /// </summary>
|
---|
| 30 | private readonly MessageHandPointer[] internalHandPointers;
|
---|
| 31 |
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Initializes a new instance of the <see cref="InteractionStreamMessage"/> class.
|
---|
| 34 | /// </summary>
|
---|
| 35 | public InteractionStreamMessage()
|
---|
| 36 | {
|
---|
| 37 | this.internalHandPointers = new MessageHandPointer[MaximumHandPointers];
|
---|
| 38 |
|
---|
| 39 | for (int i = 0; i < this.internalHandPointers.Length; ++i)
|
---|
| 40 | {
|
---|
| 41 | this.internalHandPointers[i] = new MessageHandPointer();
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /// <summary>
|
---|
| 46 | /// Serializable hand pointer array.
|
---|
| 47 | /// </summary>
|
---|
| 48 | [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "hand", Justification = "Lower case names allowed for JSON serialization.")]
|
---|
| 49 | [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Array properties allowed for JSON serialization.")]
|
---|
| 50 | public MessageHandPointer[] handPointers { get; set; }
|
---|
| 51 |
|
---|
| 52 | /// <summary>
|
---|
| 53 | /// Update hand pointers from specified user info data.
|
---|
| 54 | /// </summary>
|
---|
| 55 | /// <param name="userInfoData">
|
---|
| 56 | /// Enumeration of UserInfo structures.
|
---|
| 57 | /// </param>
|
---|
| 58 | /// <param name="primaryUserTrackingId">
|
---|
| 59 | /// Tracking ID of primary user.
|
---|
| 60 | /// </param>
|
---|
| 61 | public void UpdateHandPointers(IEnumerable<UserInfo> userInfoData, int primaryUserTrackingId)
|
---|
| 62 | {
|
---|
| 63 | int handPointerIndex = 0;
|
---|
| 64 |
|
---|
| 65 | if (userInfoData == null)
|
---|
| 66 | {
|
---|
| 67 | throw new ArgumentNullException("userInfoData");
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | foreach (var user in userInfoData)
|
---|
| 71 | {
|
---|
| 72 | foreach (var handPointer in user.HandPointers)
|
---|
| 73 | {
|
---|
| 74 | if (user.SkeletonTrackingId == SharedConstants.InvalidUserTrackingId)
|
---|
| 75 | {
|
---|
| 76 | continue;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | var messageHandPointer = this.internalHandPointers[handPointerIndex];
|
---|
| 80 | messageHandPointer.trackingId = user.SkeletonTrackingId;
|
---|
| 81 | messageHandPointer.handType = handPointer.HandType.ToString();
|
---|
| 82 | messageHandPointer.isTracked = handPointer.IsTracked;
|
---|
| 83 | messageHandPointer.isActive = handPointer.IsActive;
|
---|
| 84 | messageHandPointer.isInteractive = handPointer.IsInteractive;
|
---|
| 85 | messageHandPointer.isPressed = handPointer.IsPressed;
|
---|
| 86 | messageHandPointer.isPrimaryHandOfUser = handPointer.IsPrimaryForUser;
|
---|
| 87 | messageHandPointer.isPrimaryUser = primaryUserTrackingId == user.SkeletonTrackingId;
|
---|
| 88 | messageHandPointer.handEventType = handPointer.HandEventType.ToString();
|
---|
| 89 | messageHandPointer.x = handPointer.X;
|
---|
| 90 | messageHandPointer.y = handPointer.Y;
|
---|
| 91 | messageHandPointer.pressExtent = handPointer.PressExtent;
|
---|
| 92 | messageHandPointer.rawX = handPointer.RawX;
|
---|
| 93 | messageHandPointer.rawY = handPointer.RawY;
|
---|
| 94 | messageHandPointer.rawZ = handPointer.RawZ;
|
---|
| 95 |
|
---|
| 96 | if (++handPointerIndex >= MaximumHandPointers)
|
---|
| 97 | {
|
---|
| 98 | break;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if (handPointerIndex >= MaximumHandPointers)
|
---|
| 103 | {
|
---|
| 104 | break;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | if ((this.handPointers == null) || (this.handPointers.Length != handPointerIndex))
|
---|
| 109 | {
|
---|
| 110 | this.handPointers = new MessageHandPointer[handPointerIndex];
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | for (handPointerIndex = 0; handPointerIndex < this.handPointers.Length; ++handPointerIndex)
|
---|
| 114 | {
|
---|
| 115 | this.handPointers[handPointerIndex] = this.internalHandPointers[handPointerIndex];
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | } |
---|