UILoadingBar.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 __UILOADINGBAR_H__
  22. #define __UILOADINGBAR_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. NS_CC_BEGIN
  26. /**
  27. * @addtogroup ui
  28. * @{
  29. */
  30. struct CC_DLL ResourceData;
  31. namespace ui {
  32. class Scale9Sprite;
  33. /**
  34. *@brief Visual indicator of progress in some operation.
  35. * Displays a bar to the user representing how far the operation has progressed.
  36. *
  37. */
  38. class CC_GUI_DLL LoadingBar : public Widget
  39. {
  40. DECLARE_CLASS_GUI_INFO
  41. public:
  42. /**
  43. * Loading bar progress direction.
  44. */
  45. enum class Direction
  46. {
  47. LEFT,
  48. RIGHT
  49. };
  50. /**
  51. * Default constructor.
  52. * @js ctor
  53. * @lua new
  54. */
  55. LoadingBar();
  56. /**
  57. * Default destructor.
  58. * @js NA
  59. * @lua NA
  60. */
  61. virtual ~LoadingBar();
  62. /**
  63. * Create an empty LoadingBar.
  64. *@return A LoadingBar instance.
  65. */
  66. static LoadingBar* create();
  67. /**
  68. * @brief Create a LoadingBar with a texture name and a predefined progress value.
  69. *
  70. * @param textureName LoadingBar background texture name.
  71. * @param percentage A percentage in float.
  72. * @return A LoadingBar instance.
  73. */
  74. static LoadingBar* create(const std::string& textureName, float percentage = 0);
  75. /**
  76. * @brief Create a LoadingBar with a texture name along with its texture type and a predefined progress value.
  77. *
  78. * @param textureName LoadingBar background texture name.
  79. * @param texType LoadingBar background texture type.
  80. * @param percentage A percentage in float, default value is 0.
  81. * @return A LoadingBar instance.
  82. */
  83. static LoadingBar* create(const std::string& textureName,
  84. TextureResType texType,
  85. float percentage = 0);
  86. /**
  87. * Change the progress direction of LoadingBar.
  88. *
  89. * @see Direction `LEFT` means progress left to right, `RIGHT` otherwise.
  90. * @param direction Direction
  91. */
  92. void setDirection(Direction direction);
  93. /**
  94. * Get the progress direction of LoadingBar.
  95. *
  96. * @see Direction `LEFT` means progress left to right, `RIGHT` otherwise.
  97. * @return LoadingBar progress direction.
  98. */
  99. Direction getDirection()const;
  100. /**
  101. * Load texture for LoadingBar.
  102. *
  103. * @param texture File name of texture.
  104. * @param texType Texture resource type,@see TextureResType.
  105. */
  106. void loadTexture(const std::string& texture,TextureResType texType = TextureResType::LOCAL);
  107. /**
  108. * Changes the progress value of LoadingBar.
  109. *
  110. * @param percent Percent value from 1 to 100.
  111. */
  112. void setPercent(float percent);
  113. /**
  114. * Get the progress value of LoadingBar.
  115. *
  116. * @return Progress value from 1 to 100.
  117. */
  118. float getPercent() const;
  119. /**
  120. * Enable scale9 renderer.
  121. *
  122. * @param enabled Set to true will use scale9 renderer, false otherwise.
  123. */
  124. void setScale9Enabled(bool enabled);
  125. /**
  126. * @brief Query whether LoadingBar is using scale9 renderer or not.
  127. * @return Whether LoadingBar uses scale9 renderer or not.
  128. */
  129. bool isScale9Enabled()const;
  130. /**
  131. * Set capInsets for LoadingBar.
  132. * This setting only take effect when enable scale9 renderer.
  133. * @param capInsets CapInset in `Rect`.
  134. */
  135. void setCapInsets(const Rect &capInsets);
  136. /**
  137. * @brief Query LoadingBar's capInsets.
  138. * @return CapInsets of LoadingBar.
  139. */
  140. const Rect& getCapInsets()const;
  141. //override methods.
  142. virtual void ignoreContentAdaptWithSize(bool ignore) override;
  143. virtual Size getVirtualRendererSize() const override;
  144. virtual Node* getVirtualRenderer() override;
  145. virtual std::string getDescription() const override;
  146. ResourceData getRenderFile();
  147. protected:
  148. virtual void initRenderer() override;
  149. virtual void onSizeChanged() override;
  150. void setScale9Scale();
  151. void updateProgressBar();
  152. void barRendererScaleChangedWithSize();
  153. void setupTexture();
  154. void handleSpriteFlipX();
  155. void loadTexture(SpriteFrame* spriteframe);
  156. virtual void adaptRenderers() override;
  157. virtual Widget* createCloneInstance() override;
  158. virtual void copySpecialProperties(Widget* model) override;
  159. protected:
  160. Direction _direction;
  161. float _percent;
  162. float _totalLength;
  163. Scale9Sprite* _barRenderer;
  164. TextureResType _renderBarTexType;
  165. Size _barRendererTextureSize;
  166. Rect _originalRect;
  167. bool _scale9Enabled;
  168. bool _prevIgnoreSize;
  169. Rect _capInsets;
  170. bool _barRendererAdaptDirty;
  171. std::string _textureFile;
  172. };
  173. }
  174. // end of ui group
  175. /// @}
  176. NS_CC_END
  177. #endif /* defined(__CocoGUI__LoadingBar__) */