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"
|
2014-07-29 14:57:45 +08:00
|
|
|
#include "ui/UIScale9Sprite.h"
|
2014-06-04 14:26:21 +08:00
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
#include "2d/CCActionInterval.h"
|
2014-06-18 11:11:08 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
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);
|
2014-09-12 14:24:27 +08:00
|
|
|
static const float ZOOM_ACTION_TIME_STEP = 0.05;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
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),
|
2014-05-09 14:56:05 +08:00
|
|
|
_normalTexType(TextureResType::LOCAL),
|
|
|
|
_pressedTexType(TextureResType::LOCAL),
|
|
|
|
_disabledTexType(TextureResType::LOCAL),
|
2014-06-20 10:40:16 +08:00
|
|
|
_normalTextureSize(_contentSize),
|
|
|
|
_pressedTextureSize(_contentSize),
|
|
|
|
_disabledTextureSize(_contentSize),
|
2014-03-11 17:13:54 +08:00
|
|
|
_pressedActionEnabled(false),
|
|
|
|
_normalTextureScaleXInSize(1.0f),
|
|
|
|
_normalTextureScaleYInSize(1.0f),
|
|
|
|
_pressedTextureScaleXInSize(1.0f),
|
|
|
|
_pressedTextureScaleYInSize(1.0f),
|
2014-08-14 11:14:24 +08:00
|
|
|
_zoomScale(0.1f),
|
2014-03-11 17:13:54 +08:00
|
|
|
_normalTextureLoaded(false),
|
|
|
|
_pressedTextureLoaded(false),
|
2014-04-17 14:08:25 +08:00
|
|
|
_disabledTextureLoaded(false),
|
|
|
|
_normalTextureAdaptDirty(true),
|
|
|
|
_pressedTextureAdaptDirty(true),
|
2014-06-15 15:55:46 +08:00
|
|
|
_disabledTextureAdaptDirty(true),
|
|
|
|
_fontName("Thonburi"),
|
|
|
|
_fontSize(10),
|
2014-06-17 15:34:54 +08:00
|
|
|
_type(FontType::SYSTEM)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-24 15:51:14 +08:00
|
|
|
setTouchEnabled(true);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Button::~Button()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Button* Button::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Button* widget = new (std::nothrow) Button();
|
2014-03-11 17:13:54 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Button *btn = new (std::nothrow) Button;
|
2014-04-02 11:48:42 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::initRenderer()
|
|
|
|
{
|
2014-07-30 15:19:39 +08:00
|
|
|
_buttonNormalRenderer = Scale9Sprite::create();
|
|
|
|
_buttonClickedRenderer = Scale9Sprite::create();
|
|
|
|
_buttonDisableRenderer = Scale9Sprite::create();
|
|
|
|
_buttonClickedRenderer->setScale9Enabled(false);
|
|
|
|
_buttonNormalRenderer->setScale9Enabled(false);
|
|
|
|
_buttonDisableRenderer->setScale9Enabled(false);
|
|
|
|
|
2014-03-14 21:06:40 +08:00
|
|
|
_titleRenderer = Label::create();
|
2014-05-15 01:07:09 +08:00
|
|
|
_titleRenderer->setAnchorPoint(Vec2::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;
|
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_scale9Enabled = able;
|
2014-07-30 15:19:39 +08:00
|
|
|
|
|
|
|
_buttonNormalRenderer->setScale9Enabled(_scale9Enabled);
|
|
|
|
_buttonClickedRenderer->setScale9Enabled(_scale9Enabled);
|
|
|
|
_buttonDisableRenderer->setScale9Enabled(_scale9Enabled);
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
bool ignoreBefore = _ignoreSize;
|
|
|
|
ignoreContentAdaptWithSize(false);
|
|
|
|
_prevIgnoreSize = ignoreBefore;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ignoreContentAdaptWithSize(_prevIgnoreSize);
|
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
setCapInsetsNormalRenderer(_capInsetsNormal);
|
|
|
|
setCapInsetsPressedRenderer(_capInsetsPressed);
|
|
|
|
setCapInsetsDisabledRenderer(_capInsetsDisabled);
|
2014-07-30 15:19:39 +08:00
|
|
|
|
|
|
|
_brightStyle = BrightStyle::NONE;
|
2014-03-11 17:13:54 +08:00
|
|
|
setBright(_bright);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:06:32 +08:00
|
|
|
bool Button::isScale9Enabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
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;
|
2014-07-30 15:19:39 +08:00
|
|
|
|
|
|
|
switch (_normalTexType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-07-30 15:19:39 +08:00
|
|
|
case TextureResType::LOCAL:
|
|
|
|
_buttonNormalRenderer->initWithFile(normal);
|
|
|
|
break;
|
|
|
|
case TextureResType::PLIST:
|
|
|
|
_buttonNormalRenderer->initWithSpriteFrameName(normal);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_normalTextureSize = _buttonNormalRenderer->getContentSize();
|
|
|
|
updateFlippedX();
|
|
|
|
updateFlippedY();
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
2014-06-06 16:00:28 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
updateContentSizeWithTextureSize(_normalTextureSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_normalTextureLoaded = true;
|
2014-04-17 14:08:25 +08:00
|
|
|
_normalTextureAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2014-07-30 15:19:39 +08:00
|
|
|
|
|
|
|
switch (_pressedTexType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-07-30 15:19:39 +08:00
|
|
|
case TextureResType::LOCAL:
|
|
|
|
_buttonClickedRenderer->initWithFile(selected);
|
|
|
|
break;
|
|
|
|
case TextureResType::PLIST:
|
|
|
|
_buttonClickedRenderer->initWithSpriteFrameName(selected);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
|
2014-07-30 15:19:39 +08:00
|
|
|
//TODO: mark as dirty
|
2014-03-11 17:13:54 +08:00
|
|
|
updateFlippedX();
|
|
|
|
updateFlippedY();
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_pressedTextureLoaded = true;
|
2014-04-17 14:08:25 +08:00
|
|
|
_pressedTextureAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2014-07-30 15:19:39 +08:00
|
|
|
|
|
|
|
switch (_disabledTexType)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-07-30 15:19:39 +08:00
|
|
|
case TextureResType::LOCAL:
|
|
|
|
_buttonDisableRenderer->initWithFile(disabled);
|
|
|
|
break;
|
|
|
|
case TextureResType::PLIST:
|
|
|
|
_buttonDisableRenderer->initWithSpriteFrameName(disabled);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_disabledTextureSize = _buttonDisableRenderer->getContentSize();
|
|
|
|
updateFlippedX();
|
|
|
|
updateFlippedY();
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_disabledTextureLoaded = true;
|
2014-04-17 14:08:25 +08:00
|
|
|
_disabledTextureAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsets(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
setCapInsetsNormalRenderer(capInsets);
|
|
|
|
setCapInsetsPressedRenderer(capInsets);
|
|
|
|
setCapInsetsDisabledRenderer(capInsets);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsNormalRenderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsNormal = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
_buttonNormalRenderer->setCapInsets(capInsets);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:06:32 +08:00
|
|
|
const Rect& Button::getCapInsetsNormalRenderer()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _capInsetsNormal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsPressedRenderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsPressed = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
_buttonClickedRenderer->setCapInsets(capInsets);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:06:32 +08:00
|
|
|
const Rect& Button::getCapInsetsPressedRenderer()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _capInsetsPressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsDisabledRenderer(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
_capInsetsDisabled = capInsets;
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
_buttonDisableRenderer->setCapInsets(capInsets);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:06:32 +08:00
|
|
|
const Rect& Button::getCapInsetsDisabledRenderer()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _capInsetsDisabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToNormal()
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(true);
|
|
|
|
_buttonClickedRenderer->setVisible(false);
|
|
|
|
_buttonDisableRenderer->setVisible(false);
|
|
|
|
if (_pressedTextureLoaded)
|
|
|
|
{
|
|
|
|
if (_pressedActionEnabled)
|
|
|
|
{
|
2014-09-12 14:24:27 +08:00
|
|
|
this->stopAllActions();
|
|
|
|
Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0, 1.0);
|
|
|
|
this->runAction(zoomAction);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-12 14:24:27 +08:00
|
|
|
this->stopAllActions();
|
|
|
|
this->setScale(1.0, 1.0);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToPressed()
|
|
|
|
{
|
|
|
|
if (_pressedTextureLoaded)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(false);
|
|
|
|
_buttonClickedRenderer->setVisible(true);
|
|
|
|
_buttonDisableRenderer->setVisible(false);
|
2014-07-30 15:19:39 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_pressedActionEnabled)
|
|
|
|
{
|
2014-09-12 14:24:27 +08:00
|
|
|
this->stopAllActions();
|
|
|
|
Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0 + _zoomScale, 1.0 + _zoomScale);
|
|
|
|
this->runAction(zoomAction);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
{
|
2014-09-12 14:24:27 +08:00
|
|
|
this->stopAllActions();
|
|
|
|
this->setScale(1.0 +_zoomScale, 1.0 + _zoomScale);
|
2014-03-31 11:34:26 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToDisabled()
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(false);
|
|
|
|
_buttonClickedRenderer->setVisible(false);
|
|
|
|
_buttonDisableRenderer->setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateFlippedX()
|
|
|
|
{
|
2014-03-14 21:06:40 +08:00
|
|
|
float flip = _flippedX ? -1.0f : 1.0f;
|
|
|
|
_titleRenderer->setScaleX(flip);
|
2014-07-30 15:19:39 +08:00
|
|
|
|
|
|
|
_buttonNormalRenderer->setFlippedX(_flippedX);
|
|
|
|
_buttonClickedRenderer->setFlippedX(_flippedX);
|
|
|
|
_buttonDisableRenderer->setFlippedX(_flippedX);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateFlippedY()
|
|
|
|
{
|
2014-03-14 21:06:40 +08:00
|
|
|
float flip = _flippedY ? -1.0f : 1.0f;
|
|
|
|
_titleRenderer->setScaleY(flip);
|
2014-07-30 15:19:39 +08:00
|
|
|
_buttonNormalRenderer->setFlippedY(_flippedY);
|
|
|
|
_buttonClickedRenderer->setFlippedY(_flippedY);
|
|
|
|
_buttonDisableRenderer->setFlippedY(_flippedY);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
|
|
|
|
void Button::updateTitleLocation()
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-08-28 11:41:18 +08:00
|
|
|
_titleRenderer->setPosition(_contentSize.width * 0.5f, _contentSize.height * 0.5f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2014-04-17 14:08:25 +08:00
|
|
|
updateTitleLocation();
|
|
|
|
_normalTextureAdaptDirty = true;
|
|
|
|
_pressedTextureAdaptDirty = true;
|
|
|
|
_disabledTextureAdaptDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_normalTextureAdaptDirty)
|
|
|
|
{
|
|
|
|
normalTextureScaleChangedWithSize();
|
|
|
|
_normalTextureAdaptDirty = false;
|
|
|
|
}
|
|
|
|
if (_pressedTextureAdaptDirty)
|
|
|
|
{
|
|
|
|
pressedTextureScaleChangedWithSize();
|
|
|
|
_pressedTextureAdaptDirty = false;
|
|
|
|
}
|
|
|
|
if (_disabledTextureAdaptDirty)
|
|
|
|
{
|
|
|
|
disabledTextureScaleChangedWithSize();
|
|
|
|
_disabledTextureAdaptDirty = false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 14:39:22 +08:00
|
|
|
Size Button::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-09-12 13:50:13 +08:00
|
|
|
Size titleSize = _titleRenderer->getContentSize();
|
2014-09-12 14:24:27 +08:00
|
|
|
if (!_normalTextureLoaded && _titleRenderer->getString().size() > 0) {
|
2014-09-12 13:50:13 +08:00
|
|
|
return titleSize;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
return _normalTextureSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* Button::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
if (_bright)
|
|
|
|
{
|
|
|
|
switch (_brightStyle)
|
|
|
|
{
|
2014-05-09 15:23:34 +08:00
|
|
|
case BrightStyle::NORMAL:
|
2014-03-11 17:13:54 +08:00
|
|
|
return _buttonNormalRenderer;
|
2014-05-09 15:23:34 +08:00
|
|
|
case BrightStyle::HIGHLIGHT:
|
2014-03-11 17:13:54 +08:00
|
|
|
return _buttonClickedRenderer;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _buttonDisableRenderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::normalTextureScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setScale(1.0f);
|
|
|
|
_normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2014-07-31 11:07:20 +08:00
|
|
|
_buttonNormalRenderer->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f;
|
2014-07-31 11:07:20 +08:00
|
|
|
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize,_normalTextureScaleYInSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size textureSize = _normalTextureSize;
|
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
float scaleX = _contentSize.width / textureSize.width;
|
|
|
|
float scaleY = _contentSize.height / textureSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
_buttonNormalRenderer->setScaleX(scaleX);
|
|
|
|
_buttonNormalRenderer->setScaleY(scaleY);
|
|
|
|
_normalTextureScaleXInSize = scaleX;
|
|
|
|
_normalTextureScaleYInSize = scaleY;
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_buttonNormalRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::pressedTextureScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonClickedRenderer->setScale(1.0f);
|
|
|
|
_pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2014-07-31 11:07:20 +08:00
|
|
|
_buttonClickedRenderer->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
_pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 1.0f;
|
2014-07-31 11:07:20 +08:00
|
|
|
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize,_pressedTextureScaleYInSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size textureSize = _pressedTextureSize;
|
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_buttonClickedRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
float scaleX = _contentSize.width / _pressedTextureSize.width;
|
|
|
|
float scaleY = _contentSize.height / _pressedTextureSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
_buttonClickedRenderer->setScaleX(scaleX);
|
|
|
|
_buttonClickedRenderer->setScaleY(scaleY);
|
|
|
|
_pressedTextureScaleXInSize = scaleX;
|
|
|
|
_pressedTextureScaleYInSize = scaleY;
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_buttonClickedRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::disabledTextureScaleChangedWithSize()
|
|
|
|
{
|
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
_buttonDisableRenderer->setScale(1.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
2014-07-31 11:07:20 +08:00
|
|
|
_buttonDisableRenderer->setScale(1.0);
|
|
|
|
_buttonDisableRenderer->setPreferredSize(_contentSize);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size textureSize = _disabledTextureSize;
|
|
|
|
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
|
|
|
|
{
|
|
|
|
_buttonDisableRenderer->setScale(1.0f);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 10:40:16 +08:00
|
|
|
float scaleX = _contentSize.width / _disabledTextureSize.width;
|
|
|
|
float scaleY = _contentSize.height / _disabledTextureSize.height;
|
2014-03-11 17:13:54 +08:00
|
|
|
_buttonDisableRenderer->setScaleX(scaleX);
|
|
|
|
_buttonDisableRenderer->setScaleY(scaleY);
|
|
|
|
}
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_buttonDisableRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-06-25 13:47:29 +08:00
|
|
|
_titleRenderer->setColor(color);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& Button::getTitleColor() const
|
|
|
|
{
|
|
|
|
return _titleRenderer->getColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setTitleFontSize(float size)
|
|
|
|
{
|
2014-06-17 15:34:54 +08:00
|
|
|
if (_type == FontType::SYSTEM) {
|
2014-06-15 15:55:46 +08:00
|
|
|
_titleRenderer->setSystemFontSize(size);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
TTFConfig config = _titleRenderer->getTTFConfig();
|
|
|
|
config.fontSize = size;
|
|
|
|
_titleRenderer->setTTFConfig(config);
|
|
|
|
}
|
|
|
|
_fontSize = size;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Button::getTitleFontSize() const
|
|
|
|
{
|
2014-06-15 15:55:46 +08:00
|
|
|
return _fontSize;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-08-14 11:14:24 +08:00
|
|
|
|
|
|
|
void Button::setZoomScale(float scale)
|
|
|
|
{
|
|
|
|
_zoomScale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Button::getZoomScale()const
|
|
|
|
{
|
|
|
|
return _zoomScale;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-04-03 10:31:10 +08:00
|
|
|
void Button::setTitleFontName(const std::string& fontName)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-15 15:55:46 +08:00
|
|
|
if(FileUtils::getInstance()->isFileExist(fontName))
|
|
|
|
{
|
|
|
|
TTFConfig config = _titleRenderer->getTTFConfig();
|
|
|
|
config.fontFilePath = fontName;
|
|
|
|
config.fontSize = _fontSize;
|
|
|
|
_titleRenderer->setTTFConfig(config);
|
2014-06-17 15:34:54 +08:00
|
|
|
_type = FontType::TTF;
|
2014-06-15 15:55:46 +08:00
|
|
|
} else{
|
|
|
|
_titleRenderer->setSystemFontName(fontName);
|
2014-06-17 15:34:54 +08:00
|
|
|
_type = FontType::SYSTEM;
|
2014-06-15 15:55:46 +08:00
|
|
|
}
|
|
|
|
_fontName = 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-06-15 15:55:46 +08:00
|
|
|
return _fontName;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Button::getDescription() const
|
|
|
|
{
|
|
|
|
return "Button";
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-08-14 16:56:15 +08:00
|
|
|
setZoomScale(button->_zoomScale);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|