axmol/cocos/ui/UIButton.h

397 lines
12 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
https://adxe.org
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __UIBUTTON_H__
#define __UIBUTTON_H__
#include "ui/UIWidget.h"
#include "ui/GUIExport.h"
/**
* @addtogroup ui
* @{
*/
NS_CC_BEGIN
class Label;
class SpriteFrame;
struct CC_DLL ResourceData;
2021-12-25 10:04:45 +08:00
namespace ui
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
class Scale9Sprite;
2019-11-23 20:27:39 +08:00
/**
* Represents a push-button widget.
* Push-buttons can be pressed, or clicked, by the user to perform an action.
*/
class CC_GUI_DLL Button : public Widget
{
DECLARE_CLASS_GUI_INFO
public:
/**
* Default constructor.
*/
Button();
/**
* Default destructor.
*
* @lua NA
*/
virtual ~Button();
/**
* Create a empty Button.
*@return A empty Button instance.
*/
static Button* create();
/**
* Create a button with custom textures.
* @param normalImage normal state texture name.
* @param selectedImage selected state texture name.
* @param disableImage disabled state texture name.
* @param texType @see `TextureResType`
* @return a Button instance.
*/
static Button* create(const std::string& normalImage,
const std::string& selectedImage = "",
2021-12-25 10:04:45 +08:00
const std::string& disableImage = "",
TextureResType texType = TextureResType::LOCAL);
2019-11-23 20:27:39 +08:00
/**
* Load textures for button.
*
* @param normal normal state texture name.
* @param selected selected state texture name.
* @param disabled disabled state texture name.
* @param texType @see `TextureResType`
*/
void loadTextures(const std::string& normal,
const std::string& selected,
const std::string& disabled = "",
2021-12-25 10:04:45 +08:00
TextureResType texType = TextureResType::LOCAL);
2019-11-23 20:27:39 +08:00
/**
* Load normal state texture for button.
*
* @param normal normal state texture.
* @param texType @see `TextureResType`
*/
void loadTextureNormal(const std::string& normal, TextureResType texType = TextureResType::LOCAL);
/**
* Load selected state texture for button.
*
* @param selected selected state texture.
* @param texType @see `TextureResType`
*/
void loadTexturePressed(const std::string& selected, TextureResType texType = TextureResType::LOCAL);
/**
* Load disabled state texture for button.
*
* @param disabled dark state texture.
* @param texType @see `TextureResType`
*/
void loadTextureDisabled(const std::string& disabled, TextureResType texType = TextureResType::LOCAL);
/**
* Sets capInsets for button.
* The capInset affects all button scale9 renderer only if `setScale9Enabled(true)` is called
*
* @param capInsets capInset in Rect.
*/
2021-12-25 10:04:45 +08:00
void setCapInsets(const Rect& capInsets);
2019-11-23 20:27:39 +08:00
/**
* Sets capInsets for button, only the normal state scale9 renderer will be affected.
*
* @param capInsets capInsets in Rect.
*/
2021-12-25 10:04:45 +08:00
void setCapInsetsNormalRenderer(const Rect& capInsets);
2019-11-23 20:27:39 +08:00
/**
* Return the capInsets of normal state scale9sprite.
*@return The normal scale9 renderer capInsets.
*/
2021-12-25 10:04:45 +08:00
const Rect& getCapInsetsNormalRenderer() const;
2019-11-23 20:27:39 +08:00
/**
* Sets capInsets for button, only the pressed state scale9 renderer will be affected.
*
* @param capInsets capInsets in Rect
*/
2021-12-25 10:04:45 +08:00
void setCapInsetsPressedRenderer(const Rect& capInsets);
2019-11-23 20:27:39 +08:00
/**
* Return the capInsets of pressed state scale9sprite.
*@return The pressed scale9 renderer capInsets.
*/
2021-12-25 10:04:45 +08:00
const Rect& getCapInsetsPressedRenderer() const;
2019-11-23 20:27:39 +08:00
/**
* Sets capInsets for button, only the disabled state scale9 renderer will be affected.
*
* @param capInsets capInsets in Rect.
*/
2021-12-25 10:04:45 +08:00
void setCapInsetsDisabledRenderer(const Rect& capInsets);
2019-11-23 20:27:39 +08:00
/**
* Return the capInsets of disabled state scale9sprite.
*@return The disabled scale9 renderer capInsets.
*/
2021-12-25 10:04:45 +08:00
const Rect& getCapInsetsDisabledRenderer() const;
2019-11-23 20:27:39 +08:00
/**
* Enable scale9 renderer.
*
* @param enable Set to true will use scale9 renderer, false otherwise.
*/
virtual void setScale9Enabled(bool enable);
/**
* Query whether button is using scale9 renderer or not.
*@return whether button use scale9 renderer or not.
*/
2021-12-25 10:04:45 +08:00
bool isScale9Enabled() const;
2019-11-23 20:27:39 +08:00
/**
* Enable zooming action when button is pressed.
*
* @param enabled Set to true will enable zoom effect, false otherwise.
*/
void setPressedActionEnabled(bool enabled);
2021-12-25 10:04:45 +08:00
// override methods
2019-11-23 20:27:39 +08:00
virtual void ignoreContentAdaptWithSize(bool ignore) override;
2021-10-23 23:27:14 +08:00
virtual Vec2 getVirtualRendererSize() const override;
2019-11-23 20:27:39 +08:00
virtual Node* getVirtualRenderer() override;
virtual std::string getDescription() const override;
/**
* Return the inner title renderer of Button.
* @return The button title.
* @since v3.3
*/
2021-12-25 10:04:45 +08:00
Label* getTitleRenderer() const;
2019-11-23 20:27:39 +08:00
/**
* Change the content of button's title.
*@param text The title in std::string.
*/
void setTitleText(const std::string& text);
/**
* Query the button title content.
*@return Get the button's title content.
*/
std::string getTitleText() const;
/**
* Change the color of button's title.
*@param color The title color in Color3B.
*/
void setTitleColor(const Color3B& color);
/**
* Query the button title color.
*@return Color3B of button title.
*/
Color3B getTitleColor() const;
/**
* Change the font size of button's title
*@param size Title font size in float.
*/
void setTitleFontSize(float size);
/**
* Query the font size of button title
*@return font size in float.
*/
float getTitleFontSize() const;
/**
* Change the font name of button's title
*@param fontName a font name string.
*/
void setTitleFontName(const std::string& fontName);
/**
* Query the font name of button's title
*@return font name in std::string
*/
std::string getTitleFontName() const;
/**
* Sets the title's text horizontal alignment.
*
* @param hAlignment see TextHAlignment
*/
void setTitleAlignment(TextHAlignment hAlignment);
/**
* Sets the title's text vertical alignment.
*
* @param hAlignment see TextHAlignment.
* @param vAlignment see TextVAlignment.
*/
void setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment);
/** replaces the current Label node with a new one */
void setTitleLabel(Label* label);
/** returns the current Label being used */
Label* getTitleLabel() const;
/** @brief When user pressed the button, the button will zoom to a scale.
* The final scale of the button equals (button original scale + _zoomScale)
* @since v3.3
*/
void setZoomScale(float scale);
/**
* @brief Return a zoom scale
* @return the zoom scale in float
* @since v3.3
*/
2021-12-25 10:04:45 +08:00
float getZoomScale() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Return the nine-patch sprite of normal state
* @return the nine-patch sprite of normal state
* @since v3.9
*/
Scale9Sprite* getRendererNormal() const { return _buttonNormalRenderer; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Return the nine-patch sprite of clicked state
* @return the nine-patch sprite of clicked state
* @since v3.9
*/
Scale9Sprite* getRendererClicked() const { return _buttonClickedRenderer; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Return the nine-patch sprite of disabled state
* @return the nine-patch sprite of disabled state
* @since v3.9
*/
Scale9Sprite* getRendererDisabled() const { return _buttonDisabledRenderer; }
void resetNormalRender();
void resetPressedRender();
void resetDisabledRender();
ResourceData getNormalFile();
ResourceData getPressedFile();
ResourceData getDisabledFile();
2021-12-25 10:04:45 +08:00
CC_CONSTRUCTOR_ACCESS : virtual bool init() override;
2019-11-23 20:27:39 +08:00
virtual bool init(const std::string& normalImage,
const std::string& selectedImage = "",
2021-12-25 10:04:45 +08:00
const std::string& disableImage = "",
TextureResType texType = TextureResType::LOCAL);
2019-11-23 20:27:39 +08:00
2021-10-23 23:27:14 +08:00
virtual Vec2 getNormalTextureSize() const;
2019-11-23 20:27:39 +08:00
protected:
virtual void initRenderer() override;
virtual void onPressStateChangedToNormal() override;
virtual void onPressStateChangedToPressed() override;
virtual void onPressStateChangedToDisabled() override;
virtual void onSizeChanged() override;
void loadTextureNormal(SpriteFrame* normalSpriteFrame);
void setupNormalTexture(bool textureLoaded);
void loadTexturePressed(SpriteFrame* pressedSpriteFrame);
void setupPressedTexture(bool textureLoaded);
void loadTextureDisabled(SpriteFrame* disabledSpriteFrame);
void setupDisabledTexture(bool textureLoaded);
void normalTextureScaleChangedWithSize();
void pressedTextureScaleChangedWithSize();
void disabledTextureScaleChangedWithSize();
virtual void adaptRenderers() override;
virtual void updateTitleLocation();
void updateContentSize();
virtual void createTitleRenderer();
bool createTitleRendererIfNull();
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
2021-10-23 23:27:14 +08:00
virtual Vec2 getNormalSize() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
Scale9Sprite* _buttonNormalRenderer;
Scale9Sprite* _buttonClickedRenderer;
Scale9Sprite* _buttonDisabledRenderer;
Label* _titleRenderer;
float _zoomScale;
bool _prevIgnoreSize;
bool _scale9Enabled;
bool _pressedActionEnabled;
Rect _capInsetsNormal;
Rect _capInsetsPressed;
Rect _capInsetsDisabled;
2021-10-23 23:27:14 +08:00
Vec2 _normalTextureSize;
Vec2 _pressedTextureSize;
Vec2 _disabledTextureSize;
2019-11-23 20:27:39 +08:00
bool _normalTextureLoaded;
bool _pressedTextureLoaded;
bool _disabledTextureLoaded;
bool _normalTextureAdaptDirty;
bool _pressedTextureAdaptDirty;
bool _disabledTextureAdaptDirty;
std::string _normalFileName;
std::string _clickedFileName;
std::string _disabledFileName;
TextureResType _normalTexType;
TextureResType _pressedTexType;
TextureResType _disabledTexType;
2020-10-07 00:07:53 +08:00
protected:
2019-11-23 20:27:39 +08:00
std::string _fontName;
};
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_CC_END
// end of ui group
/// @}
#endif /* defined(__CocoGUI__Button__) */