Engine.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using AOT;
  4. namespace TouchlessA3D {
  5. /**
  6. * An engine for processing frames.
  7. *
  8. */
  9. public class Engine {
  10. private GCHandle m_handleToEventHandler;
  11. private delegate void CallbackFromNative (IntPtr context, IntPtr ta3d_event_t);
  12. private CallbackFromNative m_CallbackFromNative;
  13. private IntPtr m_ta3d_engine_t;
  14. public Engine (string unique_id, string persistent_storage_path, ICalibration calibration, EventHandler<GestureEvent> ta3dEventHandler) {
  15. m_handleToEventHandler = GCHandle.Alloc (ta3dEventHandler);
  16. IntPtr nativeCalibration = calibration == null? IntPtr.Zero : calibration.getNativeCalibration ();
  17. m_CallbackFromNative = new CallbackFromNative (ta3d_callback_implementation);
  18. m_ta3d_engine_t = NativeCalls.ta3d_engine_acquire (unique_id, persistent_storage_path, nativeCalibration,
  19. Marshal.GetFunctionPointerForDelegate (m_CallbackFromNative), GCHandle.ToIntPtr (m_handleToEventHandler));
  20. }
  21. ~Engine () {
  22. m_handleToEventHandler.Free ();
  23. NativeCalls.ta3d_engine_release (m_ta3d_engine_t);
  24. }
  25. /**
  26. * Analyzes a frame with respect to touchless interaction.
  27. * The ta3dEventhandler is notified if any touchless interaction is detected.
  28. * @param frame A frame to analyze.
  29. */
  30. [MonoPInvokeCallback (typeof (CallbackFromNative))]
  31. private static void ta3d_callback_implementation (IntPtr context, IntPtr ta3d_event_t) {
  32. GestureEvent args = new GestureEvent (ta3d_event_t);
  33. GCHandle newHandleToEventHandler = GCHandle.FromIntPtr (context); //same as m_handleToEventHandler, but static method
  34. var ta3dEventHandler = (EventHandler<GestureEvent>) (newHandleToEventHandler.Target);
  35. if (null != ta3dEventHandler) {
  36. ta3dEventHandler (ta3dEventHandler, args);
  37. }
  38. }
  39. public void handleFrame (IFrame frame) {
  40. NativeCalls.ta3d_engine_handle_frame (m_ta3d_engine_t, frame.getNativeFrame ());
  41. }
  42. }
  43. }