lua_Gamepad.cpp 15 KB

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