CCProgressTimer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /****************************************************************************
  2. Copyright (c) 2010 Lam Pham
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  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 __MISC_NODE_CCPROGRESS_TIMER_H__
  23. #define __MISC_NODE_CCPROGRESS_TIMER_H__
  24. #include "renderer/CCCustomCommand.h"
  25. #include "2d/CCNode.h"
  26. NS_CC_BEGIN
  27. class Sprite;
  28. /**
  29. * @addtogroup _2d
  30. * @{
  31. */
  32. /**
  33. * @brief ProgressTimer is a subclass of Node.
  34. * It renders the inner sprite according to the percentage.
  35. * The progress can be Radial, Horizontal or vertical.
  36. * @since v0.99.1
  37. */
  38. class CC_DLL ProgressTimer : public Node
  39. {
  40. public:
  41. /** Types of progress
  42. * @since v0.99.1
  43. */
  44. enum class Type
  45. {
  46. RADIAL,/** Radial Counter-Clockwise. */
  47. BAR,/** Bar. */
  48. };
  49. /** Creates a progress timer with the sprite as the shape the timer goes through.
  50. *
  51. * @param sp The sprite as the shape the timer goes through.
  52. * @return A ProgressTimer.
  53. */
  54. static ProgressTimer* create(Sprite* sp);
  55. /** Change the percentage to change progress.
  56. *
  57. * @return A Type
  58. */
  59. Type getType() const { return _type; }
  60. /** Percentages are from 0 to 100.
  61. *
  62. * @return Percentages.
  63. */
  64. float getPercentage() const { return _percentage; }
  65. /** The image to show the progress percentage, retain.
  66. *
  67. * @return A sprite.
  68. */
  69. Sprite* getSprite() const { return _sprite; }
  70. /** Set the initial percentage values.
  71. *
  72. * @param percentage The initial percentage values.
  73. */
  74. void setPercentage(float percentage);
  75. /** Set the sprite as the shape.
  76. *
  77. * @param sprite The sprite as the shape.
  78. */
  79. void setSprite(Sprite *sprite);
  80. /** Set the ProgressTimer type.
  81. *
  82. * @param type Is an Type.
  83. */
  84. void setType(Type type);
  85. /** Return the Reverse direction.
  86. *
  87. * @return If the direction is Anti-clockwise,it will return true.
  88. */
  89. bool isReverseDirection() { return _reverseDirection; };
  90. /** Set the Reverse direction.
  91. *
  92. * @param value If value is false it will clockwise,if is true it will Anti-clockwise.
  93. */
  94. void setReverseDirection(bool value);
  95. /** Set the Reverse direction.
  96. * @js setReverseDirection
  97. * @lua setReverseDirection
  98. * @param reverse If reverse is false it will clockwise,if is true it will Anti-clockwise.
  99. */
  100. CC_DEPRECATED_ATTRIBUTE void setReverseProgress(bool reverse) { setReverseDirection(reverse); }
  101. /**
  102. * Midpoint is used to modify the progress start position.
  103. * If you're using radials type then the midpoint changes the center point.
  104. * If you're using bar type then the midpoint changes the bar growth.
  105. * it expands from the center but clamps to the sprites edge so:
  106. * you want a left to right then set the midpoint all the way to Vec2(0,y).
  107. * you want a right to left then set the midpoint all the way to Vec2(1,y).
  108. * you want a bottom to top then set the midpoint all the way to Vec2(x,0).
  109. * you want a top to bottom then set the midpoint all the way to Vec2(x,1).
  110. * @param point A Vec2 point.
  111. */
  112. void setMidpoint(const Vec2& point);
  113. /** Returns the Midpoint.
  114. *
  115. * @return A Vec2.
  116. */
  117. Vec2 getMidpoint() const;
  118. /**
  119. * This allows the bar type to move the component at a specific rate.
  120. * Set the component to 0 to make sure it stays at 100%.
  121. * For example you want a left to right bar but not have the height stay 100%.
  122. * Set the rate to be Vec2(0,1); and set the midpoint to = Vec2(0,.5f).
  123. * @param barChangeRate A Vec2.
  124. */
  125. void setBarChangeRate(const Vec2& barChangeRate ) { _barChangeRate = barChangeRate; }
  126. /** Returns the BarChangeRate.
  127. *
  128. * @return A barChangeRate.
  129. */
  130. Vec2 getBarChangeRate() const { return _barChangeRate; }
  131. // Overrides
  132. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  133. virtual void setAnchorPoint(const Vec2& anchorPoint) override;
  134. virtual void setColor(const Color3B &color) override;
  135. virtual const Color3B& getColor() const override;
  136. virtual void setOpacity(GLubyte opacity) override;
  137. virtual GLubyte getOpacity() const override;
  138. CC_CONSTRUCTOR_ACCESS:
  139. /**
  140. * @js ctor
  141. */
  142. ProgressTimer();
  143. /**
  144. * @js NA
  145. * @lua NA
  146. */
  147. virtual ~ProgressTimer();
  148. /** Initializes a progress timer with the sprite as the shape the timer goes through */
  149. bool initWithSprite(Sprite* sp);
  150. protected:
  151. void onDraw(const Mat4 &transform, uint32_t flags);
  152. Tex2F textureCoordFromAlphaPoint(Vec2 alpha);
  153. Vec2 vertexFromAlphaPoint(Vec2 alpha);
  154. void updateProgress(void);
  155. void updateBar(void);
  156. void updateRadial(void);
  157. virtual void updateColor(void) override;
  158. Vec2 boundaryTexCoord(char index);
  159. Type _type;
  160. Vec2 _midpoint;
  161. Vec2 _barChangeRate;
  162. float _percentage;
  163. Sprite *_sprite;
  164. int _vertexDataCount;
  165. V2F_C4B_T2F *_vertexData;
  166. CustomCommand _customCommand;
  167. bool _reverseDirection;
  168. private:
  169. CC_DISALLOW_COPY_AND_ASSIGN(ProgressTimer);
  170. };
  171. // end of misc_nodes group
  172. /// @}
  173. NS_CC_END
  174. #endif //__MISC_NODE_CCPROGRESS_TIMER_H__