2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-09-03 10:59:08 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2021-09-03 10:59:08 +08:00
|
|
|
https://adxe.org
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
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"
|
2015-01-19 16:58:46 +08:00
|
|
|
#include "ui/UIHelper.h"
|
2015-05-20 17:42:48 +08:00
|
|
|
#include <algorithm>
|
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-10-09 17:19:43 +08:00
|
|
|
static const float ZOOM_ACTION_TIME_STEP = 0.05f;
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
IMPLEMENT_CLASS_GUI_INFO(Button)
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
Button::Button():
|
|
|
|
_buttonNormalRenderer(nullptr),
|
|
|
|
_buttonClickedRenderer(nullptr),
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer(nullptr),
|
2014-03-11 17:13:54 +08:00
|
|
|
_titleRenderer(nullptr),
|
2014-10-30 22:25:46 +08:00
|
|
|
_zoomScale(0.1f),
|
2014-03-11 17:13:54 +08:00
|
|
|
_prevIgnoreSize(true),
|
|
|
|
_scale9Enabled(false),
|
2015-04-14 14:23:10 +08:00
|
|
|
_pressedActionEnabled(false),
|
2014-03-11 17:13:54 +08:00
|
|
|
_capInsetsNormal(Rect::ZERO),
|
|
|
|
_capInsetsPressed(Rect::ZERO),
|
|
|
|
_capInsetsDisabled(Rect::ZERO),
|
2014-06-20 10:40:16 +08:00
|
|
|
_normalTextureSize(_contentSize),
|
|
|
|
_pressedTextureSize(_contentSize),
|
|
|
|
_disabledTextureSize(_contentSize),
|
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),
|
2018-05-14 10:34:56 +08:00
|
|
|
_normalFileName(""),
|
|
|
|
_clickedFileName(""),
|
|
|
|
_disabledFileName(""),
|
|
|
|
_normalTexType(TextureResType::LOCAL),
|
|
|
|
_pressedTexType(TextureResType::LOCAL),
|
|
|
|
_disabledTexType(TextureResType::LOCAL),
|
|
|
|
_fontName("")
|
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;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
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-12-16 16:44:04 +08:00
|
|
|
if (btn && btn->init(normalImage,selectedImage,disableImage,texType))
|
|
|
|
{
|
2014-04-02 11:48:42 +08:00
|
|
|
btn->autorelease();
|
|
|
|
return btn;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(btn);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-04-02 11:48:42 +08:00
|
|
|
bool Button::init(const std::string &normalImage,
|
|
|
|
const std::string& selectedImage ,
|
|
|
|
const std::string& disableImage,
|
|
|
|
TextureResType texType)
|
|
|
|
{
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2018-09-29 14:26:58 +08:00
|
|
|
// invoke an overridden init() at first
|
|
|
|
if (!Widget::init()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
loadTextures(normalImage, selectedImage, disableImage, texType);
|
|
|
|
|
|
|
|
return true;
|
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();
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer = Scale9Sprite::create();
|
2015-12-09 14:21:26 +08:00
|
|
|
_buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
|
|
|
_buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
|
|
|
_buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
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);
|
2015-09-14 14:27:51 +08:00
|
|
|
addProtectedChild(_buttonDisabledRenderer, DISABLED_RENDERER_Z, -1);
|
2015-04-14 14:23:10 +08:00
|
|
|
}
|
2018-09-29 14:26:58 +08:00
|
|
|
|
|
|
|
bool Button::createTitleRendererIfNull() {
|
|
|
|
if( !_titleRenderer ) {
|
|
|
|
createTitleRenderer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
|
|
|
void Button::createTitleRenderer()
|
|
|
|
{
|
|
|
|
_titleRenderer = Label::create();
|
|
|
|
_titleRenderer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_titleRenderer, TITLE_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
/** replaces the current Label node with a new one */
|
|
|
|
void Button::setTitleLabel(Label* label)
|
|
|
|
{
|
|
|
|
if (_titleRenderer != label) {
|
|
|
|
CC_SAFE_RELEASE(_titleRenderer);
|
|
|
|
_titleRenderer = label;
|
|
|
|
CC_SAFE_RETAIN(_titleRenderer);
|
|
|
|
|
|
|
|
addProtectedChild(_titleRenderer, TITLE_RENDERER_Z, -1);
|
|
|
|
updateTitleLocation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** returns the current Label being used */
|
|
|
|
Label* Button::getTitleLabel() const
|
|
|
|
{
|
|
|
|
return _titleRenderer;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void Button::setScale9Enabled(bool able)
|
|
|
|
{
|
|
|
|
if (_scale9Enabled == able)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_scale9Enabled = able;
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2015-12-09 14:21:26 +08:00
|
|
|
if (_scale9Enabled) {
|
|
|
|
_buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
|
|
|
|
_buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
|
|
|
|
_buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
|
|
|
|
}else{
|
|
|
|
_buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
|
|
|
_buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
|
|
|
_buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
|
|
|
|
}
|
|
|
|
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
bool ignoreBefore = _ignoreSize;
|
|
|
|
ignoreContentAdaptWithSize(false);
|
|
|
|
_prevIgnoreSize = ignoreBefore;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ignoreContentAdaptWithSize(_prevIgnoreSize);
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
setCapInsetsNormalRenderer(_capInsetsNormal);
|
|
|
|
setCapInsetsPressedRenderer(_capInsetsPressed);
|
|
|
|
setCapInsetsDisabledRenderer(_capInsetsDisabled);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-07-30 15:19:39 +08:00
|
|
|
_brightStyle = BrightStyle::NONE;
|
2014-03-11 17:13:54 +08:00
|
|
|
setBright(_bright);
|
2015-01-20 17:24:04 +08:00
|
|
|
|
|
|
|
_normalTextureAdaptDirty = true;
|
|
|
|
_pressedTextureAdaptDirty = true;
|
|
|
|
_disabledTextureAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2014-10-09 18:28:09 +08:00
|
|
|
if (_unifySize)
|
|
|
|
{
|
2014-10-24 17:38:32 +08:00
|
|
|
this->updateContentSize();
|
2014-10-09 18:28:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
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
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
_normalFileName = normal;
|
|
|
|
_normalTexType = texType;
|
2015-11-26 13:49:32 +08:00
|
|
|
bool textureLoaded = true;
|
2015-11-25 20:07:50 +08:00
|
|
|
if (normal.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-12-07 18:03:22 +08:00
|
|
|
_buttonNormalRenderer->resetRender();
|
2015-11-26 13:49:32 +08:00
|
|
|
textureLoaded = false;
|
2015-11-25 20:07:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (texType)
|
|
|
|
{
|
2014-07-30 15:19:39 +08:00
|
|
|
case TextureResType::LOCAL:
|
|
|
|
_buttonNormalRenderer->initWithFile(normal);
|
|
|
|
break;
|
|
|
|
case TextureResType::PLIST:
|
|
|
|
_buttonNormalRenderer->initWithSpriteFrameName(normal);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-11-25 20:07:50 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-12-09 14:21:26 +08:00
|
|
|
//FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
|
2015-12-14 11:53:09 +08:00
|
|
|
if (!_ignoreSize && _customSize.equals(Size::ZERO)) {
|
2015-12-09 14:21:26 +08:00
|
|
|
_customSize = _buttonNormalRenderer->getContentSize();
|
|
|
|
}
|
2015-11-26 13:49:32 +08:00
|
|
|
this->setupNormalTexture(textureLoaded);
|
2015-04-14 14:23:10 +08:00
|
|
|
}
|
|
|
|
|
2015-11-26 13:49:32 +08:00
|
|
|
void Button::setupNormalTexture(bool textureLoaded)
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
2014-03-11 17:13:54 +08:00
|
|
|
_normalTextureSize = _buttonNormalRenderer->getContentSize();
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-10-24 17:38:32 +08:00
|
|
|
if (_unifySize )
|
|
|
|
{
|
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
updateContentSizeWithTextureSize(this->getNormalSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updateContentSizeWithTextureSize(_normalTextureSize);
|
|
|
|
}
|
2015-11-26 13:49:32 +08:00
|
|
|
_normalTextureLoaded = textureLoaded;
|
2014-04-17 14:08:25 +08:00
|
|
|
_normalTextureAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-14 14:23:10 +08:00
|
|
|
void Button::loadTextureNormal(SpriteFrame* normalSpriteFrame)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->initWithSpriteFrame(normalSpriteFrame);
|
2015-11-26 13:49:32 +08:00
|
|
|
this->setupNormalTexture(nullptr != normalSpriteFrame);
|
2015-04-14 14:23:10 +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
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
_clickedFileName = selected;
|
|
|
|
_pressedTexType = texType;
|
2017-01-20 09:58:29 +08:00
|
|
|
bool textureLoaded = true;
|
2015-11-25 20:07:50 +08:00
|
|
|
if (selected.empty())
|
|
|
|
{
|
2015-12-07 18:03:22 +08:00
|
|
|
_buttonClickedRenderer->resetRender();
|
2017-01-20 09:58:29 +08:00
|
|
|
textureLoaded = false;
|
2015-11-25 20:07:50 +08:00
|
|
|
}
|
|
|
|
else
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-25 20:07:50 +08:00
|
|
|
switch (texType)
|
|
|
|
{
|
2014-07-30 15:19:39 +08:00
|
|
|
case TextureResType::LOCAL:
|
|
|
|
_buttonClickedRenderer->initWithFile(selected);
|
|
|
|
break;
|
|
|
|
case TextureResType::PLIST:
|
|
|
|
_buttonClickedRenderer->initWithSpriteFrameName(selected);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-11-25 20:07:50 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2017-01-20 09:58:29 +08:00
|
|
|
this->setupPressedTexture(textureLoaded);
|
2015-04-14 14:23:10 +08:00
|
|
|
}
|
|
|
|
|
2015-11-26 13:49:32 +08:00
|
|
|
void Button::setupPressedTexture(bool textureLoaded)
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
2014-03-11 17:13:54 +08:00
|
|
|
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
|
|
|
|
2015-11-26 13:49:32 +08:00
|
|
|
_pressedTextureLoaded = textureLoaded;
|
2014-04-17 14:08:25 +08:00
|
|
|
_pressedTextureAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-14 14:23:10 +08:00
|
|
|
void Button::loadTexturePressed(SpriteFrame* pressedSpriteFrame)
|
|
|
|
{
|
|
|
|
_buttonClickedRenderer->initWithSpriteFrame(pressedSpriteFrame);
|
2015-11-26 13:49:32 +08:00
|
|
|
this->setupPressedTexture(nullptr != pressedSpriteFrame);
|
2015-04-14 14:23:10 +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
|
|
|
{
|
2015-11-18 13:38:29 +08:00
|
|
|
_disabledFileName = disabled;
|
|
|
|
_disabledTexType = texType;
|
2015-11-26 13:49:32 +08:00
|
|
|
bool textureLoaded = true;
|
2015-11-25 20:07:50 +08:00
|
|
|
if (disabled.empty())
|
|
|
|
{
|
2015-12-07 18:03:22 +08:00
|
|
|
_buttonDisabledRenderer->resetRender();
|
2015-11-26 13:49:32 +08:00
|
|
|
textureLoaded = false;
|
2015-11-25 20:07:50 +08:00
|
|
|
}
|
|
|
|
else
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-11-25 20:07:50 +08:00
|
|
|
switch (texType)
|
|
|
|
{
|
2014-07-30 15:19:39 +08:00
|
|
|
case TextureResType::LOCAL:
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->initWithFile(disabled);
|
2014-07-30 15:19:39 +08:00
|
|
|
break;
|
|
|
|
case TextureResType::PLIST:
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->initWithSpriteFrameName(disabled);
|
2014-07-30 15:19:39 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2015-11-25 20:07:50 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-11-26 13:49:32 +08:00
|
|
|
this->setupDisabledTexture(textureLoaded);
|
2015-04-14 14:23:10 +08:00
|
|
|
}
|
2014-07-30 15:19:39 +08:00
|
|
|
|
2015-11-26 13:49:32 +08:00
|
|
|
void Button::setupDisabledTexture(bool textureLoaded)
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
2015-09-14 14:27:51 +08:00
|
|
|
_disabledTextureSize = _buttonDisabledRenderer->getContentSize();
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-08-11 15:04:00 +08:00
|
|
|
this->updateChildrenDisplayedRGBA();
|
|
|
|
|
2015-11-26 13:49:32 +08:00
|
|
|
_disabledTextureLoaded = textureLoaded;
|
2014-04-17 14:08:25 +08:00
|
|
|
_disabledTextureAdaptDirty = true;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-04-14 14:23:10 +08:00
|
|
|
void Button::loadTextureDisabled(SpriteFrame* disabledSpriteFrame)
|
|
|
|
{
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->initWithSpriteFrame(disabledSpriteFrame);
|
2015-11-26 13:49:32 +08:00
|
|
|
this->setupDisabledTexture(nullptr != disabledSpriteFrame);
|
2015-04-14 14:23:10 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void Button::setCapInsets(const Rect &capInsets)
|
|
|
|
{
|
|
|
|
setCapInsetsNormalRenderer(capInsets);
|
|
|
|
setCapInsetsPressedRenderer(capInsets);
|
|
|
|
setCapInsetsDisabledRenderer(capInsets);
|
|
|
|
}
|
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void Button::setCapInsetsNormalRenderer(const Rect &capInsets)
|
|
|
|
{
|
2014-12-16 16:44:04 +08:00
|
|
|
_capInsetsNormal = Helper::restrictCapInsetRect(capInsets, this->_normalTextureSize);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
//for performance issue
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
_buttonNormalRenderer->setCapInsets(_capInsetsNormal);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsPressedRenderer(const Rect &capInsets)
|
|
|
|
{
|
2014-12-16 16:44:04 +08:00
|
|
|
_capInsetsPressed = Helper::restrictCapInsetRect(capInsets, this->_pressedTextureSize);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
//for performance issue
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
_buttonClickedRenderer->setCapInsets(_capInsetsPressed);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setCapInsetsDisabledRenderer(const Rect &capInsets)
|
|
|
|
{
|
2014-12-16 16:44:04 +08:00
|
|
|
_capInsetsDisabled = Helper::restrictCapInsetRect(capInsets, this->_disabledTextureSize);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
//for performance issue
|
2014-03-11 17:13:54 +08:00
|
|
|
if (!_scale9Enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->setCapInsets(_capInsetsDisabled);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
const Rect& Button::getCapInsetsNormalRenderer()const
|
|
|
|
{
|
|
|
|
return _capInsetsNormal;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
const Rect& Button::getCapInsetsPressedRenderer()const
|
|
|
|
{
|
|
|
|
return _capInsetsPressed;
|
|
|
|
}
|
2015-04-14 14:23:10 +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);
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->setVisible(false);
|
2014-12-23 15:03:31 +08:00
|
|
|
_buttonNormalRenderer->setState(Scale9Sprite::State::NORMAL);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_pressedTextureLoaded)
|
|
|
|
{
|
|
|
|
if (_pressedActionEnabled)
|
|
|
|
{
|
2014-09-13 22:08:02 +08:00
|
|
|
_buttonNormalRenderer->stopAllActions();
|
|
|
|
_buttonClickedRenderer->stopAllActions();
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2015-01-21 14:54:37 +08:00
|
|
|
// Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, _normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
|
|
|
//fixme: the zoomAction will run in the next frame which will cause the _buttonNormalRenderer to a wrong scale
|
2015-12-09 14:21:26 +08:00
|
|
|
_buttonNormalRenderer->setScale(1.0);
|
|
|
|
_buttonClickedRenderer->setScale(1.0);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
if (nullptr != _titleRenderer)
|
2014-10-27 11:16:25 +08:00
|
|
|
{
|
2015-04-14 14:23:10 +08:00
|
|
|
_titleRenderer->stopAllActions();
|
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
Action *zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
|
|
|
|
_titleRenderer->runAction(zoomTitleAction);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_titleRenderer->setScaleX(1.0f);
|
|
|
|
_titleRenderer->setScaleY(1.0f);
|
|
|
|
}
|
2014-10-27 11:16:25 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-23 15:03:31 +08:00
|
|
|
_buttonNormalRenderer->stopAllActions();
|
2015-12-09 14:21:26 +08:00
|
|
|
_buttonNormalRenderer->setScale(1.0);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
if (nullptr != _titleRenderer)
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
|
|
|
_titleRenderer->stopAllActions();
|
|
|
|
_titleRenderer->setScaleX(1.0f);
|
|
|
|
_titleRenderer->setScaleY(1.0f);
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToPressed()
|
|
|
|
{
|
2014-12-23 15:03:31 +08:00
|
|
|
_buttonNormalRenderer->setState(Scale9Sprite::State::NORMAL);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_pressedTextureLoaded)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(false);
|
|
|
|
_buttonClickedRenderer->setVisible(true);
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->setVisible(false);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (_pressedActionEnabled)
|
|
|
|
{
|
2014-09-13 22:08:02 +08:00
|
|
|
_buttonNormalRenderer->stopAllActions();
|
|
|
|
_buttonClickedRenderer->stopAllActions();
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP,
|
2016-07-04 22:12:45 +08:00
|
|
|
1.0f + _zoomScale,
|
|
|
|
1.0f + _zoomScale);
|
2014-09-13 22:08:02 +08:00
|
|
|
_buttonClickedRenderer->runAction(zoomAction);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2016-07-04 22:12:45 +08:00
|
|
|
_buttonNormalRenderer->setScale(1.0f + _zoomScale,
|
|
|
|
1.0f + _zoomScale);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
if (nullptr != _titleRenderer)
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
|
|
|
_titleRenderer->stopAllActions();
|
|
|
|
Action *zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP,
|
|
|
|
1.0f + _zoomScale, 1.0f + _zoomScale);
|
|
|
|
_titleRenderer->runAction(zoomTitleAction);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(true);
|
|
|
|
_buttonClickedRenderer->setVisible(true);
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->setVisible(false);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
_buttonNormalRenderer->stopAllActions();
|
2016-07-04 22:12:45 +08:00
|
|
|
_buttonNormalRenderer->setScale(1.0f +_zoomScale, 1.0f + _zoomScale);
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2016-11-24 09:59:00 +08:00
|
|
|
if (nullptr != _titleRenderer)
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
|
|
|
_titleRenderer->stopAllActions();
|
|
|
|
_titleRenderer->setScaleX(1.0f + _zoomScale);
|
|
|
|
_titleRenderer->setScaleY(1.0f + _zoomScale);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onPressStateChangedToDisabled()
|
|
|
|
{
|
2014-12-23 15:03:31 +08:00
|
|
|
//if disable resource is null
|
|
|
|
if (!_disabledTextureLoaded)
|
|
|
|
{
|
|
|
|
if (_normalTextureLoaded)
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setState(Scale9Sprite::State::GRAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_buttonNormalRenderer->setVisible(false);
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->setVisible(true);
|
2014-12-23 15:03:31 +08:00
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_buttonClickedRenderer->setVisible(false);
|
2015-12-09 14:21:26 +08:00
|
|
|
_buttonNormalRenderer->setScale(1.0);
|
|
|
|
_buttonClickedRenderer->setScale(1.0);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-14 14:23:10 +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
|
|
|
}
|
|
|
|
|
2014-10-11 12:13:04 +08:00
|
|
|
void Button::updateContentSize()
|
|
|
|
{
|
2014-10-24 17:38:32 +08:00
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
if (_scale9Enabled)
|
|
|
|
{
|
|
|
|
ProtectedNode::setContentSize(_customSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Size s = getNormalSize();
|
|
|
|
ProtectedNode::setContentSize(s);
|
|
|
|
}
|
|
|
|
onSizeChanged();
|
|
|
|
return;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
if (_ignoreSize)
|
|
|
|
{
|
2014-10-24 17:38:32 +08:00
|
|
|
this->setContentSize(getVirtualRendererSize());
|
|
|
|
}
|
2014-10-11 12:13:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void Button::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2016-11-24 09:59:00 +08:00
|
|
|
if (nullptr != _titleRenderer)
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
|
|
|
updateTitleLocation();
|
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_normalTextureAdaptDirty = true;
|
|
|
|
_pressedTextureAdaptDirty = true;
|
|
|
|
_disabledTextureAdaptDirty = true;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
void Button::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_normalTextureAdaptDirty)
|
|
|
|
{
|
|
|
|
normalTextureScaleChangedWithSize();
|
|
|
|
_normalTextureAdaptDirty = false;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
if (_pressedTextureAdaptDirty)
|
|
|
|
{
|
|
|
|
pressedTextureScaleChangedWithSize();
|
|
|
|
_pressedTextureAdaptDirty = false;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-04-17 14:08:25 +08:00
|
|
|
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-10-25 18:00:57 +08:00
|
|
|
if (_unifySize)
|
|
|
|
{
|
|
|
|
return this->getNormalSize();
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2015-05-18 18:33:48 +08:00
|
|
|
if (nullptr != _titleRenderer)
|
2014-12-16 16:44:04 +08:00
|
|
|
{
|
2015-04-14 14:23:10 +08:00
|
|
|
Size titleSize = _titleRenderer->getContentSize();
|
2020-08-18 14:29:09 +08:00
|
|
|
if (!_normalTextureLoaded && !_titleRenderer->getString().empty())
|
2015-04-14 14:23:10 +08:00
|
|
|
{
|
|
|
|
return titleSize;
|
|
|
|
}
|
2014-09-12 13:50:13 +08:00
|
|
|
}
|
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
|
|
|
|
{
|
2015-09-14 14:27:51 +08:00
|
|
|
return _buttonDisabledRenderer;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::normalTextureScaleChangedWithSize()
|
|
|
|
{
|
2015-12-09 14:21:26 +08:00
|
|
|
_buttonNormalRenderer->setPreferredSize(_contentSize);
|
2014-09-15 09:28:56 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2015-12-09 14:21:26 +08:00
|
|
|
_buttonClickedRenderer->setPreferredSize(_contentSize);
|
2014-10-24 17:38:32 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2015-12-09 14:21:26 +08:00
|
|
|
_buttonDisabledRenderer->setPreferredSize(_contentSize);
|
|
|
|
|
2015-09-14 14:27:51 +08:00
|
|
|
_buttonDisabledRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setPressedActionEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_pressedActionEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2015-05-18 18:33:48 +08:00
|
|
|
void Button::setTitleAlignment(TextHAlignment hAlignment)
|
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
createTitleRendererIfNull();
|
2015-05-18 18:33:48 +08:00
|
|
|
_titleRenderer->setAlignment(hAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment)
|
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
createTitleRendererIfNull();
|
2015-05-18 18:33:48 +08:00
|
|
|
_titleRenderer->setAlignment(hAlignment, vAlignment);
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void Button::setTitleText(const std::string& text)
|
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
if (text.compare(getTitleText()) == 0) {
|
2014-11-25 19:42:03 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-09-29 14:26:58 +08:00
|
|
|
|
|
|
|
createTitleRendererIfNull();
|
|
|
|
|
|
|
|
if(getTitleFontSize() <= 0) {
|
|
|
|
setTitleFontSize(CC_DEFAULT_FONT_LABEL_SIZE);
|
2015-04-14 14:23:10 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
_titleRenderer->setString(text);
|
2018-09-29 14:26:58 +08:00
|
|
|
|
2014-10-11 12:13:04 +08:00
|
|
|
updateContentSize();
|
2015-09-15 13:37:32 +08:00
|
|
|
updateTitleLocation();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2016-02-29 15:48:07 +08:00
|
|
|
std::string Button::getTitleText() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
if(!_titleRenderer) {
|
2015-04-14 14:23:10 +08:00
|
|
|
return "";
|
|
|
|
}
|
2018-09-29 14:26:58 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
return _titleRenderer->getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setTitleColor(const Color3B& color)
|
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
createTitleRendererIfNull();
|
2014-12-25 16:04:28 +08:00
|
|
|
_titleRenderer->setTextColor(Color4B(color));
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-12-25 16:04:28 +08:00
|
|
|
Color3B Button::getTitleColor() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-04-14 14:23:10 +08:00
|
|
|
if(nullptr == _titleRenderer)
|
|
|
|
{
|
|
|
|
return Color3B::WHITE;
|
|
|
|
}
|
2014-12-25 16:04:28 +08:00
|
|
|
return Color3B(_titleRenderer->getTextColor());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setTitleFontSize(float size)
|
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
createTitleRendererIfNull();
|
2015-05-25 15:45:26 +08:00
|
|
|
|
2018-09-29 14:26:58 +08:00
|
|
|
Label::LabelType titleLabelType = _titleRenderer->getLabelType();
|
|
|
|
if(titleLabelType == Label::LabelType::TTF) {
|
2014-06-15 15:55:46 +08:00
|
|
|
TTFConfig config = _titleRenderer->getTTFConfig();
|
2018-09-29 14:26:58 +08:00
|
|
|
config.fontSize = size;
|
2014-06-15 15:55:46 +08:00
|
|
|
_titleRenderer->setTTFConfig(config);
|
2018-09-29 14:26:58 +08:00
|
|
|
} else if (titleLabelType == Label::LabelType::STRING_TEXTURE) {
|
|
|
|
// the system font
|
|
|
|
_titleRenderer->setSystemFontSize(size);
|
2015-05-20 17:34:45 +08:00
|
|
|
}
|
2018-09-29 14:26:58 +08:00
|
|
|
|
2015-05-27 16:58:52 +08:00
|
|
|
//we can't change font size of BMFont.
|
2018-09-29 14:26:58 +08:00
|
|
|
if(titleLabelType != Label::LabelType::BMFONT) {
|
2015-05-27 16:58:52 +08:00
|
|
|
updateContentSize();
|
2015-05-25 15:45:26 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2018-09-29 14:26:58 +08:00
|
|
|
float Button::getTitleFontSize() const {
|
|
|
|
if(_titleRenderer) {
|
|
|
|
return _titleRenderer->getRenderingFontSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
void Button::setZoomScale(float scale)
|
|
|
|
{
|
|
|
|
_zoomScale = scale;
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
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
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
createTitleRendererIfNull();
|
|
|
|
|
|
|
|
if(FileUtils::getInstance()->isFileExist(fontName)) {
|
2015-05-20 17:34:45 +08:00
|
|
|
std::string lowerCasedFontName = fontName;
|
|
|
|
std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower);
|
2018-09-29 14:26:58 +08:00
|
|
|
if (lowerCasedFontName.find(".fnt") != std::string::npos) {
|
2015-05-20 16:56:07 +08:00
|
|
|
_titleRenderer->setBMFontFilePath(fontName);
|
2018-09-29 14:26:58 +08:00
|
|
|
} else {
|
2015-05-20 16:56:07 +08:00
|
|
|
TTFConfig config = _titleRenderer->getTTFConfig();
|
|
|
|
config.fontFilePath = fontName;
|
|
|
|
_titleRenderer->setTTFConfig(config);
|
|
|
|
}
|
2018-09-29 14:26:58 +08:00
|
|
|
} else {
|
2014-06-15 15:55:46 +08:00
|
|
|
_titleRenderer->setSystemFontName(fontName);
|
|
|
|
}
|
2015-11-18 13:38:29 +08:00
|
|
|
_fontName = fontName;
|
2018-09-29 14:26:58 +08:00
|
|
|
updateContentSize();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-09-17 10:57:40 +08:00
|
|
|
Label* Button::getTitleRenderer()const
|
|
|
|
{
|
|
|
|
return _titleRenderer;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2016-02-29 15:48:07 +08:00
|
|
|
std::string Button::getTitleFontName() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2018-09-29 14:26:58 +08:00
|
|
|
if (_titleRenderer) {
|
|
|
|
Label::LabelType titleLabelType = _titleRenderer->getLabelType();
|
|
|
|
if (titleLabelType == Label::LabelType::STRING_TEXTURE) {
|
2015-04-14 15:34:15 +08:00
|
|
|
return _titleRenderer->getSystemFontName();
|
2018-09-29 14:26:58 +08:00
|
|
|
} else if (titleLabelType == Label::LabelType::TTF) {
|
2015-04-14 15:34:15 +08:00
|
|
|
return _titleRenderer->getTTFConfig().fontFilePath;
|
2018-09-29 14:26:58 +08:00
|
|
|
} else if (titleLabelType == Label::LabelType::BMFONT) {
|
2015-05-20 16:56:07 +08:00
|
|
|
return _titleRenderer->getBMFontFilePath();
|
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
}
|
2018-09-29 14:26:58 +08:00
|
|
|
|
|
|
|
return "";
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
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);
|
2016-12-03 23:35:22 +08:00
|
|
|
|
|
|
|
// clone the inner sprite: https://github.com/cocos2d/cocos2d-x/issues/16924
|
|
|
|
button->_buttonNormalRenderer->copyTo(_buttonNormalRenderer);
|
|
|
|
_normalFileName = button->_normalFileName;
|
|
|
|
_normalTextureSize = button->_normalTextureSize;
|
|
|
|
_normalTexType = button->_normalTexType;
|
|
|
|
_normalTextureLoaded = button->_normalTextureLoaded;
|
|
|
|
setupNormalTexture(!_normalFileName.empty());
|
|
|
|
|
|
|
|
button->_buttonClickedRenderer->copyTo(_buttonClickedRenderer);
|
|
|
|
_clickedFileName = button->_clickedFileName;
|
|
|
|
_pressedTextureSize = button->_pressedTextureSize;
|
|
|
|
_pressedTexType = button->_pressedTexType;
|
|
|
|
_pressedTextureLoaded = button->_pressedTextureLoaded;
|
|
|
|
setupPressedTexture(!_clickedFileName.empty());
|
|
|
|
|
|
|
|
button->_buttonDisabledRenderer->copyTo(_buttonDisabledRenderer);
|
|
|
|
_disabledFileName = button->_disabledFileName;
|
|
|
|
_disabledTextureSize = button->_disabledTextureSize;
|
|
|
|
_disabledTexType = button->_disabledTexType;
|
|
|
|
_disabledTextureLoaded = button->_disabledTextureLoaded;
|
|
|
|
setupDisabledTexture(!_disabledFileName.empty());
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
setCapInsetsNormalRenderer(button->_capInsetsNormal);
|
|
|
|
setCapInsetsPressedRenderer(button->_capInsetsPressed);
|
|
|
|
setCapInsetsDisabledRenderer(button->_capInsetsDisabled);
|
2015-04-16 14:38:00 +08:00
|
|
|
if(nullptr != button->getTitleRenderer())
|
|
|
|
{
|
|
|
|
setTitleText(button->getTitleText());
|
|
|
|
setTitleFontName(button->getTitleFontName());
|
|
|
|
setTitleFontSize(button->getTitleFontSize());
|
|
|
|
setTitleColor(button->getTitleColor());
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
setPressedActionEnabled(button->_pressedActionEnabled);
|
2014-08-14 16:56:15 +08:00
|
|
|
setZoomScale(button->_zoomScale);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-04-14 14:23:10 +08:00
|
|
|
|
2014-10-24 17:38:32 +08:00
|
|
|
}
|
2014-10-25 18:00:57 +08:00
|
|
|
Size Button::getNormalSize() const
|
2014-10-24 17:38:32 +08:00
|
|
|
{
|
|
|
|
Size titleSize;
|
|
|
|
if (_titleRenderer != nullptr)
|
|
|
|
{
|
|
|
|
titleSize = _titleRenderer->getContentSize();
|
|
|
|
}
|
|
|
|
Size imageSize;
|
|
|
|
if (_buttonNormalRenderer != nullptr)
|
|
|
|
{
|
|
|
|
imageSize = _buttonNormalRenderer->getContentSize();
|
|
|
|
}
|
|
|
|
float width = titleSize.width > imageSize.width ? titleSize.width : imageSize.width;
|
|
|
|
float height = titleSize.height > imageSize.height ? titleSize.height : imageSize.height;
|
|
|
|
|
|
|
|
return Size(width,height);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-01-19 16:58:46 +08:00
|
|
|
Size Button::getNormalTextureSize() const
|
|
|
|
{
|
|
|
|
return _normalTextureSize;
|
|
|
|
}
|
2015-11-18 13:38:29 +08:00
|
|
|
|
|
|
|
void Button::resetNormalRender()
|
|
|
|
{
|
|
|
|
_normalFileName = "";
|
|
|
|
_normalTexType = TextureResType::LOCAL;
|
|
|
|
|
|
|
|
_normalTextureSize = Size(0, 0);
|
|
|
|
|
|
|
|
_normalTextureLoaded = false;
|
|
|
|
_normalTextureAdaptDirty = false;
|
|
|
|
|
|
|
|
_buttonNormalRenderer->resetRender();
|
|
|
|
}
|
|
|
|
void Button::resetPressedRender()
|
|
|
|
{
|
|
|
|
_clickedFileName = "";
|
|
|
|
_pressedTexType = TextureResType::LOCAL;
|
|
|
|
|
|
|
|
_pressedTextureSize = Size(0, 0);
|
|
|
|
|
|
|
|
_pressedTextureLoaded = false;
|
|
|
|
_pressedTextureAdaptDirty = false;
|
|
|
|
|
|
|
|
_buttonClickedRenderer->resetRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::resetDisabledRender()
|
|
|
|
{
|
|
|
|
_disabledFileName = "";
|
|
|
|
_disabledTexType = TextureResType::LOCAL;
|
|
|
|
|
|
|
|
_disabledTextureSize = Size(0, 0);
|
|
|
|
|
|
|
|
_disabledTextureLoaded = false;
|
|
|
|
_disabledTextureAdaptDirty = false;
|
|
|
|
|
|
|
|
_buttonDisabledRenderer->resetRender();
|
|
|
|
}
|
|
|
|
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData Button::getNormalFile()
|
2015-11-18 13:38:29 +08:00
|
|
|
{
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData rData;
|
2015-11-18 13:38:29 +08:00
|
|
|
rData.type = (int)_normalTexType;
|
|
|
|
rData.file = _normalFileName;
|
|
|
|
return rData;
|
|
|
|
}
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData Button::getPressedFile()
|
2015-11-18 13:38:29 +08:00
|
|
|
{
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData rData;
|
2015-11-18 13:38:29 +08:00
|
|
|
rData.type = (int)_pressedTexType;
|
|
|
|
rData.file = _clickedFileName;
|
|
|
|
return rData;
|
|
|
|
}
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData Button::getDisabledFile()
|
2015-11-18 13:38:29 +08:00
|
|
|
{
|
2015-12-05 00:26:07 +08:00
|
|
|
ResourceData rData;
|
2015-11-18 13:38:29 +08:00
|
|
|
rData.type = (int)_disabledTexType;
|
|
|
|
rData.file = _disabledFileName;
|
|
|
|
return rData;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|