CCComponent.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "2d/CCComponent.h"
  22. NS_CC_BEGIN
  23. Component::Component()
  24. : _owner(nullptr)
  25. , _enabled(true)
  26. {
  27. #if CC_ENABLE_SCRIPT_BINDING
  28. ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
  29. _scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone;
  30. #endif
  31. }
  32. Component::~Component()
  33. {
  34. }
  35. bool Component::init()
  36. {
  37. return true;
  38. }
  39. #if CC_ENABLE_SCRIPT_BINDING
  40. static bool sendComponentEventToJS(Component* node, int action)
  41. {
  42. auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  43. if (scriptEngine->isCalledFromScript())
  44. {
  45. scriptEngine->setCalledFromScript(false);
  46. }
  47. else
  48. {
  49. BasicScriptData data(node,(void*)&action);
  50. ScriptEvent scriptEvent(kComponentEvent,(void*)&data);
  51. if (scriptEngine->sendEvent(&scriptEvent))
  52. return true;
  53. }
  54. return false;
  55. }
  56. #endif
  57. void Component::onEnter()
  58. {
  59. #if CC_ENABLE_SCRIPT_BINDING
  60. if (_scriptType == kScriptTypeJavascript)
  61. {
  62. sendComponentEventToJS(this, kComponentOnEnter);
  63. }
  64. #endif
  65. }
  66. void Component::onExit()
  67. {
  68. #if CC_ENABLE_SCRIPT_BINDING
  69. if (_scriptType == kScriptTypeJavascript)
  70. {
  71. sendComponentEventToJS(this, kComponentOnExit);
  72. }
  73. #endif
  74. }
  75. void Component::onAdd()
  76. {
  77. #if CC_ENABLE_SCRIPT_BINDING
  78. if (_scriptType == kScriptTypeJavascript)
  79. {
  80. sendComponentEventToJS(this, kComponentOnAdd);
  81. }
  82. #endif
  83. }
  84. void Component::onRemove()
  85. {
  86. #if CC_ENABLE_SCRIPT_BINDING
  87. if (_scriptType == kScriptTypeJavascript)
  88. {
  89. sendComponentEventToJS(this, kComponentOnRemove);
  90. }
  91. #endif
  92. }
  93. void Component::update(float /*delta*/)
  94. {
  95. #if CC_ENABLE_SCRIPT_BINDING
  96. if (_scriptType == kScriptTypeJavascript)
  97. {
  98. sendComponentEventToJS(this, kComponentOnUpdate);
  99. }
  100. #endif
  101. }
  102. bool Component::serialize(void* /*ar*/)
  103. {
  104. return true;
  105. }
  106. Component* Component::create()
  107. {
  108. Component * ret = new (std::nothrow) Component();
  109. if (ret && ret->init())
  110. {
  111. ret->autorelease();
  112. }
  113. else
  114. {
  115. CC_SAFE_DELETE(ret);
  116. }
  117. return ret;
  118. }
  119. void Component::setOwner(Node *owner)
  120. {
  121. _owner = owner;
  122. }
  123. void Component::setEnabled(bool enabled)
  124. {
  125. _enabled = enabled;
  126. }
  127. NS_CC_END