lua_Gamepad.cpp 15 KB

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