CCPUDynamicAttribute.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_DYNAMIC_ATTRIBUTE_H__
  22. #define __CC_PU_PARTICLE_3D_DYNAMIC_ATTRIBUTE_H__
  23. #include "base/CCRef.h"
  24. #include "math/CCMath.h"
  25. #include "CCPUSimpleSpline.h"
  26. #include <vector>
  27. NS_CC_BEGIN
  28. enum PUInterpolationType
  29. {
  30. IT_LINEAR,
  31. IT_SPLINE
  32. };
  33. /** Comparer used for sorting vector in ascending order
  34. */
  35. struct PUControlPointSorter
  36. {
  37. bool operator() (const Vec2& a, const Vec2& b)
  38. {
  39. return a.x < b.x;
  40. }
  41. };
  42. /* The DynamicAttribute class or its child classes encapsulate an attribute with specific (dynamic) behaviour.
  43. @remarks
  44. This class provides a uniform interface for retrieving the value of an attribute, while the calculation of
  45. this value may vary. Each subclass provides its own implementation of the getValue() function and the calling
  46. application doesn't need to know about the underlying logic. A subclass could just return a value that has
  47. previously been set, but it can also return a value that is randomly generated by a pre-defined min/max interval.
  48. The DynamicAttribute class is used in situations where different behaviour of a certain attribute is needed,
  49. but where implementation of this behaviour may not be scattered or duplicated within the application that needs
  50. it.
  51. */
  52. class CC_DLL PUDynamicAttribute : public Ref
  53. {
  54. public:
  55. enum DynamicAttributeType
  56. {
  57. DAT_FIXED,
  58. DAT_RANDOM,
  59. DAT_CURVED,
  60. DAT_OSCILLATE
  61. };
  62. /** Constructor
  63. */
  64. PUDynamicAttribute (void);
  65. /** Destructor
  66. */
  67. virtual ~PUDynamicAttribute (void);
  68. /** Virtual function that needs to be implemented by its childs.
  69. */
  70. virtual float getValue (float x = 0) = 0;
  71. /** Todo
  72. */
  73. DynamicAttributeType getType (void) const;
  74. /** Todo
  75. */
  76. void setType (DynamicAttributeType type);
  77. /** Returns true if one of the attributes was changed by an external source.
  78. */
  79. bool isValueChangedExternally(void) const;
  80. virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) = 0;
  81. virtual PUDynamicAttribute* clone() = 0;
  82. protected:
  83. DynamicAttributeType _type;
  84. bool _valueChangedExternally;
  85. };
  86. /* This class is an implementation of the DynamicAttribute class in its most simple form. It just returns a value
  87. that has previously been set.
  88. @remarks
  89. Although use of a regular attribute within the class that needs it is preferred, its benefit is that it makes
  90. use of the generic 'getValue' mechanism of a DynamicAttribute.
  91. */
  92. class CC_DLL PUDynamicAttributeFixed : public PUDynamicAttribute
  93. {
  94. public:
  95. /** Constructor
  96. */
  97. PUDynamicAttributeFixed (void);
  98. /** Copy constructor
  99. */
  100. PUDynamicAttributeFixed (const PUDynamicAttributeFixed& dynamicAttributeFixed);
  101. /** Destructor
  102. */
  103. ~PUDynamicAttributeFixed (void);
  104. /** Todo
  105. */
  106. virtual float getValue (float x = 0) override;
  107. /** Todo
  108. */
  109. virtual void setValue (float value);
  110. virtual PUDynamicAttributeFixed* clone() override;
  111. virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
  112. protected:
  113. float _value;
  114. };
  115. /* This class generates random values within a given minimum and maximum interval.
  116. */
  117. class CC_DLL PUDynamicAttributeRandom : public PUDynamicAttribute
  118. {
  119. public:
  120. /** Constructor
  121. */
  122. PUDynamicAttributeRandom (void);
  123. /** Copy constructor
  124. */
  125. PUDynamicAttributeRandom (const PUDynamicAttributeRandom& dynamicAttributeRandom);
  126. /** Destructor
  127. */
  128. ~PUDynamicAttributeRandom (void);
  129. /** Todo
  130. */
  131. virtual float getValue (float x = 0) override;
  132. /** Todo
  133. */
  134. void setMin (float min);
  135. float getMin (void) const;
  136. void setMax (float max);
  137. float getMax (void) const;
  138. void setMinMax (float min, float max);
  139. virtual PUDynamicAttributeRandom* clone() override;
  140. virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
  141. protected:
  142. float _min, _max;
  143. };
  144. /* This is a more complex usage of the DynamicAttribute principle. This class returns a value on an curve.
  145. @remarks
  146. After setting a number of control points, this class is able to interpolate a point on the curve that is based
  147. on these control points. Interpolation is done in different flavours. linear?provides linear interpolation
  148. of a value on the curve, while spline?generates a smooth curve and the returns a value that lies on that curve.
  149. */
  150. class CC_DLL PUDynamicAttributeCurved : public PUDynamicAttribute
  151. {
  152. public:
  153. typedef std::vector<Vec2> ControlPointList;
  154. /** Constructor
  155. */
  156. PUDynamicAttributeCurved (void);
  157. PUDynamicAttributeCurved (PUInterpolationType interpolationType);
  158. /** Copy constructor
  159. */
  160. PUDynamicAttributeCurved (const PUDynamicAttributeCurved& dynamicAttributeCurved);
  161. /** Destructor
  162. */
  163. ~PUDynamicAttributeCurved (void);
  164. /** Get and set the curve type
  165. */
  166. void setInterpolationType (PUInterpolationType interpolationType);
  167. PUInterpolationType getInterpolationType (void) const;
  168. /** Todo
  169. */
  170. virtual float getValue (float x = 0) override;
  171. /** Todo
  172. */
  173. virtual void addControlPoint (float x, float y);
  174. /** Todo
  175. */
  176. const ControlPointList& getControlPoints (void) const;
  177. /** Todo
  178. */
  179. void processControlPoints (void);
  180. /** Todo
  181. */
  182. size_t getNumControlPoints(void) const;
  183. /** Todo
  184. */
  185. void removeAllControlPoints(void);
  186. virtual PUDynamicAttributeCurved* clone() override;
  187. virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
  188. protected:
  189. /** Todo
  190. */
  191. float _range;
  192. /** Todo
  193. */
  194. PUSimpleSpline _spline;
  195. /** Todo
  196. */
  197. PUInterpolationType _interpolationType;
  198. /** Todo
  199. */
  200. ControlPointList _controlPoints;
  201. /** Find an iterator that forms the low (left) value of the interval where x lies in.
  202. */
  203. inline ControlPointList::iterator findNearestControlPointIterator(float x);
  204. /** Helper functions
  205. */
  206. inline ControlPointList::iterator getFirstValidIterator(void);
  207. inline ControlPointList::iterator getLastValidIterator(void);
  208. };
  209. /* This class generates values based on an oscillating function (i.e. Sine).
  210. */
  211. class CC_DLL PUDynamicAttributeOscillate : public PUDynamicAttribute
  212. {
  213. public:
  214. enum OscillationType
  215. {
  216. OSCT_SINE,
  217. OSCT_SQUARE
  218. };
  219. /** Constructor
  220. */
  221. PUDynamicAttributeOscillate (void);
  222. /** Copy constructor
  223. */
  224. PUDynamicAttributeOscillate (const PUDynamicAttributeOscillate& dynamicAttributeOscillate);
  225. /** Destructor
  226. */
  227. ~PUDynamicAttributeOscillate (void);
  228. /** Todo
  229. */
  230. virtual float getValue (float x = 0) override;
  231. /** Get and set the OscillationType
  232. */
  233. OscillationType getOscillationType (void) const;
  234. void setOscillationType (OscillationType oscillationType);
  235. /** Get and set the Frequency
  236. */
  237. float getFrequency (void) const;
  238. void setFrequency (float frequency);
  239. /** Get and set the Phase
  240. */
  241. float getPhase (void) const;
  242. void setPhase (float phase);
  243. /** Get and set the Base
  244. */
  245. float getBase (void) const;
  246. void setBase (float base);
  247. /** Get and set the Amplitude
  248. */
  249. float getAmplitude (void) const;
  250. void setAmplitude (float amplitude);
  251. virtual PUDynamicAttributeOscillate* clone() override;
  252. virtual void copyAttributesTo(PUDynamicAttribute* dynamicAttribute) override;
  253. protected:
  254. OscillationType _oscillationType;
  255. float _frequency;
  256. float _phase;
  257. float _base;
  258. float _amplitude;
  259. };
  260. /* Helper class to do some generic calculation.
  261. */
  262. class PUDynamicAttributeHelper
  263. {
  264. public:
  265. /* Return the value of a DynamicAttribute, given te x value.
  266. */
  267. float calculate(PUDynamicAttribute* dyn, float x, float defaultValue = 0.0f);
  268. };
  269. NS_CC_END
  270. #endif