CCPUObserver.h 7.8 KB

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