Skip to content

Commit ea22822

Browse files
roedminggo
authored andcommitted
Added support for BMFont in TextField (#18587)
* Added support for BMFont in TextField * Added cursor support for BMFont in TextFieldTTF Made BMFont detection case insensitive and a bit more fool proof * Added TextField::createWithBMFont, so that the font size is determined internally. * Revert "Added TextField::createWithBMFont, so that the font size is determined internally." This reverts commit 46b4e72. * fix BMFont Cursor scale * remove error add subproject
1 parent bbdd289 commit ea22822

File tree

7 files changed

+157
-42
lines changed

7 files changed

+157
-42
lines changed

cocos/2d/CCLabel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,13 +1779,15 @@ Sprite* Label::getLetter(int letterIndex)
17791779
}
17801780
else
17811781
{
1782+
this->updateBMFontScale();
17821783
letter = LabelLetter::createWithTexture(_fontAtlas->getTexture(textureID), uvRect);
17831784
letter->setTextureAtlas(_batchNodes.at(textureID)->getTextureAtlas());
17841785
letter->setAtlasIndex(letterInfo.atlasIndex);
1785-
auto px = letterInfo.positionX + uvRect.size.width / 2 + _linesOffsetX[letterInfo.lineIndex];
1786-
auto py = letterInfo.positionY - uvRect.size.height / 2 + _letterOffsetY;
1786+
auto px = letterInfo.positionX + _bmfontScale * uvRect.size.width / 2 + _linesOffsetX[letterInfo.lineIndex];
1787+
auto py = letterInfo.positionY - _bmfontScale * uvRect.size.height / 2 + _letterOffsetY;
17871788
letter->setPosition(px,py);
17881789
letter->setOpacity(_realOpacity);
1790+
this->updateLetterSpriteScale(letter);
17891791
}
17901792

17911793
addChild(letter);

cocos/2d/CCTextFieldTTF.cpp

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,8 @@ bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const std
170170
setSystemFontSize(fontSize);
171171

172172
} while (false);
173-
174-
175-
Label::setTextColor(_colorSpaceHolder);
173+
174+
setTextColorInternally(_colorSpaceHolder);
176175
Label::setString(_placeHolder);
177176

178177
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
@@ -270,7 +269,7 @@ void TextFieldTTF::insertText(const char * text, size_t len)
270269
stringUTF8.replace(_inputText);
271270
stringUTF8.insert(_cursorPosition, insert);
272271

273-
setCursorPosition(_cursorPosition + countInsertChar);
272+
setCursorPosition(_cursorPosition + countInsertChar);
274273

275274
setString(stringUTF8.getAsCharSequence());
276275
}
@@ -422,12 +421,22 @@ void TextFieldTTF::setAttachWithIME(bool isAttachWithIME)
422421
}
423422
}
424423

424+
void TextFieldTTF::setTextColorInternally(const Color4B& color)
425+
{
426+
if (_currentLabelType == LabelType::BMFONT) {
427+
Label::setColor(Color3B(color));
428+
return;
429+
}
430+
431+
Label::setTextColor(color);
432+
}
433+
425434
void TextFieldTTF::setTextColor(const Color4B &color)
426435
{
427436
_colorText = color;
428-
if (!_inputText.empty())
437+
if (!_inputText.empty())
429438
{
430-
Label::setTextColor(_colorText);
439+
setTextColorInternally(color);
431440
}
432441
}
433442

@@ -454,6 +463,9 @@ void TextFieldTTF::update(float delta)
454463

