ScriptController.h 23 KB

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