From 83d61dcbf70e9372e1bde2489deb93562eadbce7 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 21 Dec 2016 16:51:04 +0800 Subject: [PATCH] add bitmap font batch rendering --- native/cocos/2d/CCFontAtlasCache.cpp | 17 +- native/cocos/2d/CCFontAtlasCache.h | 9 +- native/cocos/2d/CCFontFNT.cpp | 89 ++++------ native/cocos/2d/CCFontFNT.h | 8 +- native/cocos/2d/CCLabel.cpp | 52 +++++- native/cocos/2d/CCLabel.h | 53 ++++-- .../cocosbuilder/CCLabelBMFontLoader.cpp | 2 +- .../auto/api/jsb_cocos2dx_auto_api.js | 14 +- .../js-bindings/auto/jsb_cocos2dx_auto.cpp | 154 +++++++++++++----- native/cocos/ui/UIButton.cpp | 2 +- native/cocos/ui/UITabControl.cpp | 4 +- native/cocos/ui/UITextBMFont.cpp | 2 +- .../CCControlExtension/CCControlButton.cpp | 2 +- 13 files changed, 262 insertions(+), 146 deletions(-) diff --git a/native/cocos/2d/CCFontAtlasCache.cpp b/native/cocos/2d/CCFontAtlasCache.cpp index fb8cd4dcbb..13d6b01983 100644 --- a/native/cocos/2d/CCFontAtlasCache.cpp +++ b/native/cocos/2d/CCFontAtlasCache.cpp @@ -30,6 +30,7 @@ #include "2d/CCFontAtlas.h" #include "2d/CCFontCharMap.h" #include "2d/CCLabel.h" +#include "2d/CCSpriteFrame.h" NS_CC_BEGIN @@ -89,7 +90,9 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) return nullptr; } -FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */) +FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, + SpriteFrame* spriteFrame, + const Vec2& imageOffset /* = Vec2::ZERO */) { char tmp[ATLAS_MAP_KEY_BUFFER]; snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str()); @@ -98,7 +101,7 @@ FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, cons auto it = _atlasMap.find(atlasName); if ( it == _atlasMap.end() ) { - auto font = FontFNT::create(fontFileName,imageOffset); + auto font = FontFNT::create(fontFileName, spriteFrame, imageOffset); if(font) { @@ -230,7 +233,9 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas) return false; } -void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/) +void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, + SpriteFrame* spriteFrame, + const Vec2& imageOffset/* = Vec2::ZERO*/) { char tmp[ATLAS_MAP_KEY_BUFFER]; snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str()); @@ -242,8 +247,10 @@ void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const V CC_SAFE_RELEASE_NULL(it->second); _atlasMap.erase(it); } - FontFNT::reloadBMFontResource(fontFileName); - auto font = FontFNT::create(fontFileName, imageOffset); + + auto font = FontFNT::create(fontFileName, + spriteFrame, + imageOffset); if (font) { auto tempAtlas = font->createFontAtlas(); diff --git a/native/cocos/2d/CCFontAtlasCache.h b/native/cocos/2d/CCFontAtlasCache.h index 9b070ddaf7..d00f2ee899 100644 --- a/native/cocos/2d/CCFontAtlasCache.h +++ b/native/cocos/2d/CCFontAtlasCache.h @@ -36,12 +36,15 @@ NS_CC_BEGIN class FontAtlas; class Texture2D; struct _ttfConfig; +class SpriteFrame; class CC_DLL FontAtlasCache { public: static FontAtlas* getFontAtlasTTF(const _ttfConfig* config); - static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO); + static FontAtlas* getFontAtlasFNT(const std::string& fontFileName, + SpriteFrame* spriteFrame, + const Vec2& imageOffset = Vec2::ZERO); static FontAtlas* getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); static FontAtlas* getFontAtlasCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); @@ -58,7 +61,9 @@ public: CAUTION : All component use this font texture should be reset font name, though the file name is same! otherwise, it will cause program crash! */ - static void reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset = Vec2::ZERO); + static void reloadFontAtlasFNT(const std::string& fontFileName, + SpriteFrame* spriteFrame, + const Vec2& imageOffset = Vec2::ZERO); /** Unload all texture atlas texture create by special file name. CAUTION : All component use this font texture should be reset font name, though the file name is same! diff --git a/native/cocos/2d/CCFontFNT.cpp b/native/cocos/2d/CCFontFNT.cpp index 007bd87600..8780ba2653 100644 --- a/native/cocos/2d/CCFontFNT.cpp +++ b/native/cocos/2d/CCFontFNT.cpp @@ -34,6 +34,7 @@ #include "base/CCMap.h" #include "base/ccUTF8.h" #include "renderer/CCTextureCache.h" +#include "2d/CCSpriteFrame.h" using namespace std; NS_CC_BEGIN @@ -98,6 +99,8 @@ typedef struct _KerningHashElement UT_hash_handle hh; } tKerningHashElement; +class SpriteFrame; + /** @brief BMFontConfiguration has parsed configuration of the .fnt file @since v0.8 */ @@ -114,6 +117,7 @@ public://@public BMFontPadding _padding; //! atlas name std::string _atlasName; + SpriteFrame* _spriteFrame; //! values for kerning tKerningHashElement *_kerningDictionary; @@ -138,10 +142,10 @@ public: std::string description() const; /** allocates a BMFontConfiguration with a FNT file */ - static BMFontConfiguration * create(const std::string& FNTfile); + static BMFontConfiguration * create(const std::string& FNTfile, SpriteFrame* spriteFrame); /** initializes a BitmapFontConfiguration with a FNT file */ - bool initWithFNTfile(const std::string& FNTfile); + bool initWithFNTfile(const std::string& FNTfile, SpriteFrame* spriteFrame); inline const std::string& getAtlasName(){ return _atlasName; } inline void setAtlasName(const std::string& atlasName) { _atlasName = atlasName; } @@ -159,29 +163,13 @@ private: void purgeFontDefDictionary(); }; -// -//FNTConfig Cache - free functions -// -static Map* s_configurations = nullptr; -BMFontConfiguration* FNTConfigLoadFile(const std::string& fntFile) +BMFontConfiguration* FNTConfigLoadFile(const std::string& fntFile, SpriteFrame* spriteFrame) { BMFontConfiguration* ret = nullptr; - if( s_configurations == nullptr ) - { - s_configurations = new (std::nothrow) Map(); - } - ret = s_configurations->at(fntFile); - if( ret == nullptr ) - { - ret = BMFontConfiguration::create(fntFile); - if (ret) - { - s_configurations->insert(fntFile, ret); - } - } + ret = BMFontConfiguration::create(fntFile, spriteFrame); return ret; } @@ -190,10 +178,10 @@ BMFontConfiguration* FNTConfigLoadFile(const std::string& fntFile) //BitmapFontConfiguration // -BMFontConfiguration * BMFontConfiguration::create(const std::string& FNTfile) +BMFontConfiguration * BMFontConfiguration::create(const std::string& FNTfile, SpriteFrame* spriteFrame) { BMFontConfiguration * ret = new (std::nothrow) BMFontConfiguration(); - if (ret->initWithFNTfile(FNTfile)) + if (ret->initWithFNTfile(FNTfile, spriteFrame)) { ret->autorelease(); return ret; @@ -202,10 +190,11 @@ BMFontConfiguration * BMFontConfiguration::create(const std::string& FNTfile) return nullptr; } -bool BMFontConfiguration::initWithFNTfile(const std::string& FNTfile) +bool BMFontConfiguration::initWithFNTfile(const std::string& FNTfile, SpriteFrame* spriteFrame) { _kerningDictionary = nullptr; _fontDefDictionary = nullptr; + _spriteFrame = spriteFrame; _characterSet = this->parseConfigFile(FNTfile); @@ -275,12 +264,7 @@ void BMFontConfiguration::purgeFontDefDictionary() std::set* BMFontConfiguration::parseConfigFile(const std::string& controlFile) { - std::string data; - auto parseRet = FileUtils::getInstance()->getContents(controlFile, &data); - if (parseRet != FileUtils::Status::OK || data.empty()) - { - return nullptr; - } + std::string data = controlFile; if (data.size() >= (sizeof("BMP") - 1) && memcmp("BMF", data.c_str(), sizeof("BMP") - 1) == 0) { @@ -537,7 +521,7 @@ void BMFontConfiguration::parseImageFileName(const char* line, const std::string // file char fileName[255]; sscanf(strchr(line,'"') + 1, "%[^\"]", fileName); - _atlasName = FileUtils::getInstance()->fullPathFromRelativeFile(fileName, fntFile); + _atlasName = fntFile; } void BMFontConfiguration::parseInfoArguments(const char* line) @@ -643,14 +627,22 @@ void BMFontConfiguration::parseKerningEntry(const char* line) HASH_ADD_INT(_kerningDictionary,key, element); } -FontFNT * FontFNT::create(const std::string& fntFilePath, const Vec2& imageOffset /* = Vec2::ZERO */) +FontFNT * FontFNT::create(const std::string& fntFilePath, + SpriteFrame* spriteFrame, + const Vec2& imageOffset /* = Vec2::ZERO */) { - BMFontConfiguration *newConf = FNTConfigLoadFile(fntFilePath); + BMFontConfiguration *newConf = FNTConfigLoadFile(fntFilePath, spriteFrame); if (!newConf) return nullptr; // add the texture - Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(newConf->getAtlasName()); + Texture2D *tempTexture = nullptr; + if (spriteFrame) { + tempTexture = spriteFrame->getTexture(); + } else { + tempTexture = Director::getInstance()->getTextureCache()->addImage(newConf->getAtlasName()); + } + if (!tempTexture) { return nullptr; @@ -680,11 +672,6 @@ FontFNT::~FontFNT() void FontFNT::purgeCachedData() { - if (s_configurations) - { - s_configurations->clear(); - CC_SAFE_DELETE(s_configurations); - } } int * FontFNT::getHorizontalKerningForTextUTF16(const std::u16string& text, int &outNumLetters) const @@ -801,8 +788,11 @@ FontAtlas * FontFNT::createFontAtlas() } // add the texture (only one texture for now) + Texture2D *tempTexture = nullptr; + if(_configuration->_spriteFrame) { + tempTexture = _configuration->_spriteFrame->getTexture(); + } - Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(_configuration->getAtlasName()); if (!tempTexture) { CC_SAFE_RELEASE(tempAtlas); return nullptr; @@ -815,25 +805,4 @@ FontAtlas * FontFNT::createFontAtlas() return tempAtlas; } -void FontFNT::reloadBMFontResource(const std::string& fntFilePath) -{ - if (s_configurations == nullptr) - { - s_configurations = new (std::nothrow) Map(); - } - - BMFontConfiguration *ret = s_configurations->at(fntFilePath); - if (ret != nullptr) - { - s_configurations->erase(fntFilePath); - } - ret = BMFontConfiguration::create(fntFilePath); - if (ret) - { - s_configurations->insert(fntFilePath, ret); - Director::getInstance()->getTextureCache()->reloadTexture(ret->getAtlasName()); - - } -} - NS_CC_END diff --git a/native/cocos/2d/CCFontFNT.h b/native/cocos/2d/CCFontFNT.h index da7d961447..6e6b5e633c 100644 --- a/native/cocos/2d/CCFontFNT.h +++ b/native/cocos/2d/CCFontFNT.h @@ -33,13 +33,16 @@ NS_CC_BEGIN class BMFontConfiguration; +class SpriteFrame; class CC_DLL FontFNT : public Font { public: - static FontFNT * create(const std::string& fntFilePath, const Vec2& imageOffset = Vec2::ZERO); + static FontFNT * create(const std::string& fntFilePath, + SpriteFrame* spriteFrame, + const Vec2& imageOffset = Vec2::ZERO); /** Purges the cached data. Removes from memory the cached configurations and the atlas name dictionary. */ @@ -48,9 +51,6 @@ public: virtual FontAtlas *createFontAtlas() override; void setFontSize(float fontSize); int getOriginalFontSize()const; - - static void reloadBMFontResource(const std::string& fntFilePath); - protected: FontFNT(BMFontConfiguration *theContfig, const Vec2& imageOffset = Vec2::ZERO); diff --git a/native/cocos/2d/CCLabel.cpp b/native/cocos/2d/CCLabel.cpp index 4908ea623b..0a5cbdfc74 100644 --- a/native/cocos/2d/CCLabel.cpp +++ b/native/cocos/2d/CCLabel.cpp @@ -41,6 +41,7 @@ #include "base/CCEventDispatcher.h" #include "base/CCEventCustom.h" #include "2d/CCFontFNT.h" +#include "2d/CCSpriteFrame.h" NS_CC_BEGIN @@ -229,11 +230,18 @@ Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, return nullptr; } -Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& hAlignment /* = TextHAlignment::LEFT */, int maxLineWidth /* = 0 */, const Vec2& imageOffset /* = Vec2::ZERO */) +Label* Label::createWithBMFont(const std::string& bmfontFilePath, + const std::string& text, + SpriteFrame* spriteFrame, + const TextHAlignment& hAlignment /* = TextHAlignment::LEFT */, + int maxLineWidth /* = 0 */, + const Vec2& imageOffset /* = Vec2::ZERO */) { auto ret = new (std::nothrow) Label(hAlignment); - if (ret && ret->setBMFontFilePath(bmfontFilePath,imageOffset)) + if (ret && ret->setBMFontFilePath(bmfontFilePath, + spriteFrame, + imageOffset)) { ret->setMaxLineWidth(maxLineWidth); ret->setString(text); @@ -617,10 +625,15 @@ bool Label::setTTFConfig(const TTFConfig& ttfConfig) return setTTFConfigInternal(ttfConfig); } -bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Vec2& imageOffset, float fontSize) +bool Label::setBMFontFilePath(const std::string& bmfontFilePath, + SpriteFrame* spriteFrame, + const Vec2& imageOffset, + float fontSize) { - FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath,imageOffset); - + FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath, + spriteFrame, + imageOffset); + if (!newAtlas) { reset(); @@ -641,6 +654,7 @@ bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Vec2& ima } _bmFontPath = bmfontFilePath; + _fntSpriteFrame = spriteFrame; _currentLabelType = LabelType::BMFONT; setFontAtlas(newAtlas); @@ -939,7 +953,29 @@ bool Label::updateQuads() if (_reusedRect.size.height > 0.f && _reusedRect.size.width > 0.f) { - _reusedLetter->setTextureRect(_reusedRect, false, _reusedRect.size); + if(_currentLabelType == Label::LabelType::BMFONT) { + auto isRotated = _fntSpriteFrame->isRotated(); + auto spriteFrameRect = _fntSpriteFrame->getRect(); + + if (!isRotated) { + _reusedRect.origin.x += spriteFrameRect.origin.x; + _reusedRect.origin.y += spriteFrameRect.origin.y; + } else { + auto originalX = _reusedRect.origin.x; + _reusedRect.origin.x = spriteFrameRect.origin.x + spriteFrameRect.size.height - _reusedRect.origin.y - _reusedRect.size.height; + _reusedRect.origin.y = originalX + spriteFrameRect.origin.y; + } + + + _reusedLetter->setTextureRect(_reusedRect, isRotated, _reusedRect.size); + } else { + _reusedLetter->setTextureRect(_reusedRect, false, _reusedRect.size); + + } + + + + float letterPositionX = _lettersInfo[ctr].positionX + _linesOffsetX[_lettersInfo[ctr].lineIndex]; _reusedLetter->setPosition(letterPositionX, py); auto index = static_cast(_batchNodes.at(letterDef.textureID)->getTextureAtlas()->getTotalQuads()); @@ -1000,7 +1036,9 @@ bool Label::setTTFConfigInternal(const TTFConfig& ttfConfig) void Label::setBMFontSizeInternal(float fontSize) { if(_currentLabelType == LabelType::BMFONT){ - this->setBMFontFilePath(_bmFontPath, Vec2::ZERO, fontSize); + this->setBMFontFilePath(_bmFontPath, + _fntSpriteFrame, + Vec2::ZERO, fontSize); _contentDirty = true; } } diff --git a/native/cocos/2d/CCLabel.h b/native/cocos/2d/CCLabel.h index d8471055ac..dff4d7e102 100644 --- a/native/cocos/2d/CCLabel.h +++ b/native/cocos/2d/CCLabel.h @@ -92,6 +92,7 @@ class Sprite; class SpriteBatchNode; class DrawNode; class EventListenerCustom; +class SpriteFrame; /** * @brief Label is a subclass of Node that knows how to render text labels. @@ -153,9 +154,12 @@ public: * * @return An automatically released Label object. */ - static Label* createWithSystemFont(const std::string& text, const std::string& font, float fontSize, - const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT, - TextVAlignment vAlignment = TextVAlignment::TOP); + static Label* createWithSystemFont(const std::string& text, + const std::string& font, + float fontSize, + const Size& dimensions = Size::ZERO, + TextHAlignment hAlignment = TextHAlignment::LEFT, + TextVAlignment vAlignment = TextVAlignment::TOP); /** * Allocates and initializes a Label, base on FreeType2. @@ -169,9 +173,12 @@ public: * * @return An automatically released Label object. */ - static Label * createWithTTF(const std::string& text, const std::string& fontFilePath, float fontSize, - const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT, - TextVAlignment vAlignment = TextVAlignment::TOP); + static Label * createWithTTF(const std::string& text, + const std::string& fontFilePath, + float fontSize, + const Size& dimensions = Size::ZERO, + TextHAlignment hAlignment = TextHAlignment::LEFT, + TextVAlignment vAlignment = TextVAlignment::TOP); /** * Allocates and initializes a Label, base on FreeType2. @@ -184,13 +191,15 @@ public: * @return An automatically released Label object. * @see TTFConfig setTTFConfig setMaxLineWidth */ - static Label* createWithTTF(const TTFConfig& ttfConfig, const std::string& text, - TextHAlignment hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0); + static Label* createWithTTF(const TTFConfig& ttfConfig, + const std::string& text, + TextHAlignment hAlignment = TextHAlignment::LEFT, + int maxLineWidth = 0); /** * Allocates and initializes a Label, with a bitmap font file. * - * @param bmfontPath A bitmap font file, it's a FNT format. + * @param bmfontString A bitmap font string content * @param text The initial text. * @param hAlignment Text horizontal alignment. * @param maxLineWidth The max line width. @@ -199,9 +208,13 @@ public: * @return An automatically released Label object. * @see setBMFontFilePath setMaxLineWidth */ - static Label* createWithBMFont(const std::string& bmfontPath, const std::string& text, - const TextHAlignment& hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0, - const Vec2& imageOffset = Vec2::ZERO); + static Label* createWithBMFont(const std::string& bmfontString, + const std::string& text, + SpriteFrame* spriteFrame, + const TextHAlignment& hAlignment = TextHAlignment::LEFT, + int maxLineWidth = 0, + const Vec2& imageOffset = Vec2::ZERO); + /** * Allocates and initializes a Label, with char map configuration. @@ -213,7 +226,10 @@ public: * * @return An automatically released Label object. */ - static Label * createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap); + static Label * createWithCharMap(const std::string& charMapFile, + int itemWidth, + int itemHeight, + int startCharMap); /** * Allocates and initializes a Label, with char map configuration. @@ -225,7 +241,10 @@ public: * * @return An automatically released Label object. */ - static Label * createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap); + static Label * createWithCharMap(Texture2D* texture, + int itemWidth, + int itemHeight, + int startCharMap); /** * Allocates and initializes a Label, with char map configuration. @@ -255,7 +274,10 @@ public: virtual const TTFConfig& getTTFConfig() const { return _fontConfig;} /** Sets a new bitmap font to Label */ - virtual bool setBMFontFilePath(const std::string& bmfontFilePath, const Vec2& imageOffset = Vec2::ZERO, float fontSize = 0); + virtual bool setBMFontFilePath(const std::string& bmfontDataString, + SpriteFrame* spriteFrame, + const Vec2& imageOffset = Vec2::ZERO, + float fontSize = 0); /** Returns the bitmap font used by the Label.*/ const std::string& getBMFontFilePath() const { return _bmFontPath;} @@ -769,6 +791,7 @@ protected: float _bmfontScale; Overflow _overflow; float _originalFontSize; + SpriteFrame* _fntSpriteFrame; bool _boldEnabled; bool _italicsEnabled; diff --git a/native/cocos/editor-support/cocosbuilder/CCLabelBMFontLoader.cpp b/native/cocos/editor-support/cocosbuilder/CCLabelBMFontLoader.cpp index 095fff4a5d..bfbb80d651 100644 --- a/native/cocos/editor-support/cocosbuilder/CCLabelBMFontLoader.cpp +++ b/native/cocos/editor-support/cocosbuilder/CCLabelBMFontLoader.cpp @@ -36,7 +36,7 @@ void LabelBMFontLoader::onHandlePropTypeBlendFunc(Node * pNode, Node * pParent, void LabelBMFontLoader::onHandlePropTypeFntFile(Node * pNode, Node * pParent, const char * pPropertyName, const char* pFntFile, CCBReader * ccbReader) { if(strcmp(pPropertyName, PROPERTY_FNTFILE) == 0) { - ((Label *)pNode)->setBMFontFilePath(pFntFile); + ((Label *)pNode)->setBMFontFilePath(pFntFile, nullptr); } else { NodeLoader::onHandlePropTypeFntFile(pNode, pParent, pPropertyName, pFntFile, ccbReader); } diff --git a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js index 6f8351d662..2210e8a117 100644 --- a/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js +++ b/native/cocos/scripting/js-bindings/auto/api/jsb_cocos2dx_auto_api.js @@ -12032,12 +12032,14 @@ getOutlineSize : function ( /** * @method setBMFontFilePath * @param {String} arg0 - * @param {vec2_object} arg1 - * @param {float} arg2 + * @param {cc.SpriteFrame} arg1 + * @param {vec2_object} arg2 + * @param {float} arg3 * @return {bool} */ setBMFontFilePath : function ( str, +spriteframe, vec2, float ) @@ -12503,14 +12505,16 @@ float * @method createWithBMFont * @param {String} arg0 * @param {String} arg1 - * @param {cc.TextHAlignment} arg2 - * @param {int} arg3 - * @param {vec2_object} arg4 + * @param {cc.SpriteFrame} arg2 + * @param {cc.TextHAlignment} arg3 + * @param {int} arg4 + * @param {vec2_object} arg5 * @return {cc.Label} */ createWithBMFont : function ( str, str, +spriteframe, texthalignment, int, vec2 diff --git a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp index 44b27847c3..2986c0f153 100644 --- a/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp +++ b/native/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp @@ -33008,21 +33008,19 @@ bool js_cocos2dx_Label_setBMFontFilePath(JSContext *cx, uint32_t argc, jsval *vp js_proxy_t *proxy = jsb_get_js_proxy(obj); cocos2d::Label* cobj = (cocos2d::Label *)(proxy ? proxy->ptr : NULL); JSB_PRECONDITION2( cobj, cx, false, "js_cocos2dx_Label_setBMFontFilePath : Invalid Native Object"); - if (argc == 1) { - std::string arg0; - ok &= jsval_to_std_string(cx, args.get(0), &arg0); - JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_setBMFontFilePath : Error processing arguments"); - bool ret = cobj->setBMFontFilePath(arg0); - JS::RootedValue jsret(cx); - jsret = BOOLEAN_TO_JSVAL(ret); - args.rval().set(jsret); - return true; - } if (argc == 2) { std::string arg0; - cocos2d::Vec2 arg1; + cocos2d::SpriteFrame* arg1 = nullptr; ok &= jsval_to_std_string(cx, args.get(0), &arg0); - ok &= jsval_to_vector2(cx, args.get(1), &arg1); + do { + if (args.get(1).isNull()) { arg1 = nullptr; break; } + if (!args.get(1).isObject()) { ok = false; break; } + js_proxy_t *jsProxy; + JS::RootedObject tmpObj(cx, args.get(1).toObjectOrNull()); + jsProxy = jsb_get_js_proxy(tmpObj); + arg1 = (cocos2d::SpriteFrame*)(jsProxy ? jsProxy->ptr : NULL); + JSB_PRECONDITION2( arg1, cx, false, "Invalid Native Object"); + } while (0); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_setBMFontFilePath : Error processing arguments"); bool ret = cobj->setBMFontFilePath(arg0, arg1); JS::RootedValue jsret(cx); @@ -33032,11 +33030,19 @@ bool js_cocos2dx_Label_setBMFontFilePath(JSContext *cx, uint32_t argc, jsval *vp } if (argc == 3) { std::string arg0; - cocos2d::Vec2 arg1; - double arg2 = 0; + cocos2d::SpriteFrame* arg1 = nullptr; + cocos2d::Vec2 arg2; ok &= jsval_to_std_string(cx, args.get(0), &arg0); - ok &= jsval_to_vector2(cx, args.get(1), &arg1); - ok &= JS::ToNumber( cx, args.get(2), &arg2) && !std::isnan(arg2); + do { + if (args.get(1).isNull()) { arg1 = nullptr; break; } + if (!args.get(1).isObject()) { ok = false; break; } + js_proxy_t *jsProxy; + JS::RootedObject tmpObj(cx, args.get(1).toObjectOrNull()); + jsProxy = jsb_get_js_proxy(tmpObj); + arg1 = (cocos2d::SpriteFrame*)(jsProxy ? jsProxy->ptr : NULL); + JSB_PRECONDITION2( arg1, cx, false, "Invalid Native Object"); + } while (0); + ok &= jsval_to_vector2(cx, args.get(2), &arg2); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_setBMFontFilePath : Error processing arguments"); bool ret = cobj->setBMFontFilePath(arg0, arg1, arg2); JS::RootedValue jsret(cx); @@ -33044,8 +33050,32 @@ bool js_cocos2dx_Label_setBMFontFilePath(JSContext *cx, uint32_t argc, jsval *vp args.rval().set(jsret); return true; } + if (argc == 4) { + std::string arg0; + cocos2d::SpriteFrame* arg1 = nullptr; + cocos2d::Vec2 arg2; + double arg3 = 0; + ok &= jsval_to_std_string(cx, args.get(0), &arg0); + do { + if (args.get(1).isNull()) { arg1 = nullptr; break; } + if (!args.get(1).isObject()) { ok = false; break; } + js_proxy_t *jsProxy; + JS::RootedObject tmpObj(cx, args.get(1).toObjectOrNull()); + jsProxy = jsb_get_js_proxy(tmpObj); + arg1 = (cocos2d::SpriteFrame*)(jsProxy ? jsProxy->ptr : NULL); + JSB_PRECONDITION2( arg1, cx, false, "Invalid Native Object"); + } while (0); + ok &= jsval_to_vector2(cx, args.get(2), &arg2); + ok &= JS::ToNumber( cx, args.get(3), &arg3) && !std::isnan(arg3); + JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_setBMFontFilePath : Error processing arguments"); + bool ret = cobj->setBMFontFilePath(arg0, arg1, arg2, arg3); + JS::RootedValue jsret(cx); + jsret = BOOLEAN_TO_JSVAL(ret); + args.rval().set(jsret); + return true; + } - JS_ReportError(cx, "js_cocos2dx_Label_setBMFontFilePath : wrong number of arguments: %d, was expecting %d", argc, 1); + JS_ReportError(cx, "js_cocos2dx_Label_setBMFontFilePath : wrong number of arguments: %d, was expecting %d", argc, 2); return false; } bool js_cocos2dx_Label_initWithTTF(JSContext *cx, uint32_t argc, jsval *vp) @@ -34122,26 +34152,21 @@ bool js_cocos2dx_Label_createWithBMFont(JSContext *cx, uint32_t argc, jsval *vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); bool ok = true; - if (argc == 2) { - std::string arg0; - std::string arg1; - ok &= jsval_to_std_string(cx, args.get(0), &arg0); - ok &= jsval_to_std_string(cx, args.get(1), &arg1); - JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithBMFont : Error processing arguments"); - - auto ret = cocos2d::Label::createWithBMFont(arg0, arg1); - js_type_class_t *typeClass = js_get_type_from_native(ret); - JS::RootedObject jsret(cx, jsb_ref_autoreleased_create_jsobject(cx, ret, typeClass, "cocos2d::Label")); - args.rval().set(OBJECT_TO_JSVAL(jsret)); - return true; - } if (argc == 3) { std::string arg0; std::string arg1; - cocos2d::TextHAlignment arg2; + cocos2d::SpriteFrame* arg2 = nullptr; ok &= jsval_to_std_string(cx, args.get(0), &arg0); ok &= jsval_to_std_string(cx, args.get(1), &arg1); - ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2); + do { + if (args.get(2).isNull()) { arg2 = nullptr; break; } + if (!args.get(2).isObject()) { ok = false; break; } + js_proxy_t *jsProxy; + JS::RootedObject tmpObj(cx, args.get(2).toObjectOrNull()); + jsProxy = jsb_get_js_proxy(tmpObj); + arg2 = (cocos2d::SpriteFrame*)(jsProxy ? jsProxy->ptr : NULL); + JSB_PRECONDITION2( arg2, cx, false, "Invalid Native Object"); + } while (0); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithBMFont : Error processing arguments"); auto ret = cocos2d::Label::createWithBMFont(arg0, arg1, arg2); @@ -34153,11 +34178,19 @@ bool js_cocos2dx_Label_createWithBMFont(JSContext *cx, uint32_t argc, jsval *vp) if (argc == 4) { std::string arg0; std::string arg1; - cocos2d::TextHAlignment arg2; - int arg3 = 0; + cocos2d::SpriteFrame* arg2 = nullptr; + cocos2d::TextHAlignment arg3; ok &= jsval_to_std_string(cx, args.get(0), &arg0); ok &= jsval_to_std_string(cx, args.get(1), &arg1); - ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2); + do { + if (args.get(2).isNull()) { arg2 = nullptr; break; } + if (!args.get(2).isObject()) { ok = false; break; } + js_proxy_t *jsProxy; + JS::RootedObject tmpObj(cx, args.get(2).toObjectOrNull()); + jsProxy = jsb_get_js_proxy(tmpObj); + arg2 = (cocos2d::SpriteFrame*)(jsProxy ? jsProxy->ptr : NULL); + JSB_PRECONDITION2( arg2, cx, false, "Invalid Native Object"); + } while (0); ok &= jsval_to_int32(cx, args.get(3), (int32_t *)&arg3); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithBMFont : Error processing arguments"); @@ -34170,14 +34203,22 @@ bool js_cocos2dx_Label_createWithBMFont(JSContext *cx, uint32_t argc, jsval *vp) if (argc == 5) { std::string arg0; std::string arg1; - cocos2d::TextHAlignment arg2; - int arg3 = 0; - cocos2d::Vec2 arg4; + cocos2d::SpriteFrame* arg2 = nullptr; + cocos2d::TextHAlignment arg3; + int arg4 = 0; ok &= jsval_to_std_string(cx, args.get(0), &arg0); ok &= jsval_to_std_string(cx, args.get(1), &arg1); - ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2); + do { + if (args.get(2).isNull()) { arg2 = nullptr; break; } + if (!args.get(2).isObject()) { ok = false; break; } + js_proxy_t *jsProxy; + JS::RootedObject tmpObj(cx, args.get(2).toObjectOrNull()); + jsProxy = jsb_get_js_proxy(tmpObj); + arg2 = (cocos2d::SpriteFrame*)(jsProxy ? jsProxy->ptr : NULL); + JSB_PRECONDITION2( arg2, cx, false, "Invalid Native Object"); + } while (0); ok &= jsval_to_int32(cx, args.get(3), (int32_t *)&arg3); - ok &= jsval_to_vector2(cx, args.get(4), &arg4); + ok &= jsval_to_int32(cx, args.get(4), (int32_t *)&arg4); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithBMFont : Error processing arguments"); auto ret = cocos2d::Label::createWithBMFont(arg0, arg1, arg2, arg3, arg4); @@ -34186,6 +34227,35 @@ bool js_cocos2dx_Label_createWithBMFont(JSContext *cx, uint32_t argc, jsval *vp) args.rval().set(OBJECT_TO_JSVAL(jsret)); return true; } + if (argc == 6) { + std::string arg0; + std::string arg1; + cocos2d::SpriteFrame* arg2 = nullptr; + cocos2d::TextHAlignment arg3; + int arg4 = 0; + cocos2d::Vec2 arg5; + ok &= jsval_to_std_string(cx, args.get(0), &arg0); + ok &= jsval_to_std_string(cx, args.get(1), &arg1); + do { + if (args.get(2).isNull()) { arg2 = nullptr; break; } + if (!args.get(2).isObject()) { ok = false; break; } + js_proxy_t *jsProxy; + JS::RootedObject tmpObj(cx, args.get(2).toObjectOrNull()); + jsProxy = jsb_get_js_proxy(tmpObj); + arg2 = (cocos2d::SpriteFrame*)(jsProxy ? jsProxy->ptr : NULL); + JSB_PRECONDITION2( arg2, cx, false, "Invalid Native Object"); + } while (0); + ok &= jsval_to_int32(cx, args.get(3), (int32_t *)&arg3); + ok &= jsval_to_int32(cx, args.get(4), (int32_t *)&arg4); + ok &= jsval_to_vector2(cx, args.get(5), &arg5); + JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithBMFont : Error processing arguments"); + + auto ret = cocos2d::Label::createWithBMFont(arg0, arg1, arg2, arg3, arg4, arg5); + js_type_class_t *typeClass = js_get_type_from_native(ret); + JS::RootedObject jsret(cx, jsb_ref_autoreleased_create_jsobject(cx, ret, typeClass, "cocos2d::Label")); + args.rval().set(OBJECT_TO_JSVAL(jsret)); + return true; + } JS_ReportError(cx, "js_cocos2dx_Label_createWithBMFont : wrong number of arguments"); return false; } @@ -34443,7 +34513,7 @@ void js_register_cocos2dx_Label(JSContext *cx, JS::HandleObject global) { JS_FN("setSystemFontName", js_cocos2dx_Label_setSystemFontName, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("isWrapEnabled", js_cocos2dx_Label_isWrapEnabled, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("getOutlineSize", js_cocos2dx_Label_getOutlineSize, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), - JS_FN("setBMFontFilePath", js_cocos2dx_Label_setBMFontFilePath, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), + JS_FN("setBMFontFilePath", js_cocos2dx_Label_setBMFontFilePath, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("initWithTTF", js_cocos2dx_Label_initWithTTF, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("setLineHeight", js_cocos2dx_Label_setLineHeight, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("setSystemFontSize", js_cocos2dx_Label_setSystemFontSize, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), @@ -34493,7 +34563,7 @@ void js_register_cocos2dx_Label(JSContext *cx, JS::HandleObject global) { }; static JSFunctionSpec st_funcs[] = { - JS_FN("createWithBMFont", js_cocos2dx_Label_createWithBMFont, 2, JSPROP_PERMANENT | JSPROP_ENUMERATE), + JS_FN("createWithBMFont", js_cocos2dx_Label_createWithBMFont, 3, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("create", js_cocos2dx_Label_create, 0, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("createWithCharMap", js_cocos2dx_Label_createWithCharMap, 1, JSPROP_PERMANENT | JSPROP_ENUMERATE), JS_FN("createWithSystemFont", js_cocos2dx_Label_createWithSystemFont, 3, JSPROP_PERMANENT | JSPROP_ENUMERATE), diff --git a/native/cocos/ui/UIButton.cpp b/native/cocos/ui/UIButton.cpp index b674abecd8..31c6f9cf19 100644 --- a/native/cocos/ui/UIButton.cpp +++ b/native/cocos/ui/UIButton.cpp @@ -790,7 +790,7 @@ void Button::setTitleFontName(const std::string& fontName) std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower); if (lowerCasedFontName.find(".fnt") != std::string::npos) { - _titleRenderer->setBMFontFilePath(fontName); + _titleRenderer->setBMFontFilePath(fontName, nullptr); _type = FontType::BMFONT; } else diff --git a/native/cocos/ui/UITabControl.cpp b/native/cocos/ui/UITabControl.cpp index 6221224370..ffac38b971 100644 --- a/native/cocos/ui/UITabControl.cpp +++ b/native/cocos/ui/UITabControl.cpp @@ -628,7 +628,7 @@ namespace ui std::transform(lowerCasedFontName.begin(), lowerCasedFontName.end(), lowerCasedFontName.begin(), ::tolower); if (lowerCasedFontName.find(".fnt") != std::string::npos) { - _tabLabelRender->setBMFontFilePath(fontName); + _tabLabelRender->setBMFontFilePath(fontName, nullptr); _fontType = FontType::BMFONT; } else @@ -732,4 +732,4 @@ namespace ui } } -NS_CC_END \ No newline at end of file +NS_CC_END diff --git a/native/cocos/ui/UITextBMFont.cpp b/native/cocos/ui/UITextBMFont.cpp index d60040004c..0457a55e40 100644 --- a/native/cocos/ui/UITextBMFont.cpp +++ b/native/cocos/ui/UITextBMFont.cpp @@ -86,7 +86,7 @@ void TextBMFont::setFntFile(const std::string& fileName) return; } _fntFileName = fileName; - _labelBMFontRenderer->setBMFontFilePath(fileName); + _labelBMFontRenderer->setBMFontFilePath(fileName, nullptr); updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize()); _labelBMFontRendererAdaptDirty = true; diff --git a/native/extensions/GUI/CCControlExtension/CCControlButton.cpp b/native/extensions/GUI/CCControlExtension/CCControlButton.cpp index 3684f5b34e..655854382d 100644 --- a/native/extensions/GUI/CCControlExtension/CCControlButton.cpp +++ b/native/extensions/GUI/CCControlExtension/CCControlButton.cpp @@ -425,7 +425,7 @@ float ControlButton::getTitleTTFSizeForState(State state) void ControlButton::setTitleBMFontForState(const std::string& fntFile, State state) { std::string title = this->getTitleForState(state); - this->setTitleLabelForState(Label::createWithBMFont(fntFile, title), state); + this->setTitleLabelForState(Label::createWithBMFont(fntFile, title, nullptr), state); } const std::string& ControlButton::getTitleBMFontForState(State state)