CCComponentContainer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "2d/CCComponentContainer.h"
  21. #include "2d/CCComponent.h"
  22. #include "2d/CCNode.h"
  23. NS_CC_BEGIN
  24. ComponentContainer::ComponentContainer(Node* node)
  25. : _owner(node)
  26. {
  27. }
  28. ComponentContainer::~ComponentContainer()
  29. {
  30. }
  31. Component* ComponentContainer::get(const std::string& name) const
  32. {
  33. Component* ret = nullptr;
  34. auto it = _componentMap.find(name);
  35. if (it != _componentMap.end())
  36. {
  37. ret = it->second;
  38. }
  39. return ret;
  40. }
  41. bool ComponentContainer::add(Component *com)
  42. {
  43. bool ret = false;
  44. CCASSERT(com != nullptr, "Component must be non-nil");
  45. CCASSERT(com->getOwner() == nullptr, "Component already added. It can't be added again");
  46. do
  47. {
  48. auto componentName = com->getName();
  49. if (_componentMap.find(componentName) != _componentMap.end())
  50. {
  51. CCASSERT(false, "ComponentContainer already have this kind of component");
  52. break;
  53. }
  54. _componentMap[componentName] = com;
  55. com->retain();
  56. com->setOwner(_owner);
  57. com->onAdd();
  58. ret = true;
  59. } while(0);
  60. return ret;
  61. }
  62. bool ComponentContainer::remove(const std::string& componentName)
  63. {
  64. bool ret = false;
  65. do
  66. {
  67. auto iter = _componentMap.find(componentName);
  68. CC_BREAK_IF(iter == _componentMap.end());
  69. auto component = iter->second;
  70. _componentMap.erase(componentName);
  71. component->onRemove();
  72. component->setOwner(nullptr);
  73. component->release();
  74. ret = true;
  75. } while(0);
  76. return ret;
  77. }
  78. bool ComponentContainer::remove(Component *com)
  79. {
  80. return remove(com->getName());
  81. }
  82. void ComponentContainer::removeAll()
  83. {
  84. if (!_componentMap.empty())
  85. {
  86. for (auto& iter : _componentMap)
  87. {
  88. iter.second->onRemove();
  89. iter.second->setOwner(nullptr);
  90. iter.second->release();
  91. }
  92. _componentMap.clear();
  93. _owner->unscheduleUpdate();
  94. }
  95. }
  96. void ComponentContainer::visit(float delta)
  97. {
  98. if (!_componentMap.empty())
  99. {
  100. CC_SAFE_RETAIN(_owner);
  101. for (auto& iter : _componentMap)
  102. {
  103. iter.second->update(delta);
  104. }
  105. CC_SAFE_RELEASE(_owner);
  106. }
  107. }
  108. void ComponentContainer::onEnter()
  109. {
  110. for (auto& iter : _componentMap)
  111. {
  112. iter.second->onEnter();
  113. }
  114. }
  115. void ComponentContainer::onExit()
  116. {
  117. for (auto& iter : _componentMap)
  118. {
  119. iter.second->onExit();
  120. }
  121. }
  122. NS_CC_END