ScriptController.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. * Registers the given script callback.
  328. *
  329. * The 'callback' parameter must be one of the supported global callback
  330. * event functions. The following strings are accepted: initialize, finalize,
  331. * update, render, resizeEvent, keyEvent, touchEvent, mouseEvent, gamepadEvent.
  332. * Signatures for the registered script function must match that of the
  333. * corresponding signatures of these events on the Game class.
  334. *
  335. * @param callback The script callback to register for.
  336. * @param function The name of the script function to register.
  337. */
  338. void registerCallback(const char* callback, const char* function);
  339. /**
  340. * Unregisters the given script callback.
  341. *
  342. * @param callback The script callback to unregister for.
  343. * @param function The name of the script function to unregister.
  344. *
  345. * @see registerCallback(const char*, const char*)
  346. */
  347. void unregisterCallback(const char* callback, const char* function);
  348. /**
  349. * Calls the specified no-parameter Lua function.
  350. *
  351. * @param func The name of the function to call.
  352. *
  353. * @return The return value of the executed Lua function.
  354. */
  355. template<typename T> T executeFunction(const char* func);
  356. /**
  357. * Calls the specified Lua function using the given parameters.
  358. *
  359. * @param func The name of the function to call.
  360. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  361. * - 'b' - bool
  362. * - 'c' - char
  363. * - 'h' - short
  364. * - 'i' - int
  365. * - 'l' - long
  366. * - 'f' - float
  367. * - 'd' - double
  368. * - 'ui' - unsigned int
  369. * - 'ul' - unsigned long
  370. * - 'uc' - unsigned char
  371. * - 'uh' - unsigned short
  372. * - 's' - string
  373. * - 'p' - pointer
  374. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  375. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  376. *
  377. * @return The return value of the executed Lua function.
  378. */
  379. template<typename T> T executeFunction(const char* func, const char* args, ...);
  380. /**
  381. * Calls the specified Lua function using the given parameters.
  382. *
  383. * @param func The name of the function to call.
  384. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  385. * - 'b' - bool
  386. * - 'c' - char
  387. * - 'h' - short
  388. * - 'i' - int
  389. * - 'l' - long
  390. * - 'f' - float
  391. * - 'd' - double
  392. * - 'ui' - unsigned int
  393. * - 'ul' - unsigned long
  394. * - 'uc' - unsigned char
  395. * - 'uh' - unsigned short
  396. * - 's' - string
  397. * - 'p' - pointer
  398. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  399. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  400. * @param list The variable argument list containing the function's parameters.
  401. *
  402. * @return The return value of the executed Lua function.
  403. */
  404. template<typename T> T executeFunction(const char* func, const char* args, va_list* list);
  405. /**
  406. * Gets the global boolean 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 bool.
  410. *
  411. * @return The global boolean script variable.
  412. *
  413. * @script{ignore}
  414. */
  415. bool getBool(const char* name, bool defaultValue = false);
  416. /**
  417. * Gets the global char 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 char script variable.
  423. *
  424. * @script{ignore}
  425. */
  426. char getChar(const char* name, char defaultValue = 0);
  427. /**
  428. * Gets the global short 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 short script variable.
  434. *
  435. * @script{ignore}
  436. */
  437. short getShort(const char* name, short defaultValue = 0);
  438. /**
  439. * Gets the global int 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 int script variable.
  445. *
  446. * @script{ignore}
  447. */
  448. int getInt(const char* name, int defaultValue = 0);
  449. /**
  450. * Gets the global long 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 long script variable.
  456. *
  457. * @script{ignore}
  458. */
  459. long getLong(const char* name, long defaultValue = 0);
  460. /**
  461. * Gets the global unsigned char 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 char script variable.
  467. *
  468. * @script{ignore}
  469. */
  470. unsigned char getUnsignedChar(const char* name, unsigned char defaultValue = 0);
  471. /**
  472. * Gets the global unsigned short 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 short script variable.
  478. *
  479. * @script{ignore}
  480. */
  481. unsigned short getUnsignedShort(const char* name, unsigned short defaultValue = 0);
  482. /**
  483. * Gets the global unsigned int 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 unsigned int script variable.
  489. *
  490. * @script{ignore}
  491. */
  492. unsigned int getUnsignedInt(const char* name, unsigned int defaultValue = 0);
  493. /**
  494. * Gets the global unsigned long 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 unsigned long script variable.
  500. *
  501. * @script{ignore}
  502. */
  503. unsigned long getUnsignedLong(const char* name, unsigned long defaultValue = 0);
  504. /**
  505. * Gets the global float script variable with the given name.
  506. *
  507. * @param name The name of the variable.
  508. * @param defaultValue The default value to return if the variable is not a number.
  509. *
  510. * @return The global float script variable.
  511. *
  512. * @script{ignore}
  513. */
  514. float getFloat(const char* name, float defaultValue = 0);
  515. /**
  516. * Gets the global double script variable with the given name.
  517. *
  518. * @param name The name of the variable.
  519. * @param defaultValue The default value to return if the variable is not a number.
  520. *
  521. * @return The global double script variable.
  522. *
  523. * @script{ignore}
  524. */
  525. double getDouble(const char* name, double defaultValue = 0);
  526. /**
  527. * Gets the global string variable with the given name.
  528. *
  529. * @param name The name of the variable.
  530. *
  531. * @return The string variable or NULL if the variable is not a string.
  532. *
  533. * @script{ignore}
  534. */
  535. const char* getString(const char* name);
  536. /**
  537. * Gets the global pointer script variable of the given type with the given name.
  538. *
  539. * @param type The type of the variable in Lua.
  540. * @param name The name of the variable.
  541. *
  542. * @return The global pointer script variable.
  543. *
  544. * @script{ignore}
  545. */
  546. template<typename T>T* getObjectPointer(const char* type, const char* name);
  547. /**
  548. * Sets the global boolean script variable with the given name to the given value.
  549. *
  550. * @param name The name of the script variable.
  551. * @param v The boolean value.
  552. *
  553. * @script{ignore}
  554. */
  555. void setBool(const char* name, bool v);
  556. /**
  557. * Sets the global char script variable with the given name to the given value.
  558. *
  559. * @param name The name of the script variable.
  560. * @param v The char value.
  561. *
  562. * @script{ignore}
  563. */
  564. void setChar(const char* name, char v);
  565. /**
  566. * Sets the global short script variable with the given name to the given value.
  567. *
  568. * @param name The name of the script variable.
  569. * @param v The short value.
  570. *
  571. * @script{ignore}
  572. */
  573. void setShort(const char* name, short v);
  574. /**
  575. * Sets the global int script variable with the given name to the given value.
  576. *
  577. * @param name The name of the script variable.
  578. * @param v The int value.
  579. *
  580. * @script{ignore}
  581. */
  582. void setInt(const char* name, int v);
  583. /**
  584. * Sets the global long script variable with the given name to the given value.
  585. *
  586. * @param name The name of the script variable.
  587. * @param v The long value.
  588. *
  589. * @script{ignore}
  590. */
  591. void setLong(const char* name, long v);
  592. /**
  593. * Gets the global unsigned char script variable with the given name to the given value.
  594. *
  595. * @param name The name of the script variable.
  596. * @param v The unsigned char value.
  597. *
  598. * @script{ignore}
  599. */
  600. void setUnsignedChar(const char* name, unsigned char v);
  601. /**
  602. * Sets the global unsigned short script variable with the given name to the given value.
  603. *
  604. * @param name The name of the script variable.
  605. * @param v The unsigned short value.
  606. *
  607. * @script{ignore}
  608. */
  609. void setUnsignedShort(const char* name, unsigned short v);
  610. /**
  611. * Sets the global unsigned int script variable with the given name to the given value.
  612. *
  613. * @param name The name of the script variable.
  614. * @param v The unsigned int value.
  615. *
  616. * @script{ignore}
  617. */
  618. void setUnsignedInt(const char* name, unsigned int v);
  619. /**
  620. * Sets the global unsigned long script variable with the given name to the given value.
  621. *
  622. * @param name The name of the script variable.
  623. * @param v The unsigned long value.
  624. *
  625. * @script{ignore}
  626. */
  627. void setUnsignedLong(const char* name, unsigned long v);
  628. /**
  629. * Sets the global float script variable with the given name to the given value.
  630. *
  631. * @param name The name of the script variable.
  632. * @param v The float value.
  633. *
  634. * @script{ignore}
  635. */
  636. void setFloat(const char* name, float v);
  637. /**
  638. * Sets the global double script variable with the given name to the given value.
  639. *
  640. * @param name The name of the script variable.
  641. * @param v The double value.
  642. *
  643. * @script{ignore}
  644. */
  645. void setDouble(const char* name, double v);
  646. /**
  647. * Sets the global string script variable with the given name to the given value.
  648. *
  649. * @param name The name of the script variable.
  650. * @param v The string value.
  651. *
  652. * @script{ignore}
  653. */
  654. void setString(const char* name, const char* v);
  655. /**
  656. * Sets the global pointer script variable of the given type with the given name to the given value.
  657. *
  658. * @param type The type of the script variable.
  659. * @param name The name of the variable.
  660. * @param v The pointer value.
  661. *
  662. * @script{ignore}
  663. */
  664. template<typename T>void setObjectPointer(const char* type, const char* name, T* v);
  665. /**
  666. * Prints the string to the platform's output stream or log file.
  667. * Used for overriding Lua's print function.
  668. *
  669. * @param str The string to print.
  670. */
  671. static void print(const char* str);
  672. /**
  673. * Prints the strings to the platform's output stream or log file.
  674. * Used for overriding Lua's print function.
  675. *
  676. * @param str1 The first string to print.
  677. * @param str2 The second string to print on the same line as str1.
  678. */
  679. static void print(const char* str1, const char* str2);
  680. private:
  681. /**
  682. * Represents a callback function that can be registered for.
  683. */
  684. enum ScriptCallback
  685. {
  686. INITIALIZE = 0,
  687. UPDATE,
  688. RENDER,
  689. FINALIZE,
  690. RESIZE_EVENT,
  691. KEY_EVENT,
  692. MOUSE_EVENT,
  693. TOUCH_EVENT,
  694. GAMEPAD_EVENT,
  695. CALLBACK_COUNT,
  696. INVALID_CALLBACK = CALLBACK_COUNT
  697. };
  698. /**
  699. * Constructor.
  700. */
  701. ScriptController();
  702. /**
  703. * Copy constructor.
  704. */
  705. ScriptController(const ScriptController& copy);
  706. /**
  707. * Destructor.
  708. */
  709. ~ScriptController();
  710. /**
  711. * Callback for when the controller is initialized.
  712. */
  713. void initialize();
  714. /**
  715. * Initializes the game using the appropriate callback script (if it was specified).
  716. */
  717. void initializeGame();
  718. /*
  719. * Callback for when the controller is finalized.
  720. */
  721. void finalize();
  722. /**
  723. * Finalizes the game using the appropriate callback script (if it was specified).
  724. */
  725. void finalizeGame();
  726. /**
  727. * Callback for when the controller receives a frame update event.
  728. */
  729. void update(float elapsedTime);
  730. /**
  731. * Renders the game using the appropriate callback script (if it was specified).
  732. */
  733. void render(float elapsedTime);
  734. /**
  735. * Script callback for game resize events.
  736. *
  737. * @param width The new width of the game window content area.
  738. * @param height The new height of the game window content area.
  739. */
  740. void resizeEvent(unsigned int width, unsigned int height);
  741. /**
  742. * Script keyboard callback on key events.
  743. *
  744. * @param evt The key event that occurred.
  745. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  746. * If evt is KEY_CHAR then key is the unicode value of the character.
  747. *
  748. * @see Keyboard::KeyEvent
  749. * @see Keyboard::Key
  750. */
  751. void keyEvent(Keyboard::KeyEvent evt, int key);
  752. /**
  753. * Script touch callback on touch events.
  754. *
  755. * @param evt The touch event that occurred.
  756. * @param x The x position of the touch in pixels. Left edge is zero.
  757. * @param y The y position of the touch in pixels. Top edge is zero.
  758. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  759. *
  760. * @see Touch::TouchEvent
  761. */
  762. void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  763. /**
  764. * Script mouse callback on mouse events. If the game does not consume the mouse move event or left mouse click event
  765. * then it is interpreted as a touch event instead.
  766. *
  767. * @param evt The mouse event that occurred.
  768. * @param x The x position of the mouse in pixels. Left edge is zero.
  769. * @param y The y position of the mouse in pixels. Top edge is zero.
  770. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  771. *
  772. * @return True if the mouse event is consumed or false if it is not consumed.
  773. *
  774. * @see Mouse::MouseEvent
  775. */
  776. bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  777. /**
  778. * Script gamepad callback on gamepad events.
  779. *
  780. * @param evt The gamepad event that occurred.
  781. * @param gamepad the gamepad the event occurred on
  782. */
  783. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad);
  784. /**
  785. * Calls the specified Lua function using the given parameters.
  786. *
  787. * @param resultCount The expected number of returned values.
  788. * @param func The name of the function to call.
  789. * @param args The argument signature of the function, as a string of the form
  790. * 'xxx', where each 'x' is a parameter type and must be one of:
  791. * - 'b' - bool
  792. * - 'c' - char
  793. * - 'h' - short
  794. * - 'i' - int
  795. * - 'l' - long
  796. * - 'f' - float
  797. * - 'd' - double
  798. * - 'ui' - unsigned int
  799. * - 'ul' - unsigned long
  800. * - 'uc' - unsigned char
  801. * - 'uh' - unsigned short
  802. * - 's' - string
  803. * - 'p' - pointer
  804. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  805. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  806. * @param list The variable argument list.
  807. */
  808. void executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list);
  809. /**
  810. * Converts the given string to a valid script callback enumeration value
  811. * or to ScriptController::INVALID_CALLBACK if there is no valid conversion.
  812. *
  813. * @param name The name of the callback.
  814. *
  815. * @return The corresponding callback enumeration value.
  816. */
  817. static ScriptController::ScriptCallback toCallback(const char* name);
  818. /**
  819. * Converts a Gameplay userdata value to the type with the given class name.
  820. * This function will change the metatable of the userdata value to the metatable that matches the given string.
  821. *
  822. * Example:
  823. * <code>
  824. * local launchButton = form:getControl("launch")
  825. * convert(launchButton, "Button")
  826. * print("Button text: " .. launchButton:getText())
  827. * </code>
  828. *
  829. * <code>
  830. * -- The signature of the lua function:
  831. * -- param: object A userdata object that represents a Gameplay object.
  832. * -- param: className The name of the class to convert the object to. (Examples: "Button", "PhysicsRigidBody")
  833. * function convert(object, className)
  834. * </code>
  835. *
  836. * @param state The Lua state.
  837. *
  838. * @return The number of values being returned by this function.
  839. *
  840. * @script{ignore}
  841. */
  842. static int convert(lua_State* state);
  843. // Friend functions (used by Lua script bindings).
  844. friend void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions);
  845. friend void ScriptUtil::registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath);
  846. friend void ScriptUtil::registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath);
  847. friend void ScriptUtil::registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath);
  848. friend void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  849. lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath);
  850. friend void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  851. friend void ScriptUtil::setGlobalHierarchyPair(const std::string& base, const std::string& derived);
  852. friend void ScriptUtil::addStringFromEnumConversionFunction(luaStringEnumConversionFunction stringFromEnum);
  853. friend ScriptUtil::LuaArray<bool> ScriptUtil::getBoolPointer(int index);
  854. friend ScriptUtil::LuaArray<short> ScriptUtil::getShortPointer(int index);
  855. friend ScriptUtil::LuaArray<int> ScriptUtil::getIntPointer(int index);
  856. friend ScriptUtil::LuaArray<long> ScriptUtil::getLongPointer(int index);
  857. friend ScriptUtil::LuaArray<unsigned char> ScriptUtil::getUnsignedCharPointer(int index);
  858. friend ScriptUtil::LuaArray<unsigned short> ScriptUtil::getUnsignedShortPointer(int index);
  859. friend ScriptUtil::LuaArray<unsigned int> ScriptUtil::getUnsignedIntPointer(int index);
  860. friend ScriptUtil::LuaArray<unsigned long> ScriptUtil::getUnsignedLongPointer(int index);
  861. friend ScriptUtil::LuaArray<float> ScriptUtil::getFloatPointer(int index);
  862. friend ScriptUtil::LuaArray<double> ScriptUtil::getDoublePointer(int index);
  863. template<typename T> friend ScriptUtil::LuaArray<T> ScriptUtil::getObjectPointer(int index, const char* type, bool nonNull, bool* success);
  864. friend const char* ScriptUtil::getString(int index, bool isStdString);
  865. lua_State* _lua;
  866. unsigned int _returnCount;
  867. std::map<std::string, std::vector<std::string> > _hierarchy;
  868. std::vector<std::string> _callbacks[CALLBACK_COUNT];
  869. std::set<std::string> _loadedScripts;
  870. std::vector<luaStringEnumConversionFunction> _stringFromEnum;
  871. };
  872. /** Template specialization. */
  873. template<> void ScriptController::executeFunction<void>(const char* func);
  874. /** Template specialization. */
  875. template<> bool ScriptController::executeFunction<bool>(const char* func);
  876. /** Template specialization. */
  877. template<> char ScriptController::executeFunction<char>(const char* func);
  878. /** Template specialization. */
  879. template<> short ScriptController::executeFunction<short>(const char* func);
  880. /** Template specialization. */
  881. template<> int ScriptController::executeFunction<int>(const char* func);
  882. /** Template specialization. */
  883. template<> long ScriptController::executeFunction<long>(const char* func);
  884. /** Template specialization. */
  885. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func);
  886. /** Template specialization. */
  887. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func);
  888. /** Template specialization. */
  889. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func);
  890. /** Template specialization. */
  891. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func);
  892. /** Template specialization. */
  893. template<> float ScriptController::executeFunction<float>(const char* func);
  894. /** Template specialization. */
  895. template<> double ScriptController::executeFunction<double>(const char* func);
  896. /** Template specialization. */
  897. template<> std::string ScriptController::executeFunction<std::string>(const char* func);
  898. /** Template specialization. */
  899. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...);
  900. /** Template specialization. */
  901. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...);
  902. /** Template specialization. */
  903. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...);
  904. /** Template specialization. */
  905. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...);
  906. /** Template specialization. */
  907. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...);
  908. /** Template specialization. */
  909. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...);
  910. /** Template specialization. */
  911. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...);
  912. /** Template specialization. */
  913. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...);
  914. /** Template specialization. */
  915. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...);
  916. /** Template specialization. */
  917. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...);
  918. /** Template specialization. */
  919. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...);
  920. /** Template specialization. */
  921. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...);
  922. /** Template specialization. */
  923. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...);
  924. /** Template specialization. */
  925. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list);
  926. /** Template specialization. */
  927. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list);
  928. /** Template specialization. */
  929. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list);
  930. /** Template specialization. */
  931. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list);
  932. /** Template specialization. */
  933. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list);
  934. /** Template specialization. */
  935. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list);
  936. /** Template specialization. */
  937. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list);
  938. /** Template specialization. */
  939. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list);
  940. /** Template specialization. */
  941. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list);
  942. /** Template specialization. */
  943. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list);
  944. /** Template specialization. */
  945. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list);
  946. /** Template specialization. */
  947. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list);
  948. /** Template specialization. */
  949. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list);
  950. }
  951. #include "ScriptController.inl"
  952. #endif