UIButton.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 __UIBUTTON_H__
  22. #define __UIBUTTON_H__
  23. #include "ui/UIWidget.h"
  24. #include "ui/GUIExport.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. class Label;
  31. class SpriteFrame;
  32. struct CC_DLL ResourceData;
  33. namespace ui{
  34. class Scale9Sprite;
  35. /**
  36. * Represents a push-button widget.
  37. * Push-buttons can be pressed, or clicked, by the user to perform an action.
  38. */
  39. class CC_GUI_DLL Button : public Widget
  40. {
  41. DECLARE_CLASS_GUI_INFO
  42. public:
  43. /**
  44. * Default constructor.
  45. */
  46. Button();
  47. /**
  48. * Default destructor.
  49. *
  50. * @lua NA
  51. */
  52. virtual ~Button();
  53. /**
  54. * Create a empty Button.
  55. *@return A empty Button instance.
  56. */
  57. static Button* create();
  58. /**
  59. * Create a button with custom textures.
  60. * @param normalImage normal state texture name.
  61. * @param selectedImage selected state texture name.
  62. * @param disableImage disabled state texture name.
  63. * @param texType @see `TextureResType`
  64. * @return a Button instance.
  65. */
  66. static Button* create(const std::string& normalImage,
  67. const std::string& selectedImage = "",
  68. const std::string& disableImage = "",
  69. TextureResType texType = TextureResType::LOCAL);
  70. /**
  71. * Load textures for button.
  72. *
  73. * @param normal normal state texture name.
  74. * @param selected selected state texture name.
  75. * @param disabled disabled state texture name.
  76. * @param texType @see `TextureResType`
  77. */
  78. void loadTextures(const std::string& normal,
  79. const std::string& selected,
  80. const std::string& disabled = "",
  81. TextureResType texType = TextureResType::LOCAL);
  82. /**
  83. * Load normal state texture for button.
  84. *
  85. * @param normal normal state texture.
  86. * @param texType @see `TextureResType`
  87. */
  88. void loadTextureNormal(const std::string& normal, TextureResType texType = TextureResType::LOCAL);
  89. /**
  90. * Load selected state texture for button.
  91. *
  92. * @param selected selected state texture.
  93. * @param texType @see `TextureResType`
  94. */
  95. void loadTexturePressed(const std::string& selected, TextureResType texType = TextureResType::LOCAL);
  96. /**
  97. * Load disabled state texture for button.
  98. *
  99. * @param disabled dark state texture.
  100. * @param texType @see `TextureResType`
  101. */
  102. void loadTextureDisabled(const std::string& disabled, TextureResType texType = TextureResType::LOCAL);
  103. /**
  104. * Sets capInsets for button.
  105. * The capInset affects all button scale9 renderer only if `setScale9Enabled(true)` is called
  106. *
  107. * @param capInsets capInset in Rect.
  108. */
  109. void setCapInsets(const Rect &capInsets);
  110. /**
  111. * Sets capInsets for button, only the normal state scale9 renderer will be affected.
  112. *
  113. * @param capInsets capInsets in Rect.
  114. */
  115. void setCapInsetsNormalRenderer(const Rect &capInsets);
  116. /**
  117. * Return the capInsets of normal state scale9sprite.
  118. *@return The normal scale9 renderer capInsets.
  119. */
  120. const Rect& getCapInsetsNormalRenderer()const;
  121. /**
  122. * Sets capInsets for button, only the pressed state scale9 renderer will be affected.
  123. *
  124. * @param capInsets capInsets in Rect
  125. */
  126. void setCapInsetsPressedRenderer(const Rect &capInsets);
  127. /**
  128. * Return the capInsets of pressed state scale9sprite.
  129. *@return The pressed scale9 renderer capInsets.
  130. */
  131. const Rect& getCapInsetsPressedRenderer()const;
  132. /**
  133. * Sets capInsets for button, only the disabled state scale9 renderer will be affected.
  134. *
  135. * @param capInsets capInsets in Rect.
  136. */
  137. void setCapInsetsDisabledRenderer(const Rect &capInsets);
  138. /**
  139. * Return the capInsets of disabled state scale9sprite.
  140. *@return The disabled scale9 renderer capInsets.
  141. */
  142. const Rect& getCapInsetsDisabledRenderer()const;
  143. /**
  144. * Enable scale9 renderer.
  145. *
  146. * @param enable Set to true will use scale9 renderer, false otherwise.
  147. */
  148. virtual void setScale9Enabled(bool enable);
  149. /**
  150. * Query whether button is using scale9 renderer or not.
  151. *@return whether button use scale9 renderer or not.
  152. */
  153. bool isScale9Enabled()const;
  154. /**
  155. * Enable zooming action when button is pressed.
  156. *
  157. * @param enabled Set to true will enable zoom effect, false otherwise.
  158. */
  159. void setPressedActionEnabled(bool enabled);
  160. //override methods
  161. virtual void ignoreContentAdaptWithSize(bool ignore) override;
  162. virtual Size getVirtualRendererSize() const override;
  163. virtual Node* getVirtualRenderer() override;
  164. virtual std::string getDescription() const override;
  165. /**
  166. * Return the inner title renderer of Button.
  167. * @return The button title.
  168. * @since v3.3
  169. */
  170. Label* getTitleRenderer()const;
  171. /**
  172. * Change the content of button's title.
  173. *@param text The title in std::string.
  174. */
  175. void setTitleText(const std::string& text);
  176. /**
  177. * Query the button title content.
  178. *@return Get the button's title content.
  179. */
  180. std::string getTitleText() const;
  181. /**
  182. * Change the color of button's title.
  183. *@param color The title color in Color3B.
  184. */
  185. void setTitleColor(const Color3B& color);
  186. /**
  187. * Query the button title color.
  188. *@return Color3B of button title.
  189. */
  190. Color3B getTitleColor() const;
  191. /**
  192. * Change the font size of button's title
  193. *@param size Title font size in float.
  194. */
  195. void setTitleFontSize(float size);
  196. /**
  197. * Query the font size of button title
  198. *@return font size in float.
  199. */
  200. float getTitleFontSize() const;
  201. /**
  202. * Change the font name of button's title
  203. *@param fontName a font name string.
  204. */
  205. void setTitleFontName(const std::string& fontName);
  206. /**
  207. * Query the font name of button's title
  208. *@return font name in std::string
  209. */
  210. std::string getTitleFontName() const;
  211. /**
  212. * Sets the title's text horizontal alignment.
  213. *
  214. * @param hAlignment see TextHAlignment
  215. */
  216. void setTitleAlignment(TextHAlignment hAlignment);
  217. /**
  218. * Sets the title's text vertical alignment.
  219. *
  220. * @param hAlignment see TextHAlignment.
  221. * @param vAlignment see TextVAlignment.
  222. */
  223. void setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment);
  224. /** replaces the current Label node with a new one */
  225. void setTitleLabel(Label* label);
  226. /** returns the current Label being used */
  227. Label* getTitleLabel() const;
  228. /** @brief When user pressed the button, the button will zoom to a scale.
  229. * The final scale of the button equals (button original scale + _zoomScale)
  230. * @since v3.3
  231. */
  232. void setZoomScale(float scale);
  233. /**
  234. * @brief Return a zoom scale
  235. * @return the zoom scale in float
  236. * @since v3.3
  237. */
  238. float getZoomScale()const;
  239. /**
  240. * @brief Return the nine-patch sprite of normal state
  241. * @return the nine-patch sprite of normal state
  242. * @since v3.9
  243. */
  244. Scale9Sprite* getRendererNormal() const { return _buttonNormalRenderer; }
  245. /**
  246. * @brief Return the nine-patch sprite of clicked state
  247. * @return the nine-patch sprite of clicked state
  248. * @since v3.9
  249. */
  250. Scale9Sprite* getRendererClicked() const { return _buttonClickedRenderer; }
  251. /**
  252. * @brief Return the nine-patch sprite of disabled state
  253. * @return the nine-patch sprite of disabled state
  254. * @since v3.9
  255. */
  256. Scale9Sprite* getRendererDisabled() const { return _buttonDisabledRenderer; }
  257. void resetNormalRender();
  258. void resetPressedRender();
  259. void resetDisabledRender();
  260. ResourceData getNormalFile();
  261. ResourceData getPressedFile();
  262. ResourceData getDisabledFile();
  263. void clearActionNode();
  264. void addActionNode(Node* node);
  265. CC_CONSTRUCTOR_ACCESS:
  266. virtual bool init() override;
  267. virtual bool init(const std::string& normalImage,
  268. const std::string& selectedImage = "",
  269. const std::string& disableImage = "",
  270. TextureResType texType = TextureResType::LOCAL);
  271. virtual Size getNormalTextureSize() const;
  272. protected:
  273. virtual void initRenderer() override;
  274. virtual void onPressStateChangedToNormal() override;
  275. virtual void onPressStateChangedToPressed() override;
  276. virtual void onPressStateChangedToDisabled() override;
  277. virtual void onSizeChanged() override;
  278. void loadTextureNormal(SpriteFrame* normalSpriteFrame);
  279. void setupNormalTexture(bool textureLoaded);
  280. void loadTexturePressed(SpriteFrame* pressedSpriteFrame);
  281. void setupPressedTexture(bool textureLoaded);
  282. void loadTextureDisabled(SpriteFrame* disabledSpriteFrame);
  283. void setupDisabledTexture(bool textureLoaded);
  284. void normalTextureScaleChangedWithSize();
  285. void pressedTextureScaleChangedWithSize();
  286. void disabledTextureScaleChangedWithSize();
  287. virtual void adaptRenderers() override;
  288. void updateTitleLocation();
  289. void updateContentSize();
  290. void createTitleRenderer();
  291. virtual Widget* createCloneInstance() override;
  292. virtual void copySpecialProperties(Widget* model) override;
  293. virtual Size getNormalSize() const;
  294. protected:
  295. Scale9Sprite* _buttonNormalRenderer;
  296. Scale9Sprite* _buttonClickedRenderer;
  297. Scale9Sprite* _buttonDisabledRenderer;
  298. Label* _titleRenderer;
  299. float _zoomScale;
  300. bool _prevIgnoreSize;
  301. bool _scale9Enabled;
  302. bool _pressedActionEnabled;
  303. Vector<Node*> _actionNodes;
  304. Rect _capInsetsNormal;
  305. Rect _capInsetsPressed;
  306. Rect _capInsetsDisabled;
  307. Size _normalTextureSize;
  308. Size _pressedTextureSize;
  309. Size _disabledTextureSize;
  310. bool _normalTextureLoaded;
  311. bool _pressedTextureLoaded;
  312. bool _disabledTextureLoaded;
  313. bool _normalTextureAdaptDirty;
  314. bool _pressedTextureAdaptDirty;
  315. bool _disabledTextureAdaptDirty;
  316. std::string _normalFileName;
  317. std::string _clickedFileName;
  318. std::string _disabledFileName;
  319. TextureResType _normalTexType;
  320. TextureResType _pressedTexType;
  321. TextureResType _disabledTexType;
  322. private:
  323. enum class FontType
  324. {
  325. SYSTEM,
  326. TTF,
  327. BMFONT
  328. };
  329. int _fontSize;
  330. FontType _type;
  331. std::string _fontName;
  332. };
  333. }
  334. NS_CC_END
  335. // end of ui group
  336. /// @}
  337. #endif /* defined(__CocoGUI__Button__) */