ScriptTarget.cpp 4.2 KB

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