CCMotionStreak.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /****************************************************************************
  2. Copyright (c) 2011 ForzeField Studios S.L.
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CCMOTION_STREAK_H__
  24. #define __CCMOTION_STREAK_H__
  25. #include "base/CCProtocols.h"
  26. #include "2d/CCNode.h"
  27. #include "renderer/CCCustomCommand.h"
  28. NS_CC_BEGIN
  29. class Texture2D;
  30. /**
  31. * @addtogroup _2d
  32. * @{
  33. */
  34. /** @class MotionStreak.
  35. * @brief Creates a trailing path.
  36. */
  37. class CC_DLL MotionStreak : public Node, public TextureProtocol
  38. {
  39. public:
  40. /** Creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture filename.
  41. *
  42. * @param timeToFade The fade time, in seconds.
  43. * @param minSeg The minimum segments.
  44. * @param strokeWidth The width of stroke.
  45. * @param strokeColor The color of stroke.
  46. * @param imagePath The texture file name of stoke.
  47. * @return An autoreleased MotionStreak object.
  48. */
  49. static MotionStreak* create(float timeToFade, float minSeg, float strokeWidth, const Color3B& strokeColor, const std::string& imagePath);
  50. /** Creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture.
  51. *
  52. * @param timeToFade The fade time, in seconds.
  53. * @param minSeg The minimum segments.
  54. * @param strokeWidth The width of stroke.
  55. * @param strokeColor The color of stroke.
  56. * @param texture The texture name of stoke.
  57. * @return An autoreleased MotionStreak object.
  58. */
  59. static MotionStreak* create(float timeToFade, float minSeg, float strokeWidth, const Color3B& strokeColor, Texture2D* texture);
  60. /** Color used for the tint.
  61. *
  62. * @param colors The color used for the tint.
  63. */
  64. void tintWithColor(const Color3B& colors);
  65. /** Remove all living segments of the ribbon.
  66. */
  67. void reset();
  68. /** When fast mode is enabled, new points are added faster but with lower precision.
  69. *
  70. * @return True if fast mode is enabled.
  71. */
  72. bool isFastMode() const { return _fastMode; }
  73. /** Sets fast mode or not.
  74. *
  75. * @param bFastMode True if enabled fast mode.
  76. */
  77. void setFastMode(bool bFastMode) { _fastMode = bFastMode; }
  78. /** Get stroke.
  79. *
  80. * @return float stroke.
  81. */
  82. float getStroke() const { return _stroke; }
  83. /** Set stroke.
  84. *
  85. * @param stroke The width of stroke.
  86. */
  87. void setStroke(float stroke) { _stroke = stroke; }
  88. /** Is the starting position initialized or not.
  89. *
  90. * @return True if the starting position is initialized.
  91. */
  92. bool isStartingPositionInitialized() const { return _startingPositionInitialized; }
  93. /** Sets the starting position initialized or not.
  94. *
  95. * @param bStartingPositionInitialized True if initialized the starting position.
  96. */
  97. void setStartingPositionInitialized(bool bStartingPositionInitialized)
  98. {
  99. _startingPositionInitialized = bStartingPositionInitialized;
  100. }
  101. // Overrides
  102. virtual void setPosition(const Vec2& position) override;
  103. virtual void setPosition(float x, float y) override;
  104. virtual const Vec2& getPosition() const override;
  105. virtual void getPosition(float* x, float* y) const override;
  106. virtual void setPositionX(float x) override;
  107. virtual void setPositionY(float y) override;
  108. virtual float getPositionX(void) const override;
  109. virtual float getPositionY(void) const override;
  110. virtual Vec3 getPosition3D() const override;
  111. /**
  112. * @js NA
  113. * @lua NA
  114. */
  115. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  116. /**
  117. * @lua NA
  118. */
  119. virtual void update(float delta) override;
  120. virtual Texture2D* getTexture() const override;
  121. virtual void setTexture(Texture2D *texture) override;
  122. /**
  123. * @js NA
  124. * @lua NA
  125. */
  126. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  127. /**
  128. * @js NA
  129. * @lua NA
  130. */
  131. virtual const BlendFunc& getBlendFunc() const override;
  132. virtual GLubyte getOpacity() const override;
  133. virtual void setOpacity(GLubyte opacity) override;
  134. virtual void setOpacityModifyRGB(bool value) override;
  135. virtual bool isOpacityModifyRGB() const override;
  136. CC_CONSTRUCTOR_ACCESS:
  137. MotionStreak();
  138. virtual ~MotionStreak();
  139. /** initializes a motion streak with fade in seconds, minimum segments, stroke's width, color and texture filename */
  140. bool initWithFade(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path);
  141. /** initializes a motion streak with fade in seconds, minimum segments, stroke's width, color and texture */
  142. bool initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture);
  143. protected:
  144. //renderer callback
  145. void onDraw(const Mat4 &transform, uint32_t flags);
  146. bool _fastMode;
  147. bool _startingPositionInitialized;
  148. /** texture used for the motion streak */
  149. Texture2D* _texture;
  150. BlendFunc _blendFunc;
  151. Vec2 _positionR;
  152. float _stroke;
  153. float _fadeDelta;
  154. float _minSeg;
  155. unsigned int _maxPoints;
  156. unsigned int _nuPoints;
  157. unsigned int _previousNuPoints;
  158. /** Pointers */
  159. Vec2* _pointVertexes;
  160. float* _pointState;
  161. // Opengl
  162. Vec2* _vertices;
  163. GLubyte* _colorPointer;
  164. Tex2F* _texCoords;
  165. CustomCommand _customCommand;
  166. private:
  167. CC_DISALLOW_COPY_AND_ASSIGN(MotionStreak);
  168. };
  169. // end of _2d group
  170. /// @}
  171. NS_CC_END
  172. #endif //__CCMOTION_STREAK_H__