ScriptController.h 33 KB

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