ScriptController.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. GESTURE_SWIPE_EVENT,
  696. GESTURE_PINCH_EVENT,
  697. GESTURE_TAP_EVENT,
  698. GAMEPAD_EVENT,
  699. CALLBACK_COUNT,
  700. INVALID_CALLBACK = CALLBACK_COUNT
  701. };
  702. /**
  703. * Constructor.
  704. */
  705. ScriptController();
  706. /**
  707. * Copy constructor.
  708. */
  709. ScriptController(const ScriptController& copy);
  710. /**
  711. * Destructor.
  712. */
  713. ~ScriptController();
  714. /**
  715. * Callback for when the controller is initialized.
  716. */
  717. void initialize();
  718. /**
  719. * Initializes the game using the appropriate callback script (if it was specified).
  720. */
  721. void initializeGame();
  722. /*
  723. * Callback for when the controller is finalized.
  724. */
  725. void finalize();
  726. /**
  727. * Finalizes the game using the appropriate callback script (if it was specified).
  728. */
  729. void finalizeGame();
  730. /**
  731. * Callback for when the controller receives a frame update event.
  732. */
  733. void update(float elapsedTime);
  734. /**
  735. * Renders the game using the appropriate callback script (if it was specified).
  736. */
  737. void render(float elapsedTime);
  738. /**
  739. * Script callback for game resize events.
  740. *
  741. * @param width The new width of the game window content area.
  742. * @param height The new height of the game window content area.
  743. */
  744. void resizeEvent(unsigned int width, unsigned int height);
  745. /**
  746. * Script keyboard callback on key events.
  747. *
  748. * @param evt The key event that occurred.
  749. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  750. * If evt is KEY_CHAR then key is the unicode value of the character.
  751. *
  752. * @see Keyboard::KeyEvent
  753. * @see Keyboard::Key
  754. */
  755. void keyEvent(Keyboard::KeyEvent evt, int key);
  756. /**
  757. * Script touch callback on touch events.
  758. *
  759. * @param evt The touch event that occurred.
  760. * @param x The x position of the touch in pixels. Left edge is zero.
  761. * @param y The y position of the touch in pixels. Top edge is zero.
  762. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  763. *
  764. * @see Touch::TouchEvent
  765. */
  766. void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  767. /**
  768. * Script mouse callback on mouse events. If the game does not consume the mouse move event or left mouse click event
  769. * then it is interpreted as a touch event instead.
  770. *
  771. * @param evt The mouse event that occurred.
  772. * @param x The x position of the mouse in pixels. Left edge is zero.
  773. * @param y The y position of the mouse in pixels. Top edge is zero.
  774. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  775. *
  776. * @return True if the mouse event is consumed or false if it is not consumed.
  777. *
  778. * @see Mouse::MouseEvent
  779. */
  780. bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  781. /**
  782. * Script callback for Gesture::SWIPE events.
  783. *
  784. * @param x The x-coordinate of the start of the swipe.
  785. * @param y The y-coordinate of the start of the swipe.
  786. * @param direction The direction of the swipe
  787. *
  788. * @see Gesture::SWIPE_DIRECTION_UP
  789. * @see Gesture::SWIPE_DIRECTION_DOWN
  790. * @see Gesture::SWIPE_DIRECTION_LEFT
  791. * @see Gesture::SWIPE_DIRECTION_RIGHT
  792. */
  793. void gestureSwipeEvent(int x, int y, int direction);
  794. /**
  795. * Script callback for Gesture::PINCH events.
  796. *
  797. * @param x The centroid x-coordinate of the pinch.
  798. * @param y The centroid y-coordinate of the pinch.
  799. * @param scale The scale of the pinch.
  800. */
  801. void gesturePinchEvent(int x, int y, float scale);
  802. /**
  803. * Script callback for Gesture::TAP events.
  804. *
  805. * @param x The x-coordinate of the tap.
  806. * @param y The y-coordinate of the tap.
  807. */
  808. void gestureTapEvent(int x, int y);
  809. /**
  810. * Script gamepad callback on gamepad events.
  811. *
  812. * @param evt The gamepad event that occurred.
  813. * @param gamepad the gamepad the event occurred on
  814. */
  815. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex = 0);
  816. /**
  817. * Calls the specified Lua function using the given parameters.
  818. *
  819. * @param resultCount The expected number of returned values.
  820. * @param func The name of the function to call.
  821. * @param args The argument signature of the function, as a string of the form
  822. * 'xxx', where each 'x' is a parameter type and must be one of:
  823. * - 'b' - bool
  824. * - 'c' - char
  825. * - 'h' - short
  826. * - 'i' - int
  827. * - 'l' - long
  828. * - 'f' - float
  829. * - 'd' - double
  830. * - 'ui' - unsigned int
  831. * - 'ul' - unsigned long
  832. * - 'uc' - unsigned char
  833. * - 'uh' - unsigned short
  834. * - 's' - string
  835. * - 'p' - pointer
  836. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  837. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  838. * @param list The variable argument list.
  839. */
  840. void executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list);
  841. /**
  842. * Converts the given string to a valid script callback enumeration value
  843. * or to ScriptController::INVALID_CALLBACK if there is no valid conversion.
  844. *
  845. * @param name The name of the callback.
  846. *
  847. * @return The corresponding callback enumeration value.
  848. */
  849. static ScriptController::ScriptCallback toCallback(const char* name);
  850. /**
  851. * Converts a Gameplay userdata value to the type with the given class name.
  852. * This function will change the metatable of the userdata value to the metatable that matches the given string.
  853. *
  854. * Example:
  855. * <code>
  856. * local launchButton = form:getControl("launch")
  857. * convert(launchButton, "Button")
  858. * print("Button text: " .. launchButton:getText())
  859. * </code>
  860. *
  861. * <code>
  862. * -- The signature of the lua function:
  863. * -- param: object A userdata object that represents a Gameplay object.
  864. * -- param: className The name of the class to convert the object to. (Examples: "Button", "PhysicsRigidBody")
  865. * function convert(object, className)
  866. * </code>
  867. *
  868. * @param state The Lua state.
  869. *
  870. * @return The number of values being returned by this function.
  871. *
  872. * @script{ignore}
  873. */
  874. static int convert(lua_State* state);
  875. // Friend functions (used by Lua script bindings).
  876. friend void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions);
  877. friend void ScriptUtil::registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath);
  878. friend void ScriptUtil::registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath);
  879. friend void ScriptUtil::registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath);
  880. friend void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  881. lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath);
  882. friend void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  883. friend void ScriptUtil::setGlobalHierarchyPair(const std::string& base, const std::string& derived);
  884. friend void ScriptUtil::addStringFromEnumConversionFunction(luaStringEnumConversionFunction stringFromEnum);
  885. friend ScriptUtil::LuaArray<bool> ScriptUtil::getBoolPointer(int index);
  886. friend ScriptUtil::LuaArray<short> ScriptUtil::getShortPointer(int index);
  887. friend ScriptUtil::LuaArray<int> ScriptUtil::getIntPointer(int index);
  888. friend ScriptUtil::LuaArray<long> ScriptUtil::getLongPointer(int index);
  889. friend ScriptUtil::LuaArray<unsigned char> ScriptUtil::getUnsignedCharPointer(int index);
  890. friend ScriptUtil::LuaArray<unsigned short> ScriptUtil::getUnsignedShortPointer(int index);
  891. friend ScriptUtil::LuaArray<unsigned int> ScriptUtil::getUnsignedIntPointer(int index);
  892. friend ScriptUtil::LuaArray<unsigned long> ScriptUtil::getUnsignedLongPointer(int index);
  893. friend ScriptUtil::LuaArray<float> ScriptUtil::getFloatPointer(int index);
  894. friend ScriptUtil::LuaArray<double> ScriptUtil::getDoublePointer(int index);
  895. template<typename T> friend ScriptUtil::LuaArray<T> ScriptUtil::getObjectPointer(int index, const char* type, bool nonNull, bool* success);
  896. friend const char* ScriptUtil::getString(int index, bool isStdString);
  897. lua_State* _lua;
  898. unsigned int _returnCount;
  899. std::map<std::string, std::vector<std::string> > _hierarchy;
  900. std::vector<std::string> _callbacks[CALLBACK_COUNT];
  901. std::set<std::string> _loadedScripts;
  902. std::vector<luaStringEnumConversionFunction> _stringFromEnum;
  903. };
  904. /** Template specialization. */
  905. template<> void ScriptController::executeFunction<void>(const char* func);
  906. /** Template specialization. */
  907. template<> bool ScriptController::executeFunction<bool>(const char* func);
  908. /** Template specialization. */
  909. template<> char ScriptController::executeFunction<char>(const char* func);
  910. /** Template specialization. */
  911. template<> short ScriptController::executeFunction<short>(const char* func);
  912. /** Template specialization. */
  913. template<> int ScriptController::executeFunction<int>(const char* func);
  914. /** Template specialization. */
  915. template<> long ScriptController::executeFunction<long>(const char* func);
  916. /** Template specialization. */
  917. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func);
  918. /** Template specialization. */
  919. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func);
  920. /** Template specialization. */
  921. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func);
  922. /** Template specialization. */
  923. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func);
  924. /** Template specialization. */
  925. template<> float ScriptController::executeFunction<float>(const char* func);
  926. /** Template specialization. */
  927. template<> double ScriptController::executeFunction<double>(const char* func);
  928. /** Template specialization. */
  929. template<> std::string ScriptController::executeFunction<std::string>(const char* func);
  930. /** Template specialization. */
  931. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...);
  932. /** Template specialization. */
  933. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...);
  934. /** Template specialization. */
  935. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...);
  936. /** Template specialization. */
  937. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...);
  938. /** Template specialization. */
  939. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...);
  940. /** Template specialization. */
  941. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...);
  942. /** Template specialization. */
  943. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...);
  944. /** Template specialization. */
  945. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...);
  946. /** Template specialization. */
  947. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...);
  948. /** Template specialization. */
  949. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...);
  950. /** Template specialization. */
  951. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...);
  952. /** Template specialization. */
  953. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...);
  954. /** Template specialization. */
  955. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...);
  956. /** Template specialization. */
  957. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list);
  958. /** Template specialization. */
  959. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list);
  960. /** Template specialization. */
  961. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list);
  962. /** Template specialization. */
  963. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list);
  964. /** Template specialization. */
  965. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list);
  966. /** Template specialization. */
  967. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list);
  968. /** Template specialization. */
  969. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list);
  970. /** Template specialization. */
  971. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list);
  972. /** Template specialization. */
  973. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list);
  974. /** Template specialization. */
  975. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list);
  976. /** Template specialization. */
  977. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list);
  978. /** Template specialization. */
  979. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list);
  980. /** Template specialization. */
  981. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list);
  982. }
  983. #include "ScriptController.inl"
  984. #endif