From 240bf58bfcc328cadb5abe854c42775e00de3f62 Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 14 Apr 2015 15:34:15 +0800 Subject: [PATCH] fix edge case crash --- cocos/ui/UIButton.cpp | 33 ++++++++++++++++++++++++++------- cocos/ui/UIButton.h | 2 +- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cocos/ui/UIButton.cpp b/cocos/ui/UIButton.cpp index 16b482d96c..c9eea6d84e 100644 --- a/cocos/ui/UIButton.cpp +++ b/cocos/ui/UIButton.cpp @@ -845,15 +845,22 @@ Label* Button::getTitleRenderer()const return _titleRenderer; } -const std::string& Button::getTitleFontName() const +const std::string Button::getTitleFontName() const { - if(this->_type == FontType::SYSTEM) + if (nullptr != _titleRenderer) { - return _titleRenderer->getSystemFontName(); + if(this->_type == FontType::SYSTEM) + { + return _titleRenderer->getSystemFontName(); + } + else + { + return _titleRenderer->getTTFConfig().fontFilePath; + } } else { - return _titleRenderer->getTTFConfig().fontFilePath; + return ""; } } @@ -874,9 +881,21 @@ void Button::copySpecialProperties(Widget *widget) { _prevIgnoreSize = button->_prevIgnoreSize; setScale9Enabled(button->_scale9Enabled); - loadTextureNormal(button->_buttonNormalRenderer->getSprite()->getSpriteFrame()); - loadTexturePressed(button->_buttonClickedRenderer->getSprite()->getSpriteFrame()); - loadTextureDisabled(button->_buttonDisableRenderer->getSprite()->getSpriteFrame()); + auto normalSprite = button->_buttonNormalRenderer->getSprite(); + if (nullptr != normalSprite) + { + loadTextureNormal(normalSprite->getSpriteFrame()); + } + auto clickedSprite = button->_buttonClickedRenderer->getSprite(); + if (nullptr != clickedSprite) + { + loadTexturePressed(clickedSprite->getSpriteFrame()); + } + auto disabledSprite = button->_buttonDisableRenderer->getSprite(); + if (nullptr != disabledSprite) + { + loadTextureDisabled(disabledSprite->getSpriteFrame()); + } setCapInsetsNormalRenderer(button->_capInsetsNormal); setCapInsetsPressedRenderer(button->_capInsetsPressed); setCapInsetsDisabledRenderer(button->_capInsetsDisabled); diff --git a/cocos/ui/UIButton.h b/cocos/ui/UIButton.h index 3ab95f360a..161e99651d 100644 --- a/cocos/ui/UIButton.h +++ b/cocos/ui/UIButton.h @@ -245,7 +245,7 @@ public: * Query the font name of button's title *@return font name in std::string */ - const std::string& getTitleFontName() const; + const std::string getTitleFontName() const; /** @brief When user pressed the button, the button will zoom to a scale. * The final scale of the button equals (button original scale + _zoomScale)