ScriptController.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. #ifndef SCRIPTCONTROLLER_H
  2. #define SCRIPTCONTROLLER_H
  3. #include "Base.h"
  4. #include "Gamepad.h"
  5. namespace gameplay
  6. {
  7. /**
  8. * Controls and manages all scripts.
  9. */
  10. class ScriptController
  11. {
  12. friend class Game;
  13. friend class Platform;
  14. public:
  15. /**
  16. * Represents a C++ object from within Lua.
  17. * @script{ignore}
  18. */
  19. struct LuaObject
  20. {
  21. /** The actual object instance. */
  22. void* instance;
  23. /** Whether object is owned by Lua. */
  24. bool owns;
  25. };
  26. /**
  27. * Calls the specifed Lua function using the given parameters.
  28. *
  29. * @param func The name of the function to call.
  30. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  31. * - 'b' - bool
  32. * - 'c' - char
  33. * - 'h' - short
  34. * - 'i' - int
  35. * - 'l' - long
  36. * - 'f' - float
  37. * - 'd' - double
  38. * - 'ui' - unsigned int
  39. * - 'ul' - unsigned long
  40. * - 'uc' - unsigned char
  41. * - 'uh' - unsigned short
  42. * - 's' - string
  43. * - 'p' - pointer
  44. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  45. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  46. * @return The return value of the executed Lua function.
  47. */
  48. template<typename T> T executeFunction(const char* func, const char* args, ...);
  49. /**
  50. * Used to specify the pointer type for executing Lua functions that return pointers.
  51. * @script{ignore}
  52. */
  53. struct Type
  54. {
  55. /** Constructor. */
  56. explicit Type(const char* type) : type(type) {}
  57. /** The name of the type. */
  58. const char* type;
  59. };
  60. /**
  61. * Calls the specifed Lua function using the given parameters.
  62. *
  63. * @param type The class of the return value pointer.
  64. * @param func The name of the function to call.
  65. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  66. * - 'b' - bool
  67. * - 'c' - char
  68. * - 'h' - short
  69. * - 'i' - int
  70. * - 'l' - long
  71. * - 'f' - float
  72. * - 'd' - double
  73. * - 'ui' - unsigned int
  74. * - 'ul' - unsigned long
  75. * - 'uc' - unsigned char
  76. * - 'uh' - unsigned short
  77. * - 's' - string
  78. * - 'p' - pointer
  79. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  80. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  81. * @return The return value of the executed Lua function.
  82. */
  83. template<typename T> T* executeFunction(const Type& type, const char* func, const char* args, ...);
  84. /**
  85. * Gets the global instance of the script controller.
  86. *
  87. * @return The global instance of the script controller (NULL if it hasn't yet been created).
  88. */
  89. static ScriptController* getInstance();
  90. /**
  91. * Loads the given script file and executes its global code.
  92. *
  93. * @param path The path to the script.
  94. */
  95. void loadScript(const char* path);
  96. /**
  97. * Gets a pointer to a bool (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  98. *
  99. * @param index The stack index.
  100. * @return The pointer.
  101. */
  102. bool* getBoolPointer(int index);
  103. /**
  104. * Gets a pointer to a short (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  105. *
  106. * @param index The stack index.
  107. * @return The pointer.
  108. */
  109. short* getShortPointer(int index);
  110. /**
  111. * Gets a pointer to an int (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  112. *
  113. * @param index The stack index.
  114. * @return The pointer.
  115. */
  116. int* getIntPointer(int index);
  117. /**
  118. * Gets a pointer to a long (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  119. *
  120. * @param index The stack index.
  121. * @return The pointer.
  122. */
  123. long* getLongPointer(int index);
  124. /**
  125. * Gets a pointer to an unsigned char (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  126. *
  127. * @param index The stack index.
  128. * @return The pointer.
  129. */
  130. unsigned char* getUnsignedCharPointer(int index);
  131. /**
  132. * Gets a pointer to an unsigned short (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  133. *
  134. * @param index The stack index.
  135. * @return The pointer.
  136. */
  137. unsigned short* getUnsignedShortPointer(int index);
  138. /**
  139. * Gets a pointer to an unsigned int (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  140. *
  141. * @param index The stack index.
  142. * @return The pointer.
  143. */
  144. unsigned int* getUnsignedIntPointer(int index);
  145. /**
  146. * Gets a pointer to an unsigned long (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  147. *
  148. * @param index The stack index.
  149. * @return The pointer.
  150. */
  151. unsigned long* getUnsignedLongPointer(int index);
  152. /**
  153. * Gets a pointer to a float (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  154. *
  155. * @param index The stack index.
  156. * @return The pointer.
  157. */
  158. float* getFloatPointer(int index);
  159. /**
  160. * Gets a pointer to a double (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  161. *
  162. * @param index The stack index.
  163. * @return The pointer.
  164. */
  165. double* getDoublePointer(int index);
  166. /**
  167. * Gets an object pointer of the given type for the given stack index.
  168. *
  169. * @param type The type of object pointer to retrieve.
  170. * @param index The stack index.
  171. * @param nonNull Whether the pointer must be non-null (e.g. if the parameter we
  172. * are retreiving is actually a reference or by-value parameter).
  173. * @return The object pointer or <code>NULL</code> if the data at the stack index
  174. * is not an object or if the object is not derived from the given type.
  175. * @script{ignore}
  176. */
  177. template<typename T> T* getObjectPointer(int index, const char* type, bool nonNull);
  178. /**
  179. * Gets a string for the given stack index.
  180. *
  181. * @param index The stack index.
  182. * @param isStdString Whether the string being retrieved is a std::string object or not.
  183. * @return The string or <code>NULL</code>.
  184. * @script{ignore}
  185. */
  186. const char* getString(int index, bool isStdString);
  187. /**
  188. * Gets the global boolean variable with the given name.
  189. *
  190. * @param name The name of the variable.
  191. * @return The global boolean variable.
  192. * @script{ignore}
  193. */
  194. bool getBool(const char* name);
  195. /**
  196. * Gets the global char variable with the given name.
  197. *
  198. * @param name The name of the variable.
  199. * @return The global char variable.
  200. * @script{ignore}
  201. */
  202. char getChar(const char* name);
  203. /**
  204. * Gets the global short variable with the given name.
  205. *
  206. * @param name The name of the variable.
  207. * @return The global short variable.
  208. * @script{ignore}
  209. */
  210. short getShort(const char* name);
  211. /**
  212. * Gets the global int variable with the given name.
  213. *
  214. * @param name The name of the variable.
  215. * @return The global int variable.
  216. * @script{ignore}
  217. */
  218. int getInt(const char* name);
  219. /**
  220. * Gets the global long variable with the given name.
  221. *
  222. * @param name The name of the variable.
  223. * @return The global long variable.
  224. * @script{ignore}
  225. */
  226. long getLong(const char* name);
  227. /**
  228. * Gets the global unsigned char variable with the given name.
  229. *
  230. * @param name The name of the variable.
  231. * @return The global unsigned char variable.
  232. * @script{ignore}
  233. */
  234. unsigned char getUnsignedChar(const char* name);
  235. /**
  236. * Gets the global unsigned short variable with the given name.
  237. *
  238. * @param name The name of the variable.
  239. * @return The global unsigned short variable.
  240. * @script{ignore}
  241. */
  242. unsigned short getUnsignedShort(const char* name);
  243. /**
  244. * Gets the global unsigned int variable with the given name.
  245. *
  246. * @param name The name of the variable.
  247. * @return The global unsigned int variable.
  248. * @script{ignore}
  249. */
  250. unsigned int getUnsignedInt(const char* name);
  251. /**
  252. * Gets the global unsigned long variable with the given name.
  253. *
  254. * @param name The name of the variable.
  255. * @return The global unsigned long variable.
  256. * @script{ignore}
  257. */
  258. unsigned long getUnsignedLong(const char* name);
  259. /**
  260. * Gets the global float variable with the given name.
  261. *
  262. * @param name The name of the variable.
  263. * @return The global float variable.
  264. * @script{ignore}
  265. */
  266. float getFloat(const char* name);
  267. /**
  268. * Gets the global double variable with the given name.
  269. *
  270. * @param name The name of the variable.
  271. * @return The global double variable.
  272. * @script{ignore}
  273. */
  274. double getDouble(const char* name);
  275. /**
  276. * Gets the global string variable with the given name.
  277. *
  278. * @param name The name of the variable.
  279. * @return The global string variable.
  280. * @script{ignore}
  281. */
  282. const char* getString(const char* name);
  283. /**
  284. * Gets the global pointer variable of the given type with the given name.
  285. *
  286. * @param type The type of the variable in Lua.
  287. * @param name The name of the variable.
  288. * @return The global pointer variable.
  289. * @script{ignore}
  290. */
  291. template<typename T>T* getObjectPointer(const char* type, const char* name);
  292. /**
  293. * Sets the global boolean variable with the given name.
  294. *
  295. * @param name The name of the variable.
  296. * @param v The boolean variable.
  297. * @script{ignore}
  298. */
  299. void setBool(const char* name, bool v);
  300. /**
  301. * Sets the global char variable with the given name.
  302. *
  303. * @param name The name of the variable.
  304. * @param v The char variable.
  305. * @script{ignore}
  306. */
  307. void setChar(const char* name, char v);
  308. /**
  309. * Sets the global short variable with the given name.
  310. *
  311. * @param name The name of the variable.
  312. * @param v The short variable.
  313. * @script{ignore}
  314. */
  315. void setShort(const char* name, short v);
  316. /**
  317. * Sets the global int variable with the given name.
  318. *
  319. * @param name The name of the variable.
  320. * @param v The int variable.
  321. * @script{ignore}
  322. */
  323. void setInt(const char* name, int v);
  324. /**
  325. * Sets the global long variable with the given name.
  326. *
  327. * @param name The name of the variable.
  328. * @param v The long variable.
  329. * @script{ignore}
  330. */
  331. void setLong(const char* name, long v);
  332. /**
  333. * Gets the global unsigned char variable with the given name.
  334. *
  335. * @param name The name of the variable.
  336. * @param v The unsigned char variable.
  337. * @script{ignore}
  338. */
  339. void setUnsignedChar(const char* name, unsigned char v);
  340. /**
  341. * Sets the global unsigned short variable with the given name.
  342. *
  343. * @param name The name of the variable.
  344. * @param v The unsigned short variable.
  345. * @script{ignore}
  346. */
  347. void setUnsignedShort(const char* name, unsigned short v);
  348. /**
  349. * Sets the global unsigned int variable with the given name.
  350. *
  351. * @param name The name of the variable.
  352. * @param v The unsigned int variable.
  353. * @script{ignore}
  354. */
  355. void setUnsignedInt(const char* name, unsigned int v);
  356. /**
  357. * Sets the global unsigned long variable with the given name.
  358. *
  359. * @param name The name of the variable.
  360. * @param v The unsigned long variable.
  361. * @script{ignore}
  362. */
  363. void setUnsignedLong(const char* name, unsigned long v);
  364. /**
  365. * Sets the global float variable with the given name.
  366. *
  367. * @param name The name of the variable.
  368. * @param v The float variable.
  369. * @script{ignore}
  370. */
  371. void setFloat(const char* name, float v);
  372. /**
  373. * Sets the global double variable with the given name.
  374. *
  375. * @param name The name of the variable.
  376. * @param v The double variable.
  377. * @script{ignore}
  378. */
  379. void setDouble(const char* name, double v);
  380. /**
  381. * Sets the global string variable with the given name.
  382. *
  383. * @param name The name of the variable.
  384. * @param v The string variable.
  385. * @script{ignore}
  386. */
  387. void setString(const char* name, const char* v);
  388. /**
  389. * Sets the global pointer variable of the given type with the given name.
  390. *
  391. * @param type The type of the variable in Lua.
  392. * @param name The name of the variable.
  393. * @param v The pointer variable.
  394. * @script{ignore}
  395. */
  396. template<typename T>void setObjectPointer(const char* type, const char* name, T* v);
  397. /**
  398. * Registers the given library with Lua.
  399. *
  400. * @param name The name of the library from within Lua.
  401. * @param functions The library function mapping (Lua function names to C++ functions).
  402. * @script{ignore}
  403. */
  404. void registerLibrary(const char* name, const luaL_Reg* functions);
  405. /**
  406. * Registers the given boolean constant as valid for the given scope path.
  407. *
  408. * @param name The name of the constant (what the user would use from Lua).
  409. * @param value The constant's value.
  410. * @param scopePath The list of containing classes, going inward from the most outer class.
  411. * @script{ignore}
  412. */
  413. void registerConstantBool(std::string name, bool value, std::vector<std::string> scopePath);
  414. /**
  415. * Registers the given number constant as valid for the given scope path.
  416. *
  417. * @param name The name of the constant (what the user would use from Lua).
  418. * @param value The constant's value.
  419. * @param scopePath The list of containing classes, going inward from the most outer class.
  420. * @script{ignore}
  421. */
  422. void registerConstantNumber(std::string name, double value, std::vector<std::string> scopePath);
  423. /**
  424. * Registers the given string constant as valid for the given scope path.
  425. *
  426. * @param name The name of the constant (what the user would use from Lua).
  427. * @param value The constant's value.
  428. * @param scopePath The list of containing classes, going inward from the most outer class.
  429. * @script{ignore}
  430. */
  431. void registerConstantString(std::string name, std::string value, std::vector<std::string> scopePath);
  432. /**
  433. * Registers the given class type with Lua.
  434. *
  435. * @param name The name of the class from within Lua.
  436. * @param members The library function mapping for all the member functions (Lua function names to C++ functions).
  437. * @param newFunction The function to call that creates an instance of the class.
  438. * @param deleteFunction The function to call that destroys an instance of the class.
  439. * @param statics The library function mapping for all the static functions (Lua function names to C++ functions).
  440. * @param scopePath For an inner class, this is a list of its containing classes, going inward from the most outer class.
  441. * @script{ignore}
  442. */
  443. void registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction, lua_CFunction deleteFunction, const luaL_Reg* statics,
  444. std::vector<std::string> scopePath = std::vector<std::string>());
  445. /**
  446. * Register a function with Lua.
  447. *
  448. * @param luaFunction The name of the function from within Lua.
  449. * @param cppFunction The C++ function pointer.
  450. * @script{ignore}
  451. */
  452. void registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  453. /**
  454. * Sets the global inheritance hierarchy.
  455. *
  456. * @param hierarchy The inheritance hierarchy stored as a map of class names
  457. * to a list of all derived class names.
  458. * @script{ignore}
  459. */
  460. void setGlobalHierarchy(std::map<std::string, std::vector<std::string> > hierarchy);
  461. /**
  462. * Checks that the parameter at the given stack position is a boolean and returns it.
  463. *
  464. * @param state The Lua state.
  465. * @param n The stack index.
  466. * @return The boolean (if successful; otherwise it logs an error).
  467. * @script{ignore}
  468. */
  469. static bool luaCheckBool(lua_State* state, int n);
  470. private:
  471. /**
  472. * Represents a Lua callback function binding.
  473. */
  474. enum ScriptCallback
  475. {
  476. INITIALIZE = 0,
  477. UPDATE,
  478. RENDER,
  479. FINALIZE,
  480. KEY_EVENT,
  481. MOUSE_EVENT,
  482. TOUCH_EVENT,
  483. GAMEPAD_EVENT,
  484. CALLBACK_COUNT,
  485. INVALID_CALLBACK = CALLBACK_COUNT
  486. };
  487. /**
  488. * Constructor.
  489. */
  490. ScriptController();
  491. /**
  492. * Copy constructor.
  493. */
  494. ScriptController(const ScriptController& copy);
  495. /**
  496. * Destructor.
  497. */
  498. ~ScriptController();
  499. /**
  500. * Callback for when the controller is initialized.
  501. */
  502. void initialize();
  503. /**
  504. * Initializes the game using the appropriate callback script (if it was specified).
  505. */
  506. void initializeGame();
  507. /*
  508. * Callback for when the controller is finalized.
  509. */
  510. void finalize();
  511. /**
  512. * Finalizes the game using the appropriate callback script (if it was specified).
  513. */
  514. void finalizeGame();
  515. /**
  516. * Callback for when the controller receives a frame update event.
  517. */
  518. void update(float elapsedTime);
  519. /**
  520. * Renders the game using the appropriate callback script (if it was specified).
  521. */
  522. void render(float elapsedTime);
  523. /**
  524. * Script keyboard callback on key events.
  525. *
  526. * @param evt The key event that occured.
  527. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  528. * If evt is KEY_CHAR then key is the unicode value of the character.
  529. *
  530. * @see Keyboard::KeyEvent
  531. * @see Keyboard::Key
  532. */
  533. void keyEvent(Keyboard::KeyEvent evt, int key);
  534. /**
  535. * Script touch callback on touch events.
  536. *
  537. * @param evt The touch event that occurred.
  538. * @param x The x position of the touch in pixels. Left edge is zero.
  539. * @param y The y position of the touch in pixels. Top edge is zero.
  540. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  541. *
  542. * @see Touch::TouchEvent
  543. */
  544. void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  545. /**
  546. * Script mouse callback on mouse events. If the game does not consume the mouse move event or left mouse click event
  547. * then it is interpreted as a touch event instead.
  548. *
  549. * @param evt The mouse event that occurred.
  550. * @param x The x position of the mouse in pixels. Left edge is zero.
  551. * @param y The y position of the mouse in pixels. Top edge is zero.
  552. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  553. *
  554. * @return True if the mouse event is consumed or false if it is not consumed.
  555. *
  556. * @see Mouse::MouseEvent
  557. */
  558. bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  559. /**
  560. * Script gamepad callback on gamepad events.
  561. *
  562. * @param evt The gamepad event that occurred.
  563. * @param gamepad the gamepad the event occurred on
  564. */
  565. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad);
  566. /**
  567. * Calls the specifed Lua function using the given parameters.
  568. *
  569. * @param resultCount The expected number of returned values.
  570. * @param func The name of the function to call.
  571. * @param args The argument signature of the function, as a string of the form
  572. * 'xxx', where each 'x' is a parameter type and must be one of:
  573. * - 'b' - bool
  574. * - 'c' - char
  575. * - 'h' - short
  576. * - 'i' - int
  577. * - 'l' - long
  578. * - 'f' - float
  579. * - 'd' - double
  580. * - 'ui' - unsigned int
  581. * - 'ul' - unsigned long
  582. * - 'uc' - unsigned char
  583. * - 'uh' - unsigned short
  584. * - 's' - string
  585. * - 'p' - pointer
  586. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  587. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  588. * @param list The variable argument list.
  589. */
  590. void executeFunctionHelper(int resultCount, const char* func, const char* args, va_list& list);
  591. /**
  592. * Registers the given script callback.
  593. *
  594. * @param callback The script callback to register for.
  595. * @param function The name of the function within the Lua script to call.
  596. */
  597. void registerCallback(ScriptCallback callback, std::string function);
  598. lua_State* _lua;
  599. unsigned int _returnCount;
  600. std::map<std::string, std::vector<std::string> > _hierarchy;
  601. static ScriptController* __instance;
  602. std::string* _callbacks[CALLBACK_COUNT];
  603. };
  604. /** Template specialization. */
  605. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...);
  606. /** Template specialization. */
  607. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...);
  608. /** Template specialization. */
  609. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...);
  610. /** Template specialization. */
  611. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...);
  612. /** Template specialization. */
  613. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...);
  614. /** Template specialization. */
  615. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...);
  616. /** Template specialization. */
  617. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...);
  618. /** Template specialization. */
  619. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...);
  620. /** Template specialization. */
  621. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...);
  622. /** Template specialization. */
  623. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...);
  624. /** Template specialization. */
  625. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...);
  626. /** Template specialization. */
  627. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...);
  628. /** Template specialization. */
  629. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...);
  630. }
  631. #include "ScriptController.inl"
  632. #endif