leapMotionManager.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _LEAPMOTIONMANAGER_H_
  23. #define _LEAPMOTIONMANAGER_H_
  24. #ifndef _UTILITY_H_
  25. #include "2d/core/Utility.h"
  26. #endif
  27. #ifndef _LEAPMOTIONCONSTANTS_H_
  28. #include "input/leapMotion/leapMotionConstants.h"
  29. #endif
  30. #ifndef __Leap_h__
  31. #include "Leap.h"
  32. #endif
  33. class LeapMotionManager
  34. {
  35. protected:
  36. bool mEnabled;
  37. F32 mMinCircleProgress;
  38. struct FingerEvent
  39. {
  40. S32 id;
  41. F32 x;
  42. F32 y;
  43. F32 z;
  44. FingerEvent( S32 ID, F32 X, F32 Y, F32 Z)
  45. {
  46. id = ID;
  47. x = X;
  48. y = Y;
  49. z = Z;
  50. }
  51. };
  52. class MotionListener : public Leap::Listener
  53. {
  54. public:
  55. MotionListener() {}
  56. virtual ~MotionListener() {}
  57. virtual void onConnect (const Leap::Controller& controller);
  58. virtual void onDisconnect (const Leap::Controller& controller);
  59. virtual void onInit(const Leap::Controller& controller);
  60. virtual void onFrame(const Leap::Controller& controller);
  61. virtual void onFocusGained(const Leap::Controller& controller);
  62. virtual void onFocusLost(const Leap::Controller& controller);
  63. };
  64. /// The connection to the Leap Motion
  65. Leap::Controller* mController;
  66. /// Our Leap Motion listener class
  67. MotionListener* mListener;
  68. /// Used with the LM listener object
  69. void* mActiveMutex;
  70. /// Is the Leap Motion active
  71. bool mActive;
  72. /// Is the Manager acting like a mouse
  73. bool mMouseControl;
  74. /// Last stored frame
  75. Leap::Frame mLastFrame;
  76. public:
  77. static bool smEnableDevice;
  78. // Indicates that events for each hand and pointable will be created
  79. static bool smGenerateIndividualEvents;
  80. // Indicates that we track hand IDs and will ensure that the same hand
  81. // will remain at the same index between frames.
  82. static bool smKeepHandIndexPersistent;
  83. // Indicates that we track pointable IDs and will ensure that the same
  84. // pointable will remain at the same index between frames.
  85. static bool smKeepPointableIndexPersistent;
  86. // Broadcast single hand rotation as axis
  87. static bool smGenerateSingleHandRotationAsAxisEvents;
  88. // The maximum hand angle when used as an axis event
  89. // as measured from a vector pointing straight up (in degrees)
  90. static F32 smMaximumHandAxisAngle;
  91. // Indicates that a whole frame event should be generated and frames
  92. // should be buffered.
  93. static bool smGenerateWholeFrameEvents;
  94. // Frame action codes
  95. static U32 LM_FRAMEVALIDDATA; // SI_BUTTON
  96. // Hand action codes
  97. static U32 LM_HAND[LeapMotionConstants::MaxHands]; // SI_POS
  98. //static U32 LM_HANDROT[LeapMotionConstants::MaxHands]; // SI_ROT
  99. static U32 LM_HANDAXISX; // SI_AXIS
  100. static U32 LM_HANDAXISY;
  101. // Pointables action codes
  102. static U32 LM_HANDPOINTABLE[LeapMotionConstants::MaxHands][LeapMotionConstants::MaxPointablesPerHand]; // SI_POS
  103. static U32 LM_HANDPOINTABLEROT[LeapMotionConstants::MaxHands][LeapMotionConstants::MaxPointablesPerHand]; // SI_ROT
  104. // Whole frame
  105. static U32 LM_FRAME; // SI_INT
  106. public:
  107. LeapMotionManager();
  108. ~LeapMotionManager();
  109. static void staticInit();
  110. void enable(bool enabledState);
  111. void disable();
  112. bool getEnabled() { return mEnabled; }
  113. bool getActive();
  114. void setActive(bool enabledState);
  115. void toggleMouseControl(bool enabledState);
  116. bool getMouseControlToggle();
  117. bool setMinCircleProgress(const F32 value);
  118. F32 getMinCircleProgress() { return mMinCircleProgress; }
  119. bool configureLeapGesture(const char* configString, const F32 value);
  120. void process(const Leap::Controller& controller);
  121. void processHand(const Leap::Hand& hand, S32 id);
  122. void processHandPointables(const Leap::PointableList& pointables);
  123. void processGestures(const Leap::GestureList& gestures);
  124. void generateMouseEvent(const Leap::Controller& controller);
  125. Vector2 getPointFromProjection(Point3F position);
  126. Vector2 getPointFromIntersection(S32 pointableID);
  127. };
  128. static LeapMotionManager* gLeapMotionManager;
  129. #endif