2020-08-04 12:31:33 +08:00
|
|
|
#include "FUILabel.h"
|
|
|
|
#include "BitmapFont.h"
|
|
|
|
#include "UIConfig.h"
|
|
|
|
#include "UIPackage.h"
|
|
|
|
|
|
|
|
NS_FGUI_BEGIN
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2020-08-04 12:31:33 +08:00
|
|
|
|
|
|
|
static Color3B toGrayed(const Color3B& source)
|
|
|
|
{
|
|
|
|
Color3B c = source;
|
|
|
|
c.r = c.g = c.b = c.r * 0.299f + c.g * 0.587f + c.b * 0.114f;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
FUILabel::FUILabel() : _fontSize(-1),
|
2020-09-08 19:15:28 +08:00
|
|
|
_bmFontCanTint(false),
|
|
|
|
_textFormat(new TextFormat()),
|
|
|
|
_grayed(false)
|
2020-08-04 12:31:33 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FUILabel::~FUILabel()
|
|
|
|
{
|
|
|
|
delete _textFormat;
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:35:34 +08:00
|
|
|
void FUILabel::setText(std::string_view value)
|
2020-08-04 12:31:33 +08:00
|
|
|
{
|
|
|
|
if (_fontSize < 0)
|
|
|
|
applyTextFormat();
|
|
|
|
|
|
|
|
setString(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUILabel::applyTextFormat()
|
|
|
|
{
|
|
|
|
if (_fontSize < 0 /**first time**/ || _fontName != _textFormat->face)
|
|
|
|
{
|
|
|
|
_fontName = _textFormat->face;
|
|
|
|
Label::LabelType oldType = _currentLabelType;
|
|
|
|
|
|
|
|
if (_fontName.find("ui://") != -1)
|
|
|
|
{
|
|
|
|
setBMFontFilePath(_fontName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool ttf = false;
|
|
|
|
const std::string& fontName = UIConfig::getRealFontName(_fontName, &ttf);
|
|
|
|
if (ttf)
|
|
|
|
{
|
|
|
|
_fontConfig.fontFilePath = fontName;
|
|
|
|
_fontConfig.fontSize = _textFormat->fontSize;
|
|
|
|
setTTFConfig(_fontConfig);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setSystemFontName(fontName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldType == LabelType::BMFONT)
|
|
|
|
setTextColor((Color4B)_textFormat->color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fontSize != _textFormat->fontSize)
|
|
|
|
{
|
|
|
|
_fontSize = _textFormat->fontSize;
|
|
|
|
if (_currentLabelType == LabelType::STRING_TEXTURE)
|
|
|
|
{
|
|
|
|
setSystemFontSize(_fontSize);
|
|
|
|
}
|
|
|
|
else if (_currentLabelType == LabelType::BMFONT)
|
|
|
|
{
|
|
|
|
setBMFontSize(_fontSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_fontConfig.fontSize = _fontSize;
|
|
|
|
setTTFConfig(_fontConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 19:15:28 +08:00
|
|
|
if (_currentLabelType != LabelType::BMFONT || _bmFontCanTint)
|
|
|
|
{
|
|
|
|
//setTextColor((Color4B)(_grayed ? toGrayed(_textFormat->color) : _textFormat->color));
|
2020-08-04 12:31:33 +08:00
|
|
|
setColor(_grayed ? toGrayed(_textFormat->color) : _textFormat->color);
|
2020-09-08 19:15:28 +08:00
|
|
|
}
|
2020-08-04 12:31:33 +08:00
|
|
|
|
|
|
|
if (_textFormat->underline)
|
|
|
|
enableUnderline();
|
|
|
|
else
|
|
|
|
disableEffect(LabelEffect::UNDERLINE);
|
|
|
|
|
|
|
|
if (_textFormat->italics)
|
|
|
|
enableItalics();
|
2020-09-08 19:15:28 +08:00
|
|
|
//else //Cant call this, cocos will do setRotationSkew(0)!
|
|
|
|
// disableEffect(LabelEffect::ITALICS);
|
2020-08-04 12:31:33 +08:00
|
|
|
|
|
|
|
if (_textFormat->bold && _currentLabelType != LabelType::STRING_TEXTURE)
|
|
|
|
enableBold();
|
|
|
|
else
|
|
|
|
disableEffect(LabelEffect::BOLD);
|
|
|
|
|
|
|
|
setLineSpacing(_textFormat->lineSpacing);
|
|
|
|
setHorizontalAlignment(_textFormat->align);
|
|
|
|
setVerticalAlignment(_textFormat->verticalAlign);
|
|
|
|
|
|
|
|
if (_textFormat->hasEffect(TextFormat::OUTLINE))
|
|
|
|
enableOutline((Color4B)(_grayed ? toGrayed(_textFormat->outlineColor) : _textFormat->outlineColor), _textFormat->outlineSize);
|
|
|
|
else
|
|
|
|
disableEffect(LabelEffect::OUTLINE);
|
|
|
|
|
|
|
|
if (_textFormat->hasEffect(TextFormat::SHADOW))
|
|
|
|
enableShadow((Color4B)(_grayed ? toGrayed(_textFormat->shadowColor) : _textFormat->shadowColor), _textFormat->shadowOffset);
|
|
|
|
else if (!_textFormat->bold)
|
|
|
|
disableEffect(LabelEffect::SHADOW);
|
|
|
|
}
|
|
|
|
|
2021-12-27 15:10:29 +08:00
|
|
|
bool FUILabel::setBMFontFilePath(std::string_view bmfontFilePath, const Vec2& imageOffset, float fontSize)
|
2020-08-04 12:31:33 +08:00
|
|
|
{
|
|
|
|
BitmapFont* bmFont = (BitmapFont*)UIPackage::getItemAssetByURL(bmfontFilePath, PackageItemType::FONT);
|
|
|
|
if (bmFont == nullptr)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//assign the default fontSize
|
|
|
|
if (std::abs(fontSize) < FLT_EPSILON)
|
|
|
|
{
|
|
|
|
float originalFontSize = bmFont->getOriginalFontSize();
|
|
|
|
_bmFontSize = originalFontSize / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fontSize > 0.0f && bmFont->isResizable())
|
|
|
|
{
|
|
|
|
_bmFontSize = fontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
_bmFontPath = bmfontFilePath;
|
|
|
|
_bmFontCanTint = bmFont->canTint();
|
|
|
|
|
|
|
|
_currentLabelType = LabelType::BMFONT;
|
2021-11-11 18:04:38 +08:00
|
|
|
setFontAtlas(bmFont->getFontAtlas());
|
2020-08-04 12:31:33 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUILabel::setGrayed(bool value)
|
|
|
|
{
|
|
|
|
if (_grayed != value)
|
|
|
|
{
|
|
|
|
_grayed = value;
|
|
|
|
|
|
|
|
if (_currentLabelType != LabelType::BMFONT)
|
|
|
|
setTextColor((Color4B)(_grayed ? toGrayed(_textFormat->color) : _textFormat->color));
|
|
|
|
else if (_bmFontCanTint)
|
|
|
|
setColor(_grayed ? toGrayed(_textFormat->color) : _textFormat->color);
|
|
|
|
|
|
|
|
if (_textFormat->hasEffect(TextFormat::OUTLINE))
|
|
|
|
enableOutline((Color4B)(_grayed ? toGrayed(_textFormat->outlineColor) : _textFormat->outlineColor), _textFormat->outlineSize);
|
|
|
|
|
|
|
|
if (_textFormat->hasEffect(TextFormat::SHADOW))
|
|
|
|
enableShadow((Color4B)(_grayed ? toGrayed(_textFormat->shadowColor) : _textFormat->shadowColor), _textFormat->shadowOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FUILabel::updateBMFontScale()
|
|
|
|
{
|
|
|
|
auto font = _fontAtlas->getFont();
|
|
|
|
if (_currentLabelType == LabelType::BMFONT)
|
|
|
|
{
|
|
|
|
BitmapFont* bmFont = (BitmapFont*)font;
|
|
|
|
float originalFontSize = bmFont->getOriginalFontSize();
|
|
|
|
_bmfontScale = _bmFontSize * CC_CONTENT_SCALE_FACTOR() / originalFontSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_bmfontScale = 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void FUILabel::setUnderlineColor(const axis::Color3B& value)
|
2020-09-08 19:15:28 +08:00
|
|
|
{
|
|
|
|
//NOT IMPLEMENTED
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:31:33 +08:00
|
|
|
NS_FGUI_END
|