ScriptController.h 37 KB

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