ScriptController.h 32 KB

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