razerHydraFrame.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/razerHydra/razerHydraFrame.h"
  23. #include "platform/input/razerHydra/razerHydraUtil.h"
  24. #include "console/engineAPI.h"
  25. #include "math/mAngAxis.h"
  26. #include "math/mTransform.h"
  27. U32 RazerHydraFrame::smNextInternalFrameId = 0;
  28. IMPLEMENT_CONOBJECT(RazerHydraFrame);
  29. RazerHydraFrame::RazerHydraFrame()
  30. {
  31. clear();
  32. }
  33. RazerHydraFrame::~RazerHydraFrame()
  34. {
  35. clear();
  36. for(U32 i=0; i<RazerHydraConstants::MaxControllers; ++i)
  37. {
  38. mControllerData[i].mEnabled = false;
  39. }
  40. }
  41. void RazerHydraFrame::initPersistFields()
  42. {
  43. Parent::initPersistFields();
  44. }
  45. bool RazerHydraFrame::onAdd()
  46. {
  47. if (!Parent::onAdd())
  48. return false;
  49. return true;
  50. }
  51. void RazerHydraFrame::onRemove()
  52. {
  53. Parent::onRemove();
  54. }
  55. void RazerHydraFrame::clear()
  56. {
  57. mFrameValid = false;
  58. }
  59. void RazerHydraFrame::copyFromFrame(const sixenseAllControllerData& frame, const F32& maxAxisRadius)
  60. {
  61. clear();
  62. // Calling this method automatically makes this a valid frame
  63. mFrameValid = true;
  64. mFrameInternalId = smNextInternalFrameId;
  65. ++smNextInternalFrameId;
  66. mFrameSimTime = Sim::getCurrentTime();
  67. mFrameRealTime = Platform::getRealMilliseconds();
  68. // Process the controllers
  69. for(U32 i=0; i<RazerHydraConstants::MaxControllers; ++i)
  70. {
  71. const sixenseControllerData& controller = frame.controllers[i];
  72. ControllerData& data = mControllerData[i];
  73. // General controller data
  74. data.mEnabled = controller.enabled;
  75. data.mIsDocked = controller.is_docked;
  76. data.mSequenceNum = controller.sequence_number;
  77. // Controller position
  78. RazerHydraUtil::convertPosition(controller.pos, data.mRawPos);
  79. data.mPos.x = (S32)mFloor(data.mRawPos.x);
  80. data.mPos.y = (S32)mFloor(data.mRawPos.y);
  81. data.mPos.z = (S32)mFloor(data.mRawPos.z);
  82. // Controller rotation
  83. RazerHydraUtil::convertRotation(controller.rot_mat, data.mRot);
  84. data.mRotQuat.set(data.mRot);
  85. // Controller as axis rotation
  86. RazerHydraUtil::calculateAxisRotation(data.mRot, maxAxisRadius, data.mRotAxis);
  87. // Controller thumb stick
  88. data.mThumbStick.x = controller.joystick_x;
  89. data.mThumbStick.y = controller.joystick_y;
  90. // Trigger
  91. data.mTrigger = controller.trigger;
  92. // Controller buttons
  93. data.mShoulder = controller.buttons & SIXENSE_BUTTON_BUMPER;
  94. data.mThumb = controller.buttons & SIXENSE_BUTTON_JOYSTICK;
  95. data.mStart = controller.buttons & SIXENSE_BUTTON_START;
  96. data.mButton1 = controller.buttons & SIXENSE_BUTTON_1;
  97. data.mButton2 = controller.buttons & SIXENSE_BUTTON_2;
  98. data.mButton3 = controller.buttons & SIXENSE_BUTTON_3;
  99. data.mButton4 = controller.buttons & SIXENSE_BUTTON_4;
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. DefineEngineMethod( RazerHydraFrame, isFrameValid, bool, ( ),,
  104. "@brief Checks if this frame is valid.\n\n"
  105. "@return True if the frame is valid.\n\n")
  106. {
  107. return object->isFrameValid();
  108. }
  109. DefineEngineMethod( RazerHydraFrame, getFrameInternalId, S32, ( ),,
  110. "@brief Provides the internal ID for this frame.\n\n"
  111. "@return Internal ID of this frame.\n\n")
  112. {
  113. return object->getFrameInternalId();
  114. }
  115. DefineEngineMethod( RazerHydraFrame, getFrameSimTime, S32, ( ),,
  116. "@brief Get the sim time that this frame was generated.\n\n"
  117. "@return Sim time of this frame in milliseconds.\n\n")
  118. {
  119. return object->getFrameSimTime();
  120. }
  121. DefineEngineMethod( RazerHydraFrame, getFrameRealTime, S32, ( ),,
  122. "@brief Get the real time that this frame was generated.\n\n"
  123. "@return Real time of this frame in milliseconds.\n\n")
  124. {
  125. return object->getFrameRealTime();
  126. }
  127. DefineEngineMethod( RazerHydraFrame, getControllerCount, S32, ( ),,
  128. "@brief Get the number of controllers defined in this frame.\n\n"
  129. "@return The number of defined controllers.\n\n")
  130. {
  131. return RazerHydraConstants::MaxControllers;
  132. }
  133. DefineEngineMethod( RazerHydraFrame, getControllerEnabled, bool, ( S32 index ),,
  134. "@brief Get the enabled state of the controller.\n\n"
  135. "@param index The controller index to check.\n"
  136. "@return True if the requested controller is enabled.\n\n")
  137. {
  138. return object->getEnabled(index);
  139. }
  140. DefineEngineMethod( RazerHydraFrame, getControllerDocked, bool, ( S32 index ),,
  141. "@brief Get the docked state of the controller.\n\n"
  142. "@param index The controller index to check.\n"
  143. "@return True if the requested controller is docked.\n\n")
  144. {
  145. return object->getDocked(index);
  146. }
  147. DefineEngineMethod( RazerHydraFrame, getControllerSequenceNum, S32, ( S32 index ),,
  148. "@brief Get the controller sequence number.\n\n"
  149. "@param index The controller index to check.\n"
  150. "@return The sequence number of the requested controller.\n\n")
  151. {
  152. return object->getSequenceNum(index);
  153. }
  154. DefineEngineMethod( RazerHydraFrame, getControllerRawPos, Point3F, ( S32 index ),,
  155. "@brief Get the raw position of the requested controller.\n\n"
  156. "The raw position is the controller's floating point position converted to "
  157. "Torque 3D coordinates (in millimeters).\n"
  158. "@param index The controller index to check.\n"
  159. "@return Raw position of the requested controller (in millimeters).\n\n")
  160. {
  161. return object->getRawPos(index);
  162. }
  163. DefineEngineMethod( RazerHydraFrame, getControllerPos, Point3I, ( S32 index ),,
  164. "@brief Get the position of the requested controller.\n\n"
  165. "The position is the controller's integer position converted to "
  166. "Torque 3D coordinates (in millimeters).\n"
  167. "@param index The controller index to check.\n"
  168. "@return Integer position of the requested controller (in millimeters).\n\n")
  169. {
  170. return object->getPos(index);
  171. }
  172. DefineEngineMethod( RazerHydraFrame, getControllerRot, AngAxisF, ( S32 index ),,
  173. "@brief Get the rotation of the requested controller.\n\n"
  174. "The Razer Hydra controller rotation as converted into the Torque 3D"
  175. "coordinate system.\n"
  176. "@param index The controller index to check.\n"
  177. "@return Rotation of the requested controller.\n\n")
  178. {
  179. AngAxisF aa(object->getRot(index));
  180. return aa;
  181. }
  182. DefineEngineMethod( RazerHydraFrame, getControllerRawTransform, TransformF, ( S32 index ),,
  183. "@brief Get the raw transform of the requested controller.\n\n"
  184. "@param index The controller index to check.\n"
  185. "@return The raw position and rotation of the requested controller (in Torque 3D coordinates).\n\n")
  186. {
  187. const Point3F& pos = object->getRawPos(index);
  188. const QuatF& qa = object->getRotQuat(index);
  189. AngAxisF aa(qa);
  190. aa.axis.normalize();
  191. TransformF trans(pos, aa);
  192. return trans;
  193. }
  194. DefineEngineMethod( RazerHydraFrame, getControllerTransform, TransformF, ( S32 index ),,
  195. "@brief Get the transform of the requested controller.\n\n"
  196. "@param index The controller index to check.\n"
  197. "@return The position and rotation of the requested controller (in Torque 3D coordinates).\n\n")
  198. {
  199. const Point3I& pos = object->getPos(index);
  200. const QuatF& qa = object->getRotQuat(index);
  201. AngAxisF aa(qa);
  202. aa.axis.normalize();
  203. TransformF trans;
  204. trans.mPosition = Point3F(pos.x, pos.y, pos.z);
  205. trans.mOrientation = aa;
  206. return trans;
  207. }
  208. DefineEngineMethod( RazerHydraFrame, getControllerRotAxis, Point2F, ( S32 index ),,
  209. "@brief Get the axis rotation of the requested controller.\n\n"
  210. "This is the axis rotation of the controller as if the controller were a gamepad thumb stick. "
  211. "Imagine a stick coming out the top of the controller and tilting the controller front, back, "
  212. "left and right controls that stick. The values returned along the x and y stick "
  213. "axis are normalized from -1.0 to 1.0 with the maximum controller tilt angle for these "
  214. "values as defined by $RazerHydra::MaximumAxisAngle.\n"
  215. "@param index The controller index to check.\n"
  216. "@return Axis rotation of the requested controller.\n\n"
  217. "@see RazerHydra::MaximumAxisAngle\n")
  218. {
  219. return object->getRotAxis(index);
  220. }
  221. DefineEngineMethod( RazerHydraFrame, getControllerThumbStick, Point2F, ( S32 index ),,
  222. "@brief Get the thumb stick values of the requested controller.\n\n"
  223. "The thumb stick values are in the range of -1.0..1.0\n"
  224. "@param index The controller index to check.\n"
  225. "@return Thumb stick values of the requested controller.\n\n")
  226. {
  227. return object->getThumbStick(index);
  228. }
  229. DefineEngineMethod( RazerHydraFrame, getControllerTrigger, F32, ( S32 index ),,
  230. "@brief Get the trigger value for the requested controller.\n\n"
  231. "The trigger value is in the range of -1.0..1.0\n"
  232. "@param index The controller index to check.\n"
  233. "@return Trigger value of the requested controller.\n\n")
  234. {
  235. return object->getTrigger(index);
  236. }
  237. DefineEngineMethod( RazerHydraFrame, getControllerShoulderButton, bool, ( S32 index ),,
  238. "@brief Get the shoulder button state for the requested controller.\n\n"
  239. "@param index The controller index to check.\n"
  240. "@return Shoulder button state requested controller as true or false.\n\n")
  241. {
  242. return object->getShoulder(index);
  243. }
  244. DefineEngineMethod( RazerHydraFrame, getControllerThumbButton, bool, ( S32 index ),,
  245. "@brief Get the thumb button state for the requested controller.\n\n"
  246. "@param index The controller index to check.\n"
  247. "@return Thumb button state requested controller as true or false.\n\n")
  248. {
  249. return object->getThumb(index);
  250. }
  251. DefineEngineMethod( RazerHydraFrame, getControllerStartButton, bool, ( S32 index ),,
  252. "@brief Get the start button state for the requested controller.\n\n"
  253. "@param index The controller index to check.\n"
  254. "@return Start button state requested controller as true or false.\n\n")
  255. {
  256. return object->getStart(index);
  257. }
  258. DefineEngineMethod( RazerHydraFrame, getControllerButton1, bool, ( S32 index ),,
  259. "@brief Get the button 1 state for the requested controller.\n\n"
  260. "@param index The controller index to check.\n"
  261. "@return Button 1 state requested controller as true or false.\n\n")
  262. {
  263. return object->getButton1(index);
  264. }
  265. DefineEngineMethod( RazerHydraFrame, getControllerButton2, bool, ( S32 index ),,
  266. "@brief Get the button 2 state for the requested controller.\n\n"
  267. "@param index The controller index to check.\n"
  268. "@return Button 2 state requested controller as true or false.\n\n")
  269. {
  270. return object->getButton2(index);
  271. }
  272. DefineEngineMethod( RazerHydraFrame, getControllerButton3, bool, ( S32 index ),,
  273. "@brief Get the button 3 state for the requested controller.\n\n"
  274. "@param index The controller index to check.\n"
  275. "@return Button 3 state requested controller as true or false.\n\n")
  276. {
  277. return object->getButton3(index);
  278. }
  279. DefineEngineMethod( RazerHydraFrame, getControllerButton4, bool, ( S32 index ),,
  280. "@brief Get the button 4 state for the requested controller.\n\n"
  281. "@param index The controller index to check.\n"
  282. "@return Button 4 state requested controller as true or false.\n\n")
  283. {
  284. return object->getButton4(index);
  285. }