2013-07-23 02:17:51 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-07-23 02:17:51 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCLabel.h"
|
2013-07-25 01:22:46 +08:00
|
|
|
#include "CCFontAtlasCache.h"
|
2013-08-08 07:36:04 +08:00
|
|
|
#include "CCLabelTextFormatter.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "CCShaderCache.h"
|
|
|
|
#include "ccUTF8.h"
|
|
|
|
#include "CCSpriteFrame.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
2014-01-20 11:43:13 +08:00
|
|
|
#include "CCFont.h"
|
2014-03-14 14:59:26 +08:00
|
|
|
#include "CCEventListenerCustom.h"
|
|
|
|
#include "CCEventDispatcher.h"
|
|
|
|
#include "CCEventType.h"
|
|
|
|
#include "CCEventCustom.h"
|
2014-03-31 15:29:24 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-04-09 22:30:49 +08:00
|
|
|
#include "deprecated/CCString.h"
|
2014-04-22 16:42:13 +08:00
|
|
|
#include "CCProfiling.h"
|
2013-07-23 02:17:51 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-03-13 20:52:33 +08:00
|
|
|
const int Label::DistanceFieldFontSize = 50;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
Label* Label::create()
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto ret = new Label();
|
2013-08-14 02:28:54 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
2014-01-11 22:33:07 +08:00
|
|
|
|
|
|
|
return ret;
|
2013-07-23 02:17:51 +08:00
|
|
|
}
|
|
|
|
|
2014-04-10 10:58:49 +08:00
|
|
|
Label* Label::create(const std::string& text, const std::string& font, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-04-10 10:58:49 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(font))
|
|
|
|
{
|
|
|
|
return createWithTTF(text,font,fontSize,dimensions,hAlignment,vAlignment);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return createWithSystemFont(text,font,fontSize,dimensions,hAlignment,vAlignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
Label* 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 */)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-03-12 16:11:12 +08:00
|
|
|
auto ret = new Label(nullptr,hAlignment,vAlignment);
|
2014-03-10 19:42:43 +08:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2014-04-10 10:23:00 +08:00
|
|
|
ret->setSystemFontName(font);
|
2014-04-09 23:31:05 +08:00
|
|
|
ret->setSystemFontSize(fontSize);
|
|
|
|
ret->setDimensions(dimensions.width, dimensions.height);
|
2014-03-10 19:42:43 +08:00
|
|
|
ret->setString(text);
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
ret->autorelease();
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
return ret;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
Label* Label::createWithTTF(const std::string& text, const std::string& fontFile, float fontSize, const Size& dimensions /* = Size::ZERO */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */, TextVAlignment vAlignment /* = TextVAlignment::TOP */)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-03-12 16:11:12 +08:00
|
|
|
auto ret = new Label(nullptr,hAlignment,vAlignment);
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
if (ret && FileUtils::getInstance()->isFileExist(fontFile))
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
TTFConfig ttfConfig(fontFile.c_str(),fontSize,GlyphCollection::DYNAMIC);
|
|
|
|
if (ret->setTTFConfig(ttfConfig))
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
ret->setDimensions(dimensions.width,dimensions.height);
|
2014-04-09 21:35:08 +08:00
|
|
|
ret->setString(text);
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
ret->autorelease();
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 14:57:00 +08:00
|
|
|
Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment alignment /* = TextHAlignment::CENTER */, int maxLineWidth /* = 0 */)
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto ret = new Label(nullptr,alignment);
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
if (ret && FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) && ret->setTTFConfig(ttfConfig))
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2014-04-09 21:35:08 +08:00
|
|
|
ret->setMaxLineWidth(maxLineWidth);
|
|
|
|
ret->setString(text);
|
|
|
|
ret->autorelease();
|
2014-04-01 11:37:36 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2013-07-23 02:17:51 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
2014-01-13 16:32:35 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& alignment /* = TextHAlignment::LEFT */, int maxLineWidth /* = 0 */, const Vector2& imageOffset /* = Vector2::ZERO */)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto ret = new Label(nullptr,alignment);
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
if (ret && ret->setBMFontFilePath(bmfontFilePath,imageOffset))
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
ret->setMaxLineWidth(maxLineWidth);
|
2014-01-23 13:42:27 +08:00
|
|
|
ret->setString(text);
|
2013-08-08 07:36:04 +08:00
|
|
|
ret->autorelease();
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-01-16 16:37:29 +08:00
|
|
|
Label* Label::createWithCharMap(const std::string& plistFile)
|
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto ret = new Label();
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
if (ret && ret->setCharMap(plistFile))
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Label* Label::createWithCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto ret = new Label();
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
if (ret && ret->setCharMap(texture,itemWidth,itemHeight,startCharMap))
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Label* Label::createWithCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto ret = new Label();
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
if (ret && ret->setCharMap(charMapFile,itemWidth,itemHeight,startCharMap))
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
|
|
|
delete ret;
|
|
|
|
return nullptr;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::setCharMap(const std::string& plistFile)
|
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto newAtlas = FontAtlasCache::getFontAtlasCharMap(plistFile);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
if (!newAtlas)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
reset();
|
|
|
|
return false;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
_currentLabelType = LabelType::CHARMAP;
|
2014-03-20 20:56:10 +08:00
|
|
|
setFontAtlas(newAtlas);
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
return true;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
if (!newAtlas)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
reset();
|
|
|
|
return false;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
_currentLabelType = LabelType::CHARMAP;
|
2014-03-20 20:56:10 +08:00
|
|
|
setFontAtlas(newAtlas);
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
return true;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::setCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
2014-04-02 14:57:00 +08:00
|
|
|
auto newAtlas = FontAtlasCache::getFontAtlasCharMap(charMapFile,itemWidth,itemHeight,startCharMap);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
if (!newAtlas)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
reset();
|
|
|
|
return false;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
_currentLabelType = LabelType::CHARMAP;
|
2014-03-20 20:56:10 +08:00
|
|
|
setFontAtlas(newAtlas);
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
return true;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 16:11:12 +08:00
|
|
|
Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = TextHAlignment::LEFT */,
|
|
|
|
TextVAlignment vAlignment /* = TextVAlignment::TOP */,bool useDistanceField /* = false */,bool useA8Shader /* = false */)
|
2013-11-13 11:22:34 +08:00
|
|
|
: _reusedLetter(nullptr)
|
2013-12-27 15:06:16 +08:00
|
|
|
, _commonLineHeight(0.0f)
|
2013-08-22 11:12:09 +08:00
|
|
|
, _lineBreakWithoutSpaces(false)
|
2014-03-07 15:58:49 +08:00
|
|
|
, _maxLineWidth(0)
|
|
|
|
, _labelWidth(0)
|
|
|
|
, _labelHeight(0)
|
2014-03-11 14:32:07 +08:00
|
|
|
, _labelDimensions(Size::ZERO)
|
2014-03-12 16:11:12 +08:00
|
|
|
, _hAlignment(hAlignment)
|
|
|
|
, _vAlignment(vAlignment)
|
2014-01-11 22:33:07 +08:00
|
|
|
, _currentUTF16String(nullptr)
|
|
|
|
, _originalUTF16String(nullptr)
|
2014-01-21 17:55:49 +08:00
|
|
|
, _horizontalKernings(nullptr)
|
2013-11-13 11:22:34 +08:00
|
|
|
, _fontAtlas(atlas)
|
2014-01-26 21:19:40 +08:00
|
|
|
, _isOpacityModifyRGB(false)
|
2013-12-27 15:06:16 +08:00
|
|
|
, _useDistanceField(useDistanceField)
|
|
|
|
, _useA8Shader(useA8Shader)
|
2014-02-19 20:26:14 +08:00
|
|
|
, _fontScale(1.0f)
|
2013-12-27 15:06:16 +08:00
|
|
|
, _uniformEffectColor(0)
|
2014-03-12 11:06:51 +08:00
|
|
|
, _currNumLines(-1)
|
|
|
|
, _textSprite(nullptr)
|
|
|
|
, _contentDirty(false)
|
2014-03-31 18:16:08 +08:00
|
|
|
, _shadowDirty(false)
|
2014-04-14 13:42:37 +08:00
|
|
|
, _compatibleMode(false)
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
2014-04-15 18:13:57 +08:00
|
|
|
setAnchorPoint(Vector2::ANCHOR_MIDDLE);
|
2014-03-13 18:46:35 +08:00
|
|
|
reset();
|
2014-03-14 14:59:26 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2014-04-09 21:35:08 +08:00
|
|
|
auto toBackgroundListener = EventListenerCustom::create(EVENT_COME_TO_BACKGROUND, [this](EventCustom* event){
|
|
|
|
if (_fontAtlas && _currentLabelType == LabelType::TTF)
|
|
|
|
{
|
|
|
|
_batchNodes.clear();
|
|
|
|
_batchNodes.push_back(this);
|
|
|
|
Node::removeAllChildrenWithCleanup(true);
|
|
|
|
}
|
|
|
|
});
|
2014-03-14 14:59:26 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(toBackgroundListener, this);
|
|
|
|
#endif
|
2014-04-09 21:35:08 +08:00
|
|
|
auto purgeTextureListener = EventListenerCustom::create(FontAtlas::EVENT_PURGE_TEXTURES, [this](EventCustom* event){
|
|
|
|
if (_fontAtlas && _currentLabelType == LabelType::TTF && event->getUserData() == _fontAtlas)
|
|
|
|
{
|
|
|
|
alignText();
|
|
|
|
}
|
|
|
|
});
|
2014-03-14 14:59:26 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(purgeTextureListener, this);
|
2013-07-23 02:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Label::~Label()
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2013-10-31 17:52:22 +08:00
|
|
|
delete [] _currentUTF16String;
|
|
|
|
delete [] _originalUTF16String;
|
2014-01-21 17:55:49 +08:00
|
|
|
delete [] _horizontalKernings;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
if (_fontAtlas)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2013-08-08 07:36:04 +08:00
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
2014-03-07 14:58:44 +08:00
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_reusedLetter);
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
void Label::reset()
|
|
|
|
{
|
|
|
|
TTFConfig temp;
|
|
|
|
_fontConfig = temp;
|
|
|
|
|
|
|
|
_fontDirty = false;
|
2014-04-09 23:31:05 +08:00
|
|
|
_systemFont = "Helvetica";
|
|
|
|
_systemFontSize = 12;
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
_batchNodes.clear();
|
|
|
|
_batchNodes.push_back(this);
|
|
|
|
|
|
|
|
if (_fontAtlas)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
|
|
|
_fontAtlas = nullptr;
|
2013-10-29 20:25:03 +08:00
|
|
|
}
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
_currentLabelType = LabelType::STRING_TEXTURE;
|
2014-03-05 15:54:40 +08:00
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
2014-03-13 18:46:35 +08:00
|
|
|
_shadowBlurRadius = 0;
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
Node::removeAllChildrenWithCleanup(true);
|
|
|
|
_textSprite = nullptr;
|
2014-03-26 11:09:16 +08:00
|
|
|
_shadowNode = nullptr;
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
CC_SAFE_RELEASE_NULL(_reusedLetter);
|
2014-03-20 20:56:10 +08:00
|
|
|
|
|
|
|
_textColor = Color4B::WHITE;
|
|
|
|
_textColorF = Color4F::WHITE;
|
|
|
|
setColor(Color3B::WHITE);
|
2014-03-24 14:16:27 +08:00
|
|
|
|
2014-03-26 11:09:16 +08:00
|
|
|
_shadowEnabled = false;
|
2014-03-24 14:16:27 +08:00
|
|
|
_clipEnabled = false;
|
2014-03-31 15:45:06 +08:00
|
|
|
_blendFuncDirty = false;
|
2014-01-11 22:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-03-13 21:00:36 +08:00
|
|
|
void Label::updateShaderProgram()
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
|
|
|
switch (_currLabelEffect)
|
|
|
|
{
|
|
|
|
case cocos2d::LabelEffect::NORMAL:
|
|
|
|
if (_useDistanceField)
|
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL));
|
|
|
|
else if (_useA8Shader)
|
2014-03-20 20:56:10 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_LABEL_NORMAL));
|
2014-03-05 15:54:40 +08:00
|
|
|
else
|
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
|
|
|
|
|
|
|
break;
|
2014-03-20 20:56:10 +08:00
|
|
|
case cocos2d::LabelEffect::OUTLINE:
|
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_LABEL_OUTLINE));
|
|
|
|
_uniformEffectColor = glGetUniformLocation(_shaderProgram->getProgram(), "v_effectColor");
|
2014-03-05 15:54:40 +08:00
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::GLOW:
|
|
|
|
if (_useDistanceField)
|
2014-03-20 20:56:10 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW));
|
2014-03-20 20:56:10 +08:00
|
|
|
_uniformEffectColor = glGetUniformLocation(_shaderProgram->getProgram(), "v_effectColor");
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2014-03-20 20:56:10 +08:00
|
|
|
|
|
|
|
_uniformTextColor = glGetUniformLocation(_shaderProgram->getProgram(), "v_textColor");
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
2014-03-13 20:52:33 +08:00
|
|
|
void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */)
|
2014-01-11 22:33:07 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
if (atlas == _fontAtlas)
|
|
|
|
{
|
|
|
|
FontAtlasCache::releaseFontAtlas(atlas);
|
2014-03-13 20:52:33 +08:00
|
|
|
return;
|
2014-03-13 18:46:35 +08:00
|
|
|
}
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
if (_fontAtlas)
|
2014-01-11 22:33:07 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
|
|
|
_fontAtlas = nullptr;
|
|
|
|
}
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
_fontAtlas = atlas;
|
2014-03-27 14:28:32 +08:00
|
|
|
|
|
|
|
if (_textureAtlas)
|
|
|
|
{
|
|
|
|
_textureAtlas->setTexture(_fontAtlas->getTexture(0));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SpriteBatchNode::initWithTexture(_fontAtlas->getTexture(0), 30);
|
|
|
|
}
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
if (_reusedLetter == nullptr)
|
|
|
|
{
|
|
|
|
_reusedLetter = Sprite::createWithTexture(_fontAtlas->getTexture(0));
|
|
|
|
_reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB);
|
|
|
|
_reusedLetter->retain();
|
2014-04-15 18:13:57 +08:00
|
|
|
_reusedLetter->setAnchorPoint(Vector2::ANCHOR_TOP_LEFT);
|
2014-03-13 18:46:35 +08:00
|
|
|
_reusedLetter->setBatchNode(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_reusedLetter->setTexture(_fontAtlas->getTexture(0));
|
2014-01-11 22:33:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_fontAtlas)
|
|
|
|
{
|
|
|
|
_commonLineHeight = _fontAtlas->getCommonLineHeight();
|
2014-03-10 19:42:43 +08:00
|
|
|
_contentDirty = true;
|
2014-01-11 22:33:07 +08:00
|
|
|
}
|
2014-03-13 18:46:35 +08:00
|
|
|
_useDistanceField = distanceFieldEnabled;
|
|
|
|
_useA8Shader = useA8Shader;
|
2014-03-20 20:56:10 +08:00
|
|
|
|
|
|
|
if (_currentLabelType != LabelType::TTF)
|
|
|
|
{
|
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
|
|
|
updateShaderProgram();
|
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-01-15 17:21:08 +08:00
|
|
|
bool Label::setTTFConfig(const TTFConfig& ttfConfig)
|
2014-01-11 22:33:07 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig);
|
2014-01-11 22:33:07 +08:00
|
|
|
|
|
|
|
if (!newAtlas)
|
2014-03-13 18:46:35 +08:00
|
|
|
{
|
|
|
|
reset();
|
2014-01-11 22:33:07 +08:00
|
|
|
return false;
|
2014-03-13 18:46:35 +08:00
|
|
|
}
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
_currentLabelType = LabelType::TTF;
|
2014-03-14 09:51:39 +08:00
|
|
|
setFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true);
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
_fontConfig = ttfConfig;
|
|
|
|
if (_fontConfig.outlineSize > 0)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
_fontConfig.distanceFieldEnabled = false;
|
|
|
|
_useDistanceField = false;
|
|
|
|
_useA8Shader = false;
|
|
|
|
_currLabelEffect = LabelEffect::OUTLINE;
|
2014-03-13 21:00:36 +08:00
|
|
|
updateShaderProgram();
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2014-03-20 20:56:10 +08:00
|
|
|
else
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-20 20:56:10 +08:00
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
|
|
|
updateShaderProgram();
|
|
|
|
if(ttfConfig.distanceFieldEnabled)
|
|
|
|
{
|
|
|
|
this->setFontScale(1.0f * ttfConfig.fontSize / DistanceFieldFontSize);
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
return true;
|
2014-01-11 22:33:07 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Vector2& imageOffset /* = Vector2::ZERO */)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-02-20 22:33:52 +08:00
|
|
|
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath,imageOffset);
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-01-15 17:21:08 +08:00
|
|
|
if (!newAtlas)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
reset();
|
|
|
|
return false;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2014-03-17 18:33:15 +08:00
|
|
|
_bmFontPath = bmfontFilePath;
|
2014-03-13 18:46:35 +08:00
|
|
|
_currentLabelType = LabelType::BMFONT;
|
2014-03-20 20:56:10 +08:00
|
|
|
setFontAtlas(newAtlas);
|
2014-03-13 18:46:35 +08:00
|
|
|
|
|
|
|
return true;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 20:35:36 +08:00
|
|
|
void Label::setString(const std::string& text)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
|
|
|
_originalUTF8String = text;
|
|
|
|
_contentDirty = true;
|
2014-03-07 14:58:44 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
void Label::setAlignment(TextHAlignment hAlignment,TextVAlignment vAlignment)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
|
|
|
if (hAlignment != _hAlignment || vAlignment != _vAlignment)
|
|
|
|
{
|
|
|
|
_hAlignment = hAlignment;
|
|
|
|
_vAlignment = vAlignment;
|
2014-03-10 19:42:43 +08:00
|
|
|
|
|
|
|
_contentDirty = true;
|
2014-03-07 14:58:44 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-23 13:42:27 +08:00
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
void Label::setMaxLineWidth(unsigned int maxLineWidth)
|
|
|
|
{
|
2014-03-11 14:32:07 +08:00
|
|
|
if (_labelWidth == 0 && _maxLineWidth != maxLineWidth)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
|
|
|
_maxLineWidth = maxLineWidth;
|
2014-03-10 19:42:43 +08:00
|
|
|
_contentDirty = true;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
void Label::setDimensions(unsigned int width,unsigned int height)
|
|
|
|
{
|
|
|
|
if (height != _labelHeight || width != _labelWidth)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-03-07 14:58:44 +08:00
|
|
|
_labelWidth = width;
|
2014-03-11 14:32:07 +08:00
|
|
|
_labelHeight = height;
|
|
|
|
_labelDimensions.width = width;
|
|
|
|
_labelDimensions.height = height;
|
|
|
|
|
2014-02-19 20:26:14 +08:00
|
|
|
_maxLineWidth = width;
|
2014-03-10 19:42:43 +08:00
|
|
|
_contentDirty = true;
|
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setLineBreakWithoutSpace(bool breakWithoutSpace)
|
|
|
|
{
|
|
|
|
if (breakWithoutSpace != _lineBreakWithoutSpaces)
|
|
|
|
{
|
|
|
|
_lineBreakWithoutSpaces = breakWithoutSpace;
|
2014-03-11 14:32:07 +08:00
|
|
|
_contentDirty = true;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setScale(float scale)
|
|
|
|
{
|
2013-12-13 12:42:15 +08:00
|
|
|
if (_useDistanceField)
|
|
|
|
{
|
2014-02-19 20:26:14 +08:00
|
|
|
scale *= _fontScale;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
Node::setScale(scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setScaleX(float scaleX)
|
|
|
|
{
|
2013-12-13 12:42:15 +08:00
|
|
|
if (_useDistanceField)
|
|
|
|
{
|
2014-02-19 20:26:14 +08:00
|
|
|
scaleX *= _fontScale;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
Node::setScaleX(scaleX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setScaleY(float scaleY)
|
|
|
|
{
|
2013-12-13 12:42:15 +08:00
|
|
|
if (_useDistanceField)
|
|
|
|
{
|
2014-02-19 20:26:14 +08:00
|
|
|
scaleY *= _fontScale;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
Node::setScaleY(scaleY);
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getScaleY() const
|
|
|
|
{
|
|
|
|
if (_useDistanceField)
|
|
|
|
{
|
2014-02-19 20:26:14 +08:00
|
|
|
return _scaleY / _fontScale;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _scaleY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getScaleX() const
|
|
|
|
{
|
|
|
|
if (_useDistanceField)
|
|
|
|
{
|
2014-02-19 20:26:14 +08:00
|
|
|
return _scaleX / _fontScale;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _scaleX;
|
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::alignText()
|
2014-01-23 13:42:27 +08:00
|
|
|
{
|
2014-03-07 14:58:44 +08:00
|
|
|
if (_fontAtlas == nullptr)
|
2014-02-19 20:26:14 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:41:01 +08:00
|
|
|
for (const auto& batchNode:_batchNodes)
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->removeAllQuads();
|
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
_fontAtlas->prepareLetterDefinitions(_currentUTF16String);
|
2014-02-18 14:30:51 +08:00
|
|
|
auto textures = _fontAtlas->getTextures();
|
|
|
|
if (textures.size() > _batchNodes.size())
|
|
|
|
{
|
2014-02-19 20:26:14 +08:00
|
|
|
for (auto index = _batchNodes.size(); index < textures.size(); ++index)
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
2014-03-20 11:58:36 +08:00
|
|
|
auto batchNode = SpriteBatchNode::createWithTexture(textures[index]);
|
2014-04-15 18:13:57 +08:00
|
|
|
batchNode->setAnchorPoint(Vector2::ANCHOR_TOP_LEFT);
|
|
|
|
batchNode->setPosition(Vector2::ZERO);
|
2014-02-18 14:30:51 +08:00
|
|
|
Node::addChild(batchNode,0,Node::INVALID_TAG);
|
|
|
|
_batchNodes.push_back(batchNode);
|
|
|
|
}
|
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
LabelTextFormatter::createStringSprites(this);
|
2014-02-19 20:26:14 +08:00
|
|
|
if(_maxLineWidth > 0 && _contentSize.width > _maxLineWidth && LabelTextFormatter::multilineText(this) )
|
2013-08-08 07:36:04 +08:00
|
|
|
LabelTextFormatter::createStringSprites(this);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-11 14:32:07 +08:00
|
|
|
if(_labelWidth > 0 || (_currNumLines > 1 && _hAlignment != TextHAlignment::LEFT))
|
2014-02-10 11:21:54 +08:00
|
|
|
LabelTextFormatter::alignText(this);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
int strLen = cc_wcslen(_currentUTF16String);
|
2014-02-19 20:26:14 +08:00
|
|
|
Rect uvRect;
|
|
|
|
Sprite* letterSprite;
|
2013-12-19 10:33:04 +08:00
|
|
|
for(const auto &child : _children) {
|
|
|
|
int tag = child->getTag();
|
2014-02-18 14:30:51 +08:00
|
|
|
if(tag >= strLen)
|
2014-02-19 20:26:14 +08:00
|
|
|
{
|
2013-12-19 10:33:04 +08:00
|
|
|
SpriteBatchNode::removeChild(child, true);
|
2014-02-19 20:26:14 +08:00
|
|
|
}
|
|
|
|
else if(tag >= 0)
|
|
|
|
{
|
|
|
|
letterSprite = dynamic_cast<Sprite*>(child);
|
|
|
|
if (letterSprite)
|
|
|
|
{
|
|
|
|
uvRect.size.height = _lettersInfo[tag].def.height;
|
|
|
|
uvRect.size.width = _lettersInfo[tag].def.width;
|
|
|
|
uvRect.origin.x = _lettersInfo[tag].def.U;
|
|
|
|
uvRect.origin.y = _lettersInfo[tag].def.V;
|
|
|
|
|
|
|
|
letterSprite->setTexture(textures[_lettersInfo[tag].def.textureID]);
|
|
|
|
letterSprite->setTextureRect(uvRect);
|
|
|
|
}
|
|
|
|
}
|
2013-12-19 10:33:04 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-17 18:33:15 +08:00
|
|
|
updateQuads();
|
2014-01-23 16:27:28 +08:00
|
|
|
|
|
|
|
updateColor();
|
2013-07-23 02:17:51 +08:00
|
|
|
}
|
|
|
|
|
2014-01-21 17:55:49 +08:00
|
|
|
bool Label::computeHorizontalKernings(unsigned short int *stringToRender)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-01-21 17:55:49 +08:00
|
|
|
if (_horizontalKernings)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-01-21 17:55:49 +08:00
|
|
|
delete [] _horizontalKernings;
|
2014-03-07 14:58:44 +08:00
|
|
|
_horizontalKernings = nullptr;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
int letterCount = 0;
|
2014-01-21 17:55:49 +08:00
|
|
|
_horizontalKernings = _fontAtlas->getFont()->getHorizontalKerningForTextUTF16(stringToRender, letterCount);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-01-21 17:55:49 +08:00
|
|
|
if(!_horizontalKernings)
|
2013-08-08 07:36:04 +08:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::setOriginalString(unsigned short *stringToSet)
|
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
if (_originalUTF16String)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
delete [] _originalUTF16String;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
int newStringLenght = cc_wcslen(stringToSet);
|
2013-10-29 20:25:03 +08:00
|
|
|
_originalUTF16String = new unsigned short int [newStringLenght + 1];
|
|
|
|
memset(_originalUTF16String, 0, (newStringLenght + 1) * 2);
|
|
|
|
memcpy(_originalUTF16String, stringToSet, (newStringLenght * 2));
|
|
|
|
_originalUTF16String[newStringLenght] = 0;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::setCurrentString(unsigned short *stringToSet)
|
|
|
|
{
|
|
|
|
// set the new string
|
2013-10-29 20:25:03 +08:00
|
|
|
if (_currentUTF16String)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
delete [] _currentUTF16String;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
_currentUTF16String = stringToSet;
|
2014-02-10 11:21:54 +08:00
|
|
|
computeStringNumLines();
|
2014-03-07 14:58:44 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
// compute the advances
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_fontAtlas)
|
|
|
|
{
|
|
|
|
computeHorizontalKernings(stringToSet);
|
|
|
|
}
|
|
|
|
return true;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-17 18:33:15 +08:00
|
|
|
void Label::updateQuads()
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-03-17 18:33:15 +08:00
|
|
|
int index;
|
|
|
|
for (int ctr = 0; ctr < _limitShowCount; ++ctr)
|
|
|
|
{
|
|
|
|
auto &letterDef = _lettersInfo[ctr].def;
|
|
|
|
|
|
|
|
if (letterDef.validDefinition)
|
|
|
|
{
|
|
|
|
_reusedRect.size.height = letterDef.height;
|
|
|
|
_reusedRect.size.width = letterDef.width;
|
|
|
|
_reusedRect.origin.x = letterDef.U;
|
|
|
|
_reusedRect.origin.y = letterDef.V;
|
|
|
|
_reusedLetter->setTextureRect(_reusedRect,false,_reusedRect.size);
|
|
|
|
|
|
|
|
_reusedLetter->setPosition(_lettersInfo[ctr].position);
|
2014-03-20 10:58:04 +08:00
|
|
|
index = static_cast<int>(_batchNodes[letterDef.textureID]->getTextureAtlas()->getTotalQuads());
|
2014-03-17 18:33:15 +08:00
|
|
|
_lettersInfo[ctr].atlasIndex = index;
|
|
|
|
_batchNodes[letterDef.textureID]->insertQuadFromSprite(_reusedLetter,index);
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:13:57 +08:00
|
|
|
bool Label::recordLetterInfo(const cocos2d::Vector2& point,const FontLetterDefinition& letterDef, int spriteIndex)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-11-13 11:22:34 +08:00
|
|
|
if (static_cast<std::size_t>(spriteIndex) >= _lettersInfo.size())
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
LetterInfo tmpInfo;
|
|
|
|
_lettersInfo.push_back(tmpInfo);
|
2014-02-19 20:26:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_lettersInfo[spriteIndex].def = letterDef;
|
2013-10-29 20:25:03 +08:00
|
|
|
_lettersInfo[spriteIndex].position = point;
|
|
|
|
_lettersInfo[spriteIndex].contentSize.width = _lettersInfo[spriteIndex].def.width;
|
|
|
|
_lettersInfo[spriteIndex].contentSize.height = _lettersInfo[spriteIndex].def.height;
|
2014-03-07 14:58:44 +08:00
|
|
|
_limitShowCount++;
|
2013-08-08 07:36:04 +08:00
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
return _lettersInfo[spriteIndex].def.validDefinition;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
bool Label::recordPlaceholderInfo(int spriteIndex)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-11-13 11:22:34 +08:00
|
|
|
if (static_cast<std::size_t>(spriteIndex) >= _lettersInfo.size())
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
LetterInfo tmpInfo;
|
|
|
|
_lettersInfo.push_back(tmpInfo);
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
|
|
|
|
_lettersInfo[spriteIndex].def.validDefinition = false;
|
2014-03-07 14:58:44 +08:00
|
|
|
_limitShowCount++;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
return false;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
void Label::addChild(Node * child, int zOrder/* =0 */, int tag/* =0 */)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
CCASSERT(0, "addChild: is not supported on Label.");
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 16:23:23 +08:00
|
|
|
void Label::sortAllChildren()
|
|
|
|
{
|
2014-03-30 20:45:44 +08:00
|
|
|
// Label ignore sort children
|
2014-03-26 16:23:23 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
void Label::enableGlow(const Color4B& glowColor)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-11 14:32:07 +08:00
|
|
|
if(! _useDistanceField)
|
2014-03-05 15:54:40 +08:00
|
|
|
return;
|
|
|
|
_currLabelEffect = LabelEffect::GLOW;
|
|
|
|
_effectColor = glowColor;
|
2014-03-20 20:56:10 +08:00
|
|
|
_effectColorF.r = _effectColor.r / 255.0f;
|
|
|
|
_effectColorF.g = _effectColor.g / 255.0f;
|
|
|
|
_effectColorF.b = _effectColor.b / 255.0f;
|
|
|
|
_effectColorF.a = _effectColor.a / 255.0f;
|
2014-03-13 21:00:36 +08:00
|
|
|
updateShaderProgram();
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = -1 */)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-20 20:56:10 +08:00
|
|
|
_effectColor = outlineColor;
|
|
|
|
_effectColorF.r = _effectColor.r / 255.0f;
|
|
|
|
_effectColorF.g = _effectColor.g / 255.0f;
|
|
|
|
_effectColorF.b = _effectColor.b / 255.0f;
|
|
|
|
_effectColorF.a = _effectColor.a / 255.0f;
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
if (outlineSize > 0)
|
|
|
|
{
|
2014-04-09 21:35:08 +08:00
|
|
|
_outlineSize = outlineSize;
|
2014-03-10 20:35:36 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_fontConfig.outlineSize != outlineSize)
|
|
|
|
{
|
|
|
|
auto config = _fontConfig;
|
|
|
|
config.outlineSize = outlineSize;
|
|
|
|
setTTFConfig(config);
|
2014-03-13 21:00:36 +08:00
|
|
|
updateShaderProgram();
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
|
|
|
|
_currLabelEffect = LabelEffect::OUTLINE;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
2014-03-21 13:52:12 +08:00
|
|
|
void Label::enableShadow(const Color4B& shadowColor /* = Color4B::BLACK */,const Size &offset /* = Size(2 ,-2)*/, int blurRadius /* = 0 */)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-26 11:09:16 +08:00
|
|
|
_shadowEnabled = true;
|
2014-03-31 18:16:08 +08:00
|
|
|
_shadowDirty = true;
|
2014-03-26 11:09:16 +08:00
|
|
|
|
2014-04-01 14:15:35 +08:00
|
|
|
_shadowColor.r = shadowColor.r;
|
|
|
|
_shadowColor.g = shadowColor.g;
|
|
|
|
_shadowColor.b = shadowColor.b;
|
|
|
|
_shadowOpacity = shadowColor.a / 255.0f;
|
2014-03-26 11:09:16 +08:00
|
|
|
|
2014-03-25 10:41:53 +08:00
|
|
|
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
|
|
|
|
_shadowOffset.width = offset.width * contentScaleFactor;
|
|
|
|
_shadowOffset.height = offset.height * contentScaleFactor;
|
2014-03-05 15:54:40 +08:00
|
|
|
//todo:support blur for shadow
|
|
|
|
_shadowBlurRadius = 0;
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2014-03-26 11:09:16 +08:00
|
|
|
if (_textSprite && _shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->setColor(_shadowColor);
|
2014-04-01 14:15:35 +08:00
|
|
|
_shadowNode->setOpacity(_shadowOpacity * _displayedOpacity);
|
2014-03-26 11:09:16 +08:00
|
|
|
_shadowNode->setPosition(_shadowOffset.width, _shadowOffset.height);
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::disableEffect()
|
|
|
|
{
|
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE)
|
|
|
|
{
|
|
|
|
_fontConfig.outlineSize = 0;
|
|
|
|
setTTFConfig(_fontConfig);
|
|
|
|
}
|
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
2014-03-13 21:00:36 +08:00
|
|
|
updateShaderProgram();
|
2014-03-10 19:42:43 +08:00
|
|
|
_contentDirty = true;
|
2014-03-26 11:09:16 +08:00
|
|
|
_shadowEnabled = false;
|
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
Node::removeChild(_shadowNode,true);
|
|
|
|
_shadowNode = nullptr;
|
|
|
|
}
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
2014-02-19 20:26:14 +08:00
|
|
|
void Label::setFontScale(float fontScale)
|
2013-12-13 12:42:15 +08:00
|
|
|
{
|
2014-03-14 14:59:26 +08:00
|
|
|
_fontScale = fontScale * CC_CONTENT_SCALE_FACTOR();
|
2014-02-19 20:26:14 +08:00
|
|
|
Node::setScale(_fontScale);
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
2014-04-09 10:06:30 +08:00
|
|
|
void Label::onDraw(const Matrix& transform, bool transformUpdated)
|
2013-12-13 12:42:15 +08:00
|
|
|
{
|
2014-02-19 20:26:14 +08:00
|
|
|
CC_PROFILER_START("Label - draw");
|
2013-12-13 12:42:15 +08:00
|
|
|
|
|
|
|
// Optimization: Fast Dispatch
|
2014-02-18 14:30:51 +08:00
|
|
|
if( _batchNodes.size() == 1 && _textureAtlas->getTotalQuads() == 0 )
|
2013-12-13 12:42:15 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
_shaderProgram->use();
|
|
|
|
GL::blendFunc( _blendFunc.src, _blendFunc.dst );
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-03-26 11:09:16 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
|
|
|
_shaderProgram->setUniformLocationWith4f(_uniformTextColor,
|
|
|
|
_textColorF.r,_textColorF.g,_textColorF.b,_textColorF.a);
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE || _currLabelEffect == LabelEffect::GLOW)
|
2013-12-13 12:42:15 +08:00
|
|
|
{
|
2014-03-20 20:56:10 +08:00
|
|
|
_shaderProgram->setUniformLocationWith4f(_uniformEffectColor,
|
|
|
|
_effectColorF.r,_effectColorF.g,_effectColorF.b,_effectColorF.a);
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
2014-04-01 11:37:36 +08:00
|
|
|
|
|
|
|
if(_shadowEnabled && _shadowBlurRadius <= 0)
|
2014-02-18 16:41:01 +08:00
|
|
|
{
|
2014-03-05 15:54:40 +08:00
|
|
|
drawShadowWithoutBlur();
|
2014-02-18 16:41:01 +08:00
|
|
|
}
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-03-06 07:49:08 +08:00
|
|
|
_shaderProgram->setUniformsForBuiltins(transform);
|
2014-03-31 18:16:08 +08:00
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
for(const auto &child: _children)
|
|
|
|
{
|
|
|
|
if(child->getTag() >= 0)
|
|
|
|
child->updateTransform();
|
2014-03-20 20:56:10 +08:00
|
|
|
}
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-02-18 16:41:01 +08:00
|
|
|
for (const auto& batchNode:_batchNodes)
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->drawQuads();
|
|
|
|
}
|
2013-12-13 12:42:15 +08:00
|
|
|
|
2014-02-19 20:26:14 +08:00
|
|
|
CC_PROFILER_STOP("Label - draw");
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
void Label::drawShadowWithoutBlur()
|
|
|
|
{
|
|
|
|
Color3B oldColor = _realColor;
|
|
|
|
GLubyte oldOPacity = _displayedOpacity;
|
2014-04-01 14:15:35 +08:00
|
|
|
_displayedOpacity = _shadowOpacity * _displayedOpacity;
|
2014-03-26 11:09:16 +08:00
|
|
|
setColor(_shadowColor);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-31 18:16:08 +08:00
|
|
|
_shaderProgram->setUniformsForBuiltins(_shadowTransform);
|
2014-03-05 15:54:40 +08:00
|
|
|
for(const auto &child: _children)
|
|
|
|
{
|
|
|
|
child->updateTransform();
|
|
|
|
}
|
|
|
|
for (const auto& batchNode:_batchNodes)
|
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->drawQuads();
|
|
|
|
}
|
|
|
|
|
2014-03-26 11:09:16 +08:00
|
|
|
_displayedOpacity = oldOPacity;
|
|
|
|
setColor(oldColor);
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
2014-04-08 22:07:35 +08:00
|
|
|
void Label::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
|
2014-01-04 10:56:14 +08:00
|
|
|
{
|
2014-01-19 03:35:27 +08:00
|
|
|
_customCommand.init(_globalZOrder);
|
2014-03-06 07:49:08 +08:00
|
|
|
_customCommand.func = CC_CALLBACK_0(Label::onDraw, this, transform, transformUpdated);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2014-01-04 10:56:14 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
void Label::createSpriteWithFontDefinition()
|
|
|
|
{
|
2014-03-10 20:35:36 +08:00
|
|
|
_currentLabelType = LabelType::STRING_TEXTURE;
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
auto texture = new Texture2D;
|
|
|
|
texture->initWithString(_originalUTF8String.c_str(),_fontDefinition);
|
|
|
|
|
|
|
|
_textSprite = Sprite::createWithTexture(texture);
|
2014-04-15 18:13:57 +08:00
|
|
|
_textSprite->setAnchorPoint(Vector2::ANCHOR_BOTTOM_LEFT);
|
2014-03-10 19:42:43 +08:00
|
|
|
this->setContentSize(_textSprite->getContentSize());
|
|
|
|
texture->release();
|
2014-03-31 15:45:06 +08:00
|
|
|
if (_blendFuncDirty)
|
|
|
|
{
|
|
|
|
_textSprite->setBlendFunc(_blendFunc);
|
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
|
|
|
|
Node::addChild(_textSprite,0,Node::INVALID_TAG);
|
2014-03-20 20:56:10 +08:00
|
|
|
|
|
|
|
_textSprite->updateDisplayedColor(_displayedColor);
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
void Label::setFontDefinition(const FontDefinition& textDefinition)
|
|
|
|
{
|
|
|
|
_fontDefinition = textDefinition;
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
|
|
|
|
if (_fontDefinition._stroke._strokeEnabled)
|
|
|
|
{
|
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
|
|
|
}
|
|
|
|
_fontDefinition._stroke._strokeEnabled = false;
|
|
|
|
#endif
|
|
|
|
if (_fontDefinition._shadow._shadowEnabled)
|
|
|
|
{
|
|
|
|
_fontDefinition._shadow._shadowEnabled = false;
|
|
|
|
enableShadow(Color4B(0,0,0,255 * _fontDefinition._shadow._shadowOpacity),_fontDefinition._shadow._shadowOffset,_fontDefinition._shadow._shadowBlur);
|
|
|
|
}
|
2014-04-14 13:42:37 +08:00
|
|
|
_compatibleMode = true;
|
2014-04-09 21:35:08 +08:00
|
|
|
}
|
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
void Label::updateContent()
|
|
|
|
{
|
|
|
|
auto utf16String = cc_utf8_to_utf16(_originalUTF8String.c_str());
|
|
|
|
setCurrentString(utf16String);
|
|
|
|
setOriginalString(utf16String);
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_textSprite)
|
|
|
|
{
|
2014-03-11 14:32:07 +08:00
|
|
|
Node::removeChild(_textSprite,true);
|
2014-03-10 19:42:43 +08:00
|
|
|
_textSprite = nullptr;
|
2014-03-26 11:09:16 +08:00
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
Node::removeChild(_shadowNode,true);
|
|
|
|
_shadowNode = nullptr;
|
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_fontAtlas)
|
|
|
|
{
|
|
|
|
alignText();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-14 13:42:37 +08:00
|
|
|
if (!_compatibleMode)
|
2014-04-14 11:33:46 +08:00
|
|
|
{
|
|
|
|
_fontDefinition._fontName = _systemFont;
|
|
|
|
_fontDefinition._fontSize = _systemFontSize;
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-04-14 11:33:46 +08:00
|
|
|
_fontDefinition._alignment = _hAlignment;
|
|
|
|
_fontDefinition._vertAlignment = _vAlignment;
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-04-14 11:33:46 +08:00
|
|
|
_fontDefinition._dimensions.width = _labelWidth;
|
|
|
|
_fontDefinition._dimensions.height = _labelHeight;
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-04-14 11:33:46 +08:00
|
|
|
_fontDefinition._fontFillColor.r = _textColor.r;
|
|
|
|
_fontDefinition._fontFillColor.g = _textColor.g;
|
|
|
|
_fontDefinition._fontFillColor.b = _textColor.b;
|
2014-03-20 20:56:10 +08:00
|
|
|
|
2014-04-14 11:33:46 +08:00
|
|
|
_fontDefinition._shadow._shadowEnabled = false;
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-04-14 11:33:46 +08:00
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE && _outlineSize > 0)
|
|
|
|
{
|
|
|
|
_fontDefinition._stroke._strokeEnabled = true;
|
|
|
|
_fontDefinition._stroke._strokeSize = _outlineSize;
|
|
|
|
_fontDefinition._stroke._strokeColor.r = _effectColor.r;
|
|
|
|
_fontDefinition._stroke._strokeColor.g = _effectColor.g;
|
|
|
|
_fontDefinition._stroke._strokeColor.b = _effectColor.b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_fontDefinition._stroke._strokeEnabled = false;
|
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
|
2014-04-14 11:33:46 +08:00
|
|
|
if (_fontDefinition._stroke._strokeEnabled)
|
|
|
|
{
|
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
|
|
|
}
|
|
|
|
_fontDefinition._stroke._strokeEnabled = false;
|
2014-04-09 21:35:08 +08:00
|
|
|
#endif
|
2014-04-14 11:33:46 +08:00
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
createSpriteWithFontDefinition();
|
|
|
|
}
|
|
|
|
_contentDirty = false;
|
|
|
|
}
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
void Label::updateFont()
|
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
if (_fontAtlas)
|
2014-03-13 18:46:35 +08:00
|
|
|
{
|
2014-04-09 21:35:08 +08:00
|
|
|
_batchNodes.clear();
|
|
|
|
_batchNodes.push_back(this);
|
|
|
|
|
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
|
|
|
_fontAtlas = nullptr;
|
2014-03-13 18:46:35 +08:00
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
_contentDirty = true;
|
|
|
|
_fontDirty = false;
|
|
|
|
}
|
|
|
|
|
2014-03-26 11:09:16 +08:00
|
|
|
void Label::drawTextSprite(Renderer *renderer, bool parentTransformUpdated)
|
|
|
|
{
|
|
|
|
if (_fontDefinition._fontFillColor != _textColor)
|
|
|
|
{
|
2014-04-09 21:35:08 +08:00
|
|
|
updateContent();
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_shadowEnabled && _shadowNode == nullptr)
|
|
|
|
{
|
|
|
|
_shadowNode = Sprite::createWithTexture(_textSprite->getTexture());
|
2014-04-09 20:41:09 +08:00
|
|
|
if (_shadowNode)
|
2014-03-31 15:45:06 +08:00
|
|
|
{
|
2014-04-09 20:41:09 +08:00
|
|
|
if (_blendFuncDirty)
|
2014-04-09 21:35:08 +08:00
|
|
|
{
|
2014-04-09 20:41:09 +08:00
|
|
|
_shadowNode->setBlendFunc(_blendFunc);
|
2014-04-09 21:35:08 +08:00
|
|
|
}
|
2014-04-15 18:13:57 +08:00
|
|
|
_shadowNode->setAnchorPoint(Vector2::ANCHOR_BOTTOM_LEFT);
|
2014-04-09 20:41:09 +08:00
|
|
|
_shadowNode->setColor(_shadowColor);
|
|
|
|
_shadowNode->setOpacity(_shadowOpacity * _displayedOpacity);
|
|
|
|
_shadowNode->setPosition(_shadowOffset.width, _shadowOffset.height);
|
|
|
|
Node::addChild(_shadowNode,0,Node::INVALID_TAG);
|
2014-03-31 15:45:06 +08:00
|
|
|
}
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->visit(renderer, _modelViewTransform, parentTransformUpdated);
|
|
|
|
}
|
|
|
|
_textSprite->visit(renderer, _modelViewTransform, parentTransformUpdated);
|
|
|
|
}
|
|
|
|
|
2014-04-08 21:45:54 +08:00
|
|
|
void Label::visit(Renderer *renderer, const Matrix &parentTransform, bool parentTransformUpdated)
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
2014-03-12 15:03:22 +08:00
|
|
|
if (! _visible || _originalUTF8String.empty())
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-03-13 18:46:35 +08:00
|
|
|
if (_fontDirty)
|
|
|
|
{
|
|
|
|
updateFont();
|
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_contentDirty)
|
|
|
|
{
|
|
|
|
updateContent();
|
|
|
|
}
|
2014-03-01 03:20:53 +08:00
|
|
|
|
2014-03-31 18:16:08 +08:00
|
|
|
bool dirty = parentTransformUpdated || _transformUpdated;
|
|
|
|
|
|
|
|
if (_shadowEnabled && _shadowBlurRadius <= 0 && (_shadowDirty || dirty))
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-31 18:16:08 +08:00
|
|
|
_position.x += _shadowOffset.width;
|
|
|
|
_position.y += _shadowOffset.height;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
|
|
|
|
|
|
|
_shadowTransform = transform(parentTransform);
|
|
|
|
|
|
|
|
_position.x -= _shadowOffset.width;
|
|
|
|
_position.y -= _shadowOffset.height;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
|
|
|
|
|
|
|
_shadowDirty = false;
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2014-02-18 14:30:51 +08:00
|
|
|
|
2014-03-31 18:16:08 +08:00
|
|
|
if(dirty)
|
|
|
|
{
|
|
|
|
_modelViewTransform = transform(parentTransform);
|
|
|
|
}
|
|
|
|
_transformUpdated = false;
|
2014-02-18 14:30:51 +08:00
|
|
|
|
2014-03-31 18:16:08 +08:00
|
|
|
// IMPORTANT:
|
2014-04-10 11:14:46 +08:00
|
|
|
// To ease the migration to v3.0, we still support the Matrix stack,
|
2014-03-31 18:16:08 +08:00
|
|
|
// but it is deprecated and your code should not rely on it
|
2014-04-03 14:32:49 +08:00
|
|
|
Director* director = Director::getInstance();
|
|
|
|
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
|
|
|
|
|
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-31 18:16:08 +08:00
|
|
|
if (_textSprite)
|
|
|
|
{
|
|
|
|
drawTextSprite(renderer,dirty);
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2014-03-31 18:16:08 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
draw(renderer, _modelViewTransform, dirty);
|
|
|
|
}
|
|
|
|
|
2014-04-03 14:32:49 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-02-18 14:30:51 +08:00
|
|
|
setOrderOfArrival(0);
|
|
|
|
}
|
|
|
|
|
2014-04-10 10:23:00 +08:00
|
|
|
void Label::setSystemFontName(const std::string& systemFont)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
if (systemFont != _systemFont)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
_systemFont = systemFont;
|
2014-03-13 18:46:35 +08:00
|
|
|
_fontDirty = true;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
void Label::setSystemFontSize(float fontSize)
|
2014-03-12 14:49:19 +08:00
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
if (_systemFontSize != fontSize)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
_systemFontSize = fontSize;
|
2014-03-13 18:46:35 +08:00
|
|
|
_fontDirty = true;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
///// PROTOCOL STUFF
|
2014-03-18 13:48:41 +08:00
|
|
|
Sprite * Label::getLetter(int letterIndex)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-03-17 18:33:15 +08:00
|
|
|
if (_fontDirty)
|
|
|
|
{
|
2014-03-18 13:48:41 +08:00
|
|
|
updateFont();
|
2014-04-09 23:31:05 +08:00
|
|
|
return nullptr;
|
2014-03-17 18:33:15 +08:00
|
|
|
}
|
2014-04-09 23:31:05 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_contentDirty)
|
|
|
|
{
|
|
|
|
updateContent();
|
|
|
|
}
|
|
|
|
|
2014-03-18 13:48:41 +08:00
|
|
|
if (! _textSprite && letterIndex < _limitShowCount)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
2014-03-18 13:48:41 +08:00
|
|
|
const auto &letter = _lettersInfo[letterIndex];
|
2014-03-17 18:33:15 +08:00
|
|
|
|
2014-03-18 13:48:41 +08:00
|
|
|
if(! letter.def.validDefinition)
|
2013-10-29 20:25:03 +08:00
|
|
|
return nullptr;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-18 13:48:41 +08:00
|
|
|
Sprite* sp = static_cast<Sprite*>(this->getChildByTag(letterIndex));
|
2013-10-29 20:25:03 +08:00
|
|
|
|
|
|
|
if (!sp)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-10-29 20:25:03 +08:00
|
|
|
Rect uvRect;
|
2014-03-18 13:48:41 +08:00
|
|
|
uvRect.size.height = letter.def.height;
|
|
|
|
uvRect.size.width = letter.def.width;
|
|
|
|
uvRect.origin.x = letter.def.U;
|
|
|
|
uvRect.origin.y = letter.def.V;
|
|
|
|
|
|
|
|
sp = Sprite::createWithTexture(_fontAtlas->getTexture(letter.def.textureID),uvRect);
|
|
|
|
sp->setBatchNode(_batchNodes[letter.def.textureID]);
|
2014-04-15 18:13:57 +08:00
|
|
|
sp->setPosition(Vector2(letter.position.x + uvRect.size.width / 2,
|
2014-03-18 13:48:41 +08:00
|
|
|
letter.position.y - uvRect.size.height / 2));
|
2013-10-29 20:25:03 +08:00
|
|
|
sp->setOpacity(_realOpacity);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-03-18 13:48:41 +08:00
|
|
|
_batchNodes[letter.def.textureID]->addSpriteWithoutQuad(sp, letter.atlasIndex, letterIndex);
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
return sp;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
return nullptr;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2013-09-13 11:46:46 +08:00
|
|
|
int Label::getCommonLineHeight() const
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-03-10 19:42:43 +08:00
|
|
|
return _textSprite ? 0 : _commonLineHeight;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 11:21:54 +08:00
|
|
|
void Label::computeStringNumLines()
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
|
|
|
int quantityOfLines = 1;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-04-09 20:41:09 +08:00
|
|
|
int stringLen = _currentUTF16String ? cc_wcslen(_currentUTF16String) : -1;
|
2014-01-22 14:57:11 +08:00
|
|
|
if (stringLen < 1)
|
2014-02-10 11:21:54 +08:00
|
|
|
{
|
|
|
|
_currNumLines = stringLen;
|
|
|
|
return;
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
// count number of lines
|
2014-04-09 20:41:09 +08:00
|
|
|
for (int i = 0; i < stringLen - 1; ++i)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-02-10 11:21:54 +08:00
|
|
|
if (_currentUTF16String[i] == '\n')
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
|
|
|
quantityOfLines++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2014-02-10 11:21:54 +08:00
|
|
|
_currNumLines = quantityOfLines;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
int Label::getStringLength() const
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-03-20 10:58:04 +08:00
|
|
|
return _currentUTF16String ? cc_wcslen(_currentUTF16String) : (int)_originalUTF8String.length();
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RGBA protocol
|
2014-03-10 20:35:36 +08:00
|
|
|
bool Label::isOpacityModifyRGB() const
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2013-08-13 04:29:54 +08:00
|
|
|
return _isOpacityModifyRGB;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setOpacityModifyRGB(bool isOpacityModifyRGB)
|
|
|
|
{
|
2013-08-13 04:29:54 +08:00
|
|
|
_isOpacityModifyRGB = isOpacityModifyRGB;
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-12-19 10:33:04 +08:00
|
|
|
for(const auto& child: _children) {
|
2013-12-19 17:05:59 +08:00
|
|
|
child->setOpacityModifyRGB(_isOpacityModifyRGB);
|
2013-12-19 10:33:04 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
_reusedLetter->setOpacityModifyRGB(true);
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-19 10:10:47 +08:00
|
|
|
void Label::updateDisplayedColor(const Color3B& parentColor)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-03-19 10:10:47 +08:00
|
|
|
_displayedColor.r = _realColor.r * parentColor.r/255.0;
|
|
|
|
_displayedColor.g = _realColor.g * parentColor.g/255.0;
|
|
|
|
_displayedColor.b = _realColor.b * parentColor.b/255.0;
|
|
|
|
updateColor();
|
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_textSprite)
|
|
|
|
{
|
2014-03-19 10:10:47 +08:00
|
|
|
_textSprite->updateDisplayedColor(_displayedColor);
|
2014-03-26 11:09:16 +08:00
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->updateDisplayedColor(_displayedColor);
|
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
void Label::updateDisplayedOpacity(GLubyte parentOpacity)
|
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity * parentOpacity/255.0;
|
|
|
|
updateColor();
|
|
|
|
|
|
|
|
if (_textSprite)
|
|
|
|
{
|
|
|
|
_textSprite->updateDisplayedOpacity(_displayedOpacity);
|
2014-03-26 11:09:16 +08:00
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->updateDisplayedOpacity(_displayedOpacity);
|
|
|
|
}
|
2014-03-20 20:56:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setTextColor(const Color4B &color)
|
|
|
|
{
|
|
|
|
_textColor = color;
|
2014-03-21 09:47:29 +08:00
|
|
|
_textColorF.r = _textColor.r / 255.0f;
|
|
|
|
_textColorF.g = _textColor.g / 255.0f;
|
|
|
|
_textColorF.b = _textColor.b / 255.0f;
|
|
|
|
_textColorF.a = _textColor.a / 255.0f;
|
2014-03-20 20:56:10 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 16:38:47 +08:00
|
|
|
void Label::updateColor()
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-03-05 14:41:59 +08:00
|
|
|
if (nullptr == _textureAtlas)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-19 10:10:47 +08:00
|
|
|
Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity );
|
|
|
|
|
2013-12-11 16:38:47 +08:00
|
|
|
// special opacity for premultiplied textures
|
|
|
|
if (_isOpacityModifyRGB)
|
2013-10-29 20:25:03 +08:00
|
|
|
{
|
2013-12-11 16:38:47 +08:00
|
|
|
color4.r *= _displayedOpacity/255.0f;
|
|
|
|
color4.g *= _displayedOpacity/255.0f;
|
|
|
|
color4.b *= _displayedOpacity/255.0f;
|
|
|
|
}
|
2014-03-05 14:41:59 +08:00
|
|
|
|
|
|
|
cocos2d::TextureAtlas* textureAtlas;
|
|
|
|
V3F_C4B_T2F_Quad *quads;
|
|
|
|
for (const auto& batchNode:_batchNodes)
|
2013-12-11 16:38:47 +08:00
|
|
|
{
|
2014-03-05 14:41:59 +08:00
|
|
|
textureAtlas = batchNode->getTextureAtlas();
|
|
|
|
quads = textureAtlas->getQuads();
|
|
|
|
auto count = textureAtlas->getTotalQuads();
|
|
|
|
|
|
|
|
for (int index = 0; index < count; ++index)
|
|
|
|
{
|
|
|
|
quads[index].bl.colors = color4;
|
|
|
|
quads[index].br.colors = color4;
|
|
|
|
quads[index].tl.colors = color4;
|
|
|
|
quads[index].tr.colors = color4;
|
|
|
|
textureAtlas->updateQuad(&quads[index], index);
|
|
|
|
}
|
2013-12-06 18:07:16 +08:00
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string Label::getDescription() const
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
return StringUtils::format("<Label | Tag = %d, Label = '%s'>", _tag, cc_utf16_to_utf8(_currentUTF16String,-1,nullptr,nullptr));
|
2013-12-13 06:30:22 +08:00
|
|
|
}
|
|
|
|
|
2014-03-11 15:03:16 +08:00
|
|
|
const Size& Label::getContentSize() const
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2014-03-13 18:46:35 +08:00
|
|
|
if (_fontDirty)
|
|
|
|
{
|
|
|
|
const_cast<Label*>(this)->updateFont();
|
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_contentDirty)
|
|
|
|
{
|
2014-03-11 15:03:16 +08:00
|
|
|
const_cast<Label*>(this)->updateContent();
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2014-03-28 15:58:28 +08:00
|
|
|
return _contentSize;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2014-03-11 18:06:14 +08:00
|
|
|
|
2014-03-28 11:03:39 +08:00
|
|
|
Rect Label::getBoundingBox() const
|
|
|
|
{
|
|
|
|
const_cast<Label*>(this)->getContentSize();
|
|
|
|
|
|
|
|
return Node::getBoundingBox();
|
|
|
|
}
|
|
|
|
|
2014-03-31 15:45:06 +08:00
|
|
|
void Label::setBlendFunc(const BlendFunc &blendFunc)
|
|
|
|
{
|
|
|
|
_blendFunc = blendFunc;
|
|
|
|
_blendFuncDirty = true;
|
|
|
|
if (_textSprite)
|
|
|
|
{
|
|
|
|
_textSprite->setBlendFunc(blendFunc);
|
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 11:37:36 +08:00
|
|
|
|
2013-12-06 18:16:26 +08:00
|
|
|
NS_CC_END
|