wrap_Joystick.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "wrap_Joystick.h"
  22. #include "wrap_JoystickModule.h"
  23. #include <vector>
  24. namespace love
  25. {
  26. namespace joystick
  27. {
  28. Joystick *luax_checkjoystick(lua_State *L, int idx)
  29. {
  30. return luax_checktype<Joystick>(L, idx, "Joystick", JOYSTICK_JOYSTICK_T);
  31. }
  32. int w_Joystick_isConnected(lua_State *L)
  33. {
  34. Joystick *j = luax_checkjoystick(L, 1);
  35. luax_pushboolean(L, j->isConnected());
  36. return 1;
  37. }
  38. int w_Joystick_getName(lua_State *L)
  39. {
  40. Joystick *j = luax_checkjoystick(L, 1);
  41. lua_pushstring(L, j->getName());
  42. return 1;
  43. }
  44. int w_Joystick_getID(lua_State *L)
  45. {
  46. Joystick *j = luax_checkjoystick(L, 1);
  47. // IDs are 1-based in Lua.
  48. lua_pushinteger(L, j->getID() + 1);
  49. int instanceid = j->getInstanceID();
  50. if (instanceid >= 0)
  51. lua_pushinteger(L, instanceid + 1);
  52. else
  53. lua_pushnil(L);
  54. return 2;
  55. }
  56. int w_Joystick_getGUID(lua_State *L)
  57. {
  58. Joystick *j = luax_checkjoystick(L, 1);
  59. luax_pushstring(L, j->getGUID());
  60. return 1;
  61. }
  62. int w_Joystick_getAxisCount(lua_State *L)
  63. {
  64. Joystick *j = luax_checkjoystick(L, 1);
  65. lua_pushinteger(L, j->getAxisCount());
  66. return 1;
  67. }
  68. int w_Joystick_getButtonCount(lua_State *L)
  69. {
  70. Joystick *j = luax_checkjoystick(L, 1);
  71. lua_pushinteger(L, j->getButtonCount());
  72. return 1;
  73. }
  74. int w_Joystick_getHatCount(lua_State *L)
  75. {
  76. Joystick *j = luax_checkjoystick(L, 1);
  77. lua_pushinteger(L, j->getHatCount());
  78. return 1;
  79. }
  80. int w_Joystick_getAxis(lua_State *L)
  81. {
  82. Joystick *j = luax_checkjoystick(L, 1);
  83. int axisindex = luaL_checkint(L, 2) - 1;
  84. lua_pushnumber(L, j->getAxis(axisindex));
  85. return 1;
  86. }
  87. int w_Joystick_getAxes(lua_State *L)
  88. {
  89. Joystick *j = luax_checkjoystick(L, 1);
  90. std::vector<float> axes = j->getAxes();
  91. for (size_t i = 0; i < axes.size(); i++)
  92. lua_pushnumber(L, axes[i]);
  93. return (int) axes.size();
  94. }
  95. int w_Joystick_getHat(lua_State *L)
  96. {
  97. Joystick *j = luax_checkjoystick(L, 1);
  98. int hatindex = luaL_checkint(L, 2) - 1;
  99. Joystick::Hat h = j->getHat(hatindex);
  100. const char *direction = "";
  101. Joystick::getConstant(h, direction);
  102. lua_pushstring(L, direction);
  103. return 1;
  104. }
  105. int w_Joystick_isDown(lua_State *L)
  106. {
  107. Joystick *j = luax_checkjoystick(L, 1);
  108. luaL_checkinteger(L, 2);
  109. std::vector<int> buttons;
  110. for (int i = 2; i <= lua_gettop(L); i++)
  111. buttons.push_back(luaL_checkint(L, i) - 1);
  112. luax_pushboolean(L, j->isDown(buttons));
  113. return 1;
  114. }
  115. int w_Joystick_isGamepad(lua_State *L)
  116. {
  117. Joystick *j = luax_checkjoystick(L, 1);
  118. luax_pushboolean(L, j->isGamepad());
  119. return 1;
  120. }
  121. int w_Joystick_getGamepadAxis(lua_State *L)
  122. {
  123. Joystick *j = luax_checkjoystick(L, 1);
  124. const char *str = luaL_checkstring(L, 2);
  125. Joystick::GamepadAxis axis;
  126. if (!joystick::Joystick::getConstant(str, axis))
  127. return luaL_error(L, "Invalid gamepad axis: %s", str);
  128. lua_pushnumber(L, j->getGamepadAxis(axis));
  129. return 1;
  130. }
  131. int w_Joystick_isGamepadDown(lua_State *L)
  132. {
  133. Joystick *j = luax_checkjoystick(L, 1);
  134. std::vector<Joystick::GamepadButton> buttons;
  135. buttons.reserve(lua_gettop(L) - 1);
  136. luaL_checkstring(L, 2);
  137. for (int i = 2; i <= lua_gettop(L); i++)
  138. {
  139. const char *str = luaL_checkstring(L, i);
  140. Joystick::GamepadButton button;
  141. if (!joystick::Joystick::getConstant(str, button))
  142. return luaL_error(L, "Invalid gamepad button: %s", str);
  143. buttons.push_back(button);
  144. }
  145. luax_pushboolean(L, j->isGamepadDown(buttons));
  146. return 1;
  147. }
  148. int w_Joystick_isVibrationSupported(lua_State *L)
  149. {
  150. Joystick *j = luax_checkjoystick(L, 1);
  151. luax_pushboolean(L, j->isVibrationSupported());
  152. return 1;
  153. }
  154. int w_Joystick_setVibration(lua_State *L)
  155. {
  156. Joystick *j = luax_checkjoystick(L, 1);
  157. bool success = false;
  158. if (lua_isnoneornil(L, 2))
  159. {
  160. // Disable joystick vibration if no argument is given.
  161. success = j->setVibration();
  162. }
  163. else
  164. {
  165. float left = (float) luaL_checknumber(L, 2);
  166. float right = (float) luaL_optnumber(L, 3, left);
  167. float duration = (float) luaL_optnumber(L, 4, -1.0); // -1 is infinite.
  168. success = j->setVibration(left, right, duration);
  169. }
  170. luax_pushboolean(L, success);
  171. return 1;
  172. }
  173. int w_Joystick_getVibration(lua_State *L)
  174. {
  175. Joystick *j = luax_checkjoystick(L, 1);
  176. float left, right;
  177. j->getVibration(left, right);
  178. lua_pushnumber(L, left);
  179. lua_pushnumber(L, right);
  180. return 2;
  181. }
  182. // List of functions to wrap.
  183. static const luaL_Reg functions[] =
  184. {
  185. { "isConnected", w_Joystick_isConnected },
  186. { "getName", w_Joystick_getName },
  187. { "getID", w_Joystick_getID },
  188. { "getGUID", w_Joystick_getGUID },
  189. { "getAxisCount", w_Joystick_getAxisCount },
  190. { "getButtonCount", w_Joystick_getButtonCount },
  191. { "getHatCount", w_Joystick_getHatCount },
  192. { "getAxis", w_Joystick_getAxis },
  193. { "getAxes", w_Joystick_getAxes },
  194. { "getHat", w_Joystick_getHat },
  195. { "isDown", w_Joystick_isDown },
  196. { "isGamepad", w_Joystick_isGamepad },
  197. { "getGamepadAxis", w_Joystick_getGamepadAxis },
  198. { "isGamepadDown", w_Joystick_isGamepadDown },
  199. { "isVibrationSupported", w_Joystick_isVibrationSupported },
  200. { "setVibration", w_Joystick_setVibration },
  201. { "getVibration", w_Joystick_getVibration },
  202. // From wrap_JoystickModule.
  203. { "getConnectedIndex", w_getIndex },
  204. { "getGamepadMapping", w_getGamepadMapping },
  205. { 0, 0 },
  206. };
  207. extern "C" int luaopen_joystick(lua_State *L)
  208. {
  209. return luax_register_type(L, "Joystick", functions);
  210. }
  211. } // joystick
  212. } // love