axmol/cocos/gui/UIButton.h

241 lines
7.0 KiB
C
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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.
****************************************************************************/
2013-09-13 22:20:20 +08:00
#ifndef __UIBUTTON_H__
#define __UIBUTTON_H__
#include "gui/UIWidget.h"
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
NS_CC_BEGIN
namespace ui{
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
/**
* @js NA
* @lua NA
*/
class Button : public Widget
2013-09-13 22:20:20 +08:00
{
public:
/**
* Default constructor
*/
2013-12-23 15:02:52 +08:00
Button();
2013-09-13 22:20:20 +08:00
/**
* Default destructor
*/
2013-12-23 15:02:52 +08:00
virtual ~Button();
2013-09-13 22:20:20 +08:00
/**
* Allocates and initializes.
*/
2013-12-23 15:02:52 +08:00
static Button* create();
2013-09-13 22:20:20 +08:00
/**
* Load textures for button.
*
* @param normal normal state texture.
*
* @param selected selected state texture.
*
* @param disabled dark state texture.
*
* @param texType @see UI_TEX_TYPE_LOCAL
*/
void loadTextures(const char* normal,const char* selected,const char* disabled,TextureResType texType = UI_TEX_TYPE_LOCAL);
/**
* Load normal state texture for button.
*
* @param normal normal state texture.
*
* @param texType @see UI_TEX_TYPE_LOCAL
*/
void loadTextureNormal(const char* normal, TextureResType texType = UI_TEX_TYPE_LOCAL);
/**
* Load selected state texture for button.
*
* @param selected selected state texture.
*
* @param texType @see UI_TEX_TYPE_LOCAL
*/
void loadTexturePressed(const char* selected, TextureResType texType = UI_TEX_TYPE_LOCAL);
/**
* Load dark state texture for button.
*
* @param disabled dark state texture.
*
* @param texType @see UI_TEX_TYPE_LOCAL
*/
void loadTextureDisabled(const char* disabled, TextureResType texType = UI_TEX_TYPE_LOCAL);
/**
* Sets capinsets for button, if button is using scale9 renderer.
*
* @param capInsets capinsets for button
*/
2013-12-23 15:02:52 +08:00
void setCapInsets(const Rect &capInsets);
2013-09-13 22:20:20 +08:00
/**
* Sets capinsets for button, if button is using scale9 renderer.
*
* @param capInsets capinsets for button
*/
2013-12-23 15:02:52 +08:00
void setCapInsetsNormalRenderer(const Rect &capInsets);
2013-09-13 22:20:20 +08:00
2014-03-01 22:34:25 +08:00
const Rect& getCapInsetsNormalRenderer();
2013-09-13 22:20:20 +08:00
/**
* Sets capinsets for button, if button is using scale9 renderer.
*
* @param capInsets capinsets for button
*/
2013-12-23 15:02:52 +08:00
void setCapInsetsPressedRenderer(const Rect &capInsets);
2013-09-13 22:20:20 +08:00
2014-03-01 22:34:25 +08:00
const Rect& getCapInsetsPressedRenderer();
2013-09-13 22:20:20 +08:00
/**
* Sets capinsets for button, if button is using scale9 renderer.
*
* @param capInsets capinsets for button
*/
2013-12-23 15:02:52 +08:00
void setCapInsetsDisabledRenderer(const Rect &capInsets);
2013-09-13 22:20:20 +08:00
2014-03-01 22:34:25 +08:00
const Rect& getCapInsetsDisabledRenderer();
2013-09-13 22:20:20 +08:00
//override "setAnchorPoint" of widget.
2014-01-03 15:14:48 +08:00
virtual void setAnchorPoint(const Point &pt) override;
2013-09-13 22:20:20 +08:00
/**
* Sets if button is using scale9 renderer.
*
* @param true that using scale9 renderer, false otherwise.
*/
virtual void setScale9Enabled(bool able);
2014-03-01 22:34:25 +08:00
bool isScale9Enabled();
2013-09-13 22:20:20 +08:00
//override "setFlipX" of widget.
2014-01-03 15:14:48 +08:00
virtual void setFlipX(bool flipX) override;
2013-09-13 22:20:20 +08:00
//override "setFlipY" of widget.
2014-01-03 15:14:48 +08:00
virtual void setFlipY(bool flipY) override;
2013-09-13 22:20:20 +08:00
//override "isFlipX" of widget.
2014-01-03 15:14:48 +08:00
virtual bool isFlipX() override;
2013-09-13 22:20:20 +08:00
//override "isFlipY" of widget.
2014-01-03 15:14:48 +08:00
virtual bool isFlipY() override;
2013-09-13 22:20:20 +08:00
/**
* Changes if button can be clicked zoom effect.
*
* @param true that can be clicked zoom effect, false otherwise.
*/
void setPressedActionEnabled(bool enabled);
//override "ignoreContentAdaptWithSize" method of widget.
2014-01-03 15:14:48 +08:00
virtual void ignoreContentAdaptWithSize(bool ignore) override;
2013-09-13 22:20:20 +08:00
//override "getContentSize" method of widget.
2014-01-03 15:14:48 +08:00
virtual const Size& getContentSize() const override;
2013-09-13 22:20:20 +08:00
//override "getVirtualRenderer" method of widget.
2014-01-03 15:14:48 +08:00
virtual Node* getVirtualRenderer() override;
2013-09-13 22:20:20 +08:00
/**
* Sets color to widget
*
* It default change the color of widget's children.
*
* @param color
*/
2014-01-03 15:14:48 +08:00
virtual void setColor(const Color3B &color) override;
2013-09-13 22:20:20 +08:00
2013-11-06 16:04:06 +08:00
/**
* Returns the "class name" of widget.
*/
2014-01-03 15:14:48 +08:00
virtual std::string getDescription() const override;
2013-11-06 16:04:06 +08:00
void setTitleText(const std::string& text);
const std::string& getTitleText() const;
2013-12-23 15:02:52 +08:00
void setTitleColor(const Color3B& color);
const Color3B& getTitleColor() const;
2013-09-13 22:20:20 +08:00
void setTitleFontSize(float size);
float getTitleFontSize() const;
void setTitleFontName(const char* fontName);
const char* getTitleFontName() const;
2013-11-06 16:04:06 +08:00
2013-09-13 22:20:20 +08:00
protected:
2014-01-03 15:14:48 +08:00
virtual bool init() override;
virtual void initRenderer() override;
virtual void onPressStateChangedToNormal() override;
virtual void onPressStateChangedToPressed() override;
virtual void onPressStateChangedToDisabled() override;
virtual void onSizeChanged() override;
2013-09-13 22:20:20 +08:00
void normalTextureScaleChangedWithSize();
void pressedTextureScaleChangedWithSize();
void disabledTextureScaleChangedWithSize();
2014-01-03 15:14:48 +08:00
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
2013-09-13 22:20:20 +08:00
protected:
2013-12-23 15:02:52 +08:00
Node* _buttonNormalRenderer;
Node* _buttonClickedRenderer;
Node* _buttonDisableRenderer;
LabelTTF* _titleRenderer;
2013-09-16 20:54:13 +08:00
std::string _normalFileName;
std::string _clickedFileName;
std::string _disabledFileName;
bool _prevIgnoreSize;
bool _scale9Enabled;
2013-12-23 15:02:52 +08:00
Rect _capInsetsNormal;
Rect _capInsetsPressed;
Rect _capInsetsDisabled;
2013-09-16 20:54:13 +08:00
TextureResType _normalTexType;
TextureResType _pressedTexType;
TextureResType _disabledTexType;
2013-12-23 15:02:52 +08:00
Size _normalTextureSize;
Size _pressedTextureSize;
Size _disabledTextureSize;
2013-09-16 20:54:13 +08:00
bool _pressedActionEnabled;
2013-12-23 15:02:52 +08:00
Color3B _titleColor;
2014-01-03 12:14:21 +08:00
float _normalTextureScaleXInSize;
float _normalTextureScaleYInSize;
float _pressedTextureScaleXInSize;
float _pressedTextureScaleYInSize;
bool _normalTextureLoaded;
bool _pressedTextureLoaded;
bool _disabledTextureLoaded;
2013-09-13 22:20:20 +08:00
};
}
2013-09-13 22:20:20 +08:00
2013-12-23 15:02:52 +08:00
NS_CC_END
#endif /* defined(__CocoGUI__Button__) */