2
0

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. {"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. 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_getJoystickAxisValues(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 3:
  222. {
  223. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  224. lua_type(state, 2) == LUA_TNUMBER &&
  225. (lua_type(state, 3) == LUA_TUSERDATA || lua_type(state, 3) == LUA_TTABLE || lua_type(state, 3) == LUA_TNIL))
  226. {
  227. // Get parameter 1 off the stack.
  228. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  229. // Get parameter 2 off the stack.
  230. ScriptUtil::LuaArray<Vector2> param2 = ScriptUtil::getObjectPointer<Vector2>(3, "Vector2", false);
  231. Gamepad* instance = getInstance(state);
  232. instance->getJoystickAxisValues(param1, param2);
  233. return 0;
  234. }
  235. else
  236. {
  237. lua_pushstring(state, "lua_Gamepad_getJoystickAxisValues - Failed to match the given parameters to a valid function signature.");
  238. lua_error(state);
  239. }
  240. break;
  241. }
  242. default:
  243. {
  244. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  245. lua_error(state);
  246. break;
  247. }
  248. }
  249. return 0;
  250. }
  251. int lua_Gamepad_getJoystickAxisX(lua_State* state)
  252. {
  253. // Get the number of parameters.
  254. int paramCount = lua_gettop(state);
  255. // Attempt to match the parameters to a valid binding.
  256. switch (paramCount)
  257. {
  258. case 2:
  259. {
  260. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  261. lua_type(state, 2) == LUA_TNUMBER)
  262. {
  263. // Get parameter 1 off the stack.
  264. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  265. Gamepad* instance = getInstance(state);
  266. float result = instance->getJoystickAxisX(param1);
  267. // Push the return value onto the stack.
  268. lua_pushnumber(state, result);
  269. return 1;
  270. }
  271. else
  272. {
  273. lua_pushstring(state, "lua_Gamepad_getJoystickAxisX - Failed to match the given parameters to a valid function signature.");
  274. lua_error(state);
  275. }
  276. break;
  277. }
  278. default:
  279. {
  280. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  281. lua_error(state);
  282. break;
  283. }
  284. }
  285. return 0;
  286. }
  287. int lua_Gamepad_getJoystickAxisY(lua_State* state)
  288. {
  289. // Get the number of parameters.
  290. int paramCount = lua_gettop(state);
  291. // Attempt to match the parameters to a valid binding.
  292. switch (paramCount)
  293. {
  294. case 2:
  295. {
  296. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  297. lua_type(state, 2) == LUA_TNUMBER)
  298. {
  299. // Get parameter 1 off the stack.
  300. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  301. Gamepad* instance = getInstance(state);
  302. float result = instance->getJoystickAxisY(param1);
  303. // Push the return value onto the stack.
  304. lua_pushnumber(state, result);
  305. return 1;
  306. }
  307. else
  308. {
  309. lua_pushstring(state, "lua_Gamepad_getJoystickAxisY - Failed to match the given parameters to a valid function signature.");
  310. lua_error(state);
  311. }
  312. break;
  313. }
  314. default:
  315. {
  316. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  317. lua_error(state);
  318. break;
  319. }
  320. }
  321. return 0;
  322. }
  323. int lua_Gamepad_getJoystickCount(lua_State* state)
  324. {
  325. // Get the number of parameters.
  326. int paramCount = lua_gettop(state);
  327. // Attempt to match the parameters to a valid binding.
  328. switch (paramCount)
  329. {
  330. case 1:
  331. {
  332. if ((lua_type(state, 1) == LUA_TUSERDATA))
  333. {
  334. Gamepad* instance = getInstance(state);
  335. unsigned int result = instance->getJoystickCount();
  336. // Push the return value onto the stack.
  337. lua_pushunsigned(state, result);
  338. return 1;
  339. }
  340. else
  341. {
  342. lua_pushstring(state, "lua_Gamepad_getJoystickCount - 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 1).");
  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. }