CCControlButton.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * http://www.cocos2d-x.org
  4. *
  5. * Copyright 2011 Yannick Loriot. All rights reserved.
  6. * http://yannickloriot.com
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. * Converted to c++ / cocos2d-x by Angus C
  27. */
  28. #ifndef __CCCONTROL_BUTTON_H__
  29. #define __CCCONTROL_BUTTON_H__
  30. #include "CCControl.h"
  31. #include "CCInvocation.h"
  32. #include "base/CCMap.h"
  33. #include "ui/UIScale9Sprite.h"
  34. NS_CC_EXT_BEGIN
  35. /* Define the button margin for Left/Right edge */
  36. #define ControlButtonMarginLR 8 // px
  37. /* Define the button margin for Top/Bottom edge */
  38. #define ControlButtonMarginTB 2 // px
  39. /**
  40. * @addtogroup GUI
  41. * @{
  42. * @addtogroup control_extension
  43. * @{
  44. */
  45. /** @class ControlButton Button control for Cocos2D. */
  46. class CC_EX_DLL ControlButton : public Control
  47. {
  48. public:
  49. static ControlButton* create();
  50. static ControlButton* create(cocos2d::ui::Scale9Sprite* sprite);
  51. static ControlButton* create(Node* label, cocos2d::ui::Scale9Sprite* backgroundSprite);
  52. static ControlButton* create(const std::string& title, const std::string& fontName, float fontSize);
  53. static ControlButton* create(Node* label, cocos2d::ui::Scale9Sprite* backgroundSprite, bool adjustBackGroundSize);
  54. virtual void needsLayout(void) override;
  55. virtual void setEnabled(bool enabled) override;
  56. virtual void setSelected(bool enabled) override;
  57. virtual void setHighlighted(bool enabled) override;
  58. bool isPushed() const { return _isPushed; }
  59. /**
  60. * Returns the title used for a state.
  61. *
  62. * @param state The state that uses the title. Possible values are described in
  63. * "CCControlState".
  64. *
  65. * @return The title for the specified state.
  66. */
  67. virtual std::string getTitleForState(State state);
  68. /**
  69. * Sets the title string to use for the specified state.
  70. * If a property is not specified for a state, the default is to use
  71. * the ButtonStateNormal value.
  72. *
  73. * @param title The title string to use for the specified state.
  74. * @param state The state that uses the specified title. The values are described
  75. * in "CCControlState".
  76. */
  77. virtual void setTitleForState(const std::string& title, State state);
  78. /**
  79. * Returns the title color used for a state.
  80. *
  81. * @param state The state that uses the specified color. The values are described
  82. * in "CCControlState".
  83. *
  84. * @return The color of the title for the specified state.
  85. */
  86. virtual Color3B getTitleColorForState(State state) const;
  87. /**
  88. * Sets the color of the title to use for the specified state.
  89. *
  90. * @param color The color of the title to use for the specified state.
  91. * @param state The state that uses the specified color. The values are described
  92. * in "CCControlState".
  93. */
  94. virtual void setTitleColorForState(const Color3B& color, State state);
  95. /**
  96. * Returns the title label used for a state.
  97. *
  98. * @param state The state that uses the title label. Possible values are described
  99. * in "CCControlState".
  100. */
  101. virtual Node* getTitleLabelForState(State state);
  102. /**
  103. * Sets the title label to use for the specified state.
  104. * If a property is not specified for a state, the default is to use
  105. * the ButtonStateNormal value.
  106. *
  107. * @param label The title label to use for the specified state.
  108. * @param state The state that uses the specified title. The values are described
  109. * in "CCControlState".
  110. */
  111. virtual void setTitleLabelForState(Node* label, State state);
  112. virtual void setTitleTTFForState(const std::string& fntFile, State state);
  113. virtual const std::string& getTitleTTFForState(State state);
  114. virtual void setTitleTTFSizeForState(float size, State state);
  115. virtual float getTitleTTFSizeForState(State state);
  116. /**
  117. * Sets the font of the label, changes the label to a BMFont if necessary.
  118. * @param fntFile The name of the font to change to
  119. * @param state The state that uses the specified fntFile. The values are described
  120. * in "CCControlState".
  121. */
  122. virtual void setTitleBMFontForState(const std::string& fntFile, State state);
  123. virtual const std::string& getTitleBMFontForState(State state);
  124. /**
  125. * Returns the background sprite used for a state.
  126. *
  127. * @param state The state that uses the background sprite. Possible values are
  128. * described in "CCControlState".
  129. */
  130. virtual cocos2d::ui::Scale9Sprite* getBackgroundSpriteForState(State state);
  131. /**
  132. * Sets the background sprite to use for the specified button state.
  133. *
  134. * @param sprite The background sprite to use for the specified state.
  135. * @param state The state that uses the specified image. The values are described
  136. * in "CCControlState".
  137. */
  138. virtual void setBackgroundSpriteForState(cocos2d::ui::Scale9Sprite* sprite, State state);
  139. /**
  140. * Sets the background spriteFrame to use for the specified button state.
  141. *
  142. * @param spriteFrame The background spriteFrame to use for the specified state.
  143. * @param state The state that uses the specified image. The values are described
  144. * in "CCControlState".
  145. */
  146. virtual void setBackgroundSpriteFrameForState(SpriteFrame * spriteFrame, State state);
  147. //set the margins at once (so we only have to do one call of needsLayout)
  148. virtual void setMargins(int marginH, int marginV);
  149. /** Adjust the background image. YES by default. If the property is set to NO, the
  150. background will use the preferred size of the background image. */
  151. bool doesAdjustBackgroundImage();
  152. void setAdjustBackgroundImage(bool adjustBackgroundImage);
  153. // Overrides
  154. virtual bool onTouchBegan(Touch *touch, Event *event) override;
  155. virtual void onTouchMoved(Touch *touch, Event *event) override;
  156. virtual void onTouchEnded(Touch *touch, Event *event) override;
  157. virtual void onTouchCancelled(Touch *touch, Event *event) override;
  158. virtual void setOpacity(GLubyte var) override;
  159. virtual void updateDisplayedOpacity(GLubyte parentOpacity) override;
  160. virtual void setColor(const Color3B&) override;
  161. virtual void updateDisplayedColor(const Color3B& parentColor) override;
  162. const std::string& getCurrentTitle() const { return _currentTitle; };
  163. std::string getCurrentTitle() { return _currentTitle; };
  164. CC_CONSTRUCTOR_ACCESS:
  165. /**
  166. * @js ctor
  167. */
  168. ControlButton();
  169. /**
  170. * @js NA
  171. * @lua NA
  172. */
  173. virtual ~ControlButton();
  174. virtual bool init() override;
  175. virtual bool initWithLabelAndBackgroundSprite(Node* label, cocos2d::ui::Scale9Sprite* backgroundSprite, bool adjustBackGroundSize);
  176. virtual bool initWithBackgroundSprite(cocos2d::ui::Scale9Sprite* sprite);
  177. virtual bool initWithTitleAndFontNameAndFontSize(const std::string& title, const std::string& fontName, float fontSize);
  178. protected:
  179. bool _isPushed;
  180. bool _parentInited;
  181. bool _doesAdjustBackgroundImage;
  182. /** The current title that is displayed on the button. */
  183. std::string _currentTitle;
  184. /** The current color used to display the title. */
  185. CC_SYNTHESIZE_READONLY_PASS_BY_REF(Color3B, _currentTitleColor, CurrentTitleColor);
  186. /** The current title label. */
  187. CC_SYNTHESIZE_RETAIN(Node*, _titleLabel, TitleLabel);
  188. /** The current background sprite. */
  189. CC_SYNTHESIZE_RETAIN(cocos2d::ui::Scale9Sprite*, _backgroundSprite, BackgroundSprite);
  190. /** The preferred size of the button, if label is larger it will be expanded. */
  191. CC_PROPERTY_PASS_BY_REF(Size, _preferredSize, PreferredSize);
  192. /** Adjust the button zooming on touchdown. Default value is YES. */
  193. CC_PROPERTY(bool, _zoomOnTouchDown, ZoomOnTouchDown);
  194. /** Scale ratio button on touchdown. Default value 1.1f */
  195. CC_SYNTHESIZE(float, _scaleRatio, ScaleRatio);
  196. CC_PROPERTY_PASS_BY_REF(Vec2, _labelAnchorPoint, LabelAnchorPoint);
  197. std::unordered_map<int, std::string> _titleDispatchTable;
  198. std::unordered_map<int, Color3B> _titleColorDispatchTable;
  199. Map<int, Node*> _titleLabelDispatchTable;
  200. Map<int, cocos2d::ui::Scale9Sprite*> _backgroundSpriteDispatchTable;
  201. /* Define the button margin for Top/Bottom edge */
  202. CC_SYNTHESIZE_READONLY(int, _marginV, VerticalMargin);
  203. /* Define the button margin for Left/Right edge */
  204. CC_SYNTHESIZE_READONLY(int, _marginH, HorizontalOrigin);
  205. private:
  206. CC_DISALLOW_COPY_AND_ASSIGN(ControlButton);
  207. };
  208. // end of GUI group
  209. /// @}
  210. /// @}
  211. NS_CC_EXT_END
  212. #endif