lua_Gamepad.cpp 17 KB

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