lua_Gamepad.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_Gamepad.h"
  4. #include "Base.h"
  5. #include "Game.h"
  6. #include "Gamepad.h"
  7. #include "lua_GamepadButtonState.h"
  8. namespace gameplay
  9. {
  10. void luaRegister_Gamepad()
  11. {
  12. const luaL_Reg lua_members[] =
  13. {
  14. {"draw", lua_Gamepad_draw},
  15. {"getButtonCount", lua_Gamepad_getButtonCount},
  16. {"getButtonState", lua_Gamepad_getButtonState},
  17. {"getForm", lua_Gamepad_getForm},
  18. {"getId", lua_Gamepad_getId},
  19. {"getJoystickCount", lua_Gamepad_getJoystickCount},
  20. {"getJoystickValue", lua_Gamepad_getJoystickValue},
  21. {"isJoystickActive", lua_Gamepad_isJoystickActive},
  22. {"isVirtual", lua_Gamepad_isVirtual},
  23. {"update", lua_Gamepad_update},
  24. {NULL, NULL}
  25. };
  26. const luaL_Reg* lua_statics = NULL;
  27. std::vector<std::string> scopePath;
  28. ScriptUtil::registerClass("Gamepad", lua_members, NULL, NULL, lua_statics, scopePath);
  29. }
  30. static Gamepad* getInstance(lua_State* state)
  31. {
  32. void* userdata = luaL_checkudata(state, 1, "Gamepad");
  33. luaL_argcheck(state, userdata != NULL, 1, "'Gamepad' expected.");
  34. return (Gamepad*)((ScriptUtil::LuaObject*)userdata)->instance;
  35. }
  36. int lua_Gamepad_draw(lua_State* state)
  37. {
  38. // Get the number of parameters.
  39. int paramCount = lua_gettop(state);
  40. // Attempt to match the parameters to a valid binding.
  41. switch (paramCount)
  42. {
  43. case 1:
  44. {
  45. if ((lua_type(state, 1) == LUA_TUSERDATA))
  46. {
  47. Gamepad* instance = getInstance(state);
  48. instance->draw();
  49. return 0;
  50. }
  51. else
  52. {
  53. lua_pushstring(state, "lua_Gamepad_draw - Failed to match the given parameters to a valid function signature.");
  54. lua_error(state);
  55. }
  56. break;
  57. }
  58. default:
  59. {
  60. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  61. lua_error(state);
  62. break;
  63. }
  64. }
  65. return 0;
  66. }
  67. int lua_Gamepad_getButtonCount(lua_State* state)
  68. {
  69. // Get the number of parameters.
  70. int paramCount = lua_gettop(state);
  71. // Attempt to match the parameters to a valid binding.
  72. switch (paramCount)
  73. {
  74. case 1:
  75. {
  76. if ((lua_type(state, 1) == LUA_TUSERDATA))
  77. {
  78. Gamepad* instance = getInstance(state);
  79. unsigned int result = instance->getButtonCount();
  80. // Push the return value onto the stack.
  81. lua_pushunsigned(state, result);
  82. return 1;
  83. }
  84. else
  85. {
  86. lua_pushstring(state, "lua_Gamepad_getButtonCount - Failed to match the given parameters to a valid function signature.");
  87. lua_error(state);
  88. }
  89. break;
  90. }
  91. default:
  92. {
  93. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  94. lua_error(state);
  95. break;
  96. }
  97. }
  98. return 0;
  99. }
  100. int lua_Gamepad_getButtonState(lua_State* state)
  101. {
  102. // Get the number of parameters.
  103. int paramCount = lua_gettop(state);
  104. // Attempt to match the parameters to a valid binding.
  105. switch (paramCount)
  106. {
  107. case 2:
  108. {
  109. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  110. lua_type(state, 2) == LUA_TNUMBER)
  111. {
  112. // Get parameter 1 off the stack.
  113. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  114. Gamepad* instance = getInstance(state);
  115. Gamepad::ButtonState result = instance->getButtonState(param1);
  116. // Push the return value onto the stack.
  117. lua_pushstring(state, lua_stringFromEnum_GamepadButtonState(result));
  118. return 1;
  119. }
  120. else
  121. {
  122. lua_pushstring(state, "lua_Gamepad_getButtonState - Failed to match the given parameters to a valid function signature.");
  123. lua_error(state);
  124. }
  125. break;
  126. }
  127. default:
  128. {
  129. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  130. lua_error(state);
  131. break;
  132. }
  133. }
  134. return 0;
  135. }
  136. int lua_Gamepad_getForm(lua_State* state)
  137. {
  138. // Get the number of parameters.
  139. int paramCount = lua_gettop(state);
  140. // Attempt to match the parameters to a valid binding.
  141. switch (paramCount)
  142. {
  143. case 1:
  144. {
  145. if ((lua_type(state, 1) == LUA_TUSERDATA))
  146. {
  147. Gamepad* instance = getInstance(state);
  148. void* returnPtr = (void*)instance->getForm();
  149. if (returnPtr)
  150. {
  151. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  152. object->instance = returnPtr;
  153. object->owns = false;
  154. luaL_getmetatable(state, "Form");
  155. lua_setmetatable(state, -2);
  156. }
  157. else
  158. {
  159. lua_pushnil(state);
  160. }
  161. return 1;
  162. }
  163. else
  164. {
  165. lua_pushstring(state, "lua_Gamepad_getForm - Failed to match the given parameters to a valid function signature.");
  166. lua_error(state);
  167. }
  168. break;
  169. }
  170. default:
  171. {
  172. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  173. lua_error(state);
  174. break;
  175. }
  176. }
  177. return 0;
  178. }
  179. int lua_Gamepad_getId(lua_State* state)
  180. {
  181. // Get the number of parameters.
  182. int paramCount = lua_gettop(state);
  183. // Attempt to match the parameters to a valid binding.
  184. switch (paramCount)
  185. {
  186. case 1:
  187. {
  188. if ((lua_type(state, 1) == LUA_TUSERDATA))
  189. {
  190. Gamepad* instance = getInstance(state);
  191. const char* result = instance->getId();
  192. // Push the return value onto the stack.
  193. lua_pushstring(state, result);
  194. return 1;
  195. }
  196. else
  197. {
  198. lua_pushstring(state, "lua_Gamepad_getId - Failed to match the given parameters to a valid function signature.");
  199. lua_error(state);
  200. }
  201. break;
  202. }
  203. default:
  204. {
  205. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  206. lua_error(state);
  207. break;
  208. }
  209. }
  210. return 0;
  211. }
  212. int lua_Gamepad_getJoystickCount(lua_State* state)
  213. {
  214. // Get the number of parameters.
  215. int paramCount = lua_gettop(state);
  216. // Attempt to match the parameters to a valid binding.
  217. switch (paramCount)
  218. {
  219. case 1:
  220. {
  221. if ((lua_type(state, 1) == LUA_TUSERDATA))
  222. {
  223. Gamepad* instance = getInstance(state);
  224. unsigned int result = instance->getJoystickCount();
  225. // Push the return value onto the stack.
  226. lua_pushunsigned(state, result);
  227. return 1;
  228. }
  229. else
  230. {
  231. lua_pushstring(state, "lua_Gamepad_getJoystickCount - Failed to match the given parameters to a valid function signature.");
  232. lua_error(state);
  233. }
  234. break;
  235. }
  236. default:
  237. {
  238. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  239. lua_error(state);
  240. break;
  241. }
  242. }
  243. return 0;
  244. }
  245. int lua_Gamepad_getJoystickValue(lua_State* state)
  246. {
  247. // Get the number of parameters.
  248. int paramCount = lua_gettop(state);
  249. // Attempt to match the parameters to a valid binding.
  250. switch (paramCount)
  251. {
  252. case 2:
  253. {
  254. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  255. lua_type(state, 2) == LUA_TNUMBER)
  256. {
  257. // Get parameter 1 off the stack.
  258. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  259. Gamepad* instance = getInstance(state);
  260. void* returnPtr = (void*)&(instance->getJoystickValue(param1));
  261. if (returnPtr)
  262. {
  263. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  264. object->instance = returnPtr;
  265. object->owns = false;
  266. luaL_getmetatable(state, "Vector2");
  267. lua_setmetatable(state, -2);
  268. }
  269. else
  270. {
  271. lua_pushnil(state);
  272. }
  273. return 1;
  274. }
  275. else
  276. {
  277. lua_pushstring(state, "lua_Gamepad_getJoystickValue - Failed to match the given parameters to a valid function signature.");
  278. lua_error(state);
  279. }
  280. break;
  281. }
  282. default:
  283. {
  284. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  285. lua_error(state);
  286. break;
  287. }
  288. }
  289. return 0;
  290. }
  291. int lua_Gamepad_isJoystickActive(lua_State* state)
  292. {
  293. // Get the number of parameters.
  294. int paramCount = lua_gettop(state);
  295. // Attempt to match the parameters to a valid binding.
  296. switch (paramCount)
  297. {
  298. case 2:
  299. {
  300. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  301. lua_type(state, 2) == LUA_TNUMBER)
  302. {
  303. // Get parameter 1 off the stack.
  304. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  305. Gamepad* instance = getInstance(state);
  306. bool result = instance->isJoystickActive(param1);
  307. // Push the return value onto the stack.
  308. lua_pushboolean(state, result);
  309. return 1;
  310. }
  311. else
  312. {
  313. lua_pushstring(state, "lua_Gamepad_isJoystickActive - Failed to match the given parameters to a valid function signature.");
  314. lua_error(state);
  315. }
  316. break;
  317. }
  318. default:
  319. {
  320. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  321. lua_error(state);
  322. break;
  323. }
  324. }
  325. return 0;
  326. }
  327. int lua_Gamepad_isVirtual(lua_State* state)
  328. {
  329. // Get the number of parameters.
  330. int paramCount = lua_gettop(state);
  331. // Attempt to match the parameters to a valid binding.
  332. switch (paramCount)
  333. {
  334. case 1:
  335. {
  336. if ((lua_type(state, 1) == LUA_TUSERDATA))
  337. {
  338. Gamepad* instance = getInstance(state);
  339. bool result = instance->isVirtual();
  340. // Push the return value onto the stack.
  341. lua_pushboolean(state, result);
  342. return 1;
  343. }
  344. else
  345. {
  346. lua_pushstring(state, "lua_Gamepad_isVirtual - Failed to match the given parameters to a valid function signature.");
  347. lua_error(state);
  348. }
  349. break;
  350. }
  351. default:
  352. {
  353. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  354. lua_error(state);
  355. break;
  356. }
  357. }
  358. return 0;
  359. }
  360. int lua_Gamepad_update(lua_State* state)
  361. {
  362. // Get the number of parameters.
  363. int paramCount = lua_gettop(state);
  364. // Attempt to match the parameters to a valid binding.
  365. switch (paramCount)
  366. {
  367. case 2:
  368. {
  369. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  370. lua_type(state, 2) == LUA_TNUMBER)
  371. {
  372. // Get parameter 1 off the stack.
  373. float param1 = (float)luaL_checknumber(state, 2);
  374. Gamepad* instance = getInstance(state);
  375. instance->update(param1);
  376. return 0;
  377. }
  378. else
  379. {
  380. lua_pushstring(state, "lua_Gamepad_update - Failed to match the given parameters to a valid function signature.");
  381. lua_error(state);
  382. }
  383. break;
  384. }
  385. default:
  386. {
  387. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  388. lua_error(state);
  389. break;
  390. }
  391. }
  392. return 0;
  393. }
  394. }