ScriptController.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. * Overloades [] 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. * @return The object pointer or <code>NULL</code> if the data at the stack index
  241. * is not an object or if the object is not derived from the given type.
  242. * @script{ignore}
  243. */
  244. template <typename T>
  245. LuaArray<T> getObjectPointer(int index, const char* type, bool nonNull);
  246. /**
  247. * Gets a string for the given stack index.
  248. *
  249. * @param index The stack index.
  250. * @param isStdString Whether the string being retrieved is a std::string object or not.
  251. * @return The string or <code>NULL</code>.
  252. * @script{ignore}
  253. */
  254. const char* getString(int index, bool isStdString);
  255. /**
  256. * Checks that the parameter at the given stack position is a boolean and returns it.
  257. *
  258. * @param state The Lua state.
  259. * @param n The stack index.
  260. * @return The boolean (if successful; otherwise it logs an error).
  261. * @script{ignore}
  262. */
  263. bool luaCheckBool(lua_State* state, int n);
  264. }
  265. /**
  266. * Controls and manages all scripts.
  267. */
  268. class ScriptController
  269. {
  270. friend class Game;
  271. friend class Platform;
  272. public:
  273. /**
  274. * Loads the given script file and executes its global code.
  275. *
  276. * @param path The path to the script.
  277. * @param forceReload Whether the script should be reloaded if it has already been loaded.
  278. */
  279. void loadScript(const char* path, bool forceReload = false);
  280. /**
  281. * Given a URL, loads the referenced file and returns the referenced function name.
  282. *
  283. * @param url The url to load.
  284. * @return The function that the URL references.
  285. */
  286. std::string loadUrl(const char* url);
  287. /**
  288. * Calls the specified no-parameter Lua function.
  289. *
  290. * @param func The name of the function to call.
  291. * @return The return value of the executed Lua function.
  292. */
  293. template<typename T> T executeFunction(const char* func);
  294. /**
  295. * Calls the specified Lua function using the given parameters.
  296. *
  297. * @param func The name of the function to call.
  298. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  299. * - 'b' - bool
  300. * - 'c' - char
  301. * - 'h' - short
  302. * - 'i' - int
  303. * - 'l' - long
  304. * - 'f' - float
  305. * - 'd' - double
  306. * - 'ui' - unsigned int
  307. * - 'ul' - unsigned long
  308. * - 'uc' - unsigned char
  309. * - 'uh' - unsigned short
  310. * - 's' - string
  311. * - 'p' - pointer
  312. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  313. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  314. * @return The return value of the executed Lua function.
  315. */
  316. template<typename T> T executeFunction(const char* func, const char* args, ...);
  317. /**
  318. * Calls the specified Lua function using the given parameters.
  319. *
  320. * @param func The name of the function to call.
  321. * @param args The argument signature of the function. Of the form 'xxx', where each 'x' is a parameter type and must be one of:
  322. * - 'b' - bool
  323. * - 'c' - char
  324. * - 'h' - short
  325. * - 'i' - int
  326. * - 'l' - long
  327. * - 'f' - float
  328. * - 'd' - double
  329. * - 'ui' - unsigned int
  330. * - 'ul' - unsigned long
  331. * - 'uc' - unsigned char
  332. * - 'uh' - unsigned short
  333. * - 's' - string
  334. * - 'p' - pointer
  335. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  336. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  337. * @param list The variable argument list containing the function's parameters.
  338. * @return The return value of the executed Lua function.
  339. */
  340. template<typename T> T executeFunction(const char* func, const char* args, va_list* list);
  341. /**
  342. * Gets the global boolean script variable with the given name.
  343. *
  344. * @param name The name of the variable.
  345. * @return The global boolean script variable.
  346. * @script{ignore}
  347. */
  348. bool getBool(const char* name);
  349. /**
  350. * Gets the global char script variable with the given name.
  351. *
  352. * @param name The name of the variable.
  353. * @return The global char script variable.
  354. * @script{ignore}
  355. */
  356. char getChar(const char* name);
  357. /**
  358. * Gets the global short script variable with the given name.
  359. *
  360. * @param name The name of the variable.
  361. * @return The global short script variable.
  362. * @script{ignore}
  363. */
  364. short getShort(const char* name);
  365. /**
  366. * Gets the global int script variable with the given name.
  367. *
  368. * @param name The name of the variable.
  369. * @return The global int script variable.
  370. * @script{ignore}
  371. */
  372. int getInt(const char* name);
  373. /**
  374. * Gets the global long script variable with the given name.
  375. *
  376. * @param name The name of the variable.
  377. * @return The global long script variable.
  378. * @script{ignore}
  379. */
  380. long getLong(const char* name);
  381. /**
  382. * Gets the global unsigned char script variable with the given name.
  383. *
  384. * @param name The name of the variable.
  385. * @return The global unsigned char script variable.
  386. * @script{ignore}
  387. */
  388. unsigned char getUnsignedChar(const char* name);
  389. /**
  390. * Gets the global unsigned short script variable with the given name.
  391. *
  392. * @param name The name of the variable.
  393. * @return The global unsigned short script variable.
  394. * @script{ignore}
  395. */
  396. unsigned short getUnsignedShort(const char* name);
  397. /**
  398. * Gets the global unsigned int script variable with the given name.
  399. *
  400. * @param name The name of the variable.
  401. * @return The global unsigned int script variable.
  402. * @script{ignore}
  403. */
  404. unsigned int getUnsignedInt(const char* name);
  405. /**
  406. * Gets the global unsigned long script variable with the given name.
  407. *
  408. * @param name The name of the variable.
  409. * @return The global unsigned long script variable.
  410. * @script{ignore}
  411. */
  412. unsigned long getUnsignedLong(const char* name);
  413. /**
  414. * Gets the global float script variable with the given name.
  415. *
  416. * @param name The name of the variable.
  417. * @return The global float script variable.
  418. * @script{ignore}
  419. */
  420. float getFloat(const char* name);
  421. /**
  422. * Gets the global double script variable with the given name.
  423. *
  424. * @param name The name of the variable.
  425. * @return The global double script variable.
  426. * @script{ignore}
  427. */
  428. double getDouble(const char* name);
  429. /**
  430. * Gets the global string script variable with the given name.
  431. *
  432. * @param name The name of the variable.
  433. * @return The global string script variable.
  434. * @script{ignore}
  435. */
  436. const char* getString(const char* name);
  437. /**
  438. * Gets the global pointer script variable of the given type with the given name.
  439. *
  440. * @param type The type of the variable in Lua.
  441. * @param name The name of the variable.
  442. * @return The global pointer script variable.
  443. * @script{ignore}
  444. */
  445. template<typename T>T* getObjectPointer(const char* type, const char* name);
  446. /**
  447. * Sets the global boolean script variable with the given name to the given value.
  448. *
  449. * @param name The name of the script variable.
  450. * @param v The boolean value.
  451. * @script{ignore}
  452. */
  453. void setBool(const char* name, bool v);
  454. /**
  455. * Sets the global char script variable with the given name to the given value.
  456. *
  457. * @param name The name of the script variable.
  458. * @param v The char value.
  459. * @script{ignore}
  460. */
  461. void setChar(const char* name, char v);
  462. /**
  463. * Sets the global short script variable with the given name to the given value.
  464. *
  465. * @param name The name of the script variable.
  466. * @param v The short value.
  467. * @script{ignore}
  468. */
  469. void setShort(const char* name, short v);
  470. /**
  471. * Sets the global int script variable with the given name to the given value.
  472. *
  473. * @param name The name of the script variable.
  474. * @param v The int value.
  475. * @script{ignore}
  476. */
  477. void setInt(const char* name, int v);
  478. /**
  479. * Sets the global long script variable with the given name to the given value.
  480. *
  481. * @param name The name of the script variable.
  482. * @param v The long value.
  483. * @script{ignore}
  484. */
  485. void setLong(const char* name, long v);
  486. /**
  487. * Gets the global unsigned char script variable with the given name to the given value.
  488. *
  489. * @param name The name of the script variable.
  490. * @param v The unsigned char value.
  491. * @script{ignore}
  492. */
  493. void setUnsignedChar(const char* name, unsigned char v);
  494. /**
  495. * Sets the global unsigned short script variable with the given name to the given value.
  496. *
  497. * @param name The name of the script variable.
  498. * @param v The unsigned short value.
  499. * @script{ignore}
  500. */
  501. void setUnsignedShort(const char* name, unsigned short v);
  502. /**
  503. * Sets the global unsigned int script variable with the given name to the given value.
  504. *
  505. * @param name The name of the script variable.
  506. * @param v The unsigned int value.
  507. * @script{ignore}
  508. */
  509. void setUnsignedInt(const char* name, unsigned int v);
  510. /**
  511. * Sets the global unsigned long script variable with the given name to the given value.
  512. *
  513. * @param name The name of the script variable.
  514. * @param v The unsigned long value.
  515. * @script{ignore}
  516. */
  517. void setUnsignedLong(const char* name, unsigned long v);
  518. /**
  519. * Sets the global float script variable with the given name to the given value.
  520. *
  521. * @param name The name of the script variable.
  522. * @param v The float value.
  523. * @script{ignore}
  524. */
  525. void setFloat(const char* name, float v);
  526. /**
  527. * Sets the global double script variable with the given name to the given value.
  528. *
  529. * @param name The name of the script variable.
  530. * @param v The double value.
  531. * @script{ignore}
  532. */
  533. void setDouble(const char* name, double v);
  534. /**
  535. * Sets the global string script variable with the given name to the given value.
  536. *
  537. * @param name The name of the script variable.
  538. * @param v The string value.
  539. * @script{ignore}
  540. */
  541. void setString(const char* name, const char* v);
  542. /**
  543. * Sets the global pointer script variable of the given type with the given name to the given value.
  544. *
  545. * @param type The type of the script variable.
  546. * @param name The name of the variable.
  547. * @param v The pointer value.
  548. * @script{ignore}
  549. */
  550. template<typename T>void setObjectPointer(const char* type, const char* name, T* v);
  551. /**
  552. * Prints the string to the platform's output stream or log file.
  553. * Used for overriding Lua's print function.
  554. *
  555. * @param str The string to print.
  556. */
  557. static void print(const char* str);
  558. /**
  559. * Prints the strings to the platform's output stream or log file.
  560. * Used for overriding Lua's print function.
  561. *
  562. * @param str1 The first string to print.
  563. * @param str2 The second string to print on the same line as str1.
  564. */
  565. static void print(const char* str1, const char* str2);
  566. private:
  567. /**
  568. * Represents a Lua callback function binding.
  569. */
  570. enum ScriptCallback
  571. {
  572. INITIALIZE = 0,
  573. UPDATE,
  574. RENDER,
  575. FINALIZE,
  576. KEY_EVENT,
  577. MOUSE_EVENT,
  578. TOUCH_EVENT,
  579. GAMEPAD_EVENT,
  580. CALLBACK_COUNT,
  581. INVALID_CALLBACK = CALLBACK_COUNT
  582. };
  583. /**
  584. * Constructor.
  585. */
  586. ScriptController();
  587. /**
  588. * Copy constructor.
  589. */
  590. ScriptController(const ScriptController& copy);
  591. /**
  592. * Destructor.
  593. */
  594. ~ScriptController();
  595. /**
  596. * Callback for when the controller is initialized.
  597. */
  598. void initialize();
  599. /**
  600. * Initializes the game using the appropriate callback script (if it was specified).
  601. */
  602. void initializeGame();
  603. /*
  604. * Callback for when the controller is finalized.
  605. */
  606. void finalize();
  607. /**
  608. * Finalizes the game using the appropriate callback script (if it was specified).
  609. */
  610. void finalizeGame();
  611. /**
  612. * Callback for when the controller receives a frame update event.
  613. */
  614. void update(float elapsedTime);
  615. /**
  616. * Renders the game using the appropriate callback script (if it was specified).
  617. */
  618. void render(float elapsedTime);
  619. /**
  620. * Script keyboard callback on key events.
  621. *
  622. * @param evt The key event that occurred.
  623. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  624. * If evt is KEY_CHAR then key is the unicode value of the character.
  625. *
  626. * @see Keyboard::KeyEvent
  627. * @see Keyboard::Key
  628. */
  629. void keyEvent(Keyboard::KeyEvent evt, int key);
  630. /**
  631. * Script touch callback on touch events.
  632. *
  633. * @param evt The touch event that occurred.
  634. * @param x The x position of the touch in pixels. Left edge is zero.
  635. * @param y The y position of the touch in pixels. Top edge is zero.
  636. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  637. *
  638. * @see Touch::TouchEvent
  639. */
  640. void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  641. /**
  642. * Script mouse callback on mouse events. If the game does not consume the mouse move event or left mouse click event
  643. * then it is interpreted as a touch event instead.
  644. *
  645. * @param evt The mouse event that occurred.
  646. * @param x The x position of the mouse in pixels. Left edge is zero.
  647. * @param y The y position of the mouse in pixels. Top edge is zero.
  648. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  649. *
  650. * @return True if the mouse event is consumed or false if it is not consumed.
  651. *
  652. * @see Mouse::MouseEvent
  653. */
  654. bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  655. /**
  656. * Script gamepad callback on gamepad events.
  657. *
  658. * @param evt The gamepad event that occurred.
  659. * @param gamepad the gamepad the event occurred on
  660. */
  661. void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad);
  662. /**
  663. * Calls the specified Lua function using the given parameters.
  664. *
  665. * @param resultCount The expected number of returned values.
  666. * @param func The name of the function to call.
  667. * @param args The argument signature of the function, as a string of the form
  668. * 'xxx', where each 'x' is a parameter type and must be one of:
  669. * - 'b' - bool
  670. * - 'c' - char
  671. * - 'h' - short
  672. * - 'i' - int
  673. * - 'l' - long
  674. * - 'f' - float
  675. * - 'd' - double
  676. * - 'ui' - unsigned int
  677. * - 'ul' - unsigned long
  678. * - 'uc' - unsigned char
  679. * - 'uh' - unsigned short
  680. * - 's' - string
  681. * - 'p' - pointer
  682. * - '<object-type>' - a <b>pointer</b> to an object of the given type (where the qualified type name is enclosed by angle brackets).
  683. * - '[enum-type]' - an enumerated value of the given type (where the qualified type name is enclosed by square brackets).
  684. * @param list The variable argument list.
  685. */
  686. void executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list);
  687. /**
  688. * Registers the given script callback.
  689. *
  690. * @param callback The script callback to register for.
  691. * @param function The name of the function within the Lua script to call.
  692. */
  693. void registerCallback(ScriptCallback callback, const std::string& function);
  694. /**
  695. * Converts the given string to a valid script callback enumeration value
  696. * or to ScriptController::INVALID_CALLBACK if there is no valid conversion.
  697. *
  698. * @param name The name of the callback.
  699. * @return The corresponding callback enumeration value.
  700. */
  701. static ScriptController::ScriptCallback toCallback(const char* name);
  702. // Friend functions (used by Lua script bindings).
  703. friend void ScriptUtil::registerLibrary(const char* name, const luaL_Reg* functions);
  704. friend void ScriptUtil::registerConstantBool(const std::string& name, bool value, const std::vector<std::string>& scopePath);
  705. friend void ScriptUtil::registerConstantNumber(const std::string& name, double value, const std::vector<std::string>& scopePath);
  706. friend void ScriptUtil::registerConstantString(const std::string& name, const std::string& value, const std::vector<std::string>& scopePath);
  707. friend void ScriptUtil::registerClass(const char* name, const luaL_Reg* members, lua_CFunction newFunction,
  708. lua_CFunction deleteFunction, const luaL_Reg* statics, const std::vector<std::string>& scopePath);
  709. friend void ScriptUtil::registerFunction(const char* luaFunction, lua_CFunction cppFunction);
  710. friend void ScriptUtil::setGlobalHierarchyPair(const std::string& base, const std::string& derived);
  711. friend void ScriptUtil::addStringFromEnumConversionFunction(luaStringEnumConversionFunction stringFromEnum);
  712. friend ScriptUtil::LuaArray<bool> ScriptUtil::getBoolPointer(int index);
  713. friend ScriptUtil::LuaArray<short> ScriptUtil::getShortPointer(int index);
  714. friend ScriptUtil::LuaArray<int> ScriptUtil::getIntPointer(int index);
  715. friend ScriptUtil::LuaArray<long> ScriptUtil::getLongPointer(int index);
  716. friend ScriptUtil::LuaArray<unsigned char> ScriptUtil::getUnsignedCharPointer(int index);
  717. friend ScriptUtil::LuaArray<unsigned short> ScriptUtil::getUnsignedShortPointer(int index);
  718. friend ScriptUtil::LuaArray<unsigned int> ScriptUtil::getUnsignedIntPointer(int index);
  719. friend ScriptUtil::LuaArray<unsigned long> ScriptUtil::getUnsignedLongPointer(int index);
  720. friend ScriptUtil::LuaArray<float> ScriptUtil::getFloatPointer(int index);
  721. friend ScriptUtil::LuaArray<double> ScriptUtil::getDoublePointer(int index);
  722. template<typename T> friend ScriptUtil::LuaArray<T> ScriptUtil::getObjectPointer(int index, const char* type, bool nonNull);
  723. friend const char* ScriptUtil::getString(int index, bool isStdString);
  724. lua_State* _lua;
  725. unsigned int _returnCount;
  726. std::map<std::string, std::vector<std::string> > _hierarchy;
  727. std::string* _callbacks[CALLBACK_COUNT];
  728. std::set<std::string> _loadedScripts;
  729. std::vector<luaStringEnumConversionFunction> _stringFromEnum;
  730. };
  731. /** Template specialization. */
  732. template<> void ScriptController::executeFunction<void>(const char* func);
  733. /** Template specialization. */
  734. template<> bool ScriptController::executeFunction<bool>(const char* func);
  735. /** Template specialization. */
  736. template<> char ScriptController::executeFunction<char>(const char* func);
  737. /** Template specialization. */
  738. template<> short ScriptController::executeFunction<short>(const char* func);
  739. /** Template specialization. */
  740. template<> int ScriptController::executeFunction<int>(const char* func);
  741. /** Template specialization. */
  742. template<> long ScriptController::executeFunction<long>(const char* func);
  743. /** Template specialization. */
  744. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func);
  745. /** Template specialization. */
  746. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func);
  747. /** Template specialization. */
  748. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func);
  749. /** Template specialization. */
  750. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func);
  751. /** Template specialization. */
  752. template<> float ScriptController::executeFunction<float>(const char* func);
  753. /** Template specialization. */
  754. template<> double ScriptController::executeFunction<double>(const char* func);
  755. /** Template specialization. */
  756. template<> std::string ScriptController::executeFunction<std::string>(const char* func);
  757. /** Template specialization. */
  758. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, ...);
  759. /** Template specialization. */
  760. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, ...);
  761. /** Template specialization. */
  762. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, ...);
  763. /** Template specialization. */
  764. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, ...);
  765. /** Template specialization. */
  766. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, ...);
  767. /** Template specialization. */
  768. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, ...);
  769. /** Template specialization. */
  770. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, ...);
  771. /** Template specialization. */
  772. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, ...);
  773. /** Template specialization. */
  774. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, ...);
  775. /** Template specialization. */
  776. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, ...);
  777. /** Template specialization. */
  778. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, ...);
  779. /** Template specialization. */
  780. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, ...);
  781. /** Template specialization. */
  782. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, ...);
  783. /** Template specialization. */
  784. template<> void ScriptController::executeFunction<void>(const char* func, const char* args, va_list* list);
  785. /** Template specialization. */
  786. template<> bool ScriptController::executeFunction<bool>(const char* func, const char* args, va_list* list);
  787. /** Template specialization. */
  788. template<> char ScriptController::executeFunction<char>(const char* func, const char* args, va_list* list);
  789. /** Template specialization. */
  790. template<> short ScriptController::executeFunction<short>(const char* func, const char* args, va_list* list);
  791. /** Template specialization. */
  792. template<> int ScriptController::executeFunction<int>(const char* func, const char* args, va_list* list);
  793. /** Template specialization. */
  794. template<> long ScriptController::executeFunction<long>(const char* func, const char* args, va_list* list);
  795. /** Template specialization. */
  796. template<> unsigned char ScriptController::executeFunction<unsigned char>(const char* func, const char* args, va_list* list);
  797. /** Template specialization. */
  798. template<> unsigned short ScriptController::executeFunction<unsigned short>(const char* func, const char* args, va_list* list);
  799. /** Template specialization. */
  800. template<> unsigned int ScriptController::executeFunction<unsigned int>(const char* func, const char* args, va_list* list);
  801. /** Template specialization. */
  802. template<> unsigned long ScriptController::executeFunction<unsigned long>(const char* func, const char* args, va_list* list);
  803. /** Template specialization. */
  804. template<> float ScriptController::executeFunction<float>(const char* func, const char* args, va_list* list);
  805. /** Template specialization. */
  806. template<> double ScriptController::executeFunction<double>(const char* func, const char* args, va_list* list);
  807. /** Template specialization. */
  808. template<> std::string ScriptController::executeFunction<std::string>(const char* func, const char* args, va_list* list);
  809. }
  810. #include "ScriptController.inl"
  811. #endif