CCPUDoEnableComponentEventHandler.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "extensions/Particle3D/PU/CCPUDoEnableComponentEventHandler.h"
  23. #include "extensions/Particle3D/PU/CCPUAffector.h"
  24. #include "extensions/Particle3D/PU/CCPUEmitter.h"
  25. #include "extensions/Particle3D/PU/CCPUObserver.h"
  26. NS_CC_BEGIN
  27. //-----------------------------------------------------------------------
  28. PUDoEnableComponentEventHandler::PUDoEnableComponentEventHandler(void) :
  29. PUEventHandler(),
  30. _componentType(CT_EMITTER),
  31. _componentEnabled(true)
  32. {
  33. }
  34. //-----------------------------------------------------------------------
  35. void PUDoEnableComponentEventHandler::handle (PUParticleSystem3D* particleSystem, PUParticle3D* /*particle*/, float /*timeElapsed*/)
  36. {
  37. /** Search for the component.
  38. */
  39. //ParticleTechnique* technique = 0;
  40. switch (_componentType)
  41. {
  42. case CT_EMITTER:
  43. {
  44. PUEmitter* emitter = particleSystem->getEmitter(_componentName);
  45. if (!emitter)
  46. {
  47. // Search all techniques in this ParticleSystem for an emitter with the correct name
  48. PUParticleSystem3D* system = particleSystem->getParentParticleSystem();
  49. if (system){
  50. auto children = system->getChildren();
  51. for(auto iter : children)
  52. {
  53. PUParticleSystem3D *child = dynamic_cast<PUParticleSystem3D *>(iter);
  54. if (child){
  55. emitter = child->getEmitter(_componentName);
  56. if (emitter)
  57. {
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. if (emitter)
  65. {
  66. emitter->setEnabled(_componentEnabled);
  67. }
  68. }
  69. break;
  70. case CT_AFFECTOR:
  71. {
  72. PUAffector* affector = particleSystem->getAffector(_componentName);
  73. if (!affector)
  74. {
  75. // Search all techniques in this ParticleSystem for an emitter with the correct name
  76. PUParticleSystem3D* system = particleSystem->getParentParticleSystem();
  77. if (system){
  78. auto children = system->getChildren();
  79. for(auto iter : children)
  80. {
  81. PUParticleSystem3D *child = dynamic_cast<PUParticleSystem3D *>(iter);
  82. if (child){
  83. affector = child->getAffector(_componentName);
  84. if (affector)
  85. {
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. if (affector)
  93. {
  94. affector->setEnabled(_componentEnabled);
  95. }
  96. }
  97. break;
  98. case CT_OBSERVER:
  99. {
  100. PUObserver* observer = particleSystem->getObserver(_componentName);
  101. if (!observer)
  102. {
  103. // Search all techniques in this ParticleSystem for an emitter with the correct name
  104. PUParticleSystem3D* system = particleSystem->getParentParticleSystem();
  105. if (system){
  106. auto children = system->getChildren();
  107. for(auto iter : children)
  108. {
  109. PUParticleSystem3D *child = dynamic_cast<PUParticleSystem3D *>(iter);
  110. if (child){
  111. observer = child->getObserver(_componentName);
  112. if (observer)
  113. {
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. if (observer)
  121. {
  122. observer->setEnabled(_componentEnabled);
  123. }
  124. }
  125. break;
  126. case CT_TECHNIQUE:
  127. {
  128. // Search in this ParticleSystem for a technique with the correct name
  129. PUParticleSystem3D* system = particleSystem->getParentParticleSystem();
  130. if (system){
  131. auto children = system->getChildren();
  132. for (auto iter : children){
  133. PUParticleSystem3D *child = dynamic_cast<PUParticleSystem3D *>(iter);
  134. if (child && child->getName() == _componentName){
  135. child->setEnabled(_componentEnabled);
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. PUDoEnableComponentEventHandler* PUDoEnableComponentEventHandler::create()
  147. {
  148. auto peh = new (std::nothrow) PUDoEnableComponentEventHandler();
  149. peh->autorelease();
  150. return peh;
  151. }
  152. void PUDoEnableComponentEventHandler::copyAttributesTo( PUEventHandler* eventHandler )
  153. {
  154. PUEventHandler::copyAttributesTo(eventHandler);
  155. PUDoEnableComponentEventHandler* doEnableComponentEventHandler = static_cast<PUDoEnableComponentEventHandler*>(eventHandler);
  156. doEnableComponentEventHandler->setComponentType(_componentType);
  157. doEnableComponentEventHandler->setComponentName(_componentName);
  158. doEnableComponentEventHandler->setComponentEnabled(_componentEnabled);
  159. }
  160. NS_CC_END