ScriptTarget.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "ScriptTarget.h"
  4. namespace gameplay
  5. {
  6. extern void splitURL(const std::string& url, std::string* file, std::string* id);
  7. ScriptTarget::~ScriptTarget()
  8. {
  9. std::map<std::string, std::vector<Callback>* >::iterator iter = _callbacks.begin();
  10. for (; iter != _callbacks.end(); iter++)
  11. {
  12. SAFE_DELETE(iter->second);
  13. }
  14. }
  15. template<> void ScriptTarget::fireEvent<void>(const char* eventName, ...)
  16. {
  17. va_list list;
  18. va_start(list, eventName);
  19. std::map<std::string, std::vector<Callback>* >::iterator iter = _callbacks.find(eventName);
  20. if (iter != _callbacks.end() && iter->second != NULL)
  21. {
  22. ScriptController* sc = Game::getInstance()->getScriptController();
  23. if (_events[eventName].size() > 0)
  24. {
  25. for (unsigned int i = 0; i < iter->second->size(); i++)
  26. {
  27. sc->executeFunction<void>((*iter->second)[i].function.c_str(), _events[eventName].c_str(), &list);
  28. }
  29. }
  30. else
  31. {
  32. for (unsigned int i = 0; i < iter->second->size(); i++)
  33. {
  34. sc->executeFunction<void>((*iter->second)[i].function.c_str(), _events[eventName].c_str());
  35. }
  36. }
  37. }
  38. va_end(list);
  39. }
  40. template<> bool ScriptTarget::fireEvent<bool>(const char* eventName, ...)
  41. {
  42. va_list list;
  43. va_start(list, eventName);
  44. std::map<std::string, std::vector<Callback>* >::iterator iter = _callbacks.find(eventName);
  45. if (iter != _callbacks.end())
  46. {
  47. ScriptController* sc = Game::getInstance()->getScriptController();
  48. if (_events[eventName].size() > 0)
  49. {
  50. for (unsigned int i = 0; i < iter->second->size(); i++)
  51. {
  52. if (sc->executeFunction<bool>((*iter->second)[i].function.c_str(), _events[eventName].c_str(), &list))
  53. {
  54. va_end(list);
  55. return true;
  56. }
  57. }
  58. }
  59. else
  60. {
  61. for (unsigned int i = 0; i < iter->second->size(); i++)
  62. {
  63. if (sc->executeFunction<bool>((*iter->second)[i].function.c_str(), _events[eventName].c_str()))
  64. {
  65. va_end(list);
  66. return true;
  67. }
  68. }
  69. }
  70. }
  71. va_end(list);
  72. return false;
  73. }
  74. void ScriptTarget::addCallback(const std::string& eventName, const std::string& function)
  75. {
  76. std::map<std::string, std::vector<Callback>* >::iterator iter = _callbacks.find(eventName);
  77. if (iter != _callbacks.end())
  78. {
  79. if (!iter->second)
  80. iter->second = new std::vector<Callback>();
  81. // Add the function to the list of callbacks.
  82. std::string functionName = Game::getInstance()->getScriptController()->loadUrl(function.c_str());
  83. iter->second->push_back(Callback(functionName));
  84. }
  85. else
  86. {
  87. GP_ERROR("Attempting to add a script callback for unsupported event '%s'.", eventName.c_str());
  88. }
  89. }
  90. void ScriptTarget::removeCallback(const std::string& eventName, const std::string& function)
  91. {
  92. std::map<std::string, std::vector<Callback>* >::iterator iter = _callbacks.find(eventName);
  93. if (iter != _callbacks.end())
  94. {
  95. if (!iter->second)
  96. return;
  97. std::string file;
  98. std::string id;
  99. splitURL(function, &file, &id);
  100. // Make sure the function isn't empty.
  101. if (id.size() <= 0)
  102. return;
  103. // Remove the function from the list of callbacks.
  104. for (unsigned int i = 0; i < iter->second->size(); i++)
  105. {
  106. if ((*iter->second)[i].function == id)
  107. {
  108. iter->second->erase(iter->second->begin() + i);
  109. return;
  110. }
  111. }
  112. }
  113. else
  114. {
  115. GP_ERROR("Attempting to remove a script callback for unsupported event '%s'.", eventName.c_str());
  116. }
  117. }
  118. void ScriptTarget::addEvent(const std::string& eventName, const char* argsString)
  119. {
  120. _events[eventName] = (argsString ? argsString : "");
  121. _callbacks[eventName] = NULL;
  122. }
  123. ScriptTarget::Callback::Callback(const std::string& function) : function(function)
  124. {
  125. }
  126. }