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 "UIButton.h"
|
|
|
|
#include "../../../GUI/CCControlExtension/CCScale9Sprite.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
#define NORMALRENDERERZ (0)
|
|
|
|
#define PRESSEDRENDERERZ (0)
|
|
|
|
#define DISABLEDRENDERERZ (0)
|
|
|
|
#define TITLERENDERERZ (1)
|
|
|
|
|
|
|
|
UIButton::UIButton():
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer(NULL),
|
|
|
|
_buttonClickedRenderer(NULL),
|
|
|
|
_buttonDisableRenderer(NULL),
|
|
|
|
_titleRenderer(NULL),
|
|
|
|
_normalFileName(""),
|
|
|
|
_clickedFileName(""),
|
|
|
|
_disabledFileName(""),
|
|
|
|
_prevIgnoreSize(true),
|
|
|
|
_scale9Enabled(false),
|
|
|
|
_capInsetsNormal(Rect::ZERO),
|
|
|
|
_capInsetsPressed(Rect::ZERO),
|
|
|
|
_capInsetsDisabled(Rect::ZERO),
|
|
|
|
_normalTexType(UI_TEX_TYPE_LOCAL),
|
|
|
|
_pressedTexType(UI_TEX_TYPE_LOCAL),
|
|
|
|
_disabledTexType(UI_TEX_TYPE_LOCAL),
|
|
|
|
_normalTextureSize(_size),
|
|
|
|
_pressedTextureSize(_size),
|
|
|
|
_disabledTextureSize(_size),
|
|
|
|
_pressedActionEnabled(false),
|
|
|
|
_titleColor(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);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIButton::init()
|
|
|
|
{
|
|
|
|
if (UIWidget::init())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::initRenderer()
|
|
|
|
{
|
|
|
|
UIWidget::initRenderer();
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer = Sprite::create();
|
|
|
|
_buttonClickedRenderer = Sprite::create();
|
|
|
|
_buttonDisableRenderer = Sprite::create();
|
|
|
|
_titleRenderer = LabelTTF::create();
|
|
|
|
_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-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer = NULL;
|
|
|
|
_buttonClickedRenderer = NULL;
|
|
|
|
_buttonDisableRenderer = NULL;
|
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer = Scale9Sprite::create();
|
|
|
|
_buttonClickedRenderer = Scale9Sprite::create();
|
|
|
|
_buttonDisableRenderer = Scale9Sprite::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer = CCSprite::create();
|
|
|
|
_buttonClickedRenderer = CCSprite::create();
|
|
|
|
_buttonDisableRenderer = CCSprite::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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonNormalRenderer)->initWithFile(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonNormalRenderer)->initWithSpriteFrameName(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonNormalRenderer)->setColor(getColor());
|
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonNormalRenderer)->setOpacity(getOpacity());
|
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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonNormalRenderer)->initWithFile(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonNormalRenderer)->initWithSpriteFrameName(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonNormalRenderer)->setColor(getColor());
|
|
|
|
dynamic_cast<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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonClickedRenderer)->initWithFile(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonClickedRenderer)->initWithSpriteFrameName(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonClickedRenderer)->setColor(getColor());
|
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonClickedRenderer)->setOpacity(getOpacity());
|
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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonClickedRenderer)->initWithFile(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonClickedRenderer)->initWithSpriteFrameName(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonClickedRenderer)->setColor(getColor());
|
|
|
|
dynamic_cast<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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonDisableRenderer)->initWithFile(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonDisableRenderer)->initWithSpriteFrameName(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonDisableRenderer)->setColor(getColor());
|
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonDisableRenderer)->setOpacity(getOpacity());
|
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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonDisableRenderer)->initWithFile(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonDisableRenderer)->initWithSpriteFrameName(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonDisableRenderer)->setColor(getColor());
|
|
|
|
dynamic_cast<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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::setCapInsets(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
setCapInsetsNormalRenderer(capInsets);
|
|
|
|
setCapInsetsPressedRenderer(capInsets);
|
|
|
|
setCapInsetsDisabledRenderer(capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::setCapInsetsNormalRenderer(const Rect &capInsets)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_capInsetsNormal = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonNormalRenderer)->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::setCapInsetsPressedRenderer(const Rect &capInsets)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_capInsetsPressed = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonClickedRenderer)->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::setCapInsetsDisabledRenderer(const Rect &capInsets)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_capInsetsDisabled = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<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-09-13 22:20:20 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, 1.0f);
|
|
|
|
Action *zoomAction1 = ScaleTo::create(0.05f, 1.0f);
|
|
|
|
Action *zoomAction2 = 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-09-13 22:20:20 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, 1.1f);
|
|
|
|
Action *zoomAction1 = ScaleTo::create(0.05f, 1.1f);
|
|
|
|
Action *zoomAction2 = 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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonNormalRenderer)->setFlippedX(flipX);
|
|
|
|
dynamic_cast<Sprite*>(_buttonClickedRenderer)->setFlippedX(flipX);
|
|
|
|
dynamic_cast<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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Sprite*>(_buttonNormalRenderer)->setFlippedY(flipY);
|
|
|
|
dynamic_cast<Sprite*>(_buttonClickedRenderer)->setFlippedY(flipY);
|
|
|
|
dynamic_cast<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-09-16 20:54:13 +08:00
|
|
|
return dynamic_cast<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-09-16 20:54:13 +08:00
|
|
|
return dynamic_cast<Sprite*>(_buttonNormalRenderer)->isFlippedY();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::setAnchorPoint(const Point &pt)
|
|
|
|
{
|
|
|
|
UIWidget::setAnchorPoint(pt);
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->setAnchorPoint(pt);
|
|
|
|
_buttonClickedRenderer->setAnchorPoint(pt);
|
|
|
|
_buttonDisableRenderer->setAnchorPoint(pt);
|
|
|
|
_titleRenderer->setPosition(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Size& UIButton::getContentSize() const
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _normalTextureSize;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* UIButton::getVirtualRenderer()
|
|
|
|
{
|
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:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonNormalRenderer)->setPreferredSize(_size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonClickedRenderer)->setPreferredSize(_size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
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-09-16 20:54:13 +08:00
|
|
|
dynamic_cast<Scale9Sprite*>(_buttonDisableRenderer)->setPreferredSize(_size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
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 char* text)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_titleRenderer->setString(text);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* UIButton::getTitleText() const
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _titleRenderer->getString();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::setTitleColor(const Color3B& color)
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_titleColor = color;
|
|
|
|
_titleRenderer->setColor(color);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& UIButton::getTitleColor() const
|
|
|
|
{
|
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
|
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _titleRenderer->getFontName();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIButton::setColor(const Color3B &color)
|
|
|
|
{
|
|
|
|
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-09-13 22:20:20 +08:00
|
|
|
NS_CC_EXT_END
|