UIAbstractCheckButton.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 __UIABSTRACTCHECKBUTTON_H__
  22. #define __UIABSTRACTCHECKBUTTON_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. class Sprite;
  31. struct CC_DLL ResourceData;
  32. namespace ui {
  33. /**
  34. * AbstractCheckButton is a specific type of two-states button that can be either checked or unchecked.
  35. */
  36. class CC_GUI_DLL AbstractCheckButton : public Widget
  37. {
  38. public:
  39. /**
  40. * Load all textures for initializing a check button.
  41. *
  42. * @param background The background image name.
  43. * @param backgroundSelected The background selected image name.
  44. * @param cross The cross image name.
  45. * @param backgroundDisabled The background disabled state texture.
  46. * @param frontCrossDisabled The front cross disabled state image name.
  47. * @param texType @see `Widget::TextureResType`
  48. */
  49. void loadTextures(const std::string& background,
  50. const std::string& backgroundSelected,
  51. const std::string& cross,
  52. const std::string& backgroundDisabled,
  53. const std::string& frontCrossDisabled,
  54. TextureResType texType = TextureResType::LOCAL);
  55. /**
  56. * Load background texture for check button.
  57. *
  58. * @param backGround The background image name.
  59. * @param type @see `Widget::TextureResType`
  60. */
  61. void loadTextureBackGround(const std::string& backGround,TextureResType type = TextureResType::LOCAL);
  62. /**
  63. * Load background selected state texture for check button.
  64. *
  65. * @param backGroundSelected The background selected state image name.
  66. * @param texType @see `Widget::TextureResType`
  67. */
  68. void loadTextureBackGroundSelected(const std::string& backGroundSelected,TextureResType texType = TextureResType::LOCAL);
  69. /**
  70. * Load cross texture for check button.
  71. *
  72. * @param crossTextureName The cross texture name.
  73. * @param texType @see `Widget::TextureResType`
  74. */
  75. void loadTextureFrontCross(const std::string& crossTextureName,TextureResType texType = TextureResType::LOCAL);
  76. /**
  77. * Load background disabled state texture for checkbox.
  78. *
  79. * @param backGroundDisabled The background disabled state texture name.
  80. *
  81. * @param texType @see `Widget::TextureResType`
  82. */
  83. void loadTextureBackGroundDisabled(const std::string& backGroundDisabled,TextureResType texType = TextureResType::LOCAL);
  84. /**
  85. * Load frontcross disabled texture for checkbox.
  86. *
  87. * @param frontCrossDisabled The front cross disabled state texture name.
  88. * @param texType @see `Widget::TextureResType`
  89. */
  90. void loadTextureFrontCrossDisabled(const std::string& frontCrossDisabled,TextureResType texType = TextureResType::LOCAL);
  91. /**
  92. * Query whether CheckBox is selected or not.
  93. *@return true means "selected", false otherwise.
  94. */
  95. bool isSelected()const;
  96. /**
  97. * Change CheckBox state.
  98. * Set to true will cause the CheckBox's state to "selected", false otherwise.
  99. *@param selected Set to true will change CheckBox to selected state, false otherwise.
  100. */
  101. void setSelected(bool selected);
  102. //override functions
  103. virtual Size getVirtualRendererSize() const override;
  104. virtual Node* getVirtualRenderer() override;
  105. /** When user pressed the CheckBox, the button will zoom to a scale.
  106. * The final scale of the CheckBox equals (CheckBox original scale + _zoomScale)
  107. * @since v3.3
  108. */
  109. void setZoomScale(float scale);
  110. /**
  111. * @brief Return a zoom scale
  112. * @return A zoom scale of Checkbox.
  113. * @since v3.3
  114. */
  115. float getZoomScale()const;
  116. /**
  117. * @brief Return the sprite instance of background
  118. * @return the sprite instance of background.
  119. */
  120. Sprite* getRendererBackground() const { return _backGroundBoxRenderer; }
  121. /**
  122. * @brief Return the sprite instance of background when selected
  123. * @return the sprite instance of background when selected
  124. */
  125. Sprite* getRendererBackgroundSelected() const { return _backGroundSelectedBoxRenderer; }
  126. /**
  127. * @brief Return the sprite instance of front cross
  128. * @return the sprite instance of front cross
  129. */
  130. Sprite* getRendererFrontCross() const { return _frontCrossRenderer; }
  131. /**
  132. * @brief Return the sprite instance of background when disabled
  133. * @return the sprite instance of background when disabled
  134. */
  135. Sprite* getRendererBackgroundDisabled() const { return _backGroundBoxDisabledRenderer; }
  136. /**
  137. * @brief Return the sprite instance of front cross when disabled
  138. * @return the sprite instance of front cross when disabled
  139. */
  140. Sprite* getRendererFrontCrossDisabled() const { return _frontCrossDisabledRenderer; }
  141. ResourceData getBackNormalFile();
  142. ResourceData getBackPressedFile();
  143. ResourceData getBackDisabledFile();
  144. ResourceData getCrossNormalFile();
  145. ResourceData getCrossDisabledFile();
  146. CC_CONSTRUCTOR_ACCESS:
  147. virtual bool init() override;
  148. virtual bool init(const std::string& backGround,
  149. const std::string& backGroundSelected,
  150. const std::string& cross,
  151. const std::string& backGroundDisabled,
  152. const std::string& frontCrossDisabled,
  153. TextureResType texType = TextureResType::LOCAL);
  154. protected:
  155. /**
  156. * Default constructor.
  157. */
  158. AbstractCheckButton();
  159. /**
  160. * Default destructor.
  161. *
  162. * @lua NA
  163. */
  164. virtual ~AbstractCheckButton();
  165. virtual void initRenderer() override;
  166. virtual void onPressStateChangedToNormal() override;
  167. virtual void onPressStateChangedToPressed() override;
  168. virtual void onPressStateChangedToDisabled() override;
  169. void setupBackgroundTexture();
  170. void loadTextureBackGround(SpriteFrame* spriteFrame);
  171. void setupBackgroundSelectedTexture();
  172. void loadTextureBackGroundSelected(SpriteFrame* spriteFrame);
  173. void setupFrontCrossTexture();
  174. void loadTextureFrontCross(SpriteFrame* spriteframe);
  175. void setupBackgroundDisable();
  176. void loadTextureBackGroundDisabled(SpriteFrame* spriteframe);
  177. void setupFrontCrossDisableTexture();
  178. void loadTextureFrontCrossDisabled(SpriteFrame* spriteframe);
  179. virtual void dispatchSelectChangedEvent(bool selected) = 0;
  180. virtual void onSizeChanged() override;
  181. void backGroundTextureScaleChangedWithSize();
  182. void backGroundSelectedTextureScaleChangedWithSize();
  183. void frontCrossTextureScaleChangedWithSize();
  184. void backGroundDisabledTextureScaleChangedWithSize();
  185. void frontCrossDisabledTextureScaleChangedWithSize();
  186. virtual void copySpecialProperties(Widget* model) override;
  187. virtual void adaptRenderers() override;
  188. protected:
  189. Sprite* _backGroundBoxRenderer;
  190. Sprite* _backGroundSelectedBoxRenderer;
  191. Sprite* _frontCrossRenderer;
  192. Sprite* _backGroundBoxDisabledRenderer;
  193. Sprite* _frontCrossDisabledRenderer;
  194. bool _isSelected;
  195. bool _isBackgroundSelectedTextureLoaded;
  196. bool _isBackgroundDisabledTextureLoaded;
  197. bool _isFrontCrossDisabledTextureLoaded;
  198. TextureResType _backGroundTexType;
  199. TextureResType _backGroundSelectedTexType;
  200. TextureResType _frontCrossTexType;
  201. TextureResType _backGroundDisabledTexType;
  202. TextureResType _frontCrossDisabledTexType;
  203. float _zoomScale;
  204. float _backgroundTextureScaleX;
  205. float _backgroundTextureScaleY;
  206. bool _backGroundBoxRendererAdaptDirty;
  207. bool _backGroundSelectedBoxRendererAdaptDirty;
  208. bool _frontCrossRendererAdaptDirty;
  209. bool _backGroundBoxDisabledRendererAdaptDirty;
  210. bool _frontCrossDisabledRendererAdaptDirty;
  211. std::string _backGroundFileName;
  212. std::string _backGroundSelectedFileName;
  213. std::string _frontCrossFileName;
  214. std::string _backGroundDisabledFileName;
  215. std::string _frontCrossDisabledFileName;
  216. };
  217. }
  218. NS_CC_END
  219. // end of ui group
  220. /// @}
  221. #endif /* defined(__UIABSTRACTCHECKBUTTON_H__) */