lua_Gamepad.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_Gamepad.h"
  4. #include "Base.h"
  5. #include "Button.h"
  6. #include "Game.h"
  7. #include "Gamepad.h"
  8. #include "lua_GamepadButtonMapping.h"
  9. namespace gameplay
  10. {
  11. void luaRegister_Gamepad()
  12. {
  13. const luaL_Reg lua_members[] =
  14. {
  15. {"draw", lua_Gamepad_draw},
  16. {"getButtonCount", lua_Gamepad_getButtonCount},
  17. {"getForm", lua_Gamepad_getForm},
  18. {"getId", lua_Gamepad_getId},
  19. {"getJoystickCount", lua_Gamepad_getJoystickCount},
  20. {"getJoystickValues", lua_Gamepad_getJoystickValues},
  21. {"getProductId", lua_Gamepad_getProductId},
  22. {"getProductString", lua_Gamepad_getProductString},
  23. {"getTriggerCount", lua_Gamepad_getTriggerCount},
  24. {"getTriggerValue", lua_Gamepad_getTriggerValue},
  25. {"getVendorId", lua_Gamepad_getVendorId},
  26. {"getVendorString", lua_Gamepad_getVendorString},
  27. {"isButtonDown", lua_Gamepad_isButtonDown},
  28. {"isVirtual", lua_Gamepad_isVirtual},
  29. {"update", lua_Gamepad_update},
  30. {NULL, NULL}
  31. };
  32. const luaL_Reg lua_statics[] =
  33. {
  34. {"getGamepads", lua_Gamepad_static_getGamepads},
  35. {NULL, NULL}
  36. };
  37. std::vector<std::string> scopePath;
  38. ScriptUtil::registerClass("Gamepad", lua_members, NULL, NULL, lua_statics, scopePath);
  39. }
  40. static Gamepad* getInstance(lua_State* state)
  41. {
  42. void* userdata = luaL_checkudata(state, 1, "Gamepad");
  43. luaL_argcheck(state, userdata != NULL, 1, "'Gamepad' expected.");
  44. return (Gamepad*)((ScriptUtil::LuaObject*)userdata)->instance;
  45. }
  46. int lua_Gamepad_draw(lua_State* state)
  47. {
  48. // Get the number of parameters.
  49. int paramCount = lua_gettop(state);
  50. // Attempt to match the parameters to a valid binding.
  51. switch (paramCount)
  52. {
  53. case 1:
  54. {
  55. if ((lua_type(state, 1) == LUA_TUSERDATA))
  56. {
  57. Gamepad* instance = getInstance(state);
  58. instance->draw();
  59. return 0;
  60. }
  61. lua_pushstring(state, "lua_Gamepad_draw - Failed to match the given parameters to a valid function signature.");
  62. lua_error(state);
  63. break;
  64. }
  65. default:
  66. {
  67. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  68. lua_error(state);
  69. break;
  70. }
  71. }
  72. return 0;
  73. }
  74. int lua_Gamepad_getButtonCount(lua_State* state)
  75. {
  76. // Get the number of parameters.
  77. int paramCount = lua_gettop(state);
  78. // Attempt to match the parameters to a valid binding.
  79. switch (paramCount)
  80. {
  81. case 1:
  82. {
  83. if ((lua_type(state, 1) == LUA_TUSERDATA))
  84. {
  85. Gamepad* instance = getInstance(state);
  86. unsigned int result = instance->getButtonCount();
  87. // Push the return value onto the stack.
  88. lua_pushunsigned(state, result);
  89. return 1;
  90. }
  91. lua_pushstring(state, "lua_Gamepad_getButtonCount - Failed to match the given parameters to a valid function signature.");
  92. lua_error(state);
  93. break;
  94. }
  95. default:
  96. {
  97. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  98. lua_error(state);
  99. break;
  100. }
  101. }
  102. return 0;
  103. }
  104. int lua_Gamepad_getForm(lua_State* state)
  105. {
  106. // Get the number of parameters.
  107. int paramCount = lua_gettop(state);
  108. // Attempt to match the parameters to a valid binding.
  109. switch (paramCount)
  110. {
  111. case 1:
  112. {
  113. if ((lua_type(state, 1) == LUA_TUSERDATA))
  114. {
  115. Gamepad* instance = getInstance(state);
  116. void* returnPtr = (void*)instance->getForm();
  117. if (returnPtr)
  118. {
  119. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  120. object->instance = returnPtr;
  121. object->owns = false;
  122. luaL_getmetatable(state, "Form");
  123. lua_setmetatable(state, -2);
  124. }
  125. else
  126. {
  127. lua_pushnil(state);
  128. }
  129. return 1;
  130. }
  131. lua_pushstring(state, "lua_Gamepad_getForm - Failed to match the given parameters to a valid function signature.");
  132. lua_error(state);
  133. break;
  134. }
  135. default:
  136. {
  137. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  138. lua_error(state);
  139. break;
  140. }
  141. }
  142. return 0;
  143. }
  144. int lua_Gamepad_getId(lua_State* state)
  145. {
  146. // Get the number of parameters.
  147. int paramCount = lua_gettop(state);
  148. // Attempt to match the parameters to a valid binding.
  149. switch (paramCount)
  150. {
  151. case 1:
  152. {
  153. if ((lua_type(state, 1) == LUA_TUSERDATA))
  154. {
  155. Gamepad* instance = getInstance(state);
  156. const char* result = instance->getId();
  157. // Push the return value onto the stack.
  158. lua_pushstring(state, result);
  159. return 1;
  160. }
  161. lua_pushstring(state, "lua_Gamepad_getId - Failed to match the given parameters to a valid function signature.");
  162. lua_error(state);
  163. break;
  164. }
  165. default:
  166. {
  167. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  168. lua_error(state);
  169. break;
  170. }
  171. }
  172. return 0;
  173. }
  174. int lua_Gamepad_getJoystickCount(lua_State* state)
  175. {
  176. // Get the number of parameters.
  177. int paramCount = lua_gettop(state);
  178. // Attempt to match the parameters to a valid binding.
  179. switch (paramCount)
  180. {
  181. case 1:
  182. {
  183. if ((lua_type(state, 1) == LUA_TUSERDATA))
  184. {
  185. Gamepad* instance = getInstance(state);
  186. unsigned int result = instance->getJoystickCount();
  187. // Push the return value onto the stack.
  188. lua_pushunsigned(state, result);
  189. return 1;
  190. }
  191. lua_pushstring(state, "lua_Gamepad_getJoystickCount - Failed to match the given parameters to a valid function signature.");
  192. lua_error(state);
  193. break;
  194. }
  195. default:
  196. {
  197. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  198. lua_error(state);
  199. break;
  200. }
  201. }
  202. return 0;
  203. }
  204. int lua_Gamepad_getJoystickValues(lua_State* state)
  205. {
  206. // Get the number of parameters.
  207. int paramCount = lua_gettop(state);
  208. // Attempt to match the parameters to a valid binding.
  209. switch (paramCount)
  210. {
  211. case 3:
  212. {
  213. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  214. lua_type(state, 2) == LUA_TNUMBER &&
  215. (lua_type(state, 3) == LUA_TUSERDATA || lua_type(state, 3) == LUA_TTABLE || lua_type(state, 3) == LUA_TNIL))
  216. {
  217. // Get parameter 1 off the stack.
  218. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  219. // Get parameter 2 off the stack.
  220. bool param2Valid;
  221. ScriptUtil::LuaArray<Vector2> param2 = ScriptUtil::getObjectPointer<Vector2>(3, "Vector2", false, &param2Valid);
  222. if (!param2Valid)
  223. {
  224. lua_pushstring(state, "Failed to convert parameter 2 to type 'Vector2'.");
  225. lua_error(state);
  226. }
  227. Gamepad* instance = getInstance(state);
  228. instance->getJoystickValues(param1, param2);
  229. return 0;
  230. }
  231. lua_pushstring(state, "lua_Gamepad_getJoystickValues - Failed to match the given parameters to a valid function signature.");
  232. lua_error(state);
  233. break;
  234. }
  235. default:
  236. {
  237. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  238. lua_error(state);
  239. break;
  240. }
  241. }
  242. return 0;
  243. }
  244. int lua_Gamepad_getProductId(lua_State* state)
  245. {
  246. // Get the number of parameters.
  247. int paramCount = lua_gettop(state);
  248. // Attempt to match the parameters to a valid binding.
  249. switch (paramCount)
  250. {
  251. case 1:
  252. {
  253. if ((lua_type(state, 1) == LUA_TUSERDATA))
  254. {
  255. Gamepad* instance = getInstance(state);
  256. unsigned int result = instance->getProductId();
  257. // Push the return value onto the stack.
  258. lua_pushunsigned(state, result);
  259. return 1;
  260. }
  261. lua_pushstring(state, "lua_Gamepad_getProductId - Failed to match the given parameters to a valid function signature.");
  262. lua_error(state);
  263. break;
  264. }
  265. default:
  266. {
  267. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  268. lua_error(state);
  269. break;
  270. }
  271. }
  272. return 0;
  273. }
  274. int lua_Gamepad_getProductString(lua_State* state)
  275. {
  276. // Get the number of parameters.
  277. int paramCount = lua_gettop(state);
  278. // Attempt to match the parameters to a valid binding.
  279. switch (paramCount)
  280. {
  281. case 1:
  282. {
  283. if ((lua_type(state, 1) == LUA_TUSERDATA))
  284. {
  285. Gamepad* instance = getInstance(state);
  286. const char* result = instance->getProductString();
  287. // Push the return value onto the stack.
  288. lua_pushstring(state, result);
  289. return 1;
  290. }
  291. lua_pushstring(state, "lua_Gamepad_getProductString - Failed to match the given parameters to a valid function signature.");
  292. lua_error(state);
  293. break;
  294. }
  295. default:
  296. {
  297. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  298. lua_error(state);
  299. break;
  300. }
  301. }
  302. return 0;
  303. }
  304. int lua_Gamepad_getTriggerCount(lua_State* state)
  305. {
  306. // Get the number of parameters.
  307. int paramCount = lua_gettop(state);
  308. // Attempt to match the parameters to a valid binding.
  309. switch (paramCount)
  310. {
  311. case 1:
  312. {
  313. if ((lua_type(state, 1) == LUA_TUSERDATA))
  314. {
  315. Gamepad* instance = getInstance(state);
  316. unsigned int result = instance->getTriggerCount();
  317. // Push the return value onto the stack.
  318. lua_pushunsigned(state, result);
  319. return 1;
  320. }
  321. lua_pushstring(state, "lua_Gamepad_getTriggerCount - Failed to match the given parameters to a valid function signature.");
  322. lua_error(state);
  323. break;
  324. }
  325. default:
  326. {
  327. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  328. lua_error(state);
  329. break;
  330. }
  331. }
  332. return 0;
  333. }
  334. int lua_Gamepad_getTriggerValue(lua_State* state)
  335. {
  336. // Get the number of parameters.
  337. int paramCount = lua_gettop(state);
  338. // Attempt to match the parameters to a valid binding.
  339. switch (paramCount)
  340. {
  341. case 2:
  342. {
  343. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  344. lua_type(state, 2) == LUA_TNUMBER)
  345. {
  346. // Get parameter 1 off the stack.
  347. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  348. Gamepad* instance = getInstance(state);
  349. float result = instance->getTriggerValue(param1);
  350. // Push the return value onto the stack.
  351. lua_pushnumber(state, result);
  352. return 1;
  353. }
  354. lua_pushstring(state, "lua_Gamepad_getTriggerValue - Failed to match the given parameters to a valid function signature.");
  355. lua_error(state);
  356. break;
  357. }
  358. default:
  359. {
  360. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  361. lua_error(state);
  362. break;
  363. }
  364. }
  365. return 0;
  366. }
  367. int lua_Gamepad_getVendorId(lua_State* state)
  368. {
  369. // Get the number of parameters.
  370. int paramCount = lua_gettop(state);
  371. // Attempt to match the parameters to a valid binding.
  372. switch (paramCount)
  373. {
  374. case 1:
  375. {
  376. if ((lua_type(state, 1) == LUA_TUSERDATA))
  377. {
  378. Gamepad* instance = getInstance(state);
  379. unsigned int result = instance->getVendorId();
  380. // Push the return value onto the stack.
  381. lua_pushunsigned(state, result);
  382. return 1;
  383. }
  384. lua_pushstring(state, "lua_Gamepad_getVendorId - Failed to match the given parameters to a valid function signature.");
  385. lua_error(state);
  386. break;
  387. }
  388. default:
  389. {
  390. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  391. lua_error(state);
  392. break;
  393. }
  394. }
  395. return 0;
  396. }
  397. int lua_Gamepad_getVendorString(lua_State* state)
  398. {
  399. // Get the number of parameters.
  400. int paramCount = lua_gettop(state);
  401. // Attempt to match the parameters to a valid binding.
  402. switch (paramCount)
  403. {
  404. case 1:
  405. {
  406. if ((lua_type(state, 1) == LUA_TUSERDATA))
  407. {
  408. Gamepad* instance = getInstance(state);
  409. const char* result = instance->getVendorString();
  410. // Push the return value onto the stack.
  411. lua_pushstring(state, result);
  412. return 1;
  413. }
  414. lua_pushstring(state, "lua_Gamepad_getVendorString - Failed to match the given parameters to a valid function signature.");
  415. lua_error(state);
  416. break;
  417. }
  418. default:
  419. {
  420. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  421. lua_error(state);
  422. break;
  423. }
  424. }
  425. return 0;
  426. }
  427. int lua_Gamepad_isButtonDown(lua_State* state)
  428. {
  429. // Get the number of parameters.
  430. int paramCount = lua_gettop(state);
  431. // Attempt to match the parameters to a valid binding.
  432. switch (paramCount)
  433. {
  434. case 2:
  435. {
  436. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  437. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  438. {
  439. // Get parameter 1 off the stack.
  440. Gamepad::ButtonMapping param1 = (Gamepad::ButtonMapping)lua_enumFromString_GamepadButtonMapping(luaL_checkstring(state, 2));
  441. Gamepad* instance = getInstance(state);
  442. bool result = instance->isButtonDown(param1);
  443. // Push the return value onto the stack.
  444. lua_pushboolean(state, result);
  445. return 1;
  446. }
  447. lua_pushstring(state, "lua_Gamepad_isButtonDown - Failed to match the given parameters to a valid function signature.");
  448. lua_error(state);
  449. break;
  450. }
  451. default:
  452. {
  453. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  454. lua_error(state);
  455. break;
  456. }
  457. }
  458. return 0;
  459. }
  460. int lua_Gamepad_isVirtual(lua_State* state)
  461. {
  462. // Get the number of parameters.
  463. int paramCount = lua_gettop(state);
  464. // Attempt to match the parameters to a valid binding.
  465. switch (paramCount)
  466. {
  467. case 1:
  468. {
  469. if ((lua_type(state, 1) == LUA_TUSERDATA))
  470. {
  471. Gamepad* instance = getInstance(state);
  472. bool result = instance->isVirtual();
  473. // Push the return value onto the stack.
  474. lua_pushboolean(state, result);
  475. return 1;
  476. }
  477. lua_pushstring(state, "lua_Gamepad_isVirtual - Failed to match the given parameters to a valid function signature.");
  478. lua_error(state);
  479. break;
  480. }
  481. default:
  482. {
  483. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  484. lua_error(state);
  485. break;
  486. }
  487. }
  488. return 0;
  489. }
  490. int lua_Gamepad_static_getGamepads(lua_State* state)
  491. {
  492. // Get the number of parameters.
  493. int paramCount = lua_gettop(state);
  494. // Attempt to match the parameters to a valid binding.
  495. switch (paramCount)
  496. {
  497. case 0:
  498. {
  499. void* returnPtr = (void*)Gamepad::getGamepads();
  500. if (returnPtr)
  501. {
  502. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  503. object->instance = returnPtr;
  504. object->owns = false;
  505. luaL_getmetatable(state, "Gamepad");
  506. lua_setmetatable(state, -2);
  507. }
  508. else
  509. {
  510. lua_pushnil(state);
  511. }
  512. return 1;
  513. break;
  514. }
  515. default:
  516. {
  517. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  518. lua_error(state);
  519. break;
  520. }
  521. }
  522. return 0;
  523. }
  524. int lua_Gamepad_update(lua_State* state)
  525. {
  526. // Get the number of parameters.
  527. int paramCount = lua_gettop(state);
  528. // Attempt to match the parameters to a valid binding.
  529. switch (paramCount)
  530. {
  531. case 2:
  532. {
  533. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  534. lua_type(state, 2) == LUA_TNUMBER)
  535. {
  536. // Get parameter 1 off the stack.
  537. float param1 = (float)luaL_checknumber(state, 2);
  538. Gamepad* instance = getInstance(state);
  539. instance->update(param1);
  540. return 0;
  541. }
  542. lua_pushstring(state, "lua_Gamepad_update - Failed to match the given parameters to a valid function signature.");
  543. lua_error(state);
  544. break;
  545. }
  546. default:
  547. {
  548. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  549. lua_error(state);
  550. break;
  551. }
  552. }
  553. return 0;
  554. }
  555. }