2014-03-11 17:13:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ui/UIButton.h"
|
|
|
|
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
static const int NORMAL_RENDERER_Z = (-2);
|
|
|
|
static const int PRESSED_RENDERER_Z = (-2);
|
|
|
|
static const int DISABLED_RENDERER_Z = (-2);
|
|
|
|
static const int TITLE_RENDERER_Z = (-1);
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(Button)
|
|
|
|
|
|
|
|
Button::Button():
|
|
|
|
_buttonNormalRenderer(nullptr),
|
|
|
|
_buttonClickedRenderer(nullptr),
|
|
|
|
_buttonDisableRenderer(nullptr),
|
|
|
|
_titleRenderer(nullptr),
|
|
|
|
_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),
|
|
|
|
_normalTextureScaleXInSize(1.0f),
|
|
|
|
_normalTextureScaleYInSize(1.0f),
|
|
|
|
_pressedTextureScaleXInSize(1.0f),
|
|
|
|
_pressedTextureScaleYInSize(1.0f),
|
|
|
|
_normalTextureLoaded(false),
|
|
|
|
_pressedTextureLoaded(false),
|
|
|
|
_disabledTextureLoaded(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::~Button()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Button* Button::create()
|
|
|
|
{
|
|
|
|
Button* widget = new Button();
|
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-04-02 11:48:42 +08:00
|
|
|
|
|
|
|
Button* Button::create(const std::string &normalImage,
|
|
|
|
const std::string& selectedImage ,
|
|
|
|
const std::string& disableImage,
|
|
|
|
TextureResType texType)
|
|
|
|
{
|
|
|
|
Button *btn = new Button;
|
|
|
|
if (btn && btn->init(normalImage,selectedImage,disableImage,texType)) {
|
|
|
|
btn->autorelease();
|
|
|
|
return btn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(btn);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::init(const std::string &normalImage,
|
|
|
|
const std::string& selectedImage ,
|
|
|
|
const std::string& disableImage,
|
|
|
|
TextureResType texType)
|
|
|
|
{
|
2014-04-04 18:26:53 +08:00
|
|
|
bool ret = true;
|
2014-04-02 11:48:42 +08:00
|
|
|
do {
|
|
|
|
if (!Widget::init()) {
|
2014-04-04 18:26:53 +08:00
|
|
|
ret = false;
|
2014-04-02 11:48:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTouchEnabled(true);
|
2014-04-02 16:08:45 +08:00
|
|
|
this->loadTextures(normalImage, selectedImage, disableImage,texType);
|
2014-04-02 11:48:42 +08:00
|
|
|
} while (0);
|
2014-04-04 18:26:53 +08:00
|
|
|
return ret;
|
2014-04-02 11:48:42 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
bool Button::init()
|
|
|
|
{
|
|
|
|
if (Widget::init())
|
|
|
|
{
|
|
|
|
setTouchEnabled(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::initRenderer()
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer = Sprite::create();
|
|
|
|
_buttonClickedRenderer = Sprite::create();
|
|
|
|
_buttonDisableRenderer = Sprite::create();
|
2014-03-14 21:06:40 +08:00
|
|
|
_titleRenderer = Label::create();
|
|
|
|
_titleRenderer->setAnchorPoint(Point::ANCHOR_MIDDLE);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
|
|
|
|
addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
|
|
|
|
addProtectedChild(_buttonDisableRenderer, DISABLED_RENDERER_Z, -1);
|
|
|
|
addProtectedChild(_titleRenderer, TITLE_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setScale9Enabled(bool able)
|
|
|
|
{
|
|
|
|
if (_scale9Enabled == able)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_brightStyle = BRIGHT_NONE;
|
|
|
|
_scale9Enabled = able;
|
2014-03-24 15:25:44 +08:00
|
|
|
removeProtectedChild(_buttonNormalRenderer);
|
|
|
|
removeProtectedChild(_buttonClickedRenderer);
|
|
|
|
removeProtectedChild(_buttonDisableRenderer);
|
2014-03-11 17:13:54 +08:00
|
|
|
_buttonNormalRenderer = nullptr;
|
|
|
|
_buttonClickedRenderer = nullptr;
|
|
|
|
_buttonDisableRenderer = nullptr;
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer = extension::Scale9Sprite::create();
|
|
|
|
_buttonClickedRenderer = extension::Scale9Sprite::create();
|
|
|
|
_buttonDisableRenderer = extension::Scale9Sprite::create();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer = Sprite::create();
|
|
|
|
_buttonClickedRenderer = Sprite::create();
|
|
|
|
_buttonDisableRenderer = Sprite::create();
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
loadTextureNormal(_normalFileName, _normalTexType);
|
|
|
|
loadTexturePressed(_clickedFileName, _pressedTexType);
|
|
|
|
loadTextureDisabled(_disabledFileName, _disabledTexType);
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
|
|
|
|
addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
|
|
|
|
addProtectedChild(_buttonDisableRenderer, DISABLED_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
bool ignoreBefore = _ignoreSize;
|
|
|
|
ignoreContentAdaptWithSize(false);
|
|
|
|
_prevIgnoreSize = ignoreBefore;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ignoreContentAdaptWithSize(_prevIgnoreSize);
|
|
|
|
}
|
|
|
|
setCapInsetsNormalRenderer(_capInsetsNormal);
|
|
|
|
setCapInsetsPressedRenderer(_capInsetsPressed);
|
|
|
|
setCapInsetsDisabledRenderer(_capInsetsDisabled);
|
|
|
|
setBright(_bright);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Button::isScale9Enabled()
|
|
|
|
{
|
|
|
|
return _scale9Enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::ignoreContentAdaptWithSize(bool ignore)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled || (_scale9Enabled && !ignore))
|
|
|
|
{
|
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
|
|
|
_prevIgnoreSize = ignore;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:08:45 +08:00
|
|
|
void Button::loadTextures(const std::string& normal,
|
|
|
|
const std::string& selected,
|
|
|
|
const std::string& disabled,
|
|
|
|
TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
loadTextureNormal(normal,texType);
|
|
|
|
loadTexturePressed(selected,texType);
|
|
|
|
loadTextureDisabled(disabled,texType);
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:08:45 +08:00
|
|
|
void Button::loadTextureNormal(const std::string& normal,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-02 16:08:45 +08:00
|
|
|
if (normal.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_normalFileName = normal;
|
|
|
|
_normalTexType = texType;
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
extension::Scale9Sprite* normalRendererScale9 = static_cast<extension::Scale9Sprite*>(_buttonNormalRenderer);
|
|
|
|
switch (_normalTexType)
|
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2014-04-02 18:35:08 +08:00
|
|
|
normalRendererScale9->initWithFile(normal);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2014-04-02 18:35:08 +08:00
|
|
|
normalRendererScale9->initWithSpriteFrameName(normal);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
normalRendererScale9->setCapInsets(_capInsetsNormal);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Sprite* normalRenderer = static_cast<Sprite*>(_buttonNormalRenderer);
|
|
|
|
switch (_normalTexType)
|
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
|
|
|
normalRenderer->setTexture(normal);
|
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
|
|
|
normalRenderer->setSpriteFrame(normal);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_normalTextureSize = _buttonNormalRenderer->getContentSize();
|
|
|
|
normalTextureScaleChangedWithSize();
|
|
|
|
updateAnchorPoint();
|
|
|
|
updateFlippedX();
|
|
|
|
updateFlippedY();
|
|
|
|
updateRGBAToRenderer(_buttonNormalRenderer);
|
|
|
|
_normalTextureLoaded = true;
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:08:45 +08:00
|
|
|
void Button::loadTexturePressed(const std::string& selected,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-02 16:08:45 +08:00
|
|
|
if (selected.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_clickedFileName = selected;
|
|
|
|
_pressedTexType = texType;
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
extension::Scale9Sprite* clickedRendererScale9 = static_cast<extension::Scale9Sprite*>(_buttonClickedRenderer);
|
|
|
|
switch (_pressedTexType)
|
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2014-04-02 18:35:08 +08:00
|
|
|
clickedRendererScale9->initWithFile(selected);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2014-04-02 18:35:08 +08:00
|
|
|
clickedRendererScale9->initWithSpriteFrameName(selected);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
clickedRendererScale9->setCapInsets(_capInsetsPressed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Sprite* clickedRenderer = static_cast<Sprite*>(_buttonClickedRenderer);
|
|
|
|
switch (_pressedTexType)
|
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
|
|
|
clickedRenderer->setTexture(selected);
|
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
|
|
|
clickedRenderer->setSpriteFrame(selected);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
|
|
|
|
pressedTextureScaleChangedWithSize();
|
|
|
|
updateAnchorPoint();
|
|
|
|
updateFlippedX();
|
|
|
|
updateFlippedY();
|
|
|
|
updateRGBAToRenderer(_buttonDisableRenderer);
|
|
|
|
_pressedTextureLoaded = true;
|
|
|
|
}
|
|
|
|
|
2014-04-02 16:08:45 +08:00
|
|
|
void Button::loadTextureDisabled(const std::string& disabled,TextureResType texType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-02 16:08:45 +08:00
|
|
|
if (disabled.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_disabledFileName = disabled;
|
|
|
|
_disabledTexType = texType;
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
extension::Scale9Sprite* disabledScale9 = static_cast<extension::Scale9Sprite*>(_buttonDisableRenderer);
|
|
|
|
switch (_disabledTexType)
|
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2014-04-02 18:35:08 +08:00
|
|
|
disabledScale9->initWithFile(disabled);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2014-04-02 18:35:08 +08:00
|
|
|
disabledScale9->initWithSpriteFrameName(disabled);
|
2014-03-11 17:13:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
disabledScale9->setCapInsets(_capInsetsDisabled);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Sprite* disabledRenderer = static_cast<Sprite*>(_buttonDisableRenderer);
|
|
|
|
switch (_disabledTexType)
|
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
|
|
|
disabledRenderer->setTexture(disabled);
|
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
|
|
|
disabledRenderer->setSpriteFrame(disabled);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_disabledTextureSize = _buttonDisableRenderer->getContentSize();
|
|
|
|
disabledTextureScaleChangedWithSize();
|
|
|
|
updateAnchorPoint();
|
|
|
|
updateFlippedX();
|
|
|
|
updateFlippedY();
|
|
|
|
updateRGBAToRenderer(_buttonDisableRenderer);
|
|
|
|
_disabledTextureLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsets(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
setCapInsetsNormalRenderer(capInsets);
|
|
|
|
setCapInsetsPressedRenderer(capInsets);
|
|
|
|
setCapInsetsDisabledRenderer(capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsNormalRenderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsNormal = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonNormalRenderer)->setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Rect& Button::getCapInsetsNormalRenderer()
|
|
|
|
{
|
|
|
|
return _capInsetsNormal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsPressedRenderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsPressed = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonClickedRenderer)->setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Rect& Button::getCapInsetsPressedRenderer()
|
|
|
|
{
|
|
|
|
return _capInsetsPressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsDisabledRenderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsDisabled = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonDisableRenderer)->setCapInsets(capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Rect& Button::getCapInsetsDisabledRenderer()
|
|
|
|
{
|
|
|
|
return _capInsetsDisabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToNormal()
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(true);
|
|
|
|
_buttonClickedRenderer->setVisible(false);
|
|
|
|
_buttonDisableRenderer->setVisible(false);
|
|
|
|
if (_pressedTextureLoaded)
|
|
|
|
{
|
|
|
|
if (_pressedActionEnabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->stopAllActions();
|
|
|
|
_buttonClickedRenderer->stopAllActions();
|
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, _normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
|
|
|
_buttonNormalRenderer->runAction(zoomAction);
|
|
|
|
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 18:54:06 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2014-03-31 11:34:26 +08:00
|
|
|
updateTextureRGBA();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->stopAllActions();
|
|
|
|
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
2014-03-28 18:54:06 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToPressed()
|
|
|
|
{
|
|
|
|
if (_pressedTextureLoaded)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(false);
|
|
|
|
_buttonClickedRenderer->setVisible(true);
|
|
|
|
_buttonDisableRenderer->setVisible(false);
|
|
|
|
if (_pressedActionEnabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->stopAllActions();
|
|
|
|
_buttonClickedRenderer->stopAllActions();
|
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, _pressedTextureScaleXInSize + 0.1f, _pressedTextureScaleYInSize + 0.1f);
|
|
|
|
_buttonClickedRenderer->runAction(zoomAction);
|
|
|
|
_buttonNormalRenderer->setScale(_pressedTextureScaleXInSize + 0.1f, _pressedTextureScaleYInSize + 0.1f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(true);
|
|
|
|
_buttonClickedRenderer->setVisible(true);
|
|
|
|
_buttonDisableRenderer->setVisible(false);
|
2014-03-31 11:34:26 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setColor(Color3B::GRAY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->stopAllActions();
|
|
|
|
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize + 0.1f, _normalTextureScaleYInSize + 0.1f);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToDisabled()
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(false);
|
|
|
|
_buttonClickedRenderer->setVisible(false);
|
|
|
|
_buttonDisableRenderer->setVisible(true);
|
|
|
|
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
|
|
|
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateFlippedX()
|
|
|
|
{
|
2014-03-14 21:06:40 +08:00
|
|
|
float flip = _flippedX ? -1.0f : 1.0f;
|
|
|
|
_titleRenderer->setScaleX(flip);
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setScaleX(flip);
|
|
|
|
_buttonClickedRenderer->setScaleX(flip);
|
|
|
|
_buttonDisableRenderer->setScaleX(flip);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static_cast<Sprite*>(_buttonNormalRenderer)->setFlippedX(_flippedX);
|
|
|
|
static_cast<Sprite*>(_buttonClickedRenderer)->setFlippedX(_flippedX);
|
|
|
|
static_cast<Sprite*>(_buttonDisableRenderer)->setFlippedX(_flippedX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateFlippedY()
|
|
|
|
{
|
2014-03-14 21:06:40 +08:00
|
|
|
float flip = _flippedY ? -1.0f : 1.0f;
|
|
|
|
_titleRenderer->setScaleY(flip);
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setScaleY(flip);
|
|
|
|
_buttonClickedRenderer->setScaleY(flip);
|
|
|
|
_buttonDisableRenderer->setScaleY(flip);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static_cast<Sprite*>(_buttonNormalRenderer)->setFlippedY(_flippedY);
|
|
|
|
static_cast<Sprite*>(_buttonClickedRenderer)->setFlippedY(_flippedY);
|
|
|
|
static_cast<Sprite*>(_buttonDisableRenderer)->setFlippedY(_flippedY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setAnchorPoint(const Point &pt)
|
|
|
|
{
|
|
|
|
Widget::setAnchorPoint(pt);
|
|
|
|
_buttonNormalRenderer->setAnchorPoint(pt);
|
|
|
|
_buttonClickedRenderer->setAnchorPoint(pt);
|
|
|
|
_buttonDisableRenderer->setAnchorPoint(pt);
|
|
|
|
_titleRenderer->setPosition(Point(_size.width*(0.5f-_anchorPoint.x), _size.height*(0.5f-_anchorPoint.y)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
|
|
|
normalTextureScaleChangedWithSize();
|
|
|
|
pressedTextureScaleChangedWithSize();
|
|
|
|
disabledTextureScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Size& Button::getContentSize() const
|
|
|
|
{
|
|
|
|
return _normalTextureSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* Button::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
if (_bright)
|
|
|
|
{
|
|
|
|
switch (_brightStyle)
|
|
|
|
{
|
|
|
|
case BRIGHT_NORMAL:
|
|
|
|
return _buttonNormalRenderer;
|
|
|
|
case BRIGHT_HIGHLIGHT:
|
|
|
|
return _buttonClickedRenderer;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _buttonDisableRenderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::normalTextureScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setScale(1.0f);
|
|
|
|
_normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f;
|
|
|
|
_size = _normalTextureSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonNormalRenderer)->setPreferredSize(_size);
|
|
|
|
_normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size textureSize = _normalTextureSize;
|
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float scaleX = _size.width / textureSize.width;
|
|
|
|
float scaleY = _size.height / textureSize.height;
|
|
|
|
_buttonNormalRenderer->setScaleX(scaleX);
|
|
|
|
_buttonNormalRenderer->setScaleY(scaleY);
|
|
|
|
_normalTextureScaleXInSize = scaleX;
|
|
|
|
_normalTextureScaleYInSize = scaleY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::pressedTextureScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonClickedRenderer->setScale(1.0f);
|
|
|
|
_pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonClickedRenderer)->setPreferredSize(_size);
|
|
|
|
_pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 1.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size textureSize = _pressedTextureSize;
|
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_buttonClickedRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float scaleX = _size.width / _pressedTextureSize.width;
|
|
|
|
float scaleY = _size.height / _pressedTextureSize.height;
|
|
|
|
_buttonClickedRenderer->setScaleX(scaleX);
|
|
|
|
_buttonClickedRenderer->setScaleY(scaleY);
|
|
|
|
_pressedTextureScaleXInSize = scaleX;
|
|
|
|
_pressedTextureScaleYInSize = scaleY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::disabledTextureScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonDisableRenderer->setScale(1.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonDisableRenderer)->setPreferredSize(_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size textureSize = _disabledTextureSize;
|
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_buttonDisableRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
float scaleX = _size.width / _disabledTextureSize.width;
|
|
|
|
float scaleY = _size.height / _disabledTextureSize.height;
|
|
|
|
_buttonDisableRenderer->setScaleX(scaleX);
|
|
|
|
_buttonDisableRenderer->setScaleY(scaleY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setPressedActionEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_pressedActionEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setTitleText(const std::string& text)
|
|
|
|
{
|
|
|
|
_titleRenderer->setString(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& Button::getTitleText() const
|
|
|
|
{
|
|
|
|
return _titleRenderer->getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setTitleColor(const Color3B& color)
|
|
|
|
{
|
|
|
|
_titleColor = color;
|
|
|
|
_titleRenderer->updateDisplayedColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& Button::getTitleColor() const
|
|
|
|
{
|
|
|
|
return _titleRenderer->getColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setTitleFontSize(float size)
|
|
|
|
{
|
|
|
|
_titleRenderer->setFontSize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Button::getTitleFontSize() const
|
|
|
|
{
|
|
|
|
return _titleRenderer->getFontSize();
|
|
|
|
}
|
|
|
|
|
2014-04-03 10:31:10 +08:00
|
|
|
void Button::setTitleFontName(const std::string& fontName)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-09 21:35:08 +08:00
|
|
|
_titleRenderer->setFont(fontName);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-04-03 10:31:10 +08:00
|
|
|
const std::string& Button::getTitleFontName() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-09 21:35:08 +08:00
|
|
|
return _titleRenderer->getFont();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Button::getDescription() const
|
|
|
|
{
|
|
|
|
return "Button";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateTextureColor()
|
|
|
|
{
|
|
|
|
updateColorToRenderer(_buttonNormalRenderer);
|
|
|
|
updateColorToRenderer(_buttonClickedRenderer);
|
|
|
|
updateColorToRenderer(_buttonDisableRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateTextureOpacity()
|
|
|
|
{
|
|
|
|
updateOpacityToRenderer(_buttonNormalRenderer);
|
|
|
|
updateOpacityToRenderer(_buttonClickedRenderer);
|
|
|
|
updateOpacityToRenderer(_buttonDisableRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateTextureRGBA()
|
|
|
|
{
|
|
|
|
updateRGBAToRenderer(_buttonNormalRenderer);
|
|
|
|
updateRGBAToRenderer(_buttonClickedRenderer);
|
|
|
|
updateRGBAToRenderer(_buttonDisableRenderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* Button::createCloneInstance()
|
|
|
|
{
|
|
|
|
return Button::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
Button* button = dynamic_cast<Button*>(widget);
|
|
|
|
if (button)
|
|
|
|
{
|
|
|
|
_prevIgnoreSize = button->_prevIgnoreSize;
|
|
|
|
setScale9Enabled(button->_scale9Enabled);
|
2014-04-02 18:35:08 +08:00
|
|
|
loadTextureNormal(button->_normalFileName, button->_normalTexType);
|
|
|
|
loadTexturePressed(button->_clickedFileName, button->_pressedTexType);
|
|
|
|
loadTextureDisabled(button->_disabledFileName, button->_disabledTexType);
|
2014-03-11 17:13:54 +08:00
|
|
|
setCapInsetsNormalRenderer(button->_capInsetsNormal);
|
|
|
|
setCapInsetsPressedRenderer(button->_capInsetsPressed);
|
|
|
|
setCapInsetsDisabledRenderer(button->_capInsetsDisabled);
|
|
|
|
setTitleText(button->getTitleText());
|
|
|
|
setTitleFontName(button->getTitleFontName());
|
|
|
|
setTitleFontSize(button->getTitleFontSize());
|
|
|
|
setTitleColor(button->getTitleColor());
|
|
|
|
setPressedActionEnabled(button->_pressedActionEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|