leapMotionFrameStore.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "platform/input/leapMotion/leapMotionFrameStore.h"
  23. #include "platform/input/leapMotion/leapMotionFrame.h"
  24. #include "core/module.h"
  25. #include "console/simSet.h"
  26. #include "console/consoleTypes.h"
  27. MODULE_BEGIN( LeapMotionFrameStore )
  28. MODULE_INIT_AFTER( LeapMotionDevice )
  29. MODULE_INIT_AFTER( Sim )
  30. MODULE_SHUTDOWN_BEFORE( Sim )
  31. MODULE_SHUTDOWN_BEFORE( LeapMotionDevice )
  32. MODULE_INIT
  33. {
  34. LeapMotionFrameStore::staticInit();
  35. ManagedSingleton< LeapMotionFrameStore >::createSingleton();
  36. }
  37. MODULE_SHUTDOWN
  38. {
  39. ManagedSingleton< LeapMotionFrameStore >::deleteSingleton();
  40. }
  41. MODULE_END;
  42. S32 LeapMotionFrameStore::smMaximumFramesStored = 30;
  43. SimGroup* LeapMotionFrameStore::smFrameGroup = NULL;
  44. LeapMotionFrameStore::LeapMotionFrameStore()
  45. {
  46. // Set up the SimGroup to store our frames
  47. smFrameGroup = new SimGroup();
  48. smFrameGroup->registerObject("LeapMotionFrameGroup");
  49. smFrameGroup->setNameChangeAllowed(false);
  50. Sim::getRootGroup()->addObject(smFrameGroup);
  51. }
  52. LeapMotionFrameStore::~LeapMotionFrameStore()
  53. {
  54. if(smFrameGroup)
  55. {
  56. smFrameGroup->deleteObject();
  57. smFrameGroup = NULL;
  58. }
  59. }
  60. void LeapMotionFrameStore::staticInit()
  61. {
  62. Con::addVariable("LeapMotion::MaximumFramesStored", TypeS32, &smMaximumFramesStored,
  63. "@brief The maximum number of frames to keep when $LeapMotion::GenerateWholeFrameEvents is true.\n\n"
  64. "@ingroup Game");
  65. }
  66. S32 LeapMotionFrameStore::generateNewFrame(const Leap::Frame& frame, const F32& maxHandAxisRadius)
  67. {
  68. // Make sure our group has been created
  69. if(!smFrameGroup)
  70. return 0;
  71. // Either create a new frame object or pull one off the end
  72. S32 frameID = 0;
  73. if(smFrameGroup->size() >= smMaximumFramesStored)
  74. {
  75. // Make the last frame the first and update
  76. LeapMotionFrame* frameObj = static_cast<LeapMotionFrame*>(smFrameGroup->last());
  77. smFrameGroup->bringObjectToFront(frameObj);
  78. frameObj->copyFromFrame(frame, maxHandAxisRadius);
  79. frameID = frameObj->getId();
  80. }
  81. else
  82. {
  83. // Create a new frame and add it to the front of the list
  84. LeapMotionFrame* frameObj = new LeapMotionFrame();
  85. frameObj->registerObject();
  86. smFrameGroup->addObject(frameObj);
  87. smFrameGroup->bringObjectToFront(frameObj);
  88. frameObj->copyFromFrame(frame, maxHandAxisRadius);
  89. frameID = frameObj->getId();
  90. }
  91. return frameID;
  92. }