CCPUObserver.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef __CC_PU_PARTICLE_3D_OBSERVER_H__
  23. #define __CC_PU_PARTICLE_3D_OBSERVER_H__
  24. #include "base/CCRef.h"
  25. #include "math/CCMath.h"
  26. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  27. #include <vector>
  28. #include <string>
  29. NS_CC_BEGIN
  30. struct PUParticle3D;
  31. class PUParticleSystem3D;
  32. class PUEventHandler;
  33. enum PUComparisionOperator
  34. {
  35. CO_LESS_THAN,
  36. CO_EQUALS,
  37. CO_GREATER_THAN
  38. };
  39. class CC_DLL PUObserver : public Ref
  40. {
  41. friend class PUParticleSystem3D;
  42. public:
  43. // Default values
  44. static const bool DEFAULT_ENABLED;
  45. static const PUParticle3D::ParticleType DEFAULT_PARTICLE_TYPE;
  46. static const float DEFAULT_INTERVAL;
  47. static const bool DEFAULT_UNTIL_EVENT;
  48. typedef std::vector<PUEventHandler*>::iterator ParticleEventHandlerIterator;
  49. typedef std::vector<PUEventHandler*>::const_iterator ParticleEventHandlerConstIterator;
  50. typedef std::vector<PUEventHandler*> ParticleEventHandlerList;
  51. /** Todo
  52. */
  53. const std::string& getObserverType(void) const {return _observerType;};
  54. void setObserverType(const std::string& observerType) {_observerType = observerType;};
  55. /** Todo
  56. */
  57. virtual bool isEnabled(void) const;
  58. /** Returns the 'enabled' value that was set in setEnabled() and not altered during execution.
  59. */
  60. bool _getOriginalEnabled(void) const;
  61. /** Todo
  62. */
  63. virtual void setEnabled(bool enabled);
  64. /** Reset internal values for 'enabled'. This means that both the mEnabled and mOriginalEnabled can be set again using setEnabled.
  65. @remarks
  66. Using _resetEnabled() makes it possible to use setEnabled() without the restriction of having a fixed mOriginalEnabled value.
  67. */
  68. void _resetEnabled(void);
  69. ///** Todo
  70. //*/
  71. //PUParticleSystem3D* getParentSystem(void) const {return mParentTechnique;};
  72. //void setParentSystem(PUParticleSystem3D* parentTechnique){mParentTechnique = parentTechnique;};
  73. /** Todo
  74. */
  75. const std::string& getName(void) const {return _name;};
  76. void setName(const std::string& name) {_name = name;};
  77. /** Todo
  78. */
  79. const PUParticle3D::ParticleType& getParticleTypeToObserve(void) const {return _particleTypeToObserve;};
  80. void setParticleTypeToObserve(const PUParticle3D::ParticleType particleTypeToObserve);
  81. virtual void notifyStart (void);
  82. virtual void notifyStop (void){/* Do nothing */};
  83. virtual void notifyRescaled(const Vec3& scale);
  84. virtual void preUpdateObserver(float deltaTime);
  85. virtual void updateObserver(PUParticle3D *particle, float deltaTime, bool firstParticle);
  86. virtual void postUpdateObserver(float deltaTime);
  87. virtual void firstParticleUpdate(PUParticle3D *particle, float deltaTime);
  88. /** This function determines whether a condition (the event) is true or false.
  89. */
  90. virtual bool observe (PUParticle3D* particle, float timeElapsed) = 0;
  91. /** Todo
  92. */
  93. PUEventHandler* createEventHandler(const std::string& eventHandlerType);
  94. /** Todo
  95. */
  96. void addEventHandler(PUEventHandler* eventHandler);
  97. /** Todo
  98. */
  99. void removeEventHandler(PUEventHandler* eventHandler);
  100. /** Todo
  101. */
  102. PUEventHandler* getEventHandler (size_t index) const;
  103. /** Todo
  104. */
  105. PUEventHandler* getEventHandler (const std::string& eventHandlerName) const;
  106. /** Todo
  107. */
  108. size_t getNumEventHandlers (void) const;
  109. /** Todo
  110. */
  111. void destroyEventHandler(PUEventHandler* eventHandler);
  112. /** Todo
  113. */
  114. void destroyEventHandler (size_t index);
  115. /** Todo
  116. */
  117. void destroyAllEventHandlers (void);
  118. /** Return the interval value, which defines at what interval the observer is called.
  119. */
  120. float getObserverInterval(void) const;
  121. /** Set the interval value, which defines at what interval the observer is called.
  122. */
  123. void setObserverInterval(float observerInterval);
  124. /** Return the value of mObserveUntilEvent.
  125. */
  126. bool getObserveUntilEvent(void) const;
  127. /** Sets the value of mObserveUntilEvent. This value determines whether observation must be continued
  128. after an event occurred and the event handlers are called.
  129. */
  130. void setObserveUntilEvent(bool observeUntilEvent);
  131. /** Returns true if the particle type to observe specifically has been set.
  132. */
  133. bool isParticleTypeToObserveSet(void) const;
  134. virtual void copyAttributesTo (PUObserver* observer);
  135. CC_CONSTRUCTOR_ACCESS:
  136. PUObserver(void);
  137. virtual ~PUObserver(void);
  138. protected:
  139. PUParticleSystem3D* _particleSystem;
  140. // Type of observer
  141. std::string _observerType;
  142. // Name of the observer (optional)
  143. std::string _name;
  144. /** Determines whether an observer is activated.
  145. */
  146. bool _enabled;
  147. bool _originalEnabled;
  148. bool _originalEnabledSet;
  149. /** Determines whether the _observe() function must be executed.
  150. @remarks
  151. This is independent of mEnabled, which means that an observer can be enabled, but if mObserve has
  152. been set to false, it still doesn't observe. It is used to enable/disable execution of the _observe()
  153. function in combination with the update interval.
  154. */
  155. bool _observe;
  156. /** Observes until an event takes place. If the event occurs, the event handlers are called and after that
  157. the observer doesn't observe anymore.
  158. */
  159. bool _observeUntilEvent;
  160. /** Flag that is set if the event handler(s) are called at least once.
  161. */
  162. bool _eventHandlersExecuted;
  163. /** Although the scale is on a Particle System level, the observer can also be scaled.
  164. */
  165. Vec3 _observerScale;
  166. // Particle type to be observed. Default is that all particles are observed.
  167. PUParticle3D::ParticleType _particleTypeToObserve;
  168. /** Determines whether mParticleTypeToObserve is set. If true only that particles of the specified type
  169. are observed. If false (= default), all particles are observed.
  170. */
  171. bool _particleTypeToObserveSet;
  172. // List with EventHandlers, which will be triggered if the condition that this observer validates occurs.
  173. ParticleEventHandlerList _eventHandlers;
  174. /** Values that determine that the observer is called after every interval.
  175. */
  176. float _observerInterval;
  177. float _observerIntervalRemainder;
  178. bool _observerIntervalSet;
  179. /** Handle the observer implementation.
  180. */
  181. virtual void handleObserve (PUParticle3D* particle, float timeElapsed);
  182. /** Handle the event.
  183. @remarks
  184. This function iterates over all at the observer registered handlers and executes them.
  185. */
  186. virtual void handleEvent (PUParticle3D* particle, float timeElapsed);
  187. };
  188. NS_CC_END
  189. #endif