455464
if (sprite)
456465
{
466+
if (_currentLabelType == LabelType::BMFONT) {
467+
sprite->setColor(getColor());
468+
}
457469
if (_cursorShowingTime >= 0.0f)
458470
{
459471
sprite->setOpacity(255);
@@ -474,22 +486,15 @@ const Color4B& TextFieldTTF::getColorSpaceHolder()
474486

475487
void TextFieldTTF::setColorSpaceHolder(const Color3B& color)
476488
{
477-
_colorSpaceHolder.r = color.r;
478-
_colorSpaceHolder.g = color.g;
479-
_colorSpaceHolder.b = color.b;
480-
_colorSpaceHolder.a = 255;
481-
if (_inputText.empty())
482-
{
483-
Label::setTextColor(_colorSpaceHolder);
484-
}
489+
setColorSpaceHolder(Color4B(color));
485490
}
486491

487492
void TextFieldTTF::setColorSpaceHolder(const Color4B& color)
488493
{
489494
_colorSpaceHolder = color;
490495
if (_inputText.empty())
491496
{
492-
Label::setTextColor(_colorSpaceHolder);
497+
setTextColorInternally(_colorSpaceHolder);
493498
}
494499
}
495500

@@ -539,14 +544,13 @@ void TextFieldTTF::setString(const std::string &text)
539544
// if there is no input text, display placeholder instead
540545
if (_inputText.empty() && (!_cursorEnabled || !_isAttachWithIME))
541546
{
542-
Label::setTextColor(_colorSpaceHolder);
547+
setTextColorInternally(_colorSpaceHolder);
543548
Label::setString(_placeHolder);
544549
}
545550
else
546551
{
547552
makeStringSupportCursor(displayText);
548-
549-
Label::setTextColor(_colorText);
553+
setTextColorInternally(_colorText);
550554
Label::setString(displayText);
551555
}
552556
_charCount = charCount;
@@ -566,7 +570,7 @@ void TextFieldTTF::makeStringSupportCursor(std::string& displayText)
566570
if (displayText.empty())
567571
{
568572
// \b - Next char not change x position
569-
if (_currentLabelType == LabelType::TTF)
573+
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
570574
displayText.push_back((char) TextFormatter::NextCharNoChangeX);
571575
displayText.push_back(_cursorChar);
572576
}
@@ -582,7 +586,7 @@ void TextFieldTTF::makeStringSupportCursor(std::string& displayText)
582586
}
583587
std::string cursorChar;
584588
// \b - Next char not change x position
585-
if (_currentLabelType == LabelType::TTF)
589+
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
586590
cursorChar.push_back((char)TextFormatter::NextCharNoChangeX);
587591
cursorChar.push_back(_cursorChar);
588592
stringUTF8.insert(_cursorPosition, cursorChar);
@@ -669,7 +673,7 @@ void TextFieldTTF::setPlaceHolder(const std::string& text)
669673
_placeHolder = text;
670674
if (_inputText.empty() && !_isAttachWithIME)
671675
{
672-
Label::setTextColor(_colorSpaceHolder);
676+
setTextColorInternally(_colorSpaceHolder);
673677
Label::setString(_placeHolder);
674678
}
675679
}
@@ -681,21 +685,24 @@ const std::string& TextFieldTTF::getPlaceHolder() const
681685

682686
void TextFieldTTF::setCursorEnabled(bool enabled)
683687
{
684-
if (_cursorEnabled != enabled)
688+
if (_cursorEnabled == enabled)
685689
{
686-
_cursorEnabled = enabled;
687-
if (_cursorEnabled)
688-
{
689-
_cursorPosition = _charCount;
690-
if (_currentLabelType == LabelType::TTF)
691-
scheduleUpdate();
692-
}
693-
else
694-
{
695-
_cursorPosition = 0;
696-
if (_currentLabelType == LabelType::TTF)
697-
unscheduleUpdate();
690+
return;
691+
}
692+
693+
_cursorEnabled = enabled;
694+
if (_cursorEnabled)
695+
{
696+
_cursorPosition = _charCount;
697+
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT) {
698+
scheduleUpdate();
698699
}
700+
return;
701+
}
702+
703+
_cursorPosition = 0;
704+
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT) {
705+
unscheduleUpdate();
699706
}
700707
}
701708

@@ -733,3 +740,4 @@ bool TextFieldTTF::isSecureTextEntry() const
733740
}
734741

735742
NS_CC_END
743+

cocos/2d/CCTextFieldTTF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ class CC_DLL TextFieldTTF : public Label, public IMEDelegate
279279
void makeStringSupportCursor(std::string& displayText);
280280
void updateCursorDisplayText();
281281
void setAttachWithIME(bool isAttachWithIME);
282+
void setTextColorInternally(const Color4B& color);
282283

283284
private:
284285
class LengthStack;

