lua_Gamepad.cpp 17 KB

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