From b27fe6ecc2a442007db3038a91d5cf157307b797 Mon Sep 17 00:00:00 2001 From: andyque Date: Wed, 2 Apr 2014 18:16:17 +0800 Subject: [PATCH] issue #4636. add parameter create method to UITextBMFont --- cocos/ui/UITextBMFont.cpp | 28 +++++++++++++------ cocos/ui/UITextBMFont.h | 8 ++++-- .../UITextBMFontTest/UITextBMFontTest.cpp | 6 ++-- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/cocos/ui/UITextBMFont.cpp b/cocos/ui/UITextBMFont.cpp index 8bb1950bb0..6574d3ee20 100644 --- a/cocos/ui/UITextBMFont.cpp +++ b/cocos/ui/UITextBMFont.cpp @@ -56,6 +56,20 @@ TextBMFont* TextBMFont::create() CC_SAFE_DELETE(widget); return nullptr; } + +TextBMFont* TextBMFont::create(const std::string &text, const std::string &filename) +{ + TextBMFont* widget = new TextBMFont(); + if (widget && widget->init()) + { + widget->setFntFile(filename); + widget->setText(text); + widget->autorelease(); + return widget; + } + CC_SAFE_DELETE(widget); + return nullptr; +} void TextBMFont::initRenderer() { @@ -63,9 +77,9 @@ void TextBMFont::initRenderer() addProtectedChild(_labelBMFontRenderer, LABELBMFONT_RENDERER_Z, -1); } -void TextBMFont::setFntFile(const char *fileName) +void TextBMFont::setFntFile(const std::string& fileName) { - if (!fileName || strcmp(fileName, "") == 0) + if (fileName.empty()) { return; } @@ -77,12 +91,8 @@ void TextBMFont::setFntFile(const char *fileName) setText(_stringValue.c_str()); } -void TextBMFont::setText(const char* value) +void TextBMFont::setText(const std::string& value) { - if (!value) - { - return; - } _stringValue = value; if (!_fntFileHasInit) { @@ -92,9 +102,9 @@ void TextBMFont::setText(const char* value) labelBMFontScaleChangedWithSize(); } -const char* TextBMFont::getStringValue() +const std::string TextBMFont::getStringValue() { - return _stringValue.c_str(); + return _stringValue; } void TextBMFont::setAnchorPoint(const Point &pt) diff --git a/cocos/ui/UITextBMFont.h b/cocos/ui/UITextBMFont.h index df96ceb937..e41cbea7fc 100644 --- a/cocos/ui/UITextBMFont.h +++ b/cocos/ui/UITextBMFont.h @@ -56,14 +56,16 @@ public: */ static TextBMFont* create(); + static TextBMFont* create(const std::string& text, const std::string& filename); + /** init a bitmap font atlas with an initial string and the FNT file */ - void setFntFile(const char* fileName); + void setFntFile(const std::string& fileName); /** set string value for labelbmfont*/ - void setText(const char* value); + void setText(const std::string& value); /** get string value for labelbmfont*/ - const char* getStringValue(); + const std::string getStringValue(); virtual void setAnchorPoint(const Point &pt) override; virtual const Size& getContentSize() const override; virtual Node* getVirtualRenderer() override; diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest.cpp index ab203af60e..95f2081358 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UITextBMFontTest/UITextBMFontTest.cpp @@ -20,9 +20,9 @@ bool UITextBMFontTest::init() _uiLayer->addChild(alert); // Create the TextBMFont - TextBMFont* textBMFont = TextBMFont::create(); - textBMFont->setFntFile("cocosui/bitmapFontTest2.fnt"); - textBMFont->setText("BMFont"); + TextBMFont* textBMFont = TextBMFont::create("BMFont", "cocosui/bitmapFontTest2.fnt"); +// textBMFont->setFntFile("cocosui/bitmapFontTest2.fnt"); +// textBMFont->setText("BMFont"); textBMFont->setPosition(Point(widgetSize.width / 2, widgetSize.height / 2.0f + textBMFont->getSize().height / 8.0f)); _uiLayer->addChild(textBMFont);