ScriptController.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. #ifndef SCRIPTCONTROLLER_H_
  2. #define SCRIPTCONTROLLER_H_
  3. #include "Base.h"
  4. #include "Game.h"
  5. //#include "Gamepad.h"
  6. #include "Control.h"
  7. namespace gameplay
  8. {
  9. /** Function pointer typedef for string-from-enum conversion functions. */
  10. typedef const char* (*luaStringEnumConversionFunction)(std::string&, unsigned int);
  11. /**
  12. * Functions and structures used by the generated Lua script bindings.
  13. */
  14. namespace ScriptUtil
  15. {
  16. /**
  17. * Represents a C++ object from within Lua.
  18. *
  19. * @script{ignore}
  20. */
  21. struct LuaObject
  22. {
  23. /** The actual object instance. */
  24. void* instance;
  25. /** Whether object is owned by Lua. */
  26. bool owns;
  27. };
  28. /**
  29. * Stores a Lua parameter of an array/pointer type that is passed from Lua to C.
  30. * Handles automatic cleanup of any temporary memory associated with the array.
  31. *
  32. * @script{ignore}
  33. */
  34. template <typename T>
  35. class LuaArray
  36. {
  37. public:
  38. /**
  39. * Creates a LuaArray to store a single pointer value.
  40. */
  41. LuaArray(T* param);
  42. /**
  43. * Allocates a LuaArray to store an array of values.
  44. *
  45. * Individual items in the array can be set using the
  46. * set(unsigned int, const T&) method.
  47. *
  48. * @param count Number of elements to store in the parameter.
  49. */
  50. LuaArray(int count);
  51. /**
  52. * Copy constructor.
  53. */
  54. LuaArray(const LuaArray<T>& copy);
  55. /**
  56. * Destructor.
  57. */
  58. ~LuaArray();
  59. /**
  60. * Assignment operator.
  61. */
  62. LuaArray<T>& operator = (const LuaArray<T>& p);
  63. /**
  64. * Copies the value of the object pointed to by itemPtr into the specified
  65. * index of this LuaArray's array.
  66. */
  67. void set(unsigned int index, const T* itemPtr);
  68. /**
  69. * Conversion operator from LuaArray to T*.
  70. */
  71. operator T* () const;
  72. /**
  73. * Overloads [] operator to get/set item value at index.
  74. */
  75. T& operator[] (int index);
  76. private:
  77. struct Data
  78. {
  79. Data() : value(NULL), refCount(0) { }
  80. T* value;
  81. int refCount;
  82. };
  83. Data* _data;
  84. };
  85. /**
  86. * Registers the given library with Lua.
  87. *
  88. * @param name The name of the library from within Lua.
  89. * @param functions The library function mapping (Lua function names to C++ functions).
  90. *
  91. * @script{ignore}
  92. */
  93. void registerLibrary(const char* name, const luaL_Reg* functions);
  94. /**
  95. * Registers the given boolean constant as valid for the given scope path.
  96. *
  97. * @param name The name of the constant (what the user would use from Lua).
  98. * @param value The constant's value.
  99. * @param scopePath The list of containing classes, going inward from the most outer class.
  100. *
  101. * @script{ignore}
  102. */
  103. void registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath);
  104. /**
  105. * Registers the given number constant as valid for the given scope path.
  106. *
  107. * @param name The name of the constant (what the user would use from Lua).
  108. * @param value The constant's value.
  109. * @param scopePath The list of containing classes, going inward from the most outer class.
  110. *
  111. * @script{ignore}
  112. */
  113. void registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath);
  114. /**
  115. * Registers the given string constant as valid for the given scope path.
  116. *
  117. * @param name The name of the constant (what the user would use from Lua).
  118. * @param value The constant's value.
  119. * @param scopePath The list of containing classes, going inward from the most outer class.
  120. *
  121. * @script{ignore}
  122. */
  123. void registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath);
  124. /**
  125. * Registers the given class type with Lua.
  126. *
  127. * @param name The name of the class from within Lua.
  128. * @param members The library function mapping for all the member functions (Lua function names to C++ functions).
  129. * @param newFunction The function to call that creates an instance of the class.
  130. * @param deleteFunction The function to call that destroys an instance of the class.
  131. * @param statics The library function mapping for all the static functions (Lua function names to C++ functions).
  132. * @param scopePath For an inner class, this is a list of its containing classes, going inward from the most outer class.
  133. *
  134. * @script{ignore}
  135. */
  136. void registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction, lua_CFunction deleteFunction, const luaL_Reg* statics,
  137. const std::vector<std::string>& scopePath);
  138. /**
  139. * Register a function with Lua.
  140. *
  141. * @param luaFunction The name of the function from within Lua.
  142. * @param cppFunction The C++ function pointer.
  143. *
  144. * @script{ignore}
  145. */
  146. void registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  147. /**
  148. * Sets an inheritance pair within the global inheritance hierarchy (base, derived).
  149. *
  150. * @param base The base class of the inheritance pair.
  151. * @param derived The derived class of the inheritance pair.
  152. *
  153. * @script{ignore}
  154. */
  155. void setGlobalHierarchyPair(const std::string& base, const std::string& derived);
  156. /**
  157. * Adds the given function as a string-from-enumerated value conversion function.
  158. *
  159. * @param stringFromEnum The pointer to the string-from-enum conversion function.
  160. *
  161. * @script{ignore}
  162. */
  163. void addStringFromEnumConversionFunction(luaStringEnumConversionFunction stringFromEnum);
  164. /**
  165. * Gets a pointer to a bool (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  166. *
  167. * @param index The stack index.
  168. *
  169. * @return The pointer.
  170. *
  171. * @script{ignore}
  172. */
  173. LuaArray<bool> getBoolPointer(int index);
  174. /**
  175. * Gets a pointer to a short (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  176. *
  177. * @param index The stack index.
  178. *
  179. * @return The pointer.
  180. *
  181. * @script{ignore}
  182. */
  183. LuaArray<short> getShortPointer(int index);
  184. /**
  185. * Gets a pointer to an int (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  186. *
  187. * @param index The stack index.
  188. *
  189. * @return The pointer.
  190. *
  191. * @script{ignore}
  192. */
  193. LuaArray<int> getIntPointer(int index);
  194. /**
  195. * Gets a pointer to a long (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  196. *
  197. * @param index The stack index.
  198. *
  199. * @return The pointer.
  200. *
  201. * @script{ignore}
  202. */
  203. LuaArray<long> getLongPointer(int index);
  204. /**
  205. * Gets a pointer to an unsigned char (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  206. *
  207. * @param index The stack index.
  208. *
  209. * @return The pointer.
  210. *
  211. * @script{ignore}
  212. */
  213. LuaArray<unsigned char> getUnsignedCharPointer(int index);
  214. /**
  215. * Gets a pointer to an unsigned short (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  216. *
  217. * @param index The stack index.
  218. *
  219. * @return The pointer.
  220. *
  221. * @script{ignore}
  222. */
  223. LuaArray<unsigned short> getUnsignedShortPointer(int index);
  224. /**
  225. * Gets a pointer to an unsigned int (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  226. *
  227. * @param index The stack index.
  228. *
  229. * @return The pointer.
  230. *
  231. * @script{ignore}
  232. */
  233. LuaArray<unsigned int> getUnsignedIntPointer(int index);
  234. /**
  235. * Gets a pointer to an unsigned long (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  236. *
  237. * @param index The stack index.
  238. *
  239. * @return The pointer.
  240. *
  241. * @script{ignore}
  242. */
  243. LuaArray<unsigned long> getUnsignedLongPointer(int index);
  244. /**
  245. * Gets a pointer to a float (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  246. *
  247. * @param index The stack index.
  248. *
  249. * @return The pointer.
  250. *
  251. * @script{ignore}
  252. */
  253. LuaArray<float> getFloatPointer(int index);
  254. /**
  255. * Gets a pointer to a double (as an array-use SAFE_DELETE_ARRAY to clean up) for the given stack index.
  256. *
  257. * @param index The stack index.
  258. *
  259. * @return The pointer.
  260. *
  261. * @script{ignore}
  262. */
  263. LuaArray<double> getDoublePointer(int index);
  264. /**
  265. * Gets an object pointer of the given type for the given stack index.
  266. *
  267. * @param type The type of object pointer to retrieve.
  268. * @param index The stack index.
  269. * @param nonNull Whether the pointer must be non-null (e.g. if the parameter we
  270. * are retrieving is actually a reference or by-value parameter).
  271. * @param success An out parameter that is set to true if the Lua parameter was successfully
  272. * converted to a valid object, or false if it was unable to perform a valid conversion.
  273. *
  274. * @return The object pointer or <code>NULL</code> if the data at the stack index
  275. * is not an object or if the object is not derived from the given type.
  276. *
  277. * @script{ignore}
  278. */
  279. template <typename T>
  280. LuaArray<T> getObjectPointer(int index, const char* type, bool nonNull, bool* success);
  281. /**
  282. * Gets a string for the given stack index.
  283. *
  284. * @param index The stack index.
  285. * @param isStdString Whether the string being retrieved is a std::string object or not.
  286. *
  287. * @return The string or <code>NULL</code>.
  288. *
  289. * @script{ignore}
  290. */
  291. const char* getString(int index, bool isStdString);
  292. /**
  293. * Checks that the parameter at the given stack position is a boolean and returns it.
  294. *
  295. * @param state The Lua state.
  296. * @param n The stack index.
  297. *
  298. * @return The boolean (if successful; otherwise it logs an error).
  299. *
  300. * @script{ignore}
  301. */
  302. bool luaCheckBool(lua_State* state, int n);
  303. }
  304. /**
  305. * Controls and manages all scripts.
  306. */
  307. class ScriptController
  308. {
  309. friend class Game;
  310. friend class Platform;
  311. public:
  312. /**
  313. * Loads the given script file and executes its global code.
  314. *
  315. * @param path The path to the script.
  316. * @param forceReload Whether the script should be reloaded if it has already been loaded.
  317. */
  318. void loadScript(const char* path, bool forceReload = false);
  319. /**
  320. * Given a URL, loads the referenced file and returns the referenced function name.
  321. *
  322. * @param url The url to load.
  323. *
  324. * @return The function that the URL references.
  325. */
  326. std::string loadUrl(const char* url);
  327. /**
  328. * Registers the given script callback.
  329. *
  330. * The 'callback' parameter must be one of the supported global callback
  331. * event functions. The following strings are accepted: initialize, finalize,
  332. * update, render, resizeEvent, keyEvent, touchEvent, mouseEvent, gamepadEvent.
  333. * Signatures for the registered script function must match that of the
  334. * corresponding signatures of these events on the Game class.
  335. *
  336. * @param callback The script callback to register for.
  337. * @param function The name of the script function to register.
  338. */
  339. void registerCallback(const char* callback, const char* function);
  340. /**
  341. * Unregisters the given script callback.
  342. *
  343. * @param callback The script callback to unregister for.
  344. * @param function The name of the script function to unregister.
  345. *
  346. * @see registerCallback(const char*, const char*)
  347. */
  348. void unregisterCallback(const char* callback, const char* function);
  349. /**
  350. * Calls the specified no-parameter Lua function.
  351. *
  352. * @param func The name of the function to call.
  353. *
  354. * @return The return value of the executed Lua function.
  355. */
  356. template<typename T> T executeFunction(const char* func);
  357. /**
  358. * Calls the specified Lua function using the given parameters.
  359. *
  360. * @param func The name of the function to call.
  361. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  362. * - 'b' - bool
  363. * - 'c' - char
  364. * - 'h' - short
  365. * - 'i' - int
  366. * - 'l' - long
  367. * - 'f' - float
  368. * - 'd' - double
  369. * - 'ui' - unsigned int
  370. * - 'ul' - unsigned long
  371. * - 'uc' - unsigned char
  372. * - 'uh' - unsigned short
  373. * - 's' - string
  374. * - 'p' - pointer
  375. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  376. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  377. *
  378. * @return The return value of the executed Lua function.
  379. */
  380. template<typename T> T executeFunction(const char* func, const char* args, ...);
  381. /**
  382. * Calls the specified Lua function using the given parameters.
  383. *
  384. * @param func The name of the function to call.
  385. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  386. * - 'b' - bool
  387. * - 'c' - char
  388. * - 'h' - short
  389. * - 'i' - int
  390. * - 'l' - long
  391. * - 'f' - float
  392. * - 'd' - double
  393. * - 'ui' - unsigned int
  394. * - 'ul' - unsigned long
  395. * - 'uc' - unsigned char
  396. * - 'uh' - unsigned short
  397. * - 's' - string
  398. * - 'p' - pointer
  399. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  400. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  401. * @param list The variable argument list containing the function's parameters.
  402. *
  403. * @return The return value of the executed Lua function.
  404. */
  405. template<typename T> T executeFunction(const char* func, const char* args, va_list* list);
  406. /**
  407. * Gets the global boolean script variable with the given name.
  408. *
  409. * @param name The name of the variable.
  410. * @param defaultValue The default value to return if the variable is not a bool.
  411. *
  412. * @return The global boolean script variable.
  413. *
  414. * @script{ignore}
  415. */
  416. bool getBool(const char* name, bool defaultValue = false);
  417. /**
  418. * Gets the global char script variable with the given name.
  419. *
  420. * @param name The name of the variable.
  421. * @param defaultValue The default value to return if the variable is not a number.
  422. *
  423. * @return The global char script variable.
  424. *
  425. * @script{ignore}
  426. */
  427. char getChar(const char* name, char defaultValue = 0);
  428. /**
  429. * Gets the global short script variable with the given name.
  430. *
  431. * @param name The name of the variable.
  432. * @param defaultValue The default value to return if the variable is not a number.
  433. *
  434. * @return The global short script variable.
  435. *
  436. * @script{ignore}
  437. */
  438. short getShort(const char* name, short defaultValue = 0);
  439. /**
  440. * Gets the global int script variable with the given name.
  441. *
  442. * @param name The name of the variable.
  443. * @param defaultValue The default value to return if the variable is not a number.
  444. *
  445. * @return The global int script variable.
  446. *
  447. * @script{ignore}
  448. */
  449. int getInt(const char* name, int defaultValue = 0);
  450. /**
  451. * Gets the global long script variable with the given name.
  452. *
  453. * @param name The name of the variable.
  454. * @param defaultValue The default value to return if the variable is not a number.
  455. *
  456. * @return The global long script variable.
  457. *
  458. * @script{ignore}
  459. */
  460. long getLong(const char* name, long defaultValue = 0);
  461. /**
  462. * Gets the global unsigned char script variable with the given name.
  463. *
  464. * @param name The name of the variable.
  465. * @param defaultValue The default value to return if the variable is not a number.
  466. *
  467. * @return The global unsigned char script variable.
  468. *
  469. * @script{ignore}
  470. */
  471. unsigned char getUnsignedChar(const char* name, unsigned char defaultValue = 0);
  472. /**
  473. * Gets the global unsigned short script variable with the given name.
  474. *
  475. * @param name The name of the variable.
  476. * @param defaultValue The default value to return if the variable is not a number.
  477. *
  478. * @return The global unsigned short script variable.
  479. *
  480. * @script{ignore}
  481. */
  482. unsigned short getUnsignedShort(const char* name, unsigned short defaultValue = 0);
  483. /**
  484. * Gets the global unsigned int script variable with the given name.
  485. *
  486. * @param name The name of the variable.
  487. * @param defaultValue The default value to return if the variable is not a number.
  488. *
  489. * @return The global unsigned int script variable.
  490. *
  491. * @script{ignore}
  492. */
  493. unsigned int getUnsignedInt(const char* name, unsigned int defaultValue = 0);
  494. /**
  495. * Gets the global unsigned long script variable with the given name.
  496. *
  497. * @param name The name of the variable.
  498. * @param defaultValue The default value to return if the variable is not a number.
  499. *
  500. * @return The global unsigned long script variable.
  501. *
  502. * @script{ignore}
  503. */
  504. unsigned long getUnsignedLong(const char* name, unsigned long defaultValue = 0);
  505. /**
  506. * Gets the global float script variable with the given name.
  507. *
  508. * @param name The name of the variable.
  509. * @param defaultValue The default value to return if the variable is not a number.
  510. *
  511. * @return The global float script variable.
  512. *
  513. * @script{ignore}
  514. */
  515. float getFloat(const char* name, float defaultValue = 0);
  516. /**
  517. * Gets the global double script variable with the given name.
  518. *
  519. * @param name The name of the variable.
  520. * @param defaultValue The default value to return if the variable is not a number.
  521. *
  522. * @return The global double script variable.
  523. *
  524. * @script{ignore}
  525. */
  526. double getDouble(const char* name, double defaultValue = 0);
  527. /**
  528. * Gets the global string variable with the given name.
  529. *
  530. * @param name The name of the variable.
  531. *
  532. * @return The string variable or NULL if the variable is not a string.
  533. *
  534. * @script{ignore}
  535. */
  536. const char* getString(const char* name);
  537. /**
  538. * Gets the global pointer script variable of the given type with the given name.
  539. *
  540. * @param type The type of the variable in Lua.
  541. * @param name The name of the variable.
  542. *
  543. * @return The global pointer script variable.
  544. *
  545. * @script{ignore}
  546. */
  547. template<typename T>T* getObjectPointer(const char* type, const char* name);
  548. /**
  549. * Sets the global boolean script variable with the given name to the given value.
  550. *
  551. * @param name The name of the script variable.
  552. * @param v The boolean value.
  553. *
  554. * @script{ignore}
  555. */
  556. void setBool(const char* name, bool v);
  557. /**
  558. * Sets the global char script variable with the given name to the given value.
  559. *
  560. * @param name The name of the script variable.
  561. * @param v The char value.
  562. *
  563. * @script{ignore}
  564. */
  565. void setChar(const char* name, char v);
  566. /**
  567. * Sets the global short script variable with the given name to the given value.
  568. *
  569. * @param name The name of the script variable.
  570. * @param v The short value.
  571. *
  572. * @script{ignore}
  573. */
  574. void setShort(const char* name, short v);
  575. /**
  576. * Sets the global int script variable with the given name to the given value.
  577. *
  578. * @param name The name of the script variable.
  579. * @param v The int value.
  580. *
  581. * @script{ignore}
  582. */
  583. void setInt(const char* name, int v);
  584. /**
  585. * Sets the global long script variable with the given name to the given value.
  586. *
  587. * @param name The name of the script variable.
  588. * @param v The long value.
  589. *
  590. * @script{ignore}
  591. */
  592. void setLong(const char* name, long v);
  593. /**
  594. * Gets the global unsigned char script variable with the given name to the given value.
  595. *
  596. * @param name The name of the script variable.
  597. * @param v The unsigned char value.
  598. *
  599. * @script{ignore}
  600. */
  601. void setUnsignedChar(const char* name, unsigned char v);
  602. /**
  603. * Sets the global unsigned short script variable with the given name to the given value.
  604. *
  605. * @param name The name of the script variable.
  606. * @param v The unsigned short value.
  607. *
  608. * @script{ignore}
  609. */
  610. void setUnsignedShort(const char* name, unsigned short v);
  611. /**
  612. * Sets the global unsigned int script variable with the given name to the given value.
  613. *
  614. * @param name The name of the script variable.
  615. * @param v The unsigned int value.
  616. *
  617. * @script{ignore}
  618. */
  619. void setUnsignedInt(const char* name, unsigned int v);
  620. /**
  621. * Sets the global unsigned long script variable with the given name to the given value.
  622. *
  623. * @param name The name of the script variable.
  624. * @param v The unsigned long value.
  625. *
  626. * @script{ignore}
  627. */
  628. void setUnsignedLong(const char* name, unsigned long v);
  629. /**
  630. * Sets the global float script variable with the given name to the given value.
  631. *
  632. * @param name The name of the script variable.
  633. * @param v The float value.
  634. *
  635. * @script{ignore}
  636. */
  637. void setFloat(const char* name, float v);
  638. /**
  639. * Sets the global double script variable with the given name to the given value.
  640. *
  641. * @param name The name of the script variable.
  642. * @param v The double value.
  643. *
  644. * @script{ignore}
  645. */
  646. void setDouble(const char* name, double v);
  647. /**
  648. * Sets the global string script variable with the given name to the given value.
  649. *
  650. * @param name The name of the script variable.
  651. * @param v The string value.
  652. *
  653. * @script{ignore}
  654. */
  655. void setString(const char* name, const char* v);
  656. /**
  657. * Sets the global pointer script variable of the given type with the given name to the given value.
  658. *
  659. * @param type The type of the script variable.
  660. * @param name The name of the variable.
  661. * @param v The pointer value.
  662. *
  663. * @script{ignore}
  664. */
  665. template<typename T>void setObjectPointer(const char* type, const char* name, T* v);
  666. /**
  667. * Prints the string to the platform's output stream or log file.
  668. * Used for overriding Lua's print function.
  669. *
  670. * @param str The string to print.
  671. */
  672. static void print(const char* str);
  673. /**
  674. * Prints the strings to the platform's output stream or log file.
  675. * Used for overriding Lua's print function.
  676. *
  677. * @param str1 The first string to print.
  678. * @param str2 The second string to print on the same line as str1.
  679. */
  680. static void print(const char* str1, const char* str2);
  681. private:
  682. /**
  683. * Represents a callback function that can be registered for.
  684. */
  685. enum ScriptCallback
  686. {
  687. INITIALIZE = 0,
  688. UPDATE,
  689. RENDER,
  690. FINALIZE,
  691. RESIZE_EVENT,
  692. KEY_EVENT,
  693. MOUSE_EVENT,
  694. TOUCH_EVENT,
  695. GAMEPAD_EVENT,
  696. CALLBACK_COUNT,
  697. INVALID_CALLBACK = CALLBACK_COUNT
  698. };
  699. /**
  700. * Constructor.
  701. */
  702. ScriptController();
  703. /**
  704. * Copy constructor.
  705. */
  706. ScriptController(const ScriptController& copy);
  707. /**
  708. * Destructor.
  709. */
  710. ~ScriptController();
  711. /**
  712. * Callback for when the controller is initialized.
  713. */
  714. void initialize();
  715. /**
  716. * Initializes the game using the appropriate callback script (if it was specified).
  717. */
  718. void initializeGame();
  719. /*
  720. * Callback for when the controller is finalized.
  721. */
  722. void finalize();
  723. /**
  724. * Finalizes the game using the appropriate callback script (if it was specified).
  725. */
  726. void finalizeGame();
  727. /**
  728. * Callback for when the controller receives a frame update event.
  729. */
  730. void update(float elapsedTime);
  731. /**
  732. * Renders the game using the appropriate callback script (if it was specified).
  733. */
  734. void render(float elapsedTime);
  735. /**
  736. * Script callback for game resize events.
  737. *
  738. * @param width The new width of the game window content area.
  739. * @param height The new height of the game window content area.
  740. */
  741. void resizeEvent(unsigned int width, unsigned int height);
  742. /**
  743. * Script keyboard callback on key events.
  744. *
  745. * @param evt The key event that occurred.
  746. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  747. * If evt is KEY_CHAR then key is the unicode value of the character.
  748. *
  749. * @see Keyboard::KeyEvent
  750. * @see Keyboard::Key
  751. */
  752. void keyEvent(Keyboard::KeyEvent evt, int key);
  753. /**
  754. * Script touch callback on touch events.
  755. *
  756. * @param evt The touch event that occurred.
  757. * @param x The x position of the touch in pixels. Left edge is zero.
  758. * @param y The y position of the touch in pixels. Top edge is zero.
  759. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  760. *
  761. * @see Touch::TouchEvent
  762. */
  763. void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  764. /**
  765. * Script mouse callback on mouse events. If the game does not consume the mouse move event or left mouse click event
  766. * then it is interpreted as a touch event instead.
  767. *
  768. * @param evt The mouse event that occurred.
  769. * @param x The x position of the mouse in pixels. Left edge is zero.
  770. * @param y The y position of the mouse in pixels. Top edge is zero.
  771. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  772. *
  773. * @return True if the mouse event is consumed or false if it is not consumed.
  774. *
  775. * @see Mouse::MouseEvent
  776. */
  777. bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  778. /**
  779. * Script gamepad callback on gamepad events.
  780. *
  781. * @param evt The gamepad event that occurred.
  782. * @param gamepad the gamepad the event occurred on
  783. */
  784. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex = 0);
  785. /**
  786. * Calls the specified Lua function using the given parameters.
  787. *
  788. * @param resultCount The expected number of returned values.
  789. * @param func The name of the function to call.
  790. * @param args The argument signature of the function, as a string of the form
  791. * 'xxx', where each 'x' is a parameter type and must be one of:
  792. * - 'b' - bool
  793. * - 'c' - char
  794. * - 'h' - short
  795. * - 'i' - int
  796. * - 'l' - long
  797. * - 'f' - float
  798. * - 'd' - double
  799. * - 'ui' - unsigned int
  800. * - 'ul' - unsigned long
  801. * - 'uc' - unsigned char
  802. * - 'uh' - unsigned short
  803. * - 's' - string
  804. * - 'p' - pointer
  805. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  806. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  807. * @param list The variable argument list.
  808. */
  809. void executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list);
  810. /**
  811. * Converts the given string to a valid script callback enumeration value
  812. * or to ScriptController::INVALID_CALLBACK if there is no valid conversion.
  813. *
  814. * @param name The name of the callback.
  815. *
  816. * @return The corresponding callback enumeration value.
  817. */
  818. static ScriptController::ScriptCallback toCallback(const char* name);
  819. /**
  820. * Converts a Gameplay userdata value to the type with the given class name.
  821. * This function will change the metatable of the userdata value to the metatable that matches the given string.
  822. *
  823. * Example:
  824. * <code>
  825. * local launchButton = form:getControl("launch")
  826. * convert(launchButton, "Button")
  827. * print("Button text: " .. launchButton:getText())
  828. * </code>
  829. *
  830. * <code>
  831. * -- The signature of the lua function:
  832. * -- param: object A userdata object that represents a Gameplay object.
  833. * -- param: className The name of the class to convert the object to. (Examples: "Button", "PhysicsRigidBody")
  834. * function convert(object, className)
  835. * </code>
  836. *
  837. * @param state The Lua state.
  838. *
  839. * @return The number of values being returned by this function.
  840. *
  841. * @script{ignore}
  842. */
  843. static int convert(lua_State* state);
  844. // Friend functions (used by Lua script bindings).
  845. friend void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions);
  846. friend void ScriptUtil::registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath);
  847. friend void ScriptUtil::registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath);
  848. friend void ScriptUtil::registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath);
  849. friend void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  850. lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath);
  851. friend void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  852. friend void ScriptUtil::setGlobalHierarchyPair(const std::string& base, const std::string& derived);
  853. friend void ScriptUtil::addStringFromEnumConversionFunction(luaStringEnumConversionFunction stringFromEnum);
  854. friend ScriptUtil::LuaArray<bool> ScriptUtil::getBoolPointer(int index);
  855. friend ScriptUtil::LuaArray<short> ScriptUtil::getShortPointer(int index);
  856. friend ScriptUtil::LuaArray<int> ScriptUtil::getIntPointer(int index);
  857. friend ScriptUtil::LuaArray<long> ScriptUtil::getLongPointer(int index);
  858. friend ScriptUtil::LuaArray<unsigned char> ScriptUtil::getUnsignedCharPointer(int index);
  859. friend ScriptUtil::LuaArray<unsigned short> ScriptUtil::getUnsignedShortPointer(int index);
  860. friend ScriptUtil::LuaArray<unsigned int> ScriptUtil::getUnsignedIntPointer(int index);
  861. friend ScriptUtil::LuaArray<unsigned long> ScriptUtil::getUnsignedLongPointer(int index);
  862. friend ScriptUtil::LuaArray<float> ScriptUtil::getFloatPointer(int index);
  863. friend ScriptUtil::LuaArray<double> ScriptUtil::getDoublePointer(int index);
  864. template<typename T> friend ScriptUtil::LuaArray<T> ScriptUtil::getObjectPointer(int index, const char* type, bool nonNull, bool* success);
  865. friend const char* ScriptUtil::getString(int index, bool isStdString);
  866. lua_State* _lua;
  867. unsigned int _returnCount;
  868. std::map<std::string, std::vector<std::string> > _hierarchy;
  869. std::vector<std::string> _callbacks[CALLBACK_COUNT];
  870. std::set<std::string> _loadedScripts;
  871. std::vector<luaStringEnumConversionFunction> _stringFromEnum;
  872. };
  873. /** Template specialization. */
  874. template<> void ScriptController::executeFunction<void>(const char* func);
  875. /** Template specialization. */
  876. template<> bool ScriptController::executeFunction<bool>(const char* func);
  877. /** Template specialization. */
  878. template<> char ScriptController::executeFunction<char>(const char* func);
  879. /** Template specialization. */
  880. template<> short ScriptController::executeFunction<short>(const char* func);
  881. /** Template specialization. */
  882. template<> int ScriptController::executeFunction<int>(const char* func);
  883. /** Template specialization. */
  884. template<> long ScriptController::executeFunction<long>(const char* func);
  885. /** Template specialization. */
  886. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func);
  887. /** Template specialization. */
  888. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func);
  889. /** Template specialization. */
  890. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func);
  891. /** Template specialization. */
  892. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func);
  893. /** Template specialization. */
  894. template<> float ScriptController::executeFunction<float>(const char* func);
  895. /** Template specialization. */
  896. template<> double ScriptController::executeFunction<double>(const char* func);
  897. /** Template specialization. */
  898. template<> std::string ScriptController::executeFunction<std::string>(const char* func);
  899. /** Template specialization. */
  900. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...);
  901. /** Template specialization. */
  902. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...);
  903. /** Template specialization. */
  904. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...);
  905. /** Template specialization. */
  906. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...);
  907. /** Template specialization. */
  908. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...);
  909. /** Template specialization. */
  910. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...);
  911. /** Template specialization. */
  912. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...);
  913. /** Template specialization. */
  914. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...);
  915. /** Template specialization. */
  916. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...);
  917. /** Template specialization. */
  918. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...);
  919. /** Template specialization. */
  920. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...);
  921. /** Template specialization. */
  922. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...);
  923. /** Template specialization. */
  924. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...);
  925. /** Template specialization. */
  926. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list);
  927. /** Template specialization. */
  928. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list);
  929. /** Template specialization. */
  930. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list);
  931. /** Template specialization. */
  932. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list);
  933. /** Template specialization. */
  934. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list);
  935. /** Template specialization. */
  936. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list);
  937. /** Template specialization. */
  938. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list);
  939. /** Template specialization. */
  940. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list);
  941. /** Template specialization. */
  942. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list);
  943. /** Template specialization. */
  944. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list);
  945. /** Template specialization. */
  946. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list);
  947. /** Template specialization. */
  948. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list);
  949. /** Template specialization. */
  950. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list);
  951. }
  952. #include "ScriptController.inl"
  953. #endif