BodyFrameSource.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using RootSystem = System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace Windows.Kinect
  5. {
  6. //
  7. // Windows.Kinect.BodyFrameSource
  8. //
  9. public sealed partial class BodyFrameSource : Helper.INativeWrapper
  10. {
  11. internal RootSystem.IntPtr _pNative;
  12. RootSystem.IntPtr Helper.INativeWrapper.nativePtr { get { return _pNative; } }
  13. // Constructors and Finalizers
  14. internal BodyFrameSource(RootSystem.IntPtr pNative)
  15. {
  16. _pNative = pNative;
  17. Windows_Kinect_BodyFrameSource_AddRefObject(ref _pNative);
  18. }
  19. ~BodyFrameSource()
  20. {
  21. Dispose(false);
  22. }
  23. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  24. private static extern void Windows_Kinect_BodyFrameSource_ReleaseObject(ref RootSystem.IntPtr pNative);
  25. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  26. private static extern void Windows_Kinect_BodyFrameSource_AddRefObject(ref RootSystem.IntPtr pNative);
  27. private void Dispose(bool disposing)
  28. {
  29. if (_pNative == RootSystem.IntPtr.Zero)
  30. {
  31. return;
  32. }
  33. __EventCleanup();
  34. Helper.NativeObjectCache.RemoveObject<BodyFrameSource>(_pNative);
  35. Windows_Kinect_BodyFrameSource_ReleaseObject(ref _pNative);
  36. _pNative = RootSystem.IntPtr.Zero;
  37. }
  38. // Public Properties
  39. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  40. private static extern int Windows_Kinect_BodyFrameSource_get_BodyCount(RootSystem.IntPtr pNative);
  41. public int BodyCount
  42. {
  43. get
  44. {
  45. if (_pNative == RootSystem.IntPtr.Zero)
  46. {
  47. throw new RootSystem.ObjectDisposedException("BodyFrameSource");
  48. }
  49. return Windows_Kinect_BodyFrameSource_get_BodyCount(_pNative);
  50. }
  51. }
  52. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  53. private static extern bool Windows_Kinect_BodyFrameSource_get_IsActive(RootSystem.IntPtr pNative);
  54. public bool IsActive
  55. {
  56. get
  57. {
  58. if (_pNative == RootSystem.IntPtr.Zero)
  59. {
  60. throw new RootSystem.ObjectDisposedException("BodyFrameSource");
  61. }
  62. return Windows_Kinect_BodyFrameSource_get_IsActive(_pNative);
  63. }
  64. }
  65. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  66. private static extern RootSystem.IntPtr Windows_Kinect_BodyFrameSource_get_KinectSensor(RootSystem.IntPtr pNative);
  67. public Windows.Kinect.KinectSensor KinectSensor
  68. {
  69. get
  70. {
  71. if (_pNative == RootSystem.IntPtr.Zero)
  72. {
  73. throw new RootSystem.ObjectDisposedException("BodyFrameSource");
  74. }
  75. RootSystem.IntPtr objectPointer = Windows_Kinect_BodyFrameSource_get_KinectSensor(_pNative);
  76. Helper.ExceptionHelper.CheckLastError();
  77. if (objectPointer == RootSystem.IntPtr.Zero)
  78. {
  79. return null;
  80. }
  81. return Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.KinectSensor>(objectPointer, n => new Windows.Kinect.KinectSensor(n));
  82. }
  83. }
  84. // Events
  85. private static RootSystem.Runtime.InteropServices.GCHandle _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle;
  86. [RootSystem.Runtime.InteropServices.UnmanagedFunctionPointer(RootSystem.Runtime.InteropServices.CallingConvention.Cdecl)]
  87. private delegate void _Windows_Kinect_FrameCapturedEventArgs_Delegate(RootSystem.IntPtr args, RootSystem.IntPtr pNative);
  88. private static Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>>> Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks = new Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>>>();
  89. [AOT.MonoPInvokeCallbackAttribute(typeof(_Windows_Kinect_FrameCapturedEventArgs_Delegate))]
  90. private static void Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler(RootSystem.IntPtr result, RootSystem.IntPtr pNative)
  91. {
  92. List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>> callbackList = null;
  93. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryGetValue(pNative, out callbackList);
  94. lock(callbackList)
  95. {
  96. var objThis = Helper.NativeObjectCache.GetObject<BodyFrameSource>(pNative);
  97. var args = new Windows.Kinect.FrameCapturedEventArgs(result);
  98. foreach(var func in callbackList)
  99. {
  100. Helper.EventPump.Instance.Enqueue(() => { try { func(objThis, args); } catch { } });
  101. }
  102. }
  103. }
  104. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  105. private static extern void Windows_Kinect_BodyFrameSource_add_FrameCaptured(RootSystem.IntPtr pNative, _Windows_Kinect_FrameCapturedEventArgs_Delegate eventCallback, bool unsubscribe);
  106. public event RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs> FrameCaptured
  107. {
  108. add
  109. {
  110. Helper.EventPump.EnsureInitialized();
  111. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  112. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  113. lock (callbackList)
  114. {
  115. callbackList.Add(value);
  116. if(callbackList.Count == 1)
  117. {
  118. var del = new _Windows_Kinect_FrameCapturedEventArgs_Delegate(Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler);
  119. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle = RootSystem.Runtime.InteropServices.GCHandle.Alloc(del);
  120. Windows_Kinect_BodyFrameSource_add_FrameCaptured(_pNative, del, false);
  121. }
  122. }
  123. }
  124. remove
  125. {
  126. if (_pNative == RootSystem.IntPtr.Zero)
  127. {
  128. return;
  129. }
  130. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  131. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  132. lock (callbackList)
  133. {
  134. callbackList.Remove(value);
  135. if(callbackList.Count == 0)
  136. {
  137. Windows_Kinect_BodyFrameSource_add_FrameCaptured(_pNative, Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler, true);
  138. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle.Free();
  139. }
  140. }
  141. }
  142. }
  143. private static RootSystem.Runtime.InteropServices.GCHandle _Windows_Data_PropertyChangedEventArgs_Delegate_Handle;
  144. [RootSystem.Runtime.InteropServices.UnmanagedFunctionPointer(RootSystem.Runtime.InteropServices.CallingConvention.Cdecl)]
  145. private delegate void _Windows_Data_PropertyChangedEventArgs_Delegate(RootSystem.IntPtr args, RootSystem.IntPtr pNative);
  146. private static Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>>> Windows_Data_PropertyChangedEventArgs_Delegate_callbacks = new Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>>>();
  147. [AOT.MonoPInvokeCallbackAttribute(typeof(_Windows_Data_PropertyChangedEventArgs_Delegate))]
  148. private static void Windows_Data_PropertyChangedEventArgs_Delegate_Handler(RootSystem.IntPtr result, RootSystem.IntPtr pNative)
  149. {
  150. List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>> callbackList = null;
  151. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryGetValue(pNative, out callbackList);
  152. lock(callbackList)
  153. {
  154. var objThis = Helper.NativeObjectCache.GetObject<BodyFrameSource>(pNative);
  155. var args = new Windows.Data.PropertyChangedEventArgs(result);
  156. foreach(var func in callbackList)
  157. {
  158. Helper.EventPump.Instance.Enqueue(() => { try { func(objThis, args); } catch { } });
  159. }
  160. }
  161. }
  162. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  163. private static extern void Windows_Kinect_BodyFrameSource_add_PropertyChanged(RootSystem.IntPtr pNative, _Windows_Data_PropertyChangedEventArgs_Delegate eventCallback, bool unsubscribe);
  164. public event RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs> PropertyChanged
  165. {
  166. add
  167. {
  168. Helper.EventPump.EnsureInitialized();
  169. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  170. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  171. lock (callbackList)
  172. {
  173. callbackList.Add(value);
  174. if(callbackList.Count == 1)
  175. {
  176. var del = new _Windows_Data_PropertyChangedEventArgs_Delegate(Windows_Data_PropertyChangedEventArgs_Delegate_Handler);
  177. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle = RootSystem.Runtime.InteropServices.GCHandle.Alloc(del);
  178. Windows_Kinect_BodyFrameSource_add_PropertyChanged(_pNative, del, false);
  179. }
  180. }
  181. }
  182. remove
  183. {
  184. if (_pNative == RootSystem.IntPtr.Zero)
  185. {
  186. return;
  187. }
  188. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  189. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  190. lock (callbackList)
  191. {
  192. callbackList.Remove(value);
  193. if(callbackList.Count == 0)
  194. {
  195. Windows_Kinect_BodyFrameSource_add_PropertyChanged(_pNative, Windows_Data_PropertyChangedEventArgs_Delegate_Handler, true);
  196. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle.Free();
  197. }
  198. }
  199. }
  200. }
  201. // Public Methods
  202. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  203. private static extern RootSystem.IntPtr Windows_Kinect_BodyFrameSource_OpenReader(RootSystem.IntPtr pNative);
  204. public Windows.Kinect.BodyFrameReader OpenReader()
  205. {
  206. if (_pNative == RootSystem.IntPtr.Zero)
  207. {
  208. throw new RootSystem.ObjectDisposedException("BodyFrameSource");
  209. }
  210. RootSystem.IntPtr objectPointer = Windows_Kinect_BodyFrameSource_OpenReader(_pNative);
  211. Helper.ExceptionHelper.CheckLastError();
  212. if (objectPointer == RootSystem.IntPtr.Zero)
  213. {
  214. return null;
  215. }
  216. return Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.BodyFrameReader>(objectPointer, n => new Windows.Kinect.BodyFrameReader(n));
  217. }
  218. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  219. private static extern void Windows_Kinect_BodyFrameSource_OverrideHandTracking(RootSystem.IntPtr pNative, ulong trackingId);
  220. public void OverrideHandTracking(ulong trackingId)
  221. {
  222. if (_pNative == RootSystem.IntPtr.Zero)
  223. {
  224. throw new RootSystem.ObjectDisposedException("BodyFrameSource");
  225. }
  226. Windows_Kinect_BodyFrameSource_OverrideHandTracking(_pNative, trackingId);
  227. Helper.ExceptionHelper.CheckLastError();
  228. }
  229. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  230. private static extern void Windows_Kinect_BodyFrameSource_OverrideHandTracking_1(RootSystem.IntPtr pNative, ulong oldTrackingId, ulong newTrackingId);
  231. public void OverrideHandTracking(ulong oldTrackingId, ulong newTrackingId)
  232. {
  233. if (_pNative == RootSystem.IntPtr.Zero)
  234. {
  235. throw new RootSystem.ObjectDisposedException("BodyFrameSource");
  236. }
  237. Windows_Kinect_BodyFrameSource_OverrideHandTracking_1(_pNative, oldTrackingId, newTrackingId);
  238. Helper.ExceptionHelper.CheckLastError();
  239. }
  240. private void __EventCleanup()
  241. {
  242. {
  243. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  244. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  245. lock (callbackList)
  246. {
  247. if (callbackList.Count > 0)
  248. {
  249. callbackList.Clear();
  250. if (_pNative != RootSystem.IntPtr.Zero)
  251. {
  252. Windows_Kinect_BodyFrameSource_add_FrameCaptured(_pNative, Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler, true);
  253. }
  254. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle.Free();
  255. }
  256. }
  257. }
  258. {
  259. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  260. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  261. lock (callbackList)
  262. {
  263. if (callbackList.Count > 0)
  264. {
  265. callbackList.Clear();
  266. if (_pNative != RootSystem.IntPtr.Zero)
  267. {
  268. Windows_Kinect_BodyFrameSource_add_PropertyChanged(_pNative, Windows_Data_PropertyChangedEventArgs_Delegate_Handler, true);
  269. }
  270. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle.Free();
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }