platformInput_ScriptBinding.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include "console/console.h"
  23. #include "platform/platformInput.h"
  24. //------------------------------------------------------------------------------
  25. /*! Use the activateKeyboard function to enable directInput polling of the keyboard.
  26. @return Returns a true if polling was successfully enabled.
  27. @sa deactivateKeyboard
  28. */
  29. ConsoleFunctionWithDocs( activateKeyboard, ConsoleBool, 1, 1, ())
  30. {
  31. return Input::activateKeyboard();
  32. }
  33. //------------------------------------------------------------------------------
  34. /*! Use the deactivateKeyboard function to disable directInput polling of the keyboard.
  35. @return No return value.
  36. @sa activateKeyboard
  37. */
  38. ConsoleFunctionWithDocs( deactivateKeyboard, ConsoleVoid, 1, 1, ())
  39. {
  40. Input::deactivateKeyboard();
  41. }
  42. //------------------------------------------------------------------------------
  43. /*! Use the enableMouse function to enable mouse input.
  44. @return No return value.
  45. @sa disableMouse
  46. */
  47. ConsoleFunctionWithDocs( enableMouse, ConsoleVoid, 1, 1, ())
  48. {
  49. Input::enableMouse();
  50. }
  51. //------------------------------------------------------------------------------
  52. /*! Use the disableMouse function to disable mouse input.
  53. @return No return value.
  54. @sa enableMouse
  55. */
  56. ConsoleFunctionWithDocs( disableMouse, ConsoleVoid, 1, 1, ())
  57. {
  58. Input::disableMouse();
  59. }
  60. //------------------------------------------------------------------------------
  61. /*! Use the enableJoystick function to enable joystick input if it is present.
  62. @return Will return true if the joystick is present and was successfully enabled, false otherwise.
  63. @sa disableJoystick, getJoystickAxes, isJoystickDetected
  64. */
  65. ConsoleFunctionWithDocs( enableJoystick, ConsoleBool, 1, 1, ())
  66. {
  67. return Input::enableJoystick();
  68. }
  69. //------------------------------------------------------------------------------
  70. /*! Use the disableJoystick function to disable joystick input.
  71. @return No return value.
  72. @sa enableJoystick, getJoystickAxes, isJoystickEnabled
  73. */
  74. ConsoleFunctionWithDocs( disableJoystick, ConsoleVoid, 1, 1, ())
  75. {
  76. Input::disableJoystick();
  77. }
  78. //------------------------------------------------------------------------------
  79. /*! Use the echoInputState function to dump the input state of the mouse, keyboard, and joystick to the console.
  80. @return No return value.
  81. @sa activateDirectInput, deactivateDirectInput, activateKeyboard, deactivateKeyboard, disableJoystick, enableJoystick, enableMouse, disableMouse
  82. */
  83. ConsoleFunctionWithDocs( echoInputState, ConsoleVoid, 1, 1, ())
  84. {
  85. Input::echoInputState();
  86. }
  87. /*! Enables XInput for Xbox 360 controllers.
  88. @return No return value.
  89. */
  90. ConsoleFunctionWithDocs( enableXInput, ConsoleBool, 1, 1, ())
  91. {
  92. return( DInputManager::enableXInput() );
  93. }
  94. /*! Disables XInput for Xbox 360 controllesr
  95. @return No return value.
  96. */
  97. ConsoleFunctionWithDocs( disableXInput, ConsoleVoid, 1, 1, ())
  98. {
  99. DInputManager::disableXInput();
  100. }
  101. /*! Rebuilds the XInput section of the InputManager.
  102. @return No return value.
  103. */
  104. ConsoleFunctionWithDocs( resetXInput, ConsoleVoid, 1, 1, ())
  105. {
  106. // This function requests a full "refresh" of events for all controllers the
  107. // next time we go through the input processing loop. This is useful to call
  108. // at the beginning of your game code after your actionMap is set up to hook
  109. // all of the appropriate events
  110. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  111. if ( mgr && mgr->isEnabled() )
  112. mgr->resetXInput();
  113. }
  114. /*! Checks to see if an Xbox 360 controller is connected
  115. @param controllerID Zero-based index of the controller to check.
  116. @return 1 if the controller is connected, 0 if it isn't, and 205 if XInput
  117. hasn't been initialized.
  118. */
  119. ConsoleFunctionWithDocs( isXInputConnected, ConsoleBool, 2, 2, ( int controllerID ))
  120. {
  121. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  122. if ( mgr && mgr->isEnabled() ) return mgr->isXInputConnected( atoi( argv[1] ) );
  123. return false;
  124. }
  125. /*! Queries the current state of a connected Xbox 360 controller.
  126. @param controllerID Zero-based index of the controller to return information about.
  127. @param property Name of input action being queried, such as \"XI_THUMBLX\".
  128. @param current True checks current device in action.
  129. @return Button queried - 1 if the button is pressed, 0 if it's not.
  130. @return Thumbstick queried - Int representing displacement from rest position.
  131. @return %Trigger queried - Int from 0 to 255 representing how far the trigger is displaced.
  132. */
  133. //------------------------------------------------------------------------------
  134. ConsoleFunctionWithDocs( getXInputState, ConsoleInt, 3, 4, ( int controllerID, string property, bool current ) )
  135. {
  136. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  137. if ( !mgr || !mgr->isEnabled() )
  138. return -1;
  139. // Use a little bit of macro magic to simplify this otherwise monolothic
  140. // block of code.
  141. #define GET_XI_STATE(constName) \
  142. if (!dStricmp(argv[2], #constName)) \
  143. return mgr->getXInputState( dAtoi( argv[1] ), constName, ( dAtoi ( argv[3] ) == 1) );
  144. GET_XI_STATE(XI_THUMBLX);
  145. GET_XI_STATE(XI_THUMBLY);
  146. GET_XI_STATE(XI_THUMBRX);
  147. GET_XI_STATE(XI_THUMBRY);
  148. GET_XI_STATE(XI_LEFT_TRIGGER);
  149. GET_XI_STATE(XI_RIGHT_TRIGGER);
  150. GET_XI_STATE(SI_UPOV);
  151. GET_XI_STATE(SI_DPOV);
  152. GET_XI_STATE(SI_LPOV);
  153. GET_XI_STATE(SI_RPOV);
  154. GET_XI_STATE(XI_START);
  155. GET_XI_STATE(XI_BACK);
  156. GET_XI_STATE(XI_LEFT_THUMB);
  157. GET_XI_STATE(XI_RIGHT_THUMB);
  158. GET_XI_STATE(XI_LEFT_SHOULDER);
  159. GET_XI_STATE(XI_RIGHT_SHOULDER);
  160. GET_XI_STATE(XI_A);
  161. GET_XI_STATE(XI_B);
  162. GET_XI_STATE(XI_X);
  163. GET_XI_STATE(XI_Y);
  164. #undef GET_XI_STATE
  165. return -1;
  166. }
  167. /*! Activates the vibration motors in the specified controller.
  168. The controller will constantly at it's xRumble and yRumble intensities until
  169. changed or told to stop.
  170. Valid inputs for xRumble/yRumble are [0 - 1].
  171. @param device Name of the device to rumble.
  172. @param xRumble Intensity to apply to the left motor.
  173. @param yRumble Intensity to apply to the right motor.
  174. @note in an Xbox 360 controller, the left motor is low-frequency,
  175. while the right motor is high-frequency.
  176. */
  177. ConsoleFunctionWithDocs( rumble, ConsoleVoid, 4, 4, (string device, float xRumble, float yRumble) )
  178. {
  179. DInputManager* mgr = dynamic_cast<DInputManager*>( Input::getManager() );
  180. if ( mgr && mgr->isEnabled() )
  181. {
  182. mgr->rumble(argv[1], dAtof(argv[2]), dAtof(argv[3]));
  183. }
  184. else
  185. {
  186. Con::printf( "DirectInput/XInput is not enabled." );
  187. }
  188. }