ScriptTarget.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef SCRIPTTARGET_H_
  2. #define SCRIPTTARGET_H_
  3. #include "Base.h"
  4. namespace gameplay
  5. {
  6. /**
  7. * Generic base class for supporting script callbacks.
  8. */
  9. class ScriptTarget
  10. {
  11. public:
  12. /**
  13. * Adds the given Lua script function as a callback for the given event.
  14. *
  15. * @param eventName The name of the event.
  16. * @param function The name of the Lua script function to call when the event is fired; can either be
  17. * just the name of a function (if the function's script file has already been loaded), or can be
  18. * a URL of the form scriptFile.lua#functionName.
  19. */
  20. virtual void addScriptCallback(const std::string& eventName, const std::string& function);
  21. /**
  22. * Removes the given Lua script function as a callback for the given event.
  23. *
  24. * @param eventName The name of the event.
  25. * @param function The name of the Lua script function.
  26. */
  27. virtual void removeScriptCallback(const std::string& eventName, const std::string& function);
  28. protected:
  29. /**
  30. * Hidden constructor.
  31. */
  32. ScriptTarget();
  33. /**
  34. * Destructor.
  35. */
  36. virtual ~ScriptTarget();
  37. /**
  38. * Adds the given event with the given Lua script parameter string ({@link ScriptController::executeFunction})
  39. * as a supported event for this script target.
  40. *
  41. * @param eventName The name of the event.
  42. * @param argsString The argument string for the event.
  43. */
  44. void addScriptEvent(const std::string& eventName, const char* argsString = NULL);
  45. /**
  46. * Fires the event with the given event name and the given arguments.
  47. *
  48. * @param eventName The name of the event.
  49. */
  50. template<typename T> T fireScriptEvent(const char* eventName, ...);
  51. /** Used to store a script callbacks for given event. */
  52. struct Callback
  53. {
  54. /** Constructor. */
  55. Callback(const std::string& string);
  56. /** Holds the Lua script callback function. */
  57. std::string function;
  58. };
  59. /** Holds the supported events for this script target. */
  60. std::map<std::string, std::string> _events;
  61. /** Holds the callbacks for this script target's events. */
  62. std::map<std::string, std::vector<Callback>*> _callbacks;
  63. };
  64. template<typename T> T ScriptTarget::fireScriptEvent(const char* eventName, ...)
  65. {
  66. GP_ERROR("Unsupported return type!");
  67. }
  68. /** Template specialization. */
  69. template<> void ScriptTarget::fireScriptEvent<void>(const char* eventName, ...);
  70. /** Template specialization. */
  71. template<> bool ScriptTarget::fireScriptEvent<bool>(const char* eventName, ...);
  72. }
  73. #endif