UISlider.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 __UISLIDER_H__
  22. #define __UISLIDER_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. NS_CC_BEGIN
  26. /**
  27. * @addtogroup ui
  28. * @{
  29. */
  30. class Sprite;
  31. struct CC_DLL ResourceData;
  32. namespace ui {
  33. class Scale9Sprite;
  34. typedef enum
  35. {
  36. SLIDER_PERCENTCHANGED
  37. }SliderEventType;
  38. typedef void (Ref::*SEL_SlidPercentChangedEvent)(Ref*,SliderEventType);
  39. #define sliderpercentchangedselector(_SELECTOR) (SEL_SlidPercentChangedEvent)(&_SELECTOR)
  40. /**
  41. * @brief UI Slider widget.
  42. */
  43. class CC_GUI_DLL Slider : public Widget
  44. {
  45. DECLARE_CLASS_GUI_INFO
  46. public:
  47. /** EventType Slider event type.
  48. */
  49. enum class EventType
  50. {
  51. ON_PERCENTAGE_CHANGED,
  52. //@since v3.7
  53. ON_SLIDEBALL_DOWN,
  54. ON_SLIDEBALL_UP,
  55. ON_SLIDEBALL_CANCEL
  56. };
  57. typedef std::function<void(Ref*,EventType)> ccSliderCallback;
  58. /**
  59. * Default constructor.
  60. * @js ctor
  61. * @lua new
  62. */
  63. Slider();
  64. /**
  65. * Default destructor.
  66. * @js NA
  67. * @lua NA
  68. */
  69. virtual ~Slider();
  70. /** Create a slider widget.
  71. *
  72. * @return An autoreleased Slider object.
  73. */
  74. static Slider* create();
  75. /** Create a slider widget with bar texture, ball texture and texture resource type.
  76. *
  77. * @param barTextureName Bar texture file name.
  78. * @param normalBallTextureName Ball texture file name.
  79. * @param resType Texture resource type.
  80. * @return An autoreleased Slider object.
  81. */
  82. static Slider* create(const std::string& barTextureName,
  83. const std::string& normalBallTextureName,
  84. TextureResType resType = TextureResType::LOCAL);
  85. /**
  86. * Load texture for slider bar.
  87. *
  88. * @param fileName File name of texture.
  89. * @param resType @see TextureResType .
  90. */
  91. void loadBarTexture(const std::string& fileName,TextureResType resType = TextureResType::LOCAL);
  92. /**
  93. * Sets if slider is using scale9 renderer.
  94. * @param able True that using scale9 renderer, false otherwise.
  95. */
  96. void setScale9Enabled(bool able);
  97. /**
  98. * Gets If slider is using scale9 renderer.
  99. * @return True that using scale9 renderer, false otherwise.
  100. */
  101. bool isScale9Enabled()const;
  102. /**
  103. * Sets capinsets for slider, if slider is using scale9 renderer.
  104. * @param capInsets Capinsets for slider.
  105. */
  106. void setCapInsets(const Rect &capInsets);
  107. /**
  108. * Sets capinsets for bar slider, if slider is using scale9 renderer.
  109. * @param capInsets Capinsets for bar slider.
  110. */
  111. void setCapInsetsBarRenderer(const Rect &capInsets);
  112. /**
  113. * Gets capinsets for bar slider, if slider is using scale9 renderer.
  114. * @return capInsets Capinsets for bar slider.
  115. */
  116. const Rect& getCapInsetsBarRenderer()const;
  117. /**
  118. * Sets capinsets for progress bar slider, if slider is using scale9 renderer.
  119. * @param capInsets Capinsets for progress bar slider.
  120. * @js NA
  121. */
  122. void setCapInsetProgressBarRenderer(const Rect &capInsets);
  123. /**
  124. * Gets capinsets for progress bar slider, if slider is using scale9 renderer.
  125. * @return Capinsets for progress bar slider.
  126. * @js NA
  127. */
  128. const Rect& getCapInsetsProgressBarRenderer()const;
  129. /**
  130. * Load textures for slider ball.
  131. *
  132. * @param normal Normal state texture.
  133. * @param pressed Pressed state texture.
  134. * @param disabled Disabled state texture.
  135. * @param texType @see TextureResType .
  136. */
  137. void loadSlidBallTextures(const std::string& normal,
  138. const std::string& pressed = "",
  139. const std::string& disabled = "",
  140. TextureResType texType = TextureResType::LOCAL);
  141. /**
  142. * Load normal state texture for slider ball.
  143. *
  144. * @param normal Normal state texture.
  145. * @param resType @see TextureResType .
  146. */
  147. void loadSlidBallTextureNormal(const std::string& normal,TextureResType resType = TextureResType::LOCAL);
  148. /**
  149. * Load pressed state texture for slider ball.
  150. *
  151. * @param pressed Pressed state texture.
  152. * @param resType @see TextureResType .
  153. */
  154. void loadSlidBallTexturePressed(const std::string& pressed,TextureResType resType = TextureResType::LOCAL);
  155. /**
  156. * Load disabled state texture for slider ball.
  157. *
  158. * @param disabled Disabled state texture.
  159. * @param resType @see TextureResType .
  160. */
  161. void loadSlidBallTextureDisabled(const std::string& disabled,TextureResType resType = TextureResType::LOCAL);
  162. /**
  163. * Load dark state texture for slider progress bar.
  164. *
  165. * @param fileName File path of texture.
  166. * @param resType @see TextureResType .
  167. */
  168. void loadProgressBarTexture(const std::string& fileName, TextureResType resType = TextureResType::LOCAL);
  169. /**
  170. * Changes the progress direction of slider.
  171. * @param percent Percent value from 1 to 100.
  172. */
  173. void setPercent(int percent);
  174. /**
  175. * Gets the progress direction of slider.
  176. *
  177. * @return percent Percent value from 1 to 100.
  178. */
  179. int getPercent()const;
  180. /**
  181. * Set a large value could give more control to the precision.
  182. * @since v3.7
  183. * @param percent The max percent of Slider.
  184. */
  185. void setMaxPercent(int percent);
  186. /**
  187. * Query the maximum percent of Slider. The default value is 100.
  188. * @since v3.7
  189. * @return The maximum percent of the Slider.
  190. */
  191. int getMaxPercent()const;
  192. CC_DEPRECATED_ATTRIBUTE void addEventListenerSlider(Ref* target,SEL_SlidPercentChangedEvent selector);
  193. /**
  194. * Add call back function called when slider's percent has changed to slider.
  195. *
  196. * @param callback An given call back function called when slider's percent has changed to slider.
  197. */
  198. void addEventListener(const ccSliderCallback& callback);
  199. virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override;
  200. virtual void onTouchMoved(Touch *touch, Event *unusedEvent) override;
  201. virtual void onTouchEnded(Touch *touch, Event *unusedEvent) override;
  202. virtual void onTouchCancelled(Touch *touch, Event *unusedEvent) override;
  203. //override "getVirtualRendererSize" method of widget.
  204. virtual Size getVirtualRendererSize() const override;
  205. //override "getVirtualRenderer" method of widget.
  206. virtual Node* getVirtualRenderer() override;
  207. //override "ignoreContentAdaptWithSize" method of widget.
  208. virtual void ignoreContentAdaptWithSize(bool ignore) override;
  209. //override the widget's hitTest function to perform its own
  210. virtual bool hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const override;
  211. /**
  212. * Returns the "class name" of widget.
  213. */
  214. virtual std::string getDescription() const override;
  215. /** When user pressed the button, the button will zoom to a scale.
  216. * The final scale of the button equals (button original scale + _zoomScale)
  217. * @since v3.3
  218. */
  219. void setZoomScale(float scale);
  220. /**
  221. * @brief Return a zoom scale
  222. * @since v3.3
  223. */
  224. float getZoomScale()const;
  225. Sprite* getSlidBallNormalRenderer() const;
  226. Sprite* getSlidBallPressedRenderer() const;
  227. Sprite* getSlidBallDisabledRenderer() const;
  228. Node* getSlidBallRenderer() const;
  229. ResourceData getBackFile();
  230. ResourceData getProgressBarFile();
  231. ResourceData getBallNormalFile();
  232. ResourceData getBallPressedFile();
  233. ResourceData getBallDisabledFile();
  234. CC_CONSTRUCTOR_ACCESS:
  235. virtual bool init() override;
  236. protected:
  237. virtual void initRenderer() override;
  238. float getPercentWithBallPos(const Vec2 &pt) const;
  239. void percentChangedEvent(EventType event);
  240. virtual void onPressStateChangedToNormal() override;
  241. virtual void onPressStateChangedToPressed() override;
  242. virtual void onPressStateChangedToDisabled() override;
  243. virtual void onSizeChanged() override;
  244. void setupBarTexture();
  245. void loadBarTexture(SpriteFrame* spriteframe);
  246. void setupProgressBarTexture();
  247. void loadProgressBarTexture(SpriteFrame* spriteframe);
  248. void loadSlidBallTextureNormal(SpriteFrame* spriteframe);
  249. void loadSlidBallTexturePressed(SpriteFrame* spriteframe);
  250. void loadSlidBallTextureDisabled(SpriteFrame* spriteframe);
  251. void barRendererScaleChangedWithSize();
  252. void progressBarRendererScaleChangedWithSize();
  253. virtual Widget* createCloneInstance() override;
  254. virtual void copySpecialProperties(Widget* model) override;
  255. virtual void adaptRenderers() override;
  256. protected:
  257. Scale9Sprite* _barRenderer;
  258. Scale9Sprite* _progressBarRenderer;
  259. Size _barTextureSize;
  260. Rect _originalBarRect;
  261. Size _progressBarTextureSize;
  262. Rect _originalProgressBarRect;
  263. Sprite* _slidBallNormalRenderer;
  264. Sprite* _slidBallPressedRenderer;
  265. Sprite* _slidBallDisabledRenderer;
  266. Node* _slidBallRenderer;
  267. float _barLength;
  268. int _percent;
  269. int _maxPercent;
  270. bool _scale9Enabled;
  271. bool _prevIgnoreSize;
  272. float _zoomScale;
  273. float _sliderBallNormalTextureScaleX;
  274. float _sliderBallNormalTextureScaleY;
  275. bool _isSliderBallPressedTextureLoaded;
  276. bool _isSliderBallDisabledTexturedLoaded;
  277. Rect _capInsetsBarRenderer;
  278. Rect _capInsetsProgressBarRenderer;
  279. Ref* _sliderEventListener;
  280. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  281. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  282. #elif _MSC_VER >= 1400 //vs 2005 or higher
  283. #pragma warning (push)
  284. #pragma warning (disable: 4996)
  285. #endif
  286. SEL_SlidPercentChangedEvent _sliderEventSelector;
  287. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  288. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  289. #elif _MSC_VER >= 1400 //vs 2005 or higher
  290. #pragma warning (pop)
  291. #endif
  292. ccSliderCallback _eventCallback;
  293. TextureResType _barTexType;
  294. TextureResType _progressBarTexType;
  295. TextureResType _ballNTexType;
  296. TextureResType _ballPTexType;
  297. TextureResType _ballDTexType;
  298. bool _barRendererAdaptDirty;
  299. bool _progressBarRendererDirty;
  300. std::string _textureFile;
  301. std::string _progressBarTextureFile;
  302. std::string _slidBallNormalTextureFile;
  303. std::string _slidBallPressedTextureFile;
  304. std::string _slidBallDisabledTextureFile;
  305. };
  306. }
  307. // end of ui group
  308. /// @}
  309. NS_CC_END
  310. #endif /* defined(__CocoGUI__Slider__) */