cocos/ui/UITextField.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ void TextField::setFontSize(int size)
446446
{
447447
_textFieldRenderer->setSystemFontSize(size);
448448
}
449+
else if (_fontType == FontType::BMFONT) {
450+
_textFieldRenderer->setBMFontSize(size);
451+
}
449452
else
450453
{
451454
TTFConfig config = _textFieldRenderer->getTTFConfig();
@@ -466,11 +469,19 @@ void TextField::setFontName(const std::string& name)
466469
{
467470
if(FileUtils::getInstance()->isFileExist(name))
468471
{
469-
TTFConfig config = _textFieldRenderer->getTTFConfig();
470-
config.fontFilePath = name;
471-
config.fontSize = _fontSize;
472-
_textFieldRenderer->setTTFConfig(config);
473-
_fontType = FontType::TTF;
472+
std::string lcName = name;
473+
std::transform(lcName.begin(), lcName.end(), lcName.begin(), ::tolower);
474+
if(lcName.substr(lcName.length() - 4) == ".fnt") {
475+
_textFieldRenderer->setBMFontFilePath(name);
476+
_fontType = FontType::BMFONT;
477+
}
478+
else {
479+
TTFConfig config = _textFieldRenderer->getTTFConfig();
480+
config.fontFilePath = name;
481+
config.fontSize = _fontSize;
482+
_textFieldRenderer->setTTFConfig(config);
483+
_fontType = FontType::TTF;
484+
}
474485
}
475486
else
476487
{

cocos/ui/UITextField.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ class CC_GUI_DLL TextField : public Widget
694694
enum class FontType
695695
{
696696
SYSTEM,
697-
TTF
697+
TTF,
698+
BMFONT
698699
};
699700

700701
std::string _fontName;

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ UITextFieldTests::UITextFieldTests()
1010
ADD_TEST_CASE(UITextFieldTest_Password);
1111
ADD_TEST_CASE(UITextFieldTest_LineWrap);
1212
ADD_TEST_CASE(UITextFieldTest_TrueTypeFont);
13+
ADD_TEST_CASE(UITextFieldTest_BMFont);
1314
ADD_TEST_CASE(UITextFieldTest_PlaceHolderColor);
1415
}
1516

@@ -416,6 +417,82 @@ void UITextFieldTest_TrueTypeFont::textFieldEvent(Ref *pSender, TextField::Event
416417
}
417418
}
418419

420+
// UITextFieldTest_BMFont
421+
UITextFieldTest_BMFont::UITextFieldTest_BMFont()
422+
: _displayValueLabel(nullptr)
423+
{
424+
425+
}
426+
427+
UITextFieldTest_BMFont::~UITextFieldTest_BMFont()
428+
{
429+
}
430+
431+
bool UITextFieldTest_BMFont::init()
432+
{
433+
if (UIScene::init())
434+
{
435+
Size widgetSize = _widget->getContentSize();
436+
437+
// Add a label in which the textfield events will be displayed
438+
_displayValueLabel = Text::create("BMFont Test - No Event","fonts/Marker Felt.ttf",32);
439+
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
440+
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
441+
_uiLayer->addChild(_displayValueLabel);
442+
443+
// Add the alert
444+
Text* alert = Text::create("TextField","fonts/Marker Felt.ttf",30);
445+
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
446+
_uiLayer->addChild(alert);
447+
448+
// Create the textfield
449+
TextField* textField = TextField::create("BMFont Text","fonts/bitmapFontTest3.fnt",30);
450+
textField->setCursorEnabled(true);
451+
textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
452+
textField->addEventListener(CC_CALLBACK_2(UITextFieldTest_BMFont::textFieldEvent, this));
453+
_uiLayer->addChild(textField);
454+
455+
return true;
456+
}
457+
return false;
458+
}
459+
460+
void UITextFieldTest_BMFont::textFieldEvent(Ref *pSender, TextField::EventType type)
461+
{
462+
switch (type)
463+
{
464+
case TextField::EventType::ATTACH_WITH_IME:
465+
{
466+
TextField* textField = dynamic_cast<TextField*>(pSender);
467+
Size screenSize = Director::getInstance()->getWinSize();
468+
textField->runAction(MoveTo::create(0.225f,
469+
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
470+
_displayValueLabel->setString(StringUtils::format("attach with IME"));
471+
}
472+
break;
473+
474+
case TextField::EventType::DETACH_WITH_IME:
475+
{
476+
TextField* textField = dynamic_cast<TextField*>(pSender);
477+
Size screenSize = Director::getInstance()->getWinSize();
478+
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
479+
_displayValueLabel->setString(StringUtils::format("detach with IME"));
480+
}
481+
break;
482+
483+
case TextField::EventType::INSERT_TEXT:
484+
_displayValueLabel->setString(StringUtils::format("insert words"));
485+
break;
486+
487+
case TextField::EventType::DELETE_BACKWARD:
488+
_displayValueLabel->setString(StringUtils::format("delete word"));
489+
break;
490+
491+
default:
492+
break;
493+
}
494+
}
495+
419496
// UITextFieldTest_PlaceHolderColor
420497
UITextFieldTest_PlaceHolderColor::UITextFieldTest_PlaceHolderColor()
421498
: _displayValueLabel(nullptr)

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ class UITextFieldTest_TrueTypeFont : public UIScene
104104
cocos2d::ui::Text* _displayValueLabel;
105105
};
106106

107+
class UITextFieldTest_BMFont : public UIScene
108+
{
109+
public:
110+
CREATE_FUNC(UITextFieldTest_BMFont);
111+
112+
UITextFieldTest_BMFont();
113+
~UITextFieldTest_BMFont();
114+
virtual bool init() override;
115+
void textFieldEvent(cocos2d::Ref* sender, cocos2d::ui::TextField::EventType type);
116+
117+
protected:
118+
119+
cocos2d::ui::Text* _displayValueLabel;
120+
};
121+
107122
class UITextFieldTest_PlaceHolderColor : public UIScene
108123
{
109124
public:

0 commit comments

Comments
 (0)