CCComponent.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/CCComponent.h"
  21. NS_CC_BEGIN
  22. Component::Component()
  23. : _owner(nullptr)
  24. , _enabled(true)
  25. {
  26. #if CC_ENABLE_SCRIPT_BINDING
  27. ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
  28. _scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone;
  29. #endif
  30. }
  31. Component::~Component()
  32. {
  33. }
  34. bool Component::init()
  35. {
  36. return true;
  37. }
  38. #if CC_ENABLE_SCRIPT_BINDING
  39. static bool sendComponentEventToJS(Component* node, int action)
  40. {
  41. auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine();
  42. if (scriptEngine->isCalledFromScript())
  43. {
  44. scriptEngine->setCalledFromScript(false);
  45. }
  46. else
  47. {
  48. BasicScriptData data(node,(void*)&action);
  49. ScriptEvent scriptEvent(kComponentEvent,(void*)&data);
  50. if (scriptEngine->sendEvent(&scriptEvent))
  51. return true;
  52. }
  53. return false;
  54. }
  55. #endif
  56. void Component::onEnter()
  57. {
  58. #if CC_ENABLE_SCRIPT_BINDING
  59. if (_scriptType == kScriptTypeJavascript)
  60. {
  61. sendComponentEventToJS(this, kComponentOnEnter);
  62. }
  63. #endif
  64. }
  65. void Component::onExit()
  66. {
  67. #if CC_ENABLE_SCRIPT_BINDING
  68. if (_scriptType == kScriptTypeJavascript)
  69. {
  70. sendComponentEventToJS(this, kComponentOnExit);
  71. }
  72. #endif
  73. }
  74. void Component::onAdd()
  75. {
  76. #if CC_ENABLE_SCRIPT_BINDING
  77. if (_scriptType == kScriptTypeJavascript)
  78. {
  79. sendComponentEventToJS(this, kComponentOnAdd);
  80. }
  81. #endif
  82. }
  83. void Component::onRemove()
  84. {
  85. #if CC_ENABLE_SCRIPT_BINDING
  86. if (_scriptType == kScriptTypeJavascript)
  87. {
  88. sendComponentEventToJS(this, kComponentOnRemove);
  89. }
  90. #endif
  91. }
  92. void Component::update(float /*delta*/)
  93. {
  94. #if CC_ENABLE_SCRIPT_BINDING
  95. if (_scriptType == kScriptTypeJavascript)
  96. {
  97. sendComponentEventToJS(this, kComponentOnUpdate);
  98. }
  99. #endif
  100. }
  101. bool Component::serialize(void* /*ar*/)
  102. {
  103. return true;
  104. }
  105. Component* Component::create()
  106. {
  107. Component * ret = new (std::nothrow) Component();
  108. if (ret && ret->init())
  109. {
  110. ret->autorelease();
  111. }
  112. else
  113. {
  114. CC_SAFE_DELETE(ret);
  115. }
  116. return ret;
  117. }
  118. void Component::setOwner(Node *owner)
  119. {
  120. _owner = owner;
  121. }
  122. void Component::setEnabled(bool enabled)
  123. {
  124. _enabled = enabled;
  125. }
  126. NS_CC_END