ScriptController.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. * Gesture callback on Gesture::LONG_TAP events.
  811. *
  812. * @param x The x-coordinate of the long tap.
  813. * @param y The y-coordinate of the long tap.
  814. * @param duration The duration of the long tap in ms.
  815. */
  816. void gestureLongTapEvent(int x, int y, float duration);
  817. /**
  818. * Gesture callback on Gesture::DRAG events.
  819. *
  820. * @param x The x-coordinate of the start of the drag event.
  821. * @param y The y-coordinate of the start of the drag event.
  822. */
  823. void gestureDragEvent(int x, int y);
  824. /**
  825. * Gesture callback on Gesture::DROP events.
  826. *
  827. * @param x The x-coordinate of the drop event.
  828. * @param y The y-coordinate of the drop event.
  829. */
  830. void gestureDropEvent(int x, int y);
  831. /**
  832. * Script gamepad callback on gamepad events.
  833. *
  834. * @param evt The gamepad event that occurred.
  835. * @param gamepad the gamepad the event occurred on
  836. */
  837. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex = 0);
  838. /**
  839. * Calls the specified Lua function using the given parameters.
  840. *
  841. * @param resultCount The expected number of returned values.
  842. * @param func The name of the function to call.
  843. * @param args The argument signature of the function, as a string of the form
  844. * 'xxx', where each 'x' is a parameter type and must be one of:
  845. * - 'b' - bool
  846. * - 'c' - char
  847. * - 'h' - short
  848. * - 'i' - int
  849. * - 'l' - long
  850. * - 'f' - float
  851. * - 'd' - double
  852. * - 'ui' - unsigned int
  853. * - 'ul' - unsigned long
  854. * - 'uc' - unsigned char
  855. * - 'uh' - unsigned short
  856. * - 's' - string
  857. * - 'p' - pointer
  858. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  859. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  860. * @param list The variable argument list.
  861. */
  862. void executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list);
  863. /**
  864. * Converts the given string to a valid script callback enumeration value
  865. * or to ScriptController::INVALID_CALLBACK if there is no valid conversion.
  866. *
  867. * @param name The name of the callback.
  868. *
  869. * @return The corresponding callback enumeration value.
  870. */
  871. static ScriptController::ScriptCallback toCallback(const char* name);
  872. /**
  873. * Converts a Gameplay userdata value to the type with the given class name.
  874. * This function will change the metatable of the userdata value to the metatable that matches the given string.
  875. *
  876. * Example:
  877. * <code>
  878. * local launchButton = form:getControl("launch")
  879. * convert(launchButton, "Button")
  880. * print("Button text: " .. launchButton:getText())
  881. * </code>
  882. *
  883. * <code>
  884. * -- The signature of the lua function:
  885. * -- param: object A userdata object that represents a Gameplay object.
  886. * -- param: className The name of the class to convert the object to. (Examples: "Button", "PhysicsRigidBody")
  887. * function convert(object, className)
  888. * </code>
  889. *
  890. * @param state The Lua state.
  891. *
  892. * @return The number of values being returned by this function.
  893. *
  894. * @script{ignore}
  895. */
  896. static int convert(lua_State* state);
  897. // Friend functions (used by Lua script bindings).
  898. friend void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions);
  899. friend void ScriptUtil::registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath);
  900. friend void ScriptUtil::registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath);
  901. friend void ScriptUtil::registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath);
  902. friend void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  903. lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath);
  904. friend void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  905. friend void ScriptUtil::setGlobalHierarchyPair(const std::string& base, const std::string& derived);
  906. friend void ScriptUtil::addStringFromEnumConversionFunction(luaStringEnumConversionFunction stringFromEnum);
  907. friend ScriptUtil::LuaArray<bool> ScriptUtil::getBoolPointer(int index);
  908. friend ScriptUtil::LuaArray<short> ScriptUtil::getShortPointer(int index);
  909. friend ScriptUtil::LuaArray<int> ScriptUtil::getIntPointer(int index);
  910. friend ScriptUtil::LuaArray<long> ScriptUtil::getLongPointer(int index);
  911. friend ScriptUtil::LuaArray<unsigned char> ScriptUtil::getUnsignedCharPointer(int index);
  912. friend ScriptUtil::LuaArray<unsigned short> ScriptUtil::getUnsignedShortPointer(int index);
  913. friend ScriptUtil::LuaArray<unsigned int> ScriptUtil::getUnsignedIntPointer(int index);
  914. friend ScriptUtil::LuaArray<unsigned long> ScriptUtil::getUnsignedLongPointer(int index);
  915. friend ScriptUtil::LuaArray<float> ScriptUtil::getFloatPointer(int index);
  916. friend ScriptUtil::LuaArray<double> ScriptUtil::getDoublePointer(int index);
  917. template<typename T> friend ScriptUtil::LuaArray<T> ScriptUtil::getObjectPointer(int index, const char* type, bool nonNull, bool* success);
  918. friend const char* ScriptUtil::getString(int index, bool isStdString);
  919. lua_State* _lua;
  920. unsigned int _returnCount;
  921. std::map<std::string, std::vector<std::string> > _hierarchy;
  922. std::vector<std::string> _callbacks[CALLBACK_COUNT];
  923. std::set<std::string> _loadedScripts;
  924. std::vector<luaStringEnumConversionFunction> _stringFromEnum;
  925. };
  926. /** Template specialization. */
  927. template<> void ScriptController::executeFunction<void>(const char* func);
  928. /** Template specialization. */
  929. template<> bool ScriptController::executeFunction<bool>(const char* func);
  930. /** Template specialization. */
  931. template<> char ScriptController::executeFunction<char>(const char* func);
  932. /** Template specialization. */
  933. template<> short ScriptController::executeFunction<short>(const char* func);
  934. /** Template specialization. */
  935. template<> int ScriptController::executeFunction<int>(const char* func);
  936. /** Template specialization. */
  937. template<> long ScriptController::executeFunction<long>(const char* func);
  938. /** Template specialization. */
  939. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func);
  940. /** Template specialization. */
  941. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func);
  942. /** Template specialization. */
  943. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func);
  944. /** Template specialization. */
  945. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func);
  946. /** Template specialization. */
  947. template<> float ScriptController::executeFunction<float>(const char* func);
  948. /** Template specialization. */
  949. template<> double ScriptController::executeFunction<double>(const char* func);
  950. /** Template specialization. */
  951. template<> std::string ScriptController::executeFunction<std::string>(const char* func);
  952. /** Template specialization. */
  953. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...);
  954. /** Template specialization. */
  955. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...);
  956. /** Template specialization. */
  957. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...);
  958. /** Template specialization. */
  959. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...);
  960. /** Template specialization. */
  961. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...);
  962. /** Template specialization. */
  963. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...);
  964. /** Template specialization. */
  965. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...);
  966. /** Template specialization. */
  967. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...);
  968. /** Template specialization. */
  969. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...);
  970. /** Template specialization. */
  971. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...);
  972. /** Template specialization. */
  973. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...);
  974. /** Template specialization. */
  975. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...);
  976. /** Template specialization. */
  977. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...);
  978. /** Template specialization. */
  979. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list);
  980. /** Template specialization. */
  981. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list);
  982. /** Template specialization. */
  983. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list);
  984. /** Template specialization. */
  985. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list);
  986. /** Template specialization. */
  987. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list);
  988. /** Template specialization. */
  989. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list);
  990. /** Template specialization. */
  991. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list);
  992. /** Template specialization. */
  993. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list);
  994. /** Template specialization. */
  995. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list);
  996. /** Template specialization. */
  997. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list);
  998. /** Template specialization. */
  999. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list);
  1000. /** Template specialization. */
  1001. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list);
  1002. /** Template specialization. */
  1003. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list);
  1004. }
  1005. #include "ScriptController.inl"
  1006. #endif