2013-09-13 22:20:20 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +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
|
|
|
|
2014-01-03 12:15:42 +08:00
|
|
|
#include "gui/UIButton.h"
|
2013-10-16 11:19:01 +08:00
|
|
|
#include "extensions/GUI/CCControlExtension/CCScale9Sprite.h"
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
namespace ui {
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2014-01-03 15:46:42 +08:00
|
|
|
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);
|
2013-09-13 22:20:20 +08:00
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Button::Button():
|
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-12-23 15:02:52 +08:00
|
|
|
_capInsetsNormal(Rect::ZERO),
|
|
|
|
_capInsetsPressed(Rect::ZERO),
|
|
|
|
_capInsetsDisabled(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),
|
2014-01-03 12:14:21 +08:00
|
|
|
_titleColor(Color3B::WHITE),
|
|
|
|
_normalTextureScaleXInSize(1.0f),
|
|
|
|
_normalTextureScaleYInSize(1.0f),
|
|
|
|
_pressedTextureScaleXInSize(1.0f),
|
|
|
|
_pressedTextureScaleYInSize(1.0f),
|
|
|
|
_normalTextureLoaded(false),
|
|
|
|
_pressedTextureLoaded(false),
|
|
|
|
_disabledTextureLoaded(false)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Button::~Button()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Button* Button::create()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Button* widget = new Button();
|
2013-09-13 22:20:20 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Button::init()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
if (Widget::init())
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::initRenderer()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_buttonNormalRenderer = Sprite::create();
|
|
|
|
_buttonClickedRenderer = Sprite::create();
|
|
|
|
_buttonDisableRenderer = Sprite::create();
|
|
|
|
_titleRenderer = LabelTTF::create();
|
2014-01-03 12:14:21 +08:00
|
|
|
|
2014-01-03 15:46:42 +08:00
|
|
|
Node::addChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
|
|
|
|
Node::addChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
|
|
|
|
Node::addChild(_buttonDisableRenderer, DISABLED_RENDERER_Z, -1);
|
|
|
|
Node::addChild(_titleRenderer, TITLE_RENDERER_Z, -1);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setScale9Enabled(bool able)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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-12-24 20:22:14 +08:00
|
|
|
Node::removeChild(_buttonNormalRenderer);
|
|
|
|
Node::removeChild(_buttonClickedRenderer);
|
|
|
|
Node::removeChild(_buttonDisableRenderer);
|
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-12-23 15:02:52 +08:00
|
|
|
_buttonNormalRenderer = extension::Scale9Sprite::create();
|
|
|
|
_buttonClickedRenderer = extension::Scale9Sprite::create();
|
|
|
|
_buttonDisableRenderer = extension::Scale9Sprite::create();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
_buttonNormalRenderer = Sprite::create();
|
|
|
|
_buttonClickedRenderer = Sprite::create();
|
|
|
|
_buttonDisableRenderer = 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);
|
2014-01-03 15:46:42 +08:00
|
|
|
Node::addChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
|
|
|
|
Node::addChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
|
|
|
|
Node::addChild(_buttonDisableRenderer, DISABLED_RENDERER_Z, -1);
|
2013-09-16 20:54:13 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::ignoreContentAdaptWithSize(bool ignore)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
if (!_scale9Enabled || (_scale9Enabled && !ignore))
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::ignoreContentAdaptWithSize(ignore);
|
2013-09-16 20:54:13 +08:00
|
|
|
_prevIgnoreSize = ignore;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::loadTextures(const char* normal,const char* selected,const char* disabled,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
loadTextureNormal(normal,texType);
|
|
|
|
loadTexturePressed(selected,texType);
|
|
|
|
loadTextureDisabled(disabled,texType);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::loadTextureNormal(const char* normal,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-12-23 15:02:52 +08:00
|
|
|
extension::Scale9Sprite* normalRendererScale9 = static_cast<extension::Scale9Sprite*>(_buttonNormalRenderer);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_normalTexType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2013-12-23 15:02:52 +08:00
|
|
|
normalRendererScale9->initWithFile(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-12-23 15:02:52 +08:00
|
|
|
normalRendererScale9->initWithSpriteFrameName(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
normalRendererScale9->setCapInsets(_capInsetsNormal);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Sprite* normalRenderer = static_cast<Sprite*>(_buttonNormalRenderer);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_normalTexType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2013-12-23 15:02:52 +08:00
|
|
|
normalRenderer->setTexture(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-12-23 15:02:52 +08:00
|
|
|
normalRenderer->setSpriteFrame(normal);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_normalTextureSize = _buttonNormalRenderer->getContentSize();
|
2013-12-25 18:30:21 +08:00
|
|
|
updateDisplayedColor(getColor());
|
|
|
|
updateDisplayedOpacity(getOpacity());
|
2013-09-13 22:20:20 +08:00
|
|
|
updateAnchorPoint();
|
|
|
|
normalTextureScaleChangedWithSize();
|
2014-01-03 12:14:21 +08:00
|
|
|
_normalTextureLoaded = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::loadTexturePressed(const char* selected,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-12-23 15:02:52 +08:00
|
|
|
extension::Scale9Sprite* clickedRendererScale9 = static_cast<extension::Scale9Sprite*>(_buttonClickedRenderer);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_pressedTexType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2013-12-23 15:02:52 +08:00
|
|
|
clickedRendererScale9->initWithFile(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-12-23 15:02:52 +08:00
|
|
|
clickedRendererScale9->initWithSpriteFrameName(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
clickedRendererScale9->setCapInsets(_capInsetsPressed);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Sprite* clickedRenderer = static_cast<Sprite*>(_buttonClickedRenderer);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_pressedTexType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2013-12-23 15:02:52 +08:00
|
|
|
clickedRenderer->setTexture(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-12-23 15:02:52 +08:00
|
|
|
clickedRenderer->setSpriteFrame(selected);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
|
2013-12-25 18:30:21 +08:00
|
|
|
updateDisplayedColor(getColor());
|
|
|
|
updateDisplayedOpacity(getOpacity());
|
2013-09-13 22:20:20 +08:00
|
|
|
updateAnchorPoint();
|
|
|
|
pressedTextureScaleChangedWithSize();
|
2014-01-03 12:14:21 +08:00
|
|
|
_pressedTextureLoaded = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::loadTextureDisabled(const char* disabled,TextureResType texType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
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-12-23 15:02:52 +08:00
|
|
|
extension::Scale9Sprite* disabledScale9 = static_cast<extension::Scale9Sprite*>(_buttonDisableRenderer);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_disabledTexType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2013-12-23 15:02:52 +08:00
|
|
|
disabledScale9->initWithFile(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-12-23 15:02:52 +08:00
|
|
|
disabledScale9->initWithSpriteFrameName(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
disabledScale9->setCapInsets(_capInsetsDisabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Sprite* disabledRenderer = static_cast<Sprite*>(_buttonDisableRenderer);
|
2013-09-16 20:54:13 +08:00
|
|
|
switch (_disabledTexType)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
case UI_TEX_TYPE_LOCAL:
|
2013-12-23 15:02:52 +08:00
|
|
|
disabledRenderer->setTexture(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
case UI_TEX_TYPE_PLIST:
|
2013-12-23 15:02:52 +08:00
|
|
|
disabledRenderer->setSpriteFrame(disabled);
|
2013-09-13 22:20:20 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
_disabledTextureSize = _buttonDisableRenderer->getContentSize();
|
2013-12-25 18:30:21 +08:00
|
|
|
updateDisplayedColor(getColor());
|
|
|
|
updateDisplayedOpacity(getOpacity());
|
2013-09-13 22:20:20 +08:00
|
|
|
updateAnchorPoint();
|
|
|
|
disabledTextureScaleChangedWithSize();
|
2014-01-03 12:14:21 +08:00
|
|
|
_disabledTextureLoaded = true;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setCapInsets(const Rect &capInsets)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
setCapInsetsNormalRenderer(capInsets);
|
|
|
|
setCapInsetsPressedRenderer(capInsets);
|
|
|
|
setCapInsetsDisabledRenderer(capInsets);
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setCapInsetsNormalRenderer(const 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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonNormalRenderer)->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setCapInsetsPressedRenderer(const 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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonClickedRenderer)->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setCapInsetsDisabledRenderer(const 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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonDisableRenderer)->setCapInsets(capInsets);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::onPressStateChangedToNormal()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->setVisible(true);
|
|
|
|
_buttonClickedRenderer->setVisible(false);
|
|
|
|
_buttonDisableRenderer->setVisible(false);
|
2014-01-03 12:14:21 +08:00
|
|
|
if (_pressedTextureLoaded)
|
|
|
|
{
|
|
|
|
if (_pressedActionEnabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->stopAllActions();
|
|
|
|
_buttonClickedRenderer->stopAllActions();
|
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, _normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
|
|
|
_buttonNormalRenderer->runAction(zoomAction);
|
|
|
|
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->stopAllActions();
|
2014-01-03 12:14:21 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, _normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->runAction(zoomAction);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::onPressStateChangedToPressed()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-01-03 12:14:21 +08:00
|
|
|
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
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2014-01-03 12:14:21 +08:00
|
|
|
_buttonNormalRenderer->setVisible(true);
|
|
|
|
_buttonClickedRenderer->setVisible(true);
|
|
|
|
_buttonDisableRenderer->setVisible(false);
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->stopAllActions();
|
2014-01-03 12:14:21 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(0.05f, _pressedTextureScaleXInSize + 0.1f, _pressedTextureScaleYInSize + 0.1f);
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->runAction(zoomAction);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::onPressStateChangedToDisabled()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->setVisible(false);
|
|
|
|
_buttonClickedRenderer->setVisible(false);
|
|
|
|
_buttonDisableRenderer->setVisible(true);
|
2014-01-03 12:14:21 +08:00
|
|
|
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
|
|
|
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setFlipX(bool flipX)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_titleRenderer->setFlippedX(flipX);
|
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
static_cast<Sprite*>(_buttonNormalRenderer)->setFlippedX(flipX);
|
|
|
|
static_cast<Sprite*>(_buttonClickedRenderer)->setFlippedX(flipX);
|
|
|
|
static_cast<Sprite*>(_buttonDisableRenderer)->setFlippedX(flipX);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setFlipY(bool flipY)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_titleRenderer->setFlippedY(flipY);
|
|
|
|
if (_scale9Enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
static_cast<Sprite*>(_buttonNormalRenderer)->setFlippedY(flipY);
|
|
|
|
static_cast<Sprite*>(_buttonClickedRenderer)->setFlippedY(flipY);
|
|
|
|
static_cast<Sprite*>(_buttonDisableRenderer)->setFlippedY(flipY);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Button::isFlipX()
|
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
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
return static_cast<Sprite*>(_buttonNormalRenderer)->isFlippedX();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
bool Button::isFlipY()
|
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
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-23 15:02:52 +08:00
|
|
|
return static_cast<Sprite*>(_buttonNormalRenderer)->isFlippedY();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setAnchorPoint(const Point &pt)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setAnchorPoint(pt);
|
2013-09-16 20:54:13 +08:00
|
|
|
_buttonNormalRenderer->setAnchorPoint(pt);
|
|
|
|
_buttonClickedRenderer->setAnchorPoint(pt);
|
|
|
|
_buttonDisableRenderer->setAnchorPoint(pt);
|
2013-12-23 15:02:52 +08:00
|
|
|
_titleRenderer->setPosition(Point(_size.width*(0.5f-_anchorPoint.x), _size.height*(0.5f-_anchorPoint.y)));
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::onSizeChanged()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::onSizeChanged();
|
2013-09-13 22:20:20 +08:00
|
|
|
normalTextureScaleChangedWithSize();
|
|
|
|
pressedTextureScaleChangedWithSize();
|
|
|
|
disabledTextureScaleChangedWithSize();
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const Size& Button::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-12-23 15:02:52 +08:00
|
|
|
Node* Button::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::normalTextureScaleChangedWithSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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);
|
2014-01-03 12:14:21 +08:00
|
|
|
_normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f;
|
2013-09-16 20:54:13 +08:00
|
|
|
_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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonNormalRenderer)->setPreferredSize(_size);
|
2014-01-03 12:14:21 +08:00
|
|
|
_normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +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);
|
2014-01-03 12:14:21 +08:00
|
|
|
_normalTextureScaleXInSize = scaleX;
|
|
|
|
_normalTextureScaleYInSize = scaleY;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::pressedTextureScaleChangedWithSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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);
|
2014-01-03 12:14:21 +08:00
|
|
|
_pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonClickedRenderer)->setPreferredSize(_size);
|
2014-01-03 12:14:21 +08:00
|
|
|
_pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 1.0f;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +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);
|
2014-01-03 12:14:21 +08:00
|
|
|
_pressedTextureScaleXInSize = scaleX;
|
|
|
|
_pressedTextureScaleYInSize = scaleY;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::disabledTextureScaleChangedWithSize()
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
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-12-23 15:02:52 +08:00
|
|
|
static_cast<extension::Scale9Sprite*>(_buttonDisableRenderer)->setPreferredSize(_size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-23 15:02:52 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setPressedActionEnabled(bool enabled)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_pressedActionEnabled = enabled;
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const std::string& Button::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-12-23 15:02:52 +08:00
|
|
|
void Button::setTitleColor(const Color3B& color)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_titleColor = color;
|
2013-12-25 18:30:21 +08:00
|
|
|
_titleRenderer->updateDisplayedColor(color);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const Color3B& Button::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
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setTitleFontSize(float size)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_titleRenderer->setFontSize(size);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
float Button::getTitleFontSize() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
return _titleRenderer->getFontSize();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setTitleFontName(const char* fontName)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-09-16 20:54:13 +08:00
|
|
|
_titleRenderer->setFontName(fontName);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
const char* Button::getTitleFontName() const
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
return _titleRenderer->getFontName().c_str();
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::setColor(const Color3B &color)
|
2013-09-13 22:20:20 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget::setColor(color);
|
2013-09-16 20:54:13 +08:00
|
|
|
setTitleColor(_titleColor);
|
2013-09-13 22:20:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
std::string Button::getDescription() const
|
2013-09-17 17:59:20 +08:00
|
|
|
{
|
|
|
|
return "Button";
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
Widget* Button::createCloneInstance()
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
return Button::create();
|
2013-11-06 16:04:06 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
void Button::copySpecialProperties(Widget *widget)
|
2013-11-06 16:04:06 +08:00
|
|
|
{
|
2013-12-23 15:02:52 +08:00
|
|
|
Button* button = dynamic_cast<Button*>(widget);
|
2013-11-06 16:04:06 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:02:52 +08:00
|
|
|
}
|
|
|
|
|
2014-02-24 18:56:45 +08:00
|
|
|
NS_CC_END
|