|
50 | 50 | #include "axmol/renderer/Shaders.h" |
51 | 51 | #include "axmol/rhi/ProgramState.h" |
52 | 52 | #include "axmol/renderer/ProgramStateRegistry.h" |
| 53 | +#include "yasio/tlx/string_view.hpp" |
53 | 54 |
|
54 | 55 | namespace ax |
55 | 56 | { |
@@ -227,11 +228,31 @@ std::array<CustomCommand*, 3> Label::BatchCommand::getCommandArray() |
227 | 228 |
|
228 | 229 | Label* Label::create() |
229 | 230 | { |
230 | | - auto ret = new Label; |
| 231 | + auto ret = new Label(); |
231 | 232 | ret->autorelease(); |
232 | 233 | return ret; |
233 | 234 | } |
234 | 235 |
|
| 236 | +Label* Label::create(std::string_view text, std::string_view fontName, float fontSize) |
| 237 | +{ |
| 238 | + if (FileUtils::getInstance()->isFileExist(fontName)) |
| 239 | + { |
| 240 | + if (tlx::ic::ends_with(fontName, ".fnt")) |
| 241 | + { |
| 242 | + return Label::createWithBMFont(fontName, text); |
| 243 | + } |
| 244 | + else |
| 245 | + { |
| 246 | + TTFConfig config(fontName, fontSize); |
| 247 | + return Label::createWithTTF(config, text); |
| 248 | + } |
| 249 | + } |
| 250 | + else |
| 251 | + { |
| 252 | + return Label::createWithSystemFont(text, fontName, fontSize); |
| 253 | + } |
| 254 | +} |
| 255 | + |
235 | 256 | Label* Label::createWithSystemFont(std::string_view text, |
236 | 257 | std::string_view font, |
237 | 258 | float fontSize, |
@@ -392,6 +413,31 @@ Label* Label::createWithCharMap(std::string_view charMapFile, int itemWidth, int |
392 | 413 | return nullptr; |
393 | 414 | } |
394 | 415 |
|
| 416 | +void Label::setFontInfo(std::string_view fontName, float fontSize) |
| 417 | +{ |
| 418 | + auto prevLableType = _currentLabelType; |
| 419 | + if (FileUtils::getInstance()->isFileExist(fontName)) |
| 420 | + { |
| 421 | + if (tlx::ic::ends_with(fontName, ".fnt"sv)) |
| 422 | + { |
| 423 | + setBMFontFilePath(fontName); |
| 424 | + } |
| 425 | + else |
| 426 | + { |
| 427 | + TTFConfig ttfConfig(fontName, fontSize, GlyphCollection::DYNAMIC); |
| 428 | + setTTFConfig(ttfConfig); |
| 429 | + } |
| 430 | + } |
| 431 | + else |
| 432 | + { |
| 433 | + setSystemFontName(fontName); |
| 434 | + setSystemFontSize(fontSize); |
| 435 | + |
| 436 | + if (prevLableType == LabelType::STRING_TEXTURE) |
| 437 | + requestSystemFontRefresh(); |
| 438 | + } |
| 439 | +} |
| 440 | + |
395 | 441 | bool Label::setCharMap(std::string_view plistFile) |
396 | 442 | { |
397 | 443 | auto newAtlas = FontAtlasCache::getFontAtlasCharMap(plistFile); |
@@ -569,7 +615,6 @@ void Label::reset() |
569 | 615 | _currLabelEffect = LabelEffect::NORMAL; |
570 | 616 | _contentDirty = false; |
571 | 617 | _numberOfLines = 0; |
572 | | - _lengthOfString = 0; |
573 | 618 | _utf32Text.clear(); |
574 | 619 | _utf8Text.clear(); |
575 | 620 |
|
@@ -907,15 +952,19 @@ bool Label::setBMFontFilePath(std::string_view bmfontFilePath, std::string_view |
907 | 952 |
|
908 | 953 | void Label::setString(std::string_view text) |
909 | 954 | { |
910 | | - if (text.compare(_utf8Text)) |
| 955 | + if (text != _utf8Text) |
911 | 956 | { |
912 | | - _utf8Text = text; |
913 | | - _contentDirty = true; |
914 | | - |
915 | 957 | std::u32string utf32String; |
916 | | - if (text_utils::UTF8ToUTF32(_utf8Text, utf32String)) |
| 958 | + if (text_utils::UTF8ToUTF32(text, utf32String)) |
917 | 959 | { |
| 960 | + _utf8Text = text; |
918 | 961 | _utf32Text = utf32String; |
| 962 | + |
| 963 | + _contentDirty = true; |
| 964 | + } |
| 965 | + else |
| 966 | + { |
| 967 | + AXLOGE("Label: setString() - Invalid utf8 text: {}", text); |
919 | 968 | } |
920 | 969 | } |
921 | 970 | } |
@@ -1006,7 +1055,7 @@ void Label::updateLabelLetters() |
1006 | 1055 | letterIndex = it->first; |
1007 | 1056 | letterSprite = (LabelLetter*)it->second; |
1008 | 1057 |
|
1009 | | - if (letterIndex >= _lengthOfString) |
| 1058 | + if (letterIndex >= getCharCount()) |
1010 | 1059 | { |
1011 | 1060 | Node::removeChild(letterSprite, true); |
1012 | 1061 | it = _letters.erase(it); |
@@ -1102,7 +1151,6 @@ void Label::alignText() |
1102 | 1151 |
|
1103 | 1152 | bool Label::tryTextPlacement(float fontSize) |
1104 | 1153 | { |
1105 | | - _lengthOfString = 0; |
1106 | 1154 | _textDesiredHeight = 0.f; |
1107 | 1155 | _linesWidth.clear(); |
1108 | 1156 |
|
@@ -1212,7 +1260,7 @@ bool Label::updateQuads() |
1212 | 1260 | batchNode->getTextureAtlas()->removeAllQuads(); |
1213 | 1261 | } |
1214 | 1262 |
|
1215 | | - for (int ctr = 0; ctr < _lengthOfString; ++ctr) |
| 1263 | + for (int ctr = 0; ctr < getCharCount(); ++ctr) |
1216 | 1264 | { |
1217 | 1265 | auto& letterInfo = _lettersInfo[ctr]; |
1218 | 1266 | if (letterInfo.valid) |
@@ -2066,7 +2114,7 @@ void Label::updateEffectUniforms(BatchCommand& batch, |
2066 | 2114 |
|
2067 | 2115 | void Label::draw(Renderer* renderer, const Mat4& transform, uint32_t flags) |
2068 | 2116 | { |
2069 | | - if (_batchNodes.empty() || _lengthOfString <= 0) |
| 2117 | + if (_batchNodes.empty() || _utf32Text.empty()) |
2070 | 2118 | { |
2071 | 2119 | return; |
2072 | 2120 | } |
@@ -2297,7 +2345,7 @@ Sprite* Label::getLetter(int letterIndex) |
2297 | 2345 | updateContent(); |
2298 | 2346 | } |
2299 | 2347 |
|
2300 | | - if (_textSprite == nullptr && letterIndex < _lengthOfString) |
| 2348 | + if (_textSprite == nullptr && letterIndex < getCharCount()) |
2301 | 2349 | { |
2302 | 2350 | const auto& letterInfo = _lettersInfo[letterIndex]; |
2303 | 2351 | if (!letterInfo.valid || letterInfo.atlasIndex < 0) |
@@ -2424,25 +2472,24 @@ void Label::computeStringNumLines() |
2424 | 2472 | _numberOfLines = quantityOfLines; |
2425 | 2473 | } |
2426 | 2474 |
|
2427 | | -int Label::getStringNumLines() |
| 2475 | +int Label::getLineCount() const |
2428 | 2476 | { |
2429 | 2477 | if (_contentDirty) |
2430 | 2478 | { |
2431 | | - updateContent(); |
| 2479 | + const_cast<Label*>(this)->updateContent(); |
2432 | 2480 | } |
2433 | 2481 |
|
2434 | 2482 | if (_currentLabelType == LabelType::STRING_TEXTURE) |
2435 | 2483 | { |
2436 | | - computeStringNumLines(); |
| 2484 | + const_cast<Label*>(this)->computeStringNumLines(); |
2437 | 2485 | } |
2438 | 2486 |
|
2439 | 2487 | return _numberOfLines; |
2440 | 2488 | } |
2441 | 2489 |
|
2442 | | -int Label::getStringLength() |
| 2490 | +int Label::getCharCount() const |
2443 | 2491 | { |
2444 | | - _lengthOfString = static_cast<int>(_utf32Text.length()); |
2445 | | - return _lengthOfString; |
| 2492 | + return static_cast<int>(_utf32Text.length()); |
2446 | 2493 | } |
2447 | 2494 |
|
2448 | 2495 | // RGBA protocol |
@@ -2901,7 +2948,7 @@ void Label::updateFontScale() |
2901 | 2948 |
|
2902 | 2949 | bool Label::multilineTextWrap(bool breakOnChar, bool ignoreOverflow) |
2903 | 2950 | { |
2904 | | - int textLen = getStringLength(); |
| 2951 | + int textLen = getCharCount(); |
2905 | 2952 | int lineIndex = 0; |
2906 | 2953 | float nextTokenX = 0.f; |
2907 | 2954 | float nextTokenY = 0.f; |
@@ -3119,7 +3166,7 @@ bool Label::isHorizontalClamp() |
3119 | 3166 | { |
3120 | 3167 | bool letterClamp = false; |
3121 | 3168 |
|
3122 | | - for (int ctr = 0; ctr < _lengthOfString; ++ctr) |
| 3169 | + for (int ctr = 0; ctr < getCharCount(); ++ctr) |
3123 | 3170 | { |
3124 | 3171 | if (_lettersInfo[ctr].valid) |
3125 | 3172 | { |
|
0 commit comments