Fix several bugs with button's title (#19073)

* Fix several bugs with button's title:
(a)when using setTitleLabel() the button didn't resolve the corresponded parameters;
(b)bug with measurement accuracy (ignores the fractional part) of the system font and ttf-fonts sizes;
(c)bug with the size of the button title (by default) did not match the value size of the label's typeface;
(d)removes high coupling the typeface parameters of button because it already contains the used label

* Hotfix to call parent `Widget::init()`
This commit is contained in:
gestern 2018-09-29 09:26:58 +03:00 committed by leda
parent ebc32043ea
commit 085ae2603f
4 changed files with 85 additions and 108 deletions

View File

@ -484,7 +484,7 @@ void Label::reset()
_bmFontPath = ""; _bmFontPath = "";
_systemFontDirty = false; _systemFontDirty = false;
_systemFont = "Helvetica"; _systemFont = "Helvetica";
_systemFontSize = 12; _systemFontSize = CC_DEFAULT_FONT_LABEL_SIZE;
if (_horizontalKernings) if (_horizontalKernings)
{ {

View File

@ -40,6 +40,7 @@ NS_CC_BEGIN
* @{ * @{
*/ */
#define CC_DEFAULT_FONT_LABEL_SIZE 12
/** /**
* @struct TTFConfig * @struct TTFConfig
@ -61,7 +62,7 @@ typedef struct _ttfConfig
bool underline; bool underline;
bool strikethrough; bool strikethrough;
_ttfConfig(const std::string& filePath = "",float size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC, _ttfConfig(const std::string& filePath = "",float size = CC_DEFAULT_FONT_LABEL_SIZE, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,
const char *customGlyphCollection = nullptr, bool useDistanceField = false, int outline = 0, const char *customGlyphCollection = nullptr, bool useDistanceField = false, int outline = 0,
bool useItalics = false, bool useBold = false, bool useUnderline = false, bool useStrikethrough = false) bool useItalics = false, bool useBold = false, bool useUnderline = false, bool useStrikethrough = false)
: fontFilePath(filePath) : fontFilePath(filePath)
@ -123,6 +124,14 @@ public:
*/ */
RESIZE_HEIGHT RESIZE_HEIGHT
}; };
enum class LabelType {
TTF,
BMFONT,
CHARMAP,
STRING_TEXTURE
};
/// @name Creators /// @name Creators
/// @{ /// @{
@ -545,6 +554,20 @@ public:
void setLineSpacing(float height); void setLineSpacing(float height);
float getLineSpacing() const; float getLineSpacing() const;
/**
Returns type of label
@warning Not support system font.
@return the type of label
@since v3.18.0
*/
LabelType getLabelType() const { return _currentLabelType; }
/**
Returns font size
*/
float getRenderingFontSize()const;
/** /**
* Sets the additional kerning of the Label. * Sets the additional kerning of the Label.
* *
@ -625,13 +648,6 @@ protected:
int lineIndex; int lineIndex;
}; };
enum class LabelType {
TTF,
BMFONT,
CHARMAP,
STRING_TEXTURE
};
virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false); virtual void setFontAtlas(FontAtlas* atlas, bool distanceFieldEnabled = false, bool useA8Shader = false);
bool getFontLetterDef(char32_t character, FontLetterDefinition& letterDef) const; bool getFontLetterDef(char32_t character, FontLetterDefinition& letterDef) const;
@ -647,7 +663,6 @@ protected:
void shrinkLabelToContentSize(const std::function<bool(void)>& lambda); void shrinkLabelToContentSize(const std::function<bool(void)>& lambda);
bool isHorizontalClamp(); bool isHorizontalClamp();
bool isVerticalClamp(); bool isVerticalClamp();
float getRenderingFontSize()const;
void rescaleWithOriginalFontSize(); void rescaleWithOriginalFontSize();
void updateLabelLetters(); void updateLabelLetters();

View File

@ -72,8 +72,6 @@ _disabledFileName(""),
_normalTexType(TextureResType::LOCAL), _normalTexType(TextureResType::LOCAL),
_pressedTexType(TextureResType::LOCAL), _pressedTexType(TextureResType::LOCAL),
_disabledTexType(TextureResType::LOCAL), _disabledTexType(TextureResType::LOCAL),
_fontSize(10),
_type(FontType::SYSTEM),
_fontName("") _fontName("")
{ {
setTouchEnabled(true); setTouchEnabled(true);
@ -115,18 +113,15 @@ bool Button::init(const std::string &normalImage,
const std::string& disableImage, const std::string& disableImage,
TextureResType texType) TextureResType texType)
{ {
bool ret = true;
do // invoke an overridden init() at first
{ if (!Widget::init()) {
if (!Widget::init()) return false;
{
ret = false;
break;
} }
this->loadTextures(normalImage, selectedImage, disableImage,texType); loadTextures(normalImage, selectedImage, disableImage, texType);
} while (0);
return ret; return true;
} }
bool Button::init() bool Button::init()
@ -152,6 +147,15 @@ void Button::initRenderer()
addProtectedChild(_buttonDisabledRenderer, DISABLED_RENDERER_Z, -1); addProtectedChild(_buttonDisabledRenderer, DISABLED_RENDERER_Z, -1);
} }
bool Button::createTitleRendererIfNull() {
if( !_titleRenderer ) {
createTitleRenderer();
return true;
}
return false;
}
void Button::createTitleRenderer() void Button::createTitleRenderer()
{ {
_titleRenderer = Label::create(); _titleRenderer = Label::create();
@ -702,53 +706,45 @@ void Button::setPressedActionEnabled(bool enabled)
void Button::setTitleAlignment(TextHAlignment hAlignment) void Button::setTitleAlignment(TextHAlignment hAlignment)
{ {
if (nullptr == _titleRenderer) createTitleRendererIfNull();
{
this->createTitleRenderer();
}
_titleRenderer->setAlignment(hAlignment); _titleRenderer->setAlignment(hAlignment);
} }
void Button::setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment) void Button::setTitleAlignment(TextHAlignment hAlignment, TextVAlignment vAlignment)
{ {
if (nullptr == _titleRenderer) createTitleRendererIfNull();
{
this->createTitleRenderer();
}
_titleRenderer->setAlignment(hAlignment, vAlignment); _titleRenderer->setAlignment(hAlignment, vAlignment);
} }
void Button::setTitleText(const std::string& text) void Button::setTitleText(const std::string& text)
{ {
if (text == getTitleText()) if (text.compare(getTitleText()) == 0) {
{
return; return;
} }
if(nullptr == _titleRenderer)
{ createTitleRendererIfNull();
this->createTitleRenderer();
if(getTitleFontSize() <= 0) {
setTitleFontSize(CC_DEFAULT_FONT_LABEL_SIZE);
} }
_titleRenderer->setString(text); _titleRenderer->setString(text);
this->setTitleFontSize(_fontSize);
updateContentSize(); updateContentSize();
updateTitleLocation(); updateTitleLocation();
} }
std::string Button::getTitleText() const std::string Button::getTitleText() const
{ {
if(nullptr == _titleRenderer) if(!_titleRenderer) {
{
return ""; return "";
} }
return _titleRenderer->getString(); return _titleRenderer->getString();
} }
void Button::setTitleColor(const Color3B& color) void Button::setTitleColor(const Color3B& color)
{ {
if(nullptr == _titleRenderer) createTitleRendererIfNull();
{
this->createTitleRenderer();
}
_titleRenderer->setTextColor(Color4B(color)); _titleRenderer->setTextColor(Color4B(color));
} }
@ -763,32 +759,30 @@ Color3B Button::getTitleColor() const
void Button::setTitleFontSize(float size) void Button::setTitleFontSize(float size)
{ {
if (nullptr == _titleRenderer) createTitleRendererIfNull();
{
this->createTitleRenderer(); Label::LabelType titleLabelType = _titleRenderer->getLabelType();
if(titleLabelType == Label::LabelType::TTF) {
TTFConfig config = _titleRenderer->getTTFConfig();
config.fontSize = size;
_titleRenderer->setTTFConfig(config);
} else if (titleLabelType == Label::LabelType::STRING_TEXTURE) {
// the system font
_titleRenderer->setSystemFontSize(size);
} }
_fontSize = size;
if (_type == FontType::SYSTEM)
{
_titleRenderer->setSystemFontSize(_fontSize);
}
else if (_type == FontType::TTF)
{
TTFConfig config = _titleRenderer->getTTFConfig();
config.fontSize = _fontSize;
_titleRenderer->setTTFConfig(config);
}
//we can't change font size of BMFont. //we can't change font size of BMFont.
if(FontType::BMFONT != _type) if(titleLabelType != Label::LabelType::BMFONT) {
{
updateContentSize(); updateContentSize();
} }
} }
float Button::getTitleFontSize() const float Button::getTitleFontSize() const {
{ if(_titleRenderer) {
return _fontSize; return _titleRenderer->getRenderingFontSize();
}
return -1;
} }
void Button::setZoomScale(float scale) void Button::setZoomScale(float scale)
@ -803,40 +797,23 @@ float Button::getZoomScale()const
void Button::setTitleFontName(const std::string& fontName) void Button::setTitleFontName(const std::string& fontName)
{ {
if(nullptr == _titleRenderer) createTitleRendererIfNull();
{
this->createTitleRenderer(); if(FileUtils::getInstance()->isFileExist(fontName)) {
}
if(FileUtils::getInstance()->isFileExist(fontName))
{
std::string lowerCasedFontName = fontName; std::string lowerCasedFontName = fontName;
std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower); std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower);
if (lowerCasedFontName.find(".fnt") != std::string::npos) if (lowerCasedFontName.find(".fnt") != std::string::npos) {
{
_titleRenderer->setBMFontFilePath(fontName); _titleRenderer->setBMFontFilePath(fontName);
_type = FontType::BMFONT; } else {
}
else
{
TTFConfig config = _titleRenderer->getTTFConfig(); TTFConfig config = _titleRenderer->getTTFConfig();
config.fontFilePath = fontName; config.fontFilePath = fontName;
config.fontSize = _fontSize;
_titleRenderer->setTTFConfig(config); _titleRenderer->setTTFConfig(config);
_type = FontType::TTF;
} }
} } else {
else
{
_titleRenderer->setSystemFontName(fontName); _titleRenderer->setSystemFontName(fontName);
if (_type == FontType::TTF)
{
_titleRenderer->requestSystemFontRefresh();
}
_titleRenderer->setSystemFontSize(_fontSize);
_type = FontType::SYSTEM;
} }
_fontName = fontName; _fontName = fontName;
this->updateContentSize(); updateContentSize();
} }
Label* Button::getTitleRenderer()const Label* Button::getTitleRenderer()const
@ -846,25 +823,18 @@ Label* Button::getTitleRenderer()const
std::string Button::getTitleFontName() const std::string Button::getTitleFontName() const
{ {
if (nullptr != _titleRenderer) if (_titleRenderer) {
{ Label::LabelType titleLabelType = _titleRenderer->getLabelType();
if (this->_type == FontType::SYSTEM) if (titleLabelType == Label::LabelType::STRING_TEXTURE) {
{
return _titleRenderer->getSystemFontName(); return _titleRenderer->getSystemFontName();
} } else if (titleLabelType == Label::LabelType::TTF) {
else if (this->_type == FontType::TTF)
{
return _titleRenderer->getTTFConfig().fontFilePath; return _titleRenderer->getTTFConfig().fontFilePath;
} } else if (titleLabelType == Label::LabelType::BMFONT) {
else
{
return _titleRenderer->getBMFontFilePath(); return _titleRenderer->getBMFontFilePath();
} }
} }
else
{ return "";
return _fontName;
}
} }
std::string Button::getDescription() const std::string Button::getDescription() const

View File

@ -343,7 +343,8 @@ protected:
virtual void adaptRenderers() override; virtual void adaptRenderers() override;
void updateTitleLocation(); void updateTitleLocation();
void updateContentSize(); void updateContentSize();
void createTitleRenderer(); virtual void createTitleRenderer();
bool createTitleRendererIfNull();
virtual Widget* createCloneInstance() override; virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override; virtual void copySpecialProperties(Widget* model) override;
@ -383,15 +384,6 @@ protected:
TextureResType _disabledTexType; TextureResType _disabledTexType;
private: private:
enum class FontType
{
SYSTEM,
TTF,
BMFONT
};
int _fontSize;
FontType _type;
std::string _fontName; std::string _fontName;
}; };