axmol/cocos/gui/UIButton.cpp

646 lines
20 KiB
C++
Raw Normal View History

2013-09-13 22:20:20 +08:00
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
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.
****************************************************************************/
#include "gui/UIButton.h"
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
2013-09-13 22:20:20 +08:00
namespace gui {
2013-09-13 22:20:20 +08:00
#define NORMALRENDERERZ (0)
#define PRESSEDRENDERERZ (0)
#define DISABLEDRENDERERZ (0)
#define TITLERENDERERZ (1)
UIButton::UIButton():
2013-11-14 11:37:46 +08:00
_buttonNormalRenderer(nullptr),
_buttonClickedRenderer(nullptr),
_buttonDisableRenderer(nullptr),
_titleRenderer(nullptr),
2013-09-16 20:54:13 +08:00
_normalFileName(""),
_clickedFileName(""),
_disabledFileName(""),
_prevIgnoreSize(true),
_scale9Enabled(false),
2013-11-06 16:04:06 +08:00
_capInsetsNormal(cocos2d::Rect::ZERO),
_capInsetsPressed(cocos2d::Rect::ZERO),
_capInsetsDisabled(cocos2d::Rect::ZERO),
2013-09-16 20:54:13 +08:00
_normalTexType(UI_TEX_TYPE_LOCAL),
_pressedTexType(UI_TEX_TYPE_LOCAL),
_disabledTexType(UI_TEX_TYPE_LOCAL),
_normalTextureSize(_size),
_pressedTextureSize(_size),
_disabledTextureSize(_size),
_pressedActionEnabled(false),
2013-11-06 16:04:06 +08:00
_titleColor(cocos2d::Color3B::WHITE)
2013-09-13 22:20:20 +08:00
{
}
UIButton::~UIButton()
{
}
UIButton* UIButton::create()
{
UIButton* widget = new UIButton();
if (widget && widget->init())
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-13 22:20:20 +08:00
}
bool UIButton::init()
{
if (UIWidget::init())
{
return true;
}
return false;
}
void UIButton::initRenderer()
{
UIWidget::initRenderer();
2013-11-06 16:04:06 +08:00
_buttonNormalRenderer = cocos2d::Sprite::create();
_buttonClickedRenderer = cocos2d::Sprite::create();
_buttonDisableRenderer = cocos2d::Sprite::create();
_titleRenderer = cocos2d::LabelTTF::create();
2013-09-16 20:54:13 +08:00
_renderer->addChild(_buttonNormalRenderer,NORMALRENDERERZ);
_renderer->addChild(_buttonClickedRenderer,PRESSEDRENDERERZ);
_renderer->addChild(_buttonDisableRenderer,DISABLEDRENDERERZ);
_renderer->addChild(_titleRenderer,TITLERENDERERZ);
2013-09-13 22:20:20 +08:00
}
void UIButton::setScale9Enabled(bool able)
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled == able)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-09-16 15:32:52 +08:00
_brightStyle = BRIGHT_NONE;
2013-09-16 20:54:13 +08:00
_scale9Enabled = able;
2013-09-13 22:20:20 +08:00
2013-09-16 20:54:13 +08:00
_renderer->removeChild(_buttonNormalRenderer, true);
_renderer->removeChild(_buttonClickedRenderer, true);
_renderer->removeChild(_buttonDisableRenderer, true);
2013-09-13 22:20:20 +08:00
2013-11-14 11:37:46 +08:00
_buttonNormalRenderer = nullptr;
_buttonClickedRenderer = nullptr;
_buttonDisableRenderer = nullptr;
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
_buttonNormalRenderer = cocos2d::extension::Scale9Sprite::create();
_buttonClickedRenderer = cocos2d::extension::Scale9Sprite::create();
_buttonDisableRenderer = cocos2d::extension::Scale9Sprite::create();
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
_buttonNormalRenderer = cocos2d::Sprite::create();
_buttonClickedRenderer = cocos2d::Sprite::create();
_buttonDisableRenderer = cocos2d::Sprite::create();
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
loadTextureNormal(_normalFileName.c_str(), _normalTexType);
loadTexturePressed(_clickedFileName.c_str(), _pressedTexType);
loadTextureDisabled(_disabledFileName.c_str(), _disabledTexType);
_renderer->addChild(_buttonNormalRenderer,NORMALRENDERERZ);
_renderer->addChild(_buttonClickedRenderer,PRESSEDRENDERERZ);
_renderer->addChild(_buttonDisableRenderer,DISABLEDRENDERERZ);
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
bool ignoreBefore = _ignoreSize;
2013-09-13 22:20:20 +08:00
ignoreContentAdaptWithSize(false);
2013-09-16 20:54:13 +08:00
_prevIgnoreSize = ignoreBefore;
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
ignoreContentAdaptWithSize(_prevIgnoreSize);
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
setCapInsetsNormalRenderer(_capInsetsNormal);
setCapInsetsPressedRenderer(_capInsetsPressed);
setCapInsetsDisabledRenderer(_capInsetsDisabled);
2013-09-16 15:32:52 +08:00
setBright(_bright);
2013-09-13 22:20:20 +08:00
}
void UIButton::ignoreContentAdaptWithSize(bool ignore)
{
2013-09-16 20:54:13 +08:00
if (!_scale9Enabled || (_scale9Enabled && !ignore))
2013-09-13 22:20:20 +08:00
{
UIWidget::ignoreContentAdaptWithSize(ignore);
2013-09-16 20:54:13 +08:00
_prevIgnoreSize = ignore;
2013-09-13 22:20:20 +08:00
}
}
void UIButton::loadTextures(const char* normal,const char* selected,const char* disabled,TextureResType texType)
{
loadTextureNormal(normal,texType);
loadTexturePressed(selected,texType);
loadTextureDisabled(disabled,texType);
}
void UIButton::loadTextureNormal(const char* normal,TextureResType texType)
{
if (!normal || strcmp(normal, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_normalFileName = normal;
_normalTexType = texType;
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
switch (_normalTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonNormalRenderer)->initWithFile(normal);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonNormalRenderer)->initWithSpriteFrameName(normal);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonNormalRenderer)->setColor(getColor());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonNormalRenderer)->setOpacity(getOpacity());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonNormalRenderer)->setCapInsets(_capInsetsNormal);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
switch (_normalTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-11-14 07:55:36 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->setTexture(normal);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-11-14 07:55:36 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->setSpriteFrame(normal);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->setColor(getColor());
dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_normalTextureSize = _buttonNormalRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
updateAnchorPoint();
normalTextureScaleChangedWithSize();
}
void UIButton::loadTexturePressed(const char* selected,TextureResType texType)
{
if (!selected || strcmp(selected, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_clickedFileName = selected;
_pressedTexType = texType;
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
switch (_pressedTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonClickedRenderer)->initWithFile(selected);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonClickedRenderer)->initWithSpriteFrameName(selected);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonClickedRenderer)->setColor(getColor());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonClickedRenderer)->setOpacity(getOpacity());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonClickedRenderer)->setCapInsets(_capInsetsPressed);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
switch (_pressedTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-11-14 07:55:36 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonClickedRenderer)->setTexture(selected);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-11-14 07:55:36 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonClickedRenderer)->setSpriteFrame(selected);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonClickedRenderer)->setColor(getColor());
dynamic_cast<cocos2d::Sprite*>(_buttonClickedRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
updateAnchorPoint();
pressedTextureScaleChangedWithSize();
}
void UIButton::loadTextureDisabled(const char* disabled,TextureResType texType)
{
if (!disabled || strcmp(disabled, "") == 0)
{
return;
}
2013-09-16 20:54:13 +08:00
_disabledFileName = disabled;
_disabledTexType = texType;
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
switch (_disabledTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonDisableRenderer)->initWithFile(disabled);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonDisableRenderer)->initWithSpriteFrameName(disabled);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonDisableRenderer)->setColor(getColor());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonDisableRenderer)->setOpacity(getOpacity());
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonDisableRenderer)->setCapInsets(_capInsetsDisabled);
2013-09-13 22:20:20 +08:00
}
else
{
2013-09-16 20:54:13 +08:00
switch (_disabledTexType)
2013-09-13 22:20:20 +08:00
{
case UI_TEX_TYPE_LOCAL:
2013-11-14 07:55:36 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonDisableRenderer)->setTexture(disabled);
2013-09-13 22:20:20 +08:00
break;
case UI_TEX_TYPE_PLIST:
2013-11-14 07:55:36 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonDisableRenderer)->setSpriteFrame(disabled);
2013-09-13 22:20:20 +08:00
break;
default:
break;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonDisableRenderer)->setColor(getColor());
dynamic_cast<cocos2d::Sprite*>(_buttonDisableRenderer)->setOpacity(getOpacity());
2013-09-13 22:20:20 +08:00
}
2013-09-16 20:54:13 +08:00
_disabledTextureSize = _buttonDisableRenderer->getContentSize();
2013-09-13 22:20:20 +08:00
updateAnchorPoint();
disabledTextureScaleChangedWithSize();
}
2013-11-06 16:04:06 +08:00
void UIButton::setCapInsets(const cocos2d::Rect &capInsets)
2013-09-13 22:20:20 +08:00
{
setCapInsetsNormalRenderer(capInsets);
setCapInsetsPressedRenderer(capInsets);
setCapInsetsDisabledRenderer(capInsets);
}
2013-11-06 16:04:06 +08:00
void UIButton::setCapInsetsNormalRenderer(const cocos2d::Rect &capInsets)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_capInsetsNormal = capInsets;
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonNormalRenderer)->setCapInsets(capInsets);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIButton::setCapInsetsPressedRenderer(const cocos2d::Rect &capInsets)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_capInsetsPressed = capInsets;
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonClickedRenderer)->setCapInsets(capInsets);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIButton::setCapInsetsDisabledRenderer(const cocos2d::Rect &capInsets)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_capInsetsDisabled = capInsets;
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonDisableRenderer)->setCapInsets(capInsets);
2013-09-13 22:20:20 +08:00
}
void UIButton::onPressStateChangedToNormal()
{
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->setVisible(true);
_buttonClickedRenderer->setVisible(false);
_buttonDisableRenderer->setVisible(false);
if (_pressedActionEnabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->stopAllActions();
_buttonClickedRenderer->stopAllActions();
_buttonDisableRenderer->stopAllActions();
2013-11-06 16:04:06 +08:00
cocos2d::Action *zoomAction = cocos2d::ScaleTo::create(0.05f, 1.0f);
cocos2d::Action *zoomAction1 = cocos2d::ScaleTo::create(0.05f, 1.0f);
cocos2d::Action *zoomAction2 = cocos2d::ScaleTo::create(0.05f, 1.0f);
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->runAction(zoomAction);
_buttonClickedRenderer->runAction(zoomAction1);
_buttonDisableRenderer->runAction(zoomAction2);
2013-09-13 22:20:20 +08:00
}
}
void UIButton::onPressStateChangedToPressed()
{
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->setVisible(false);
_buttonClickedRenderer->setVisible(true);
_buttonDisableRenderer->setVisible(false);
if (_pressedActionEnabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->stopAllActions();
_buttonClickedRenderer->stopAllActions();
_buttonDisableRenderer->stopAllActions();
2013-11-06 16:04:06 +08:00
cocos2d::Action *zoomAction = cocos2d::ScaleTo::create(0.05f, 1.1f);
cocos2d::Action *zoomAction1 = cocos2d::ScaleTo::create(0.05f, 1.1f);
cocos2d::Action *zoomAction2 = cocos2d::ScaleTo::create(0.05f, 1.1f);
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->runAction(zoomAction);
_buttonClickedRenderer->runAction(zoomAction1);
_buttonDisableRenderer->runAction(zoomAction2);
2013-09-13 22:20:20 +08:00
}
}
void UIButton::onPressStateChangedToDisabled()
{
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->setVisible(false);
_buttonClickedRenderer->setVisible(false);
_buttonDisableRenderer->setVisible(true);
2013-09-13 22:20:20 +08:00
}
void UIButton::setFlipX(bool flipX)
{
2013-09-16 20:54:13 +08:00
_titleRenderer->setFlippedX(flipX);
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->setFlippedX(flipX);
dynamic_cast<cocos2d::Sprite*>(_buttonClickedRenderer)->setFlippedX(flipX);
dynamic_cast<cocos2d::Sprite*>(_buttonDisableRenderer)->setFlippedX(flipX);
2013-09-13 22:20:20 +08:00
}
void UIButton::setFlipY(bool flipY)
{
2013-09-16 20:54:13 +08:00
_titleRenderer->setFlippedY(flipY);
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return;
}
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->setFlippedY(flipY);
dynamic_cast<cocos2d::Sprite*>(_buttonClickedRenderer)->setFlippedY(flipY);
dynamic_cast<cocos2d::Sprite*>(_buttonDisableRenderer)->setFlippedY(flipY);
2013-09-13 22:20:20 +08:00
}
bool UIButton::isFlipX()
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return false;
}
2013-11-06 16:04:06 +08:00
return dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->isFlippedX();
2013-09-13 22:20:20 +08:00
}
bool UIButton::isFlipY()
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
return false;
}
2013-11-06 16:04:06 +08:00
return dynamic_cast<cocos2d::Sprite*>(_buttonNormalRenderer)->isFlippedY();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIButton::setAnchorPoint(const cocos2d::Point &pt)
2013-09-13 22:20:20 +08:00
{
UIWidget::setAnchorPoint(pt);
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->setAnchorPoint(pt);
_buttonClickedRenderer->setAnchorPoint(pt);
_buttonDisableRenderer->setAnchorPoint(pt);
2013-11-06 16:04:06 +08:00
_titleRenderer->setPosition(cocos2d::Point(_size.width*(0.5f-_anchorPoint.x), _size.height*(0.5f-_anchorPoint.y)));
2013-09-13 22:20:20 +08:00
}
void UIButton::onSizeChanged()
{
normalTextureScaleChangedWithSize();
pressedTextureScaleChangedWithSize();
disabledTextureScaleChangedWithSize();
}
2013-11-06 16:04:06 +08:00
const cocos2d::Size& UIButton::getContentSize() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _normalTextureSize;
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
cocos2d::Node* UIButton::getVirtualRenderer()
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
if (_bright)
2013-09-13 22:20:20 +08:00
{
2013-09-16 15:32:52 +08:00
switch (_brightStyle)
2013-09-13 22:20:20 +08:00
{
case BRIGHT_NORMAL:
2013-09-16 20:54:13 +08:00
return _buttonNormalRenderer;
2013-09-13 22:20:20 +08:00
case BRIGHT_HIGHLIGHT:
2013-09-16 20:54:13 +08:00
return _buttonClickedRenderer;
2013-09-13 22:20:20 +08:00
default:
2013-11-14 11:37:46 +08:00
return nullptr;
2013-09-13 22:20:20 +08:00
}
}
else
{
2013-09-16 20:54:13 +08:00
return _buttonDisableRenderer;
2013-09-13 22:20:20 +08:00
}
}
void UIButton::normalTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->setScale(1.0f);
_size = _normalTextureSize;
2013-09-13 22:20:20 +08:00
}
}
else
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonNormalRenderer)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _normalTextureSize;
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 15:32:52 +08:00
float scaleX = _size.width / textureSize.width;
float scaleY = _size.height / textureSize.height;
2013-09-16 20:54:13 +08:00
_buttonNormalRenderer->setScaleX(scaleX);
_buttonNormalRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
}
void UIButton::pressedTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_buttonClickedRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
}
}
else
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonClickedRenderer)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _pressedTextureSize;
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_buttonClickedRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 20:54:13 +08:00
float scaleX = _size.width / _pressedTextureSize.width;
float scaleY = _size.height / _pressedTextureSize.height;
_buttonClickedRenderer->setScaleX(scaleX);
_buttonClickedRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
}
void UIButton::disabledTextureScaleChangedWithSize()
{
2013-09-16 15:32:52 +08:00
if (_ignoreSize)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
if (!_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_buttonDisableRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
}
}
else
{
2013-09-16 20:54:13 +08:00
if (_scale9Enabled)
2013-09-13 22:20:20 +08:00
{
2013-11-06 16:04:06 +08:00
dynamic_cast<cocos2d::extension::Scale9Sprite*>(_buttonDisableRenderer)->setPreferredSize(_size);
2013-09-13 22:20:20 +08:00
}
else
{
2013-11-06 16:04:06 +08:00
cocos2d::Size textureSize = _disabledTextureSize;
2013-09-13 22:20:20 +08:00
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
2013-09-16 20:54:13 +08:00
_buttonDisableRenderer->setScale(1.0f);
2013-09-13 22:20:20 +08:00
return;
}
2013-09-16 20:54:13 +08:00
float scaleX = _size.width / _disabledTextureSize.width;
float scaleY = _size.height / _disabledTextureSize.height;
_buttonDisableRenderer->setScaleX(scaleX);
_buttonDisableRenderer->setScaleY(scaleY);
2013-09-13 22:20:20 +08:00
}
}
}
void UIButton::setPressedActionEnabled(bool enabled)
{
2013-09-16 20:54:13 +08:00
_pressedActionEnabled = enabled;
2013-09-13 22:20:20 +08:00
}
void UIButton::setTitleText(const std::string& text)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_titleRenderer->setString(text);
2013-09-13 22:20:20 +08:00
}
const std::string& UIButton::getTitleText() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _titleRenderer->getString();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIButton::setTitleColor(const cocos2d::Color3B& color)
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
_titleColor = color;
_titleRenderer->setColor(color);
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
const cocos2d::Color3B& UIButton::getTitleColor() const
2013-09-13 22:20:20 +08:00
{
2013-09-16 20:54:13 +08:00
return _titleRenderer->getColor();
2013-09-13 22:20:20 +08:00
}
void UIButton::setTitleFontSize(float size)
{
2013-09-16 20:54:13 +08:00
_titleRenderer->setFontSize(size);
2013-09-13 22:20:20 +08:00
}
float UIButton::getTitleFontSize() const
{
2013-09-16 20:54:13 +08:00
return _titleRenderer->getFontSize();
2013-09-13 22:20:20 +08:00
}
void UIButton::setTitleFontName(const char* fontName)
{
2013-09-16 20:54:13 +08:00
_titleRenderer->setFontName(fontName);
2013-09-13 22:20:20 +08:00
}
const char* UIButton::getTitleFontName() const
{
return _titleRenderer->getFontName().c_str();
2013-09-13 22:20:20 +08:00
}
2013-11-06 16:04:06 +08:00
void UIButton::setColor(const cocos2d::Color3B &color)
2013-09-13 22:20:20 +08:00
{
UIWidget::setColor(color);
2013-09-16 20:54:13 +08:00
setTitleColor(_titleColor);
2013-09-13 22:20:20 +08:00
}
2013-09-17 17:59:20 +08:00
const char* UIButton::getDescription() const
{
return "Button";
}
2013-11-06 16:04:06 +08:00
UIWidget* UIButton::createCloneInstance()
{
return UIButton::create();
}
void UIButton::copySpecialProperties(UIWidget *widget)
{
UIButton* button = dynamic_cast<UIButton*>(widget);
if (button)
{
_prevIgnoreSize = button->_prevIgnoreSize;
setScale9Enabled(button->_scale9Enabled);
loadTextureNormal(button->_normalFileName.c_str(), button->_normalTexType);
loadTexturePressed(button->_clickedFileName.c_str(), button->_pressedTexType);
loadTextureDisabled(button->_disabledFileName.c_str(), button->_disabledTexType);
setCapInsetsNormalRenderer(button->_capInsetsNormal);
setCapInsetsPressedRenderer(button->_capInsetsPressed);
setCapInsetsDisabledRenderer(button->_capInsetsDisabled);
setTitleText(button->getTitleText());
setTitleFontName(button->getTitleFontName());
setTitleFontSize(button->getTitleFontSize());
setTitleColor(button->getTitleColor());
setPressedActionEnabled(button->_pressedActionEnabled);
}
}
}