axmol/cocos/ui/UIButton.cpp

1003 lines
26 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2021 Bytedance Inc.
2019-11-23 20:27:39 +08:00
https://adxe.org
2019-11-23 20:27:39 +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"
#include "ui/UIScale9Sprite.h"
#include "2d/CCLabel.h"
#include "2d/CCSprite.h"
#include "2d/CCActionInterval.h"
#include "platform/CCFileUtils.h"
#include "ui/UIHelper.h"
#include <algorithm>
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
namespace ui
{
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +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);
2019-11-23 20:27:39 +08:00
static const float ZOOM_ACTION_TIME_STEP = 0.05f;
IMPLEMENT_CLASS_GUI_INFO(Button)
2021-12-25 10:04:45 +08:00
Button::Button()
: _buttonNormalRenderer(nullptr)
, _buttonClickedRenderer(nullptr)
, _buttonDisabledRenderer(nullptr)
, _titleRenderer(nullptr)
, _zoomScale(0.1f)
, _prevIgnoreSize(true)
, _scale9Enabled(false)
, _pressedActionEnabled(false)
, _capInsetsNormal(Rect::ZERO)
, _capInsetsPressed(Rect::ZERO)
, _capInsetsDisabled(Rect::ZERO)
, _normalTextureSize(_contentSize)
, _pressedTextureSize(_contentSize)
, _disabledTextureSize(_contentSize)
, _normalTextureLoaded(false)
, _pressedTextureLoaded(false)
, _disabledTextureLoaded(false)
, _normalTextureAdaptDirty(true)
, _pressedTextureAdaptDirty(true)
, _disabledTextureAdaptDirty(true)
, _normalFileName("")
, _clickedFileName("")
, _disabledFileName("")
, _normalTexType(TextureResType::LOCAL)
, _pressedTexType(TextureResType::LOCAL)
, _disabledTexType(TextureResType::LOCAL)
, _fontName("")
2019-11-23 20:27:39 +08:00
{
setTouchEnabled(true);
}
2021-12-25 10:04:45 +08:00
Button::~Button() {}
2019-11-23 20:27:39 +08:00
Button* Button::create()
{
2021-12-08 00:11:53 +08:00
Button* widget = new Button();
if (widget->init())
2019-11-23 20:27:39 +08:00
{
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
2021-12-26 23:26:34 +08:00
Button* Button::create(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disableImage,
2019-11-23 20:27:39 +08:00
TextureResType texType)
{
2021-12-25 10:04:45 +08:00
Button* btn = new Button;
if (btn->init(normalImage, selectedImage, disableImage, texType))
2019-11-23 20:27:39 +08:00
{
btn->autorelease();
return btn;
}
CC_SAFE_DELETE(btn);
return nullptr;
}
2021-12-26 23:26:34 +08:00
bool Button::init(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disableImage,
2019-11-23 20:27:39 +08:00
TextureResType texType)
{
// invoke an overridden init() at first
2021-12-25 10:04:45 +08:00
if (!Widget::init())
{
2019-11-23 20:27:39 +08:00
return false;
}
loadTextures(normalImage, selectedImage, disableImage, texType);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
bool Button::init()
{
if (Widget::init())
{
return true;
}
return false;
}
void Button::initRenderer()
{
2021-12-25 10:04:45 +08:00
_buttonNormalRenderer = Scale9Sprite::create();
_buttonClickedRenderer = Scale9Sprite::create();
2019-11-23 20:27:39 +08:00
_buttonDisabledRenderer = Scale9Sprite::create();
_buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
_buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
_buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
addProtectedChild(_buttonDisabledRenderer, DISABLED_RENDERER_Z, -1);
}
2021-12-25 10:04:45 +08:00
bool Button::createTitleRendererIfNull()
{
if (!_titleRenderer)
{
2019-11-23 20:27:39 +08:00
createTitleRenderer();
return true;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return false;
}
void Button::createTitleRenderer()
{
_titleRenderer = Label::create();
_titleRenderer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
addProtectedChild(_titleRenderer, TITLE_RENDERER_Z, -1);
}
/** replaces the current Label node with a new one */
void Button::setTitleLabel(Label* label)
{
2021-12-25 10:04:45 +08:00
if (_titleRenderer != label)
{
2019-11-23 20:27:39 +08:00
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;
}
void Button::setScale9Enabled(bool able)
{
if (_scale9Enabled == able)
{
return;
}
_scale9Enabled = able;
2021-12-25 10:04:45 +08:00
if (_scale9Enabled)
{
2019-11-23 20:27:39 +08:00
_buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
_buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
_buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
_buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
_buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
}
if (_scale9Enabled)
{
bool ignoreBefore = _ignoreSize;
ignoreContentAdaptWithSize(false);
_prevIgnoreSize = ignoreBefore;
}
else
{
ignoreContentAdaptWithSize(_prevIgnoreSize);
}
setCapInsetsNormalRenderer(_capInsetsNormal);
setCapInsetsPressedRenderer(_capInsetsPressed);
setCapInsetsDisabledRenderer(_capInsetsDisabled);
_brightStyle = BrightStyle::NONE;
setBright(_bright);
2021-12-25 10:04:45 +08:00
_normalTextureAdaptDirty = true;
_pressedTextureAdaptDirty = true;
2019-11-23 20:27:39 +08:00
_disabledTextureAdaptDirty = true;
}
2021-12-25 10:04:45 +08:00
bool Button::isScale9Enabled() const
2019-11-23 20:27:39 +08:00
{
return _scale9Enabled;
}
void Button::ignoreContentAdaptWithSize(bool ignore)
{
if (_unifySize)
{
this->updateContentSize();
return;
}
if (!_scale9Enabled || (_scale9Enabled && !ignore))
{
Widget::ignoreContentAdaptWithSize(ignore);
_prevIgnoreSize = ignore;
}
}
2021-12-26 23:26:34 +08:00
void Button::loadTextures(std::string_view normal,
std::string_view selected,
std::string_view disabled,
2019-11-23 20:27:39 +08:00
TextureResType texType)
{
2021-12-25 10:04:45 +08:00
loadTextureNormal(normal, texType);
loadTexturePressed(selected, texType);
loadTextureDisabled(disabled, texType);
2019-11-23 20:27:39 +08:00
}
2021-12-26 23:26:34 +08:00
void Button::loadTextureNormal(std::string_view normal, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_normalFileName = normal;
_normalTexType = texType;
2019-11-23 20:27:39 +08:00
bool textureLoaded = true;
if (normal.empty())
{
_buttonNormalRenderer->resetRender();
textureLoaded = false;
}
else
{
switch (texType)
{
case TextureResType::LOCAL:
_buttonNormalRenderer->initWithFile(normal);
break;
case TextureResType::PLIST:
_buttonNormalRenderer->initWithSpriteFrameName(normal);
break;
default:
break;
}
}
2021-12-25 10:04:45 +08:00
// FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
if (!_ignoreSize && _customSize.equals(Vec2::ZERO))
{
2019-11-23 20:27:39 +08:00
_customSize = _buttonNormalRenderer->getContentSize();
}
this->setupNormalTexture(textureLoaded);
}
void Button::setupNormalTexture(bool textureLoaded)
{
_normalTextureSize = _buttonNormalRenderer->getContentSize();
this->updateChildrenDisplayedRGBA();
2021-12-25 10:04:45 +08:00
if (_unifySize)
2019-11-23 20:27:39 +08:00
{
if (!_scale9Enabled)
{
updateContentSizeWithTextureSize(this->getNormalSize());
}
}
else
{
updateContentSizeWithTextureSize(_normalTextureSize);
}
2021-12-25 10:04:45 +08:00
_normalTextureLoaded = textureLoaded;
2019-11-23 20:27:39 +08:00
_normalTextureAdaptDirty = true;
}
void Button::loadTextureNormal(SpriteFrame* normalSpriteFrame)
{
_buttonNormalRenderer->initWithSpriteFrame(normalSpriteFrame);
this->setupNormalTexture(nullptr != normalSpriteFrame);
}
2021-12-26 23:26:34 +08:00
void Button::loadTexturePressed(std::string_view selected, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_clickedFileName = selected;
_pressedTexType = texType;
2019-11-23 20:27:39 +08:00
bool textureLoaded = true;
if (selected.empty())
{
_buttonClickedRenderer->resetRender();
textureLoaded = false;
}
else
{
switch (texType)
{
case TextureResType::LOCAL:
_buttonClickedRenderer->initWithFile(selected);
break;
case TextureResType::PLIST:
_buttonClickedRenderer->initWithSpriteFrameName(selected);
break;
default:
break;
}
}
this->setupPressedTexture(textureLoaded);
}
void Button::setupPressedTexture(bool textureLoaded)
{
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
this->updateChildrenDisplayedRGBA();
2021-12-25 10:04:45 +08:00
_pressedTextureLoaded = textureLoaded;
2019-11-23 20:27:39 +08:00
_pressedTextureAdaptDirty = true;
}
void Button::loadTexturePressed(SpriteFrame* pressedSpriteFrame)
{
_buttonClickedRenderer->initWithSpriteFrame(pressedSpriteFrame);
this->setupPressedTexture(nullptr != pressedSpriteFrame);
}
2021-12-26 23:26:34 +08:00
void Button::loadTextureDisabled(std::string_view disabled, TextureResType texType)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_disabledFileName = disabled;
_disabledTexType = texType;
2019-11-23 20:27:39 +08:00
bool textureLoaded = true;
if (disabled.empty())
{
_buttonDisabledRenderer->resetRender();
textureLoaded = false;
}
else
{
switch (texType)
{
case TextureResType::LOCAL:
_buttonDisabledRenderer->initWithFile(disabled);
break;
case TextureResType::PLIST:
_buttonDisabledRenderer->initWithSpriteFrameName(disabled);
break;
default:
break;
}
}
this->setupDisabledTexture(textureLoaded);
}
void Button::setupDisabledTexture(bool textureLoaded)
{
_disabledTextureSize = _buttonDisabledRenderer->getContentSize();
this->updateChildrenDisplayedRGBA();
2021-12-25 10:04:45 +08:00
_disabledTextureLoaded = textureLoaded;
2019-11-23 20:27:39 +08:00
_disabledTextureAdaptDirty = true;
}
void Button::loadTextureDisabled(SpriteFrame* disabledSpriteFrame)
{
_buttonDisabledRenderer->initWithSpriteFrame(disabledSpriteFrame);
this->setupDisabledTexture(nullptr != disabledSpriteFrame);
}
2021-12-25 10:04:45 +08:00
void Button::setCapInsets(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
setCapInsetsNormalRenderer(capInsets);
setCapInsetsPressedRenderer(capInsets);
setCapInsetsDisabledRenderer(capInsets);
}
2021-12-25 10:04:45 +08:00
void Button::setCapInsetsNormalRenderer(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
_capInsetsNormal = Helper::restrictCapInsetRect(capInsets, this->_normalTextureSize);
2021-12-25 10:04:45 +08:00
// for performance issue
2019-11-23 20:27:39 +08:00
if (!_scale9Enabled)
{
return;
}
_buttonNormalRenderer->setCapInsets(_capInsetsNormal);
}
2021-12-25 10:04:45 +08:00
void Button::setCapInsetsPressedRenderer(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
_capInsetsPressed = Helper::restrictCapInsetRect(capInsets, this->_pressedTextureSize);
2021-12-25 10:04:45 +08:00
// for performance issue
2019-11-23 20:27:39 +08:00
if (!_scale9Enabled)
{
return;
}
_buttonClickedRenderer->setCapInsets(_capInsetsPressed);
}
2021-12-25 10:04:45 +08:00
void Button::setCapInsetsDisabledRenderer(const Rect& capInsets)
2019-11-23 20:27:39 +08:00
{
_capInsetsDisabled = Helper::restrictCapInsetRect(capInsets, this->_disabledTextureSize);
2021-12-25 10:04:45 +08:00
// for performance issue
2019-11-23 20:27:39 +08:00
if (!_scale9Enabled)
{
return;
}
_buttonDisabledRenderer->setCapInsets(_capInsetsDisabled);
}
2021-12-25 10:04:45 +08:00
const Rect& Button::getCapInsetsNormalRenderer() const
2019-11-23 20:27:39 +08:00
{
return _capInsetsNormal;
}
2021-12-25 10:04:45 +08:00
const Rect& Button::getCapInsetsPressedRenderer() const
2019-11-23 20:27:39 +08:00
{
return _capInsetsPressed;
}
2021-12-25 10:04:45 +08:00
const Rect& Button::getCapInsetsDisabledRenderer() const
2019-11-23 20:27:39 +08:00
{
return _capInsetsDisabled;
}
void Button::onPressStateChangedToNormal()
{
_buttonNormalRenderer->setVisible(true);
_buttonClickedRenderer->setVisible(false);
_buttonDisabledRenderer->setVisible(false);
_buttonNormalRenderer->setState(Scale9Sprite::State::NORMAL);
if (_pressedTextureLoaded)
{
if (_pressedActionEnabled)
{
_buttonNormalRenderer->stopAllActions();
_buttonClickedRenderer->stopAllActions();
2021-12-25 10:04:45 +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
2019-11-23 20:27:39 +08:00
_buttonNormalRenderer->setScale(1.0);
_buttonClickedRenderer->setScale(1.0);
if (nullptr != _titleRenderer)
{
_titleRenderer->stopAllActions();
if (_unifySize)
{
2021-12-25 10:04:45 +08:00
Action* zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f, 1.0f);
2019-11-23 20:27:39 +08:00
_titleRenderer->runAction(zoomTitleAction);
}
else
{
_titleRenderer->setScaleX(1.0f);
_titleRenderer->setScaleY(1.0f);
}
}
}
}
else
{
_buttonNormalRenderer->stopAllActions();
_buttonNormalRenderer->setScale(1.0);
if (nullptr != _titleRenderer)
{
_titleRenderer->stopAllActions();
_titleRenderer->setScaleX(1.0f);
_titleRenderer->setScaleY(1.0f);
}
}
}
void Button::onPressStateChangedToPressed()
{
_buttonNormalRenderer->setState(Scale9Sprite::State::NORMAL);
if (_pressedTextureLoaded)
{
_buttonNormalRenderer->setVisible(false);
_buttonClickedRenderer->setVisible(true);
_buttonDisabledRenderer->setVisible(false);
if (_pressedActionEnabled)
{
_buttonNormalRenderer->stopAllActions();
_buttonClickedRenderer->stopAllActions();
2021-12-25 10:04:45 +08:00
Action* zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f + _zoomScale, 1.0f + _zoomScale);
2019-11-23 20:27:39 +08:00
_buttonClickedRenderer->runAction(zoomAction);
2021-12-25 10:04:45 +08:00
_buttonNormalRenderer->setScale(1.0f + _zoomScale, 1.0f + _zoomScale);
2019-11-23 20:27:39 +08:00
if (nullptr != _titleRenderer)
{
_titleRenderer->stopAllActions();
2021-12-25 10:04:45 +08:00
Action* zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0f + _zoomScale, 1.0f + _zoomScale);
2019-11-23 20:27:39 +08:00
_titleRenderer->runAction(zoomTitleAction);
}
}
}
else
{
_buttonNormalRenderer->setVisible(true);
_buttonClickedRenderer->setVisible(true);
_buttonDisabledRenderer->setVisible(false);
_buttonNormalRenderer->stopAllActions();
2021-12-25 10:04:45 +08:00
_buttonNormalRenderer->setScale(1.0f + _zoomScale, 1.0f + _zoomScale);
2019-11-23 20:27:39 +08:00
if (nullptr != _titleRenderer)
{
_titleRenderer->stopAllActions();
_titleRenderer->setScaleX(1.0f + _zoomScale);
_titleRenderer->setScaleY(1.0f + _zoomScale);
}
}
}
void Button::onPressStateChangedToDisabled()
{
2021-12-25 10:04:45 +08:00
// if disable resource is null
2019-11-23 20:27:39 +08:00
if (!_disabledTextureLoaded)
{
if (_normalTextureLoaded)
{
_buttonNormalRenderer->setState(Scale9Sprite::State::GRAY);
}
}
else
{
_buttonNormalRenderer->setVisible(false);
_buttonDisabledRenderer->setVisible(true);
}
_buttonClickedRenderer->setVisible(false);
_buttonNormalRenderer->setScale(1.0);
_buttonClickedRenderer->setScale(1.0);
}
void Button::updateTitleLocation()
{
_titleRenderer->setPosition(_contentSize.width * 0.5f, _contentSize.height * 0.5f);
}
void Button::updateContentSize()
{
if (_unifySize)
{
if (_scale9Enabled)
{
ProtectedNode::setContentSize(_customSize);
}
else
{
2021-10-23 23:27:14 +08:00
Vec2 s = getNormalSize();
2019-11-23 20:27:39 +08:00
ProtectedNode::setContentSize(s);
}
onSizeChanged();
return;
}
if (_ignoreSize)
{
this->setContentSize(getVirtualRendererSize());
}
}
void Button::onSizeChanged()
{
Widget::onSizeChanged();
if (nullptr != _titleRenderer)
{
updateTitleLocation();
}
2021-12-25 10:04:45 +08:00
_normalTextureAdaptDirty = true;
_pressedTextureAdaptDirty = true;
2019-11-23 20:27:39 +08:00
_disabledTextureAdaptDirty = true;
}
void Button::adaptRenderers()
{
if (_normalTextureAdaptDirty)
{
normalTextureScaleChangedWithSize();
_normalTextureAdaptDirty = false;
}
if (_pressedTextureAdaptDirty)
{
pressedTextureScaleChangedWithSize();
_pressedTextureAdaptDirty = false;
}
if (_disabledTextureAdaptDirty)
{
disabledTextureScaleChangedWithSize();
_disabledTextureAdaptDirty = false;
}
}
2021-10-23 23:27:14 +08:00
Vec2 Button::getVirtualRendererSize() const
2019-11-23 20:27:39 +08:00
{
if (_unifySize)
{
return this->getNormalSize();
}
if (nullptr != _titleRenderer)
{
2021-10-23 23:27:14 +08:00
Vec2 titleSize = _titleRenderer->getContentSize();
2020-08-18 14:29:09 +08:00
if (!_normalTextureLoaded && !_titleRenderer->getString().empty())
2019-11-23 20:27:39 +08:00
{
return titleSize;
}
}
return _normalTextureSize;
}
Node* Button::getVirtualRenderer()
{
if (_bright)
{
switch (_brightStyle)
{
2021-12-25 10:04:45 +08:00
case BrightStyle::NORMAL:
return _buttonNormalRenderer;
case BrightStyle::HIGHLIGHT:
return _buttonClickedRenderer;
default:
return nullptr;
2019-11-23 20:27:39 +08:00
}
}
else
{
return _buttonDisabledRenderer;
}
}
void Button::normalTextureScaleChangedWithSize()
{
_buttonNormalRenderer->setPreferredSize(_contentSize);
_buttonNormalRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
}
void Button::pressedTextureScaleChangedWithSize()
{
_buttonClickedRenderer->setPreferredSize(_contentSize);
_buttonClickedRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
}
void Button::disabledTextureScaleChangedWithSize()
{
_buttonDisabledRenderer->setPreferredSize(_contentSize);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_buttonDisabledRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
}
void Button::setPressedActionEnabled(bool enabled)
{
_pressedActionEnabled = enabled;
}
void Button::setTitleAlignment(TextHAlignment hAlignment)
{
createTitleRendererIfNull();
_titleRenderer->setAlignment(hAlignment);
}
void Button::setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment)
{
createTitleRendererIfNull();
_titleRenderer->setAlignment(hAlignment, vAlignment);
}
2021-12-26 23:26:34 +08:00
void Button::setTitleText(std::string_view text)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (text.compare(getTitleText()) == 0)
{
2019-11-23 20:27:39 +08:00
return;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
createTitleRendererIfNull();
2021-12-25 10:04:45 +08:00
if (getTitleFontSize() <= 0)
{
2019-11-23 20:27:39 +08:00
setTitleFontSize(CC_DEFAULT_FONT_LABEL_SIZE);
}
_titleRenderer->setString(text);
updateContentSize();
updateTitleLocation();
}
2021-12-26 23:26:34 +08:00
std::string_view Button::getTitleText() const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (!_titleRenderer)
{
2019-11-23 20:27:39 +08:00
return "";
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return _titleRenderer->getString();
}
void Button::setTitleColor(const Color3B& color)
{
createTitleRendererIfNull();
_titleRenderer->setTextColor(Color4B(color));
}
Color3B Button::getTitleColor() const
{
2021-12-25 10:04:45 +08:00
if (nullptr == _titleRenderer)
2019-11-23 20:27:39 +08:00
{
return Color3B::WHITE;
}
return Color3B(_titleRenderer->getTextColor());
}
void Button::setTitleFontSize(float size)
{
createTitleRendererIfNull();
Label::LabelType titleLabelType = _titleRenderer->getLabelType();
2021-12-25 10:04:45 +08:00
if (titleLabelType == Label::LabelType::TTF)
{
2019-11-23 20:27:39 +08:00
TTFConfig config = _titleRenderer->getTTFConfig();
2021-12-25 10:04:45 +08:00
config.fontSize = size;
2019-11-23 20:27:39 +08:00
_titleRenderer->setTTFConfig(config);
2021-12-25 10:04:45 +08:00
}
else if (titleLabelType == Label::LabelType::STRING_TEXTURE)
{
2019-11-23 20:27:39 +08:00
// the system font
_titleRenderer->setSystemFontSize(size);
}
2021-12-25 10:04:45 +08:00
// we can't change font size of BMFont.
if (titleLabelType != Label::LabelType::BMFONT)
{
2019-11-23 20:27:39 +08:00
updateContentSize();
}
}
2021-12-25 10:04:45 +08:00
float Button::getTitleFontSize() const
{
if (_titleRenderer)
{
2019-11-23 20:27:39 +08:00
return _titleRenderer->getRenderingFontSize();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return -1;
}
void Button::setZoomScale(float scale)
{
_zoomScale = scale;
}
2021-12-25 10:04:45 +08:00
float Button::getZoomScale() const
2019-11-23 20:27:39 +08:00
{
return _zoomScale;
}
2021-12-26 23:26:34 +08:00
void Button::setTitleFontName(std::string_view fontName)
2019-11-23 20:27:39 +08:00
{
createTitleRendererIfNull();
2021-12-25 10:04:45 +08:00
if (FileUtils::getInstance()->isFileExist(fontName))
{
2021-12-26 23:26:34 +08:00
std::string lowerCasedFontName{fontName};
2019-11-23 20:27:39 +08:00
std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower);
2021-12-25 10:04:45 +08:00
if (lowerCasedFontName.find(".fnt") != std::string::npos)
{
2019-11-23 20:27:39 +08:00
_titleRenderer->setBMFontFilePath(fontName);
2021-12-25 10:04:45 +08:00
}
else
{
TTFConfig config = _titleRenderer->getTTFConfig();
2019-11-23 20:27:39 +08:00
config.fontFilePath = fontName;
_titleRenderer->setTTFConfig(config);
}
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_titleRenderer->setSystemFontName(fontName);
}
_fontName = fontName;
updateContentSize();
}
2021-12-25 10:04:45 +08:00
Label* Button::getTitleRenderer() const
2019-11-23 20:27:39 +08:00
{
return _titleRenderer;
}
2021-12-26 23:26:34 +08:00
std::string_view Button::getTitleFontName() const
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_titleRenderer)
{
2019-11-23 20:27:39 +08:00
Label::LabelType titleLabelType = _titleRenderer->getLabelType();
2021-12-25 10:04:45 +08:00
if (titleLabelType == Label::LabelType::STRING_TEXTURE)
{
2019-11-23 20:27:39 +08:00
return _titleRenderer->getSystemFontName();
2021-12-25 10:04:45 +08:00
}
else if (titleLabelType == Label::LabelType::TTF)
{
return _titleRenderer->getTTFConfig().fontFilePath;
}
else if (titleLabelType == Label::LabelType::BMFONT)
{
2019-11-23 20:27:39 +08:00
return _titleRenderer->getBMFontFilePath();
}
}
2021-12-25 10:04:45 +08:00
2021-12-26 23:26:34 +08:00
return ""sv;
2019-11-23 20:27:39 +08:00
}
std::string Button::getDescription() const
{
return "Button";
}
Widget* Button::createCloneInstance()
{
return Button::create();
}
2021-12-25 10:04:45 +08:00
void Button::copySpecialProperties(Widget* widget)
2019-11-23 20:27:39 +08:00
{
Button* button = dynamic_cast<Button*>(widget);
if (button)
{
_prevIgnoreSize = button->_prevIgnoreSize;
setScale9Enabled(button->_scale9Enabled);
// clone the inner sprite: https://github.com/cocos2d/cocos2d-x/issues/16924
button->_buttonNormalRenderer->copyTo(_buttonNormalRenderer);
2021-12-25 10:04:45 +08:00
_normalFileName = button->_normalFileName;
_normalTextureSize = button->_normalTextureSize;
_normalTexType = button->_normalTexType;
2019-11-23 20:27:39 +08:00
_normalTextureLoaded = button->_normalTextureLoaded;
setupNormalTexture(!_normalFileName.empty());
button->_buttonClickedRenderer->copyTo(_buttonClickedRenderer);
2021-12-25 10:04:45 +08:00
_clickedFileName = button->_clickedFileName;
_pressedTextureSize = button->_pressedTextureSize;
_pressedTexType = button->_pressedTexType;
2019-11-23 20:27:39 +08:00
_pressedTextureLoaded = button->_pressedTextureLoaded;
setupPressedTexture(!_clickedFileName.empty());
button->_buttonDisabledRenderer->copyTo(_buttonDisabledRenderer);
2021-12-25 10:04:45 +08:00
_disabledFileName = button->_disabledFileName;
_disabledTextureSize = button->_disabledTextureSize;
_disabledTexType = button->_disabledTexType;
2019-11-23 20:27:39 +08:00
_disabledTextureLoaded = button->_disabledTextureLoaded;
setupDisabledTexture(!_disabledFileName.empty());
setCapInsetsNormalRenderer(button->_capInsetsNormal);
setCapInsetsPressedRenderer(button->_capInsetsPressed);
setCapInsetsDisabledRenderer(button->_capInsetsDisabled);
2021-12-25 10:04:45 +08:00
if (nullptr != button->getTitleRenderer())
2019-11-23 20:27:39 +08:00
{
setTitleText(button->getTitleText());
setTitleFontName(button->getTitleFontName());
setTitleFontSize(button->getTitleFontSize());
setTitleColor(button->getTitleColor());
}
setPressedActionEnabled(button->_pressedActionEnabled);
setZoomScale(button->_zoomScale);
}
}
2021-10-23 23:27:14 +08:00
Vec2 Button::getNormalSize() const
2019-11-23 20:27:39 +08:00
{
2021-10-23 23:27:14 +08:00
Vec2 titleSize;
2019-11-23 20:27:39 +08:00
if (_titleRenderer != nullptr)
{
titleSize = _titleRenderer->getContentSize();
}
2021-10-23 23:27:14 +08:00
Vec2 imageSize;
2019-11-23 20:27:39 +08:00
if (_buttonNormalRenderer != nullptr)
{
imageSize = _buttonNormalRenderer->getContentSize();
}
2021-12-25 10:04:45 +08:00
float width = titleSize.width > imageSize.width ? titleSize.width : imageSize.width;
2019-11-23 20:27:39 +08:00
float height = titleSize.height > imageSize.height ? titleSize.height : imageSize.height;
2021-12-25 10:04:45 +08:00
return Vec2(width, height);
2019-11-23 20:27:39 +08:00
}
2021-10-23 23:27:14 +08:00
Vec2 Button::getNormalTextureSize() const
2019-11-23 20:27:39 +08:00
{
return _normalTextureSize;
}
void Button::resetNormalRender()
{
_normalFileName = "";
2021-12-25 10:04:45 +08:00
_normalTexType = TextureResType::LOCAL;
2019-11-23 20:27:39 +08:00
2021-10-23 23:27:14 +08:00
_normalTextureSize = Vec2(0, 0);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_normalTextureLoaded = false;
2019-11-23 20:27:39 +08:00
_normalTextureAdaptDirty = false;
_buttonNormalRenderer->resetRender();
}
void Button::resetPressedRender()
{
_clickedFileName = "";
2021-12-25 10:04:45 +08:00
_pressedTexType = TextureResType::LOCAL;
2019-11-23 20:27:39 +08:00
2021-10-23 23:27:14 +08:00
_pressedTextureSize = Vec2(0, 0);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_pressedTextureLoaded = false;
2019-11-23 20:27:39 +08:00
_pressedTextureAdaptDirty = false;
_buttonClickedRenderer->resetRender();
}
void Button::resetDisabledRender()
{
_disabledFileName = "";
2021-12-25 10:04:45 +08:00
_disabledTexType = TextureResType::LOCAL;
2019-11-23 20:27:39 +08:00
2021-10-23 23:27:14 +08:00
_disabledTextureSize = Vec2(0, 0);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_disabledTextureLoaded = false;
2019-11-23 20:27:39 +08:00
_disabledTextureAdaptDirty = false;
_buttonDisabledRenderer->resetRender();
}
ResourceData Button::getNormalFile()
{
ResourceData rData;
rData.type = (int)_normalTexType;
rData.file = _normalFileName;
return rData;
}
ResourceData Button::getPressedFile()
{
ResourceData rData;
rData.type = (int)_pressedTexType;
rData.file = _clickedFileName;
return rData;
}
ResourceData Button::getDisabledFile()
{
ResourceData rData;
rData.type = (int)_disabledTexType;
rData.file = _disabledFileName;
return rData;
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_CC_END