ScriptController.h 34 KB

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