lua_Gamepad.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "Gamepad.h"
  4. #include "lua_Gamepad.h"
  5. #include "lua_Global.h"
  6. namespace gameplay
  7. {
  8. void luaRegister_Gamepad()
  9. {
  10. ScriptController* sc = ScriptController::getInstance();
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"draw", lua_Gamepad_draw},
  14. {"getButtonCount", lua_Gamepad_getButtonCount},
  15. {"getButtonState", lua_Gamepad_getButtonState},
  16. {"getForm", lua_Gamepad_getForm},
  17. {"getId", lua_Gamepad_getId},
  18. {"getJoystickCount", lua_Gamepad_getJoystickCount},
  19. {"getJoystickValue", lua_Gamepad_getJoystickValue},
  20. {"isJoystickActive", lua_Gamepad_isJoystickActive},
  21. {"isVirtual", lua_Gamepad_isVirtual},
  22. {"update", lua_Gamepad_update},
  23. {NULL, NULL}
  24. };
  25. const luaL_Reg* lua_statics = NULL;
  26. std::vector<std::string> scopePath;
  27. sc->registerClass("Gamepad", lua_members, NULL, NULL, lua_statics, scopePath);
  28. }
  29. static Gamepad* getInstance(lua_State* state)
  30. {
  31. void* userdata = luaL_checkudata(state, 1, "Gamepad");
  32. luaL_argcheck(state, userdata != NULL, 1, "'Gamepad' expected.");
  33. return (Gamepad*)((ScriptController::LuaObject*)userdata)->instance;
  34. }
  35. int lua_Gamepad_draw(lua_State* state)
  36. {
  37. // Get the number of parameters.
  38. int paramCount = lua_gettop(state);
  39. // Attempt to match the parameters to a valid binding.
  40. switch (paramCount)
  41. {
  42. case 1:
  43. {
  44. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL))
  45. {
  46. Gamepad* instance = getInstance(state);
  47. instance->draw();
  48. return 0;
  49. }
  50. else
  51. {
  52. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  53. lua_error(state);
  54. }
  55. break;
  56. }
  57. default:
  58. {
  59. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  60. lua_error(state);
  61. break;
  62. }
  63. }
  64. return 0;
  65. }
  66. int lua_Gamepad_getButtonCount(lua_State* state)
  67. {
  68. // Get the number of parameters.
  69. int paramCount = lua_gettop(state);
  70. // Attempt to match the parameters to a valid binding.
  71. switch (paramCount)
  72. {
  73. case 1:
  74. {
  75. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL))
  76. {
  77. Gamepad* instance = getInstance(state);
  78. unsigned int result = instance->getButtonCount();
  79. // Push the return value onto the stack.
  80. lua_pushunsigned(state, result);
  81. return 1;
  82. }
  83. else
  84. {
  85. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  86. lua_error(state);
  87. }
  88. break;
  89. }
  90. default:
  91. {
  92. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  93. lua_error(state);
  94. break;
  95. }
  96. }
  97. return 0;
  98. }
  99. int lua_Gamepad_getButtonState(lua_State* state)
  100. {
  101. // Get the number of parameters.
  102. int paramCount = lua_gettop(state);
  103. // Attempt to match the parameters to a valid binding.
  104. switch (paramCount)
  105. {
  106. case 2:
  107. {
  108. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  109. lua_type(state, 2) == LUA_TNUMBER)
  110. {
  111. // Get parameter 1 off the stack.
  112. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  113. Gamepad* instance = getInstance(state);
  114. Gamepad::ButtonState result = instance->getButtonState(param1);
  115. // Push the return value onto the stack.
  116. lua_pushstring(state, lua_stringFromEnum_GamepadButtonState(result));
  117. return 1;
  118. }
  119. else
  120. {
  121. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  122. lua_error(state);
  123. }
  124. break;
  125. }
  126. default:
  127. {
  128. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  129. lua_error(state);
  130. break;
  131. }
  132. }
  133. return 0;
  134. }
  135. int lua_Gamepad_getForm(lua_State* state)
  136. {
  137. // Get the number of parameters.
  138. int paramCount = lua_gettop(state);
  139. // Attempt to match the parameters to a valid binding.
  140. switch (paramCount)
  141. {
  142. case 1:
  143. {
  144. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL))
  145. {
  146. Gamepad* instance = getInstance(state);
  147. void* returnPtr = (void*)instance->getForm();
  148. if (returnPtr)
  149. {
  150. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  151. object->instance = returnPtr;
  152. object->owns = false;
  153. luaL_getmetatable(state, "Form");
  154. lua_setmetatable(state, -2);
  155. }
  156. else
  157. {
  158. lua_pushnil(state);
  159. }
  160. return 1;
  161. }
  162. else
  163. {
  164. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  165. lua_error(state);
  166. }
  167. break;
  168. }
  169. default:
  170. {
  171. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  172. lua_error(state);
  173. break;
  174. }
  175. }
  176. return 0;
  177. }
  178. int lua_Gamepad_getId(lua_State* state)
  179. {
  180. // Get the number of parameters.
  181. int paramCount = lua_gettop(state);
  182. // Attempt to match the parameters to a valid binding.
  183. switch (paramCount)
  184. {
  185. case 1:
  186. {
  187. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL))
  188. {
  189. Gamepad* instance = getInstance(state);
  190. const char* result = instance->getId();
  191. // Push the return value onto the stack.
  192. lua_pushstring(state, result);
  193. return 1;
  194. }
  195. else
  196. {
  197. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  198. lua_error(state);
  199. }
  200. break;
  201. }
  202. default:
  203. {
  204. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  205. lua_error(state);
  206. break;
  207. }
  208. }
  209. return 0;
  210. }
  211. int lua_Gamepad_getJoystickCount(lua_State* state)
  212. {
  213. // Get the number of parameters.
  214. int paramCount = lua_gettop(state);
  215. // Attempt to match the parameters to a valid binding.
  216. switch (paramCount)
  217. {
  218. case 1:
  219. {
  220. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL))
  221. {
  222. Gamepad* instance = getInstance(state);
  223. unsigned int result = instance->getJoystickCount();
  224. // Push the return value onto the stack.
  225. lua_pushunsigned(state, result);
  226. return 1;
  227. }
  228. else
  229. {
  230. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  231. lua_error(state);
  232. }
  233. break;
  234. }
  235. default:
  236. {
  237. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  238. lua_error(state);
  239. break;
  240. }
  241. }
  242. return 0;
  243. }
  244. int lua_Gamepad_getJoystickValue(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 2:
  252. {
  253. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  254. lua_type(state, 2) == LUA_TNUMBER)
  255. {
  256. // Get parameter 1 off the stack.
  257. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  258. Gamepad* instance = getInstance(state);
  259. void* returnPtr = (void*)&(instance->getJoystickValue(param1));
  260. if (returnPtr)
  261. {
  262. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  263. object->instance = returnPtr;
  264. object->owns = false;
  265. luaL_getmetatable(state, "Vector2");
  266. lua_setmetatable(state, -2);
  267. }
  268. else
  269. {
  270. lua_pushnil(state);
  271. }
  272. return 1;
  273. }
  274. else
  275. {
  276. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  277. lua_error(state);
  278. }
  279. break;
  280. }
  281. default:
  282. {
  283. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  284. lua_error(state);
  285. break;
  286. }
  287. }
  288. return 0;
  289. }
  290. int lua_Gamepad_isJoystickActive(lua_State* state)
  291. {
  292. // Get the number of parameters.
  293. int paramCount = lua_gettop(state);
  294. // Attempt to match the parameters to a valid binding.
  295. switch (paramCount)
  296. {
  297. case 2:
  298. {
  299. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  300. lua_type(state, 2) == LUA_TNUMBER)
  301. {
  302. // Get parameter 1 off the stack.
  303. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  304. Gamepad* instance = getInstance(state);
  305. bool result = instance->isJoystickActive(param1);
  306. // Push the return value onto the stack.
  307. lua_pushboolean(state, result);
  308. return 1;
  309. }
  310. else
  311. {
  312. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  313. lua_error(state);
  314. }
  315. break;
  316. }
  317. default:
  318. {
  319. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  320. lua_error(state);
  321. break;
  322. }
  323. }
  324. return 0;
  325. }
  326. int lua_Gamepad_isVirtual(lua_State* state)
  327. {
  328. // Get the number of parameters.
  329. int paramCount = lua_gettop(state);
  330. // Attempt to match the parameters to a valid binding.
  331. switch (paramCount)
  332. {
  333. case 1:
  334. {
  335. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL))
  336. {
  337. Gamepad* instance = getInstance(state);
  338. bool result = instance->isVirtual();
  339. // Push the return value onto the stack.
  340. lua_pushboolean(state, result);
  341. return 1;
  342. }
  343. else
  344. {
  345. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  346. lua_error(state);
  347. }
  348. break;
  349. }
  350. default:
  351. {
  352. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  353. lua_error(state);
  354. break;
  355. }
  356. }
  357. return 0;
  358. }
  359. int lua_Gamepad_update(lua_State* state)
  360. {
  361. // Get the number of parameters.
  362. int paramCount = lua_gettop(state);
  363. // Attempt to match the parameters to a valid binding.
  364. switch (paramCount)
  365. {
  366. case 2:
  367. {
  368. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TNIL) &&
  369. lua_type(state, 2) == LUA_TNUMBER)
  370. {
  371. // Get parameter 1 off the stack.
  372. float param1 = (float)luaL_checknumber(state, 2);
  373. Gamepad* instance = getInstance(state);
  374. instance->update(param1);
  375. return 0;
  376. }
  377. else
  378. {
  379. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  380. lua_error(state);
  381. }
  382. break;
  383. }
  384. default:
  385. {
  386. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  387. lua_error(state);
  388. break;
  389. }
  390. }
  391. return 0;
  392. }
  393. }