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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCLabel.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCFontAtlasCache.h"
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2015-07-07 15:41:54 +08:00
|
|
|
#include "2d/CCSpriteBatchNode.h"
|
2014-05-10 12:29:24 +08:00
|
|
|
#include "2d/CCLabelTextFormatter.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2014-05-10 12:29:24 +08:00
|
|
|
#include "2d/CCFont.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-08-26 18:19:28 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-05-10 12:29:24 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#include "base/CCEventCustom.h"
|
2014-05-10 12:29:24 +08:00
|
|
|
#include "deprecated/CCString.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
|
|
|
|
2015-07-08 15:30:34 +08:00
|
|
|
/**
|
|
|
|
* LabelLetter used to update the quad in texture atlas without SpriteBatchNode.
|
|
|
|
*/
|
2015-07-07 14:20:23 +08:00
|
|
|
class LabelLetter : public Sprite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LabelLetter()
|
|
|
|
{
|
|
|
|
_textureAtlas = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LabelLetter* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated = false)
|
|
|
|
{
|
|
|
|
auto letter = new (std::nothrow) LabelLetter();
|
|
|
|
if (letter && letter->initWithTexture(texture, rect, rotated))
|
|
|
|
{
|
|
|
|
letter->setVisible(false);
|
|
|
|
letter->autorelease();
|
|
|
|
return letter;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(letter);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void updateTransform() override
|
|
|
|
{
|
|
|
|
if (isDirty())
|
|
|
|
{
|
|
|
|
_transformToBatch = getNodeToParentTransform();
|
|
|
|
Size &size = _rect.size;
|
|
|
|
|
|
|
|
float x1 = _offsetPosition.x;
|
|
|
|
float y1 = _offsetPosition.y;
|
|
|
|
float x2 = x1 + size.width;
|
|
|
|
float y2 = y1 + size.height;
|
|
|
|
float x = _transformToBatch.m[12];
|
|
|
|
float y = _transformToBatch.m[13];
|
|
|
|
|
|
|
|
float cr = _transformToBatch.m[0];
|
|
|
|
float sr = _transformToBatch.m[1];
|
|
|
|
float cr2 = _transformToBatch.m[5];
|
|
|
|
float sr2 = -_transformToBatch.m[4];
|
|
|
|
float ax = x1 * cr - y1 * sr2 + x;
|
|
|
|
float ay = x1 * sr + y1 * cr2 + y;
|
|
|
|
|
|
|
|
float bx = x2 * cr - y1 * sr2 + x;
|
|
|
|
float by = x2 * sr + y1 * cr2 + y;
|
|
|
|
float cx = x2 * cr - y2 * sr2 + x;
|
|
|
|
float cy = x2 * sr + y2 * cr2 + y;
|
|
|
|
float dx = x1 * cr - y2 * sr2 + x;
|
|
|
|
float dy = x1 * sr + y2 * cr2 + y;
|
|
|
|
|
2015-07-08 15:30:34 +08:00
|
|
|
_quad.bl.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(ax), SPRITE_RENDER_IN_SUBPIXEL(ay), _positionZ);
|
|
|
|
_quad.br.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(bx), SPRITE_RENDER_IN_SUBPIXEL(by), _positionZ);
|
|
|
|
_quad.tl.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(dx), SPRITE_RENDER_IN_SUBPIXEL(dy), _positionZ);
|
|
|
|
_quad.tr.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(cx), SPRITE_RENDER_IN_SUBPIXEL(cy), _positionZ);
|
2015-07-07 14:20:23 +08:00
|
|
|
|
|
|
|
if (_textureAtlas)
|
|
|
|
{
|
|
|
|
_textureAtlas->updateQuad(&_quad, _atlasIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
_recursiveDirty = false;
|
|
|
|
setDirty(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::updateTransform();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void updateColor()
|
|
|
|
{
|
|
|
|
if (_textureAtlas == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color4B color4(_displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity);
|
|
|
|
// special opacity for premultiplied textures
|
|
|
|
if (_opacityModifyRGB)
|
|
|
|
{
|
|
|
|
color4.r *= _displayedOpacity / 255.0f;
|
|
|
|
color4.g *= _displayedOpacity / 255.0f;
|
|
|
|
color4.b *= _displayedOpacity / 255.0f;
|
|
|
|
}
|
|
|
|
_quad.bl.colors = color4;
|
|
|
|
_quad.br.colors = color4;
|
|
|
|
_quad.tl.colors = color4;
|
|
|
|
_quad.tr.colors = color4;
|
|
|
|
|
|
|
|
_textureAtlas->updateQuad(&_quad, _atlasIndex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
Label* Label::create()
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) 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
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
auto ret = new (std::nothrow) Label(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
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
auto ret = new (std::nothrow) Label(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
|
|
|
}
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment hAlignment /* = TextHAlignment::CENTER */, int maxLineWidth /* = 0 */)
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
auto ret = new (std::nothrow) Label(hAlignment);
|
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
|
|
|
}
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
Label* Label::createWithBMFont(const std::string& bmfontFilePath, const std::string& text,const TextHAlignment& hAlignment /* = TextHAlignment::LEFT */, int maxLineWidth /* = 0 */, const Vec2& imageOffset /* = Vec2::ZERO */)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
auto ret = new (std::nothrow) Label(hAlignment);
|
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-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) 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-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) 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-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) 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
|
|
|
}
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
Label::Label(TextHAlignment hAlignment /* = TextHAlignment::LEFT */,
|
|
|
|
TextVAlignment vAlignment /* = TextVAlignment::TOP */)
|
|
|
|
: _textSprite(nullptr)
|
|
|
|
, _shadowNode(nullptr)
|
|
|
|
, _fontAtlas(nullptr)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _reusedLetter(nullptr)
|
|
|
|
, _horizontalKernings(nullptr)
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
2014-03-13 18:46:35 +08:00
|
|
|
reset();
|
2015-07-14 11:30:56 +08:00
|
|
|
_hAlignment = hAlignment;
|
|
|
|
_vAlignment = vAlignment;
|
2014-03-14 14:59:26 +08:00
|
|
|
|
2015-07-01 04:03:14 +08:00
|
|
|
_purgeTextureListener = EventListenerCustom::create(FontAtlas::CMD_PURGE_FONTATLAS, [this](EventCustom* event){
|
2014-04-09 21:35:08 +08:00
|
|
|
if (_fontAtlas && _currentLabelType == LabelType::TTF && event->getUserData() == _fontAtlas)
|
|
|
|
{
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
it.second->setTexture(nullptr);
|
|
|
|
}
|
2014-07-09 23:03:04 +08:00
|
|
|
_batchNodes.clear();
|
|
|
|
|
2015-01-21 21:58:18 +08:00
|
|
|
if (_fontAtlas)
|
2014-11-27 10:59:47 +08:00
|
|
|
{
|
2015-01-21 21:58:18 +08:00
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
2014-11-27 10:59:47 +08:00
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
}
|
|
|
|
});
|
2015-07-01 04:03:14 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(_purgeTextureListener, 1);
|
2015-01-21 21:58:18 +08:00
|
|
|
|
2015-07-01 04:03:14 +08:00
|
|
|
_resetTextureListener = EventListenerCustom::create(FontAtlas::CMD_RESET_FONTATLAS, [this](EventCustom* event){
|
2015-01-21 21:58:18 +08:00
|
|
|
if (_fontAtlas && _currentLabelType == LabelType::TTF && event->getUserData() == _fontAtlas)
|
|
|
|
{
|
|
|
|
_fontAtlas = nullptr;
|
|
|
|
this->setTTFConfig(_fontConfig);
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
getLetter(it.first);
|
|
|
|
}
|
2015-01-21 21:58:18 +08:00
|
|
|
}
|
|
|
|
});
|
2015-07-01 04:03:14 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(_resetTextureListener, 2);
|
2013-07-23 02:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Label::~Label()
|
2014-05-08 11:12:59 +08:00
|
|
|
{
|
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
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
Node::removeAllChildrenWithCleanup(true);
|
|
|
|
CC_SAFE_RELEASE_NULL(_reusedLetter);
|
|
|
|
_batchNodes.clear();
|
2013-08-08 07:36:04 +08:00
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
2014-03-07 14:58:44 +08:00
|
|
|
}
|
2015-07-01 09:59:33 +08:00
|
|
|
_eventDispatcher->removeEventListener(_purgeTextureListener);
|
|
|
|
_eventDispatcher->removeEventListener(_resetTextureListener);
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_textSprite);
|
|
|
|
CC_SAFE_RELEASE_NULL(_shadowNode);
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:46:35 +08:00
|
|
|
void Label::reset()
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_textSprite);
|
|
|
|
CC_SAFE_RELEASE_NULL(_shadowNode);
|
|
|
|
Node::removeAllChildrenWithCleanup(true);
|
|
|
|
CC_SAFE_RELEASE_NULL(_reusedLetter);
|
|
|
|
_letters.clear();
|
2014-03-13 18:46:35 +08:00
|
|
|
_batchNodes.clear();
|
2015-07-14 11:30:56 +08:00
|
|
|
_lettersInfo.clear();
|
2014-03-13 18:46:35 +08:00
|
|
|
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;
|
2015-07-14 11:30:56 +08:00
|
|
|
_contentDirty = false;
|
|
|
|
_currNumLines = 0;
|
|
|
|
_limitShowCount = 0;
|
|
|
|
_currentUTF16String.clear();
|
|
|
|
_originalUTF8String.clear();
|
2014-03-13 18:46:35 +08:00
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
TTFConfig temp;
|
|
|
|
_fontConfig = temp;
|
|
|
|
_outlineSize = 0.f;
|
|
|
|
_bmFontPath = "";
|
|
|
|
_systemFontDirty = false;
|
|
|
|
_systemFont = "Helvetica";
|
|
|
|
_systemFontSize = 12;
|
2014-03-20 20:56:10 +08:00
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
if (_horizontalKernings)
|
|
|
|
{
|
|
|
|
delete[] _horizontalKernings;
|
|
|
|
_horizontalKernings = nullptr;
|
|
|
|
}
|
|
|
|
_additionalKerning = 0.f;
|
|
|
|
_commonLineHeight = 0.f;
|
|
|
|
_maxLineWidth = 0.f;
|
|
|
|
_labelDimensions.width = 0.f;
|
|
|
|
_labelDimensions.height = 0.f;
|
|
|
|
_labelWidth = 0.f;
|
|
|
|
_labelHeight = 0.f;
|
|
|
|
_lineBreakWithoutSpaces = false;
|
|
|
|
_hAlignment = TextHAlignment::LEFT;
|
|
|
|
_vAlignment = TextVAlignment::TOP;
|
|
|
|
|
|
|
|
_effectColorF = Color4F::BLACK;
|
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
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
_shadowDirty = false;
|
2014-03-26 11:09:16 +08:00
|
|
|
_shadowEnabled = false;
|
2015-07-14 11:30:56 +08:00
|
|
|
_shadowBlurRadius = 0.f;
|
|
|
|
|
|
|
|
_correctionScale = 1.f;
|
|
|
|
_useDistanceField = false;
|
|
|
|
_useA8Shader = false;
|
2014-03-24 14:16:27 +08:00
|
|
|
_clipEnabled = false;
|
2014-03-31 15:45:06 +08:00
|
|
|
_blendFuncDirty = false;
|
2015-07-14 11:30:56 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
_isOpacityModifyRGB = false;
|
|
|
|
_insideBounds = true;
|
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)
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_NORMAL));
|
2014-03-05 15:54:40 +08:00
|
|
|
else if (_useA8Shader)
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_NORMAL));
|
2014-03-05 15:54:40 +08:00
|
|
|
else
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
2014-03-05 15:54:40 +08:00
|
|
|
|
|
|
|
break;
|
2014-03-20 20:56:10 +08:00
|
|
|
case cocos2d::LabelEffect::OUTLINE:
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_OUTLINE));
|
2014-05-16 08:22:53 +08:00
|
|
|
_uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "u_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-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW));
|
2014-05-16 08:22:53 +08:00
|
|
|
_uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "u_effectColor");
|
2014-03-20 20:56:10 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2014-03-20 20:56:10 +08:00
|
|
|
|
2014-05-16 08:22:53 +08:00
|
|
|
_uniformTextColor = glGetUniformLocation(getGLProgram()->getProgram(), "u_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
|
|
|
{
|
2015-07-10 14:39:21 +08:00
|
|
|
_batchNodes.clear();
|
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;
|
|
|
|
if (_reusedLetter == nullptr)
|
|
|
|
{
|
2014-06-11 22:03:10 +08:00
|
|
|
_reusedLetter = Sprite::create();
|
2014-03-13 18:46:35 +08:00
|
|
|
_reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB);
|
|
|
|
_reusedLetter->retain();
|
2014-05-15 01:07:09 +08:00
|
|
|
_reusedLetter->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
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-05-14 16:41:26 +08:00
|
|
|
_systemFontDirty = false;
|
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)
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
this->setCorrectionScale(1.0f * ttfConfig.fontSize / DistanceFieldFontSize);
|
2014-03-20 20:56:10 +08:00
|
|
|
}
|
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-05-15 01:07:09 +08:00
|
|
|
bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Vec2& imageOffset /* = Vec2::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
|
|
|
{
|
2014-05-14 16:41:26 +08:00
|
|
|
if (text.compare(_originalUTF8String))
|
2014-05-08 21:30:12 +08:00
|
|
|
{
|
2014-05-14 16:41:26 +08:00
|
|
|
_originalUTF8String = text;
|
|
|
|
_contentDirty = true;
|
|
|
|
|
|
|
|
std::u16string utf16String;
|
|
|
|
if (StringUtils::UTF8ToUTF16(_originalUTF8String, utf16String))
|
|
|
|
{
|
|
|
|
_currentUTF16String = utf16String;
|
|
|
|
}
|
2014-05-08 21:30:12 +08:00
|
|
|
}
|
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
|
|
|
|
2015-01-20 11:46:43 +08:00
|
|
|
void Label::setMaxLineWidth(float maxLineWidth)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 11:46:43 +08:00
|
|
|
void Label::setDimensions(float width, float height)
|
2014-03-07 14:58:44 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
scale *= _correctionScale;
|
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)
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
scaleX *= _correctionScale;
|
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)
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
scaleY *= _correctionScale;
|
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)
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
return _scaleY / _correctionScale;
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return _scaleY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getScaleX() const
|
|
|
|
{
|
|
|
|
if (_useDistanceField)
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
return _scaleX / _correctionScale;
|
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-05-08 11:12:59 +08:00
|
|
|
if (_fontAtlas == nullptr || _currentUTF16String.empty())
|
2014-02-19 20:26:14 +08:00
|
|
|
{
|
2014-12-15 14:36:57 +08:00
|
|
|
setContentSize(Size::ZERO);
|
2014-02-19 20:26:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-29 20:25:03 +08:00
|
|
|
_fontAtlas->prepareLetterDefinitions(_currentUTF16String);
|
2014-12-21 14:05:34 +08:00
|
|
|
auto& textures = _fontAtlas->getTextures();
|
2014-02-18 14:30:51 +08:00
|
|
|
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-12-21 14:05:34 +08:00
|
|
|
auto batchNode = SpriteBatchNode::createWithTexture(textures.at(index));
|
2015-07-07 14:20:23 +08:00
|
|
|
if (batchNode)
|
|
|
|
{
|
|
|
|
_blendFunc = batchNode->getBlendFunc();
|
|
|
|
batchNode->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
|
|
|
batchNode->setPosition(Vec2::ZERO);
|
|
|
|
_batchNodes.pushBack(batchNode);
|
|
|
|
}
|
2014-02-18 14:30:51 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-07 14:20:23 +08:00
|
|
|
if (_batchNodes.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_reusedLetter->setBatchNode(_batchNodes.at(0));
|
|
|
|
|
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
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
if (!_letters.empty())
|
2015-04-30 15:13:41 +08:00
|
|
|
{
|
|
|
|
Rect uvRect;
|
|
|
|
Sprite* letterSprite;
|
2015-07-07 14:20:23 +08:00
|
|
|
int letterIndex;
|
|
|
|
|
|
|
|
for (auto it = _letters.begin(); it != _letters.end();)
|
|
|
|
{
|
|
|
|
letterIndex = it->first;
|
|
|
|
letterSprite = it->second;
|
|
|
|
|
|
|
|
if (letterIndex >= _limitShowCount)
|
2015-04-30 15:13:41 +08:00
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
Node::removeChild(letterSprite, true);
|
|
|
|
it = _letters.erase(it);
|
2015-05-03 22:15:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
auto& letterDef = _lettersInfo[letterIndex].def;
|
|
|
|
uvRect.size.height = letterDef.height;
|
|
|
|
uvRect.size.width = letterDef.width;
|
|
|
|
uvRect.origin.x = letterDef.U;
|
|
|
|
uvRect.origin.y = letterDef.V;
|
|
|
|
|
|
|
|
letterSprite->setBatchNode(_batchNodes.at(letterDef.textureID));
|
|
|
|
letterSprite->setTextureRect(uvRect, false, uvRect.size);
|
|
|
|
letterSprite->setPosition(_lettersInfo[letterIndex].position.x + letterDef.width / 2,
|
|
|
|
_lettersInfo[letterIndex].position.y - letterDef.height / 2);
|
|
|
|
|
|
|
|
++it;
|
2014-12-21 14:05:34 +08:00
|
|
|
}
|
2014-02-19 20:26:14 +08:00
|
|
|
}
|
2013-12-19 10:33:04 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& batchNode : _batchNodes)
|
2015-04-30 15:13:41 +08:00
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->removeAllQuads();
|
|
|
|
}
|
|
|
|
|
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-05-08 11:12:59 +08:00
|
|
|
bool Label::computeHorizontalKernings(const std::u16string& 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;
|
|
|
|
}
|
|
|
|
|
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;
|
2015-07-13 22:44:41 +08:00
|
|
|
|
2014-03-17 18:33:15 +08:00
|
|
|
_reusedLetter->setTextureRect(_reusedRect,false,_reusedRect.size);
|
|
|
|
|
|
|
|
_reusedLetter->setPosition(_lettersInfo[ctr].position);
|
2015-07-07 14:20:23 +08:00
|
|
|
index = static_cast<int>(_batchNodes.at(letterDef.textureID)->getTextureAtlas()->getTotalQuads());
|
2014-03-17 18:33:15 +08:00
|
|
|
_lettersInfo[ctr].atlasIndex = index;
|
2015-07-07 14:20:23 +08:00
|
|
|
_batchNodes.at(letterDef.textureID)->insertQuadFromSprite(_reusedLetter,index);
|
2014-03-17 18:33:15 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool Label::recordLetterInfo(const cocos2d::Vec2& 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;
|
2015-06-19 13:39:19 +08:00
|
|
|
_lettersInfo[spriteIndex].contentSize.width = letterDef.width;
|
|
|
|
_lettersInfo[spriteIndex].contentSize.height = letterDef.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
|
|
|
}
|
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
void Label::enableGlow(const Color4B& glowColor)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-06-12 12:01:15 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
|
|
|
if (_fontConfig.distanceFieldEnabled == false)
|
|
|
|
{
|
|
|
|
auto config = _fontConfig;
|
|
|
|
config.outlineSize = 0;
|
|
|
|
config.distanceFieldEnabled = true;
|
|
|
|
setTTFConfig(config);
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
_currLabelEffect = LabelEffect::GLOW;
|
2015-07-14 11:30:56 +08:00
|
|
|
_effectColorF.r = glowColor.r / 255.0f;
|
|
|
|
_effectColorF.g = glowColor.g / 255.0f;
|
|
|
|
_effectColorF.b = glowColor.b / 255.0f;
|
|
|
|
_effectColorF.a = glowColor.a / 255.0f;
|
2014-06-12 12:01:15 +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-06-12 14:41:01 +08:00
|
|
|
CCASSERT(_currentLabelType == LabelType::STRING_TEXTURE || _currentLabelType == LabelType::TTF, "Only supported system font and TTF!");
|
|
|
|
|
2015-04-07 15:51:57 +08:00
|
|
|
if (outlineSize > 0 || _currLabelEffect == LabelEffect::OUTLINE)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2014-03-10 20:35:36 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF)
|
2014-03-05 15:54:40 +08:00
|
|
|
{
|
2015-04-07 15:51:57 +08:00
|
|
|
_effectColorF.r = outlineColor.r / 255.0f;
|
|
|
|
_effectColorF.g = outlineColor.g / 255.0f;
|
|
|
|
_effectColorF.b = outlineColor.b / 255.0f;
|
|
|
|
_effectColorF.a = outlineColor.a / 255.0f;
|
|
|
|
|
|
|
|
if (outlineSize > 0 && _fontConfig.outlineSize != outlineSize)
|
|
|
|
{
|
|
|
|
_fontConfig.outlineSize = outlineSize;
|
|
|
|
setTTFConfig(_fontConfig);
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2015-07-14 11:30:56 +08:00
|
|
|
else if (_effectColorF != outlineColor || _outlineSize != outlineSize)
|
2015-04-07 15:51:57 +08:00
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
_effectColorF.r = outlineColor.r / 255.f;
|
|
|
|
_effectColorF.g = outlineColor.g / 255.f;
|
|
|
|
_effectColorF.b = outlineColor.b / 255.f;
|
|
|
|
_effectColorF.a = outlineColor.a / 255.f;
|
2015-04-07 15:51:57 +08:00
|
|
|
_outlineSize = outlineSize;
|
|
|
|
_currLabelEffect = LabelEffect::OUTLINE;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
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-03-25 10:41:53 +08:00
|
|
|
auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
|
|
|
|
_shadowOffset.width = offset.width * contentScaleFactor;
|
|
|
|
_shadowOffset.height = offset.height * contentScaleFactor;
|
2014-08-30 03:54:24 +08:00
|
|
|
//TODO: support blur for shadow
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2015-04-17 15:42:41 +08:00
|
|
|
_shadowColor3B.r = shadowColor.r;
|
|
|
|
_shadowColor3B.g = shadowColor.g;
|
|
|
|
_shadowColor3B.b = shadowColor.b;
|
|
|
|
_shadowOpacity = shadowColor.a;
|
|
|
|
|
2015-04-17 15:26:43 +08:00
|
|
|
if (!_systemFontDirty && !_contentDirty && _textSprite)
|
2014-03-26 11:09:16 +08:00
|
|
|
{
|
2015-07-14 13:47:35 +08:00
|
|
|
auto fontDef = _getFontDefinition();
|
2015-04-17 15:26:43 +08:00
|
|
|
if (_shadowNode)
|
2015-04-07 11:17:01 +08:00
|
|
|
{
|
2015-04-17 15:26:43 +08:00
|
|
|
if (shadowColor != _shadowColor4F)
|
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
_shadowNode->release();
|
2015-04-17 15:26:43 +08:00
|
|
|
_shadowNode = nullptr;
|
2015-07-14 11:30:56 +08:00
|
|
|
createShadowSpriteForSystemFont(fontDef);
|
2015-04-17 15:26:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_shadowNode->setPosition(_shadowOffset.width, _shadowOffset.height);
|
|
|
|
}
|
2015-04-07 11:17:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
createShadowSpriteForSystemFont(fontDef);
|
2015-04-07 11:17:01 +08:00
|
|
|
}
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
2015-04-07 11:17:01 +08:00
|
|
|
|
|
|
|
_shadowColor4F.r = shadowColor.r / 255.0f;
|
|
|
|
_shadowColor4F.g = shadowColor.g / 255.0f;
|
|
|
|
_shadowColor4F.b = shadowColor.b / 255.0f;
|
|
|
|
_shadowColor4F.a = shadowColor.a / 255.0f;
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::disableEffect()
|
|
|
|
{
|
2015-04-07 15:51:57 +08:00
|
|
|
disableEffect(LabelEffect::GLOW);
|
|
|
|
disableEffect(LabelEffect::OUTLINE);
|
|
|
|
disableEffect(LabelEffect::SHADOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::disableEffect(LabelEffect effect)
|
|
|
|
{
|
|
|
|
switch (effect)
|
2014-03-26 11:09:16 +08:00
|
|
|
{
|
2015-04-07 15:51:57 +08:00
|
|
|
case cocos2d::LabelEffect::NORMAL:
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::OUTLINE:
|
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE)
|
|
|
|
{
|
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
|
|
|
_fontConfig.outlineSize = 0;
|
|
|
|
setTTFConfig(_fontConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::SHADOW:
|
|
|
|
if (_shadowEnabled)
|
|
|
|
{
|
|
|
|
_shadowEnabled = false;
|
2015-07-07 14:20:23 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_shadowNode);
|
2015-04-07 15:51:57 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::GLOW:
|
|
|
|
if (_currLabelEffect == LabelEffect::GLOW)
|
|
|
|
{
|
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
|
|
|
updateShaderProgram();
|
|
|
|
}
|
|
|
|
break;
|
2015-04-17 14:39:52 +08:00
|
|
|
case LabelEffect::ALL:
|
|
|
|
{
|
|
|
|
disableEffect(LabelEffect::SHADOW);
|
|
|
|
disableEffect(LabelEffect::GLOW);
|
|
|
|
disableEffect(LabelEffect::OUTLINE);
|
|
|
|
}
|
|
|
|
break;
|
2015-04-07 15:51:57 +08:00
|
|
|
default:
|
|
|
|
break;
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
void Label::setCorrectionScale(float correctionScale)
|
2013-12-13 12:42:15 +08:00
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
_correctionScale = correctionScale * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
Node::setScale(_correctionScale);
|
2013-12-13 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
void Label::createSpriteForSystemFont(const FontDefinition& fontDef)
|
2015-03-16 15:31:29 +08:00
|
|
|
{
|
2015-04-07 11:17:01 +08:00
|
|
|
_currentLabelType = LabelType::STRING_TEXTURE;
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
auto texture = new (std::nothrow) Texture2D;
|
2015-07-14 11:30:56 +08:00
|
|
|
texture->initWithString(_originalUTF8String.c_str(), fontDef);
|
2014-03-10 19:42:43 +08:00
|
|
|
|
|
|
|
_textSprite = Sprite::createWithTexture(texture);
|
2015-03-18 11:58:36 +08:00
|
|
|
//set camera mask using label's camera mask, because _textSprite may be null when setting camera mask to label
|
2015-03-18 11:41:43 +08:00
|
|
|
_textSprite->setCameraMask(getCameraMask());
|
2014-05-15 01:07:09 +08:00
|
|
|
_textSprite->setAnchorPoint(Vec2::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
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
_textSprite->retain();
|
2014-03-20 20:56:10 +08:00
|
|
|
_textSprite->updateDisplayedColor(_displayedColor);
|
2014-05-08 10:12:29 +08:00
|
|
|
_textSprite->updateDisplayedOpacity(_displayedOpacity);
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
void Label::createShadowSpriteForSystemFont(const FontDefinition& fontDef)
|
2015-04-07 11:17:01 +08:00
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
if (!fontDef._stroke._strokeEnabled && fontDef._fontFillColor == _shadowColor3B
|
|
|
|
&& (fontDef._fontAlpha == _shadowOpacity))
|
2015-04-07 11:17:01 +08:00
|
|
|
{
|
|
|
|
_shadowNode = Sprite::createWithTexture(_textSprite->getTexture());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
FontDefinition shadowFontDefinition = fontDef;
|
2015-04-17 15:42:41 +08:00
|
|
|
shadowFontDefinition._fontFillColor.r = _shadowColor3B.r;
|
|
|
|
shadowFontDefinition._fontFillColor.g = _shadowColor3B.g;
|
|
|
|
shadowFontDefinition._fontFillColor.b = _shadowColor3B.b;
|
|
|
|
shadowFontDefinition._fontAlpha = _shadowOpacity;
|
2015-04-07 11:17:01 +08:00
|
|
|
|
|
|
|
shadowFontDefinition._stroke._strokeColor = shadowFontDefinition._fontFillColor;
|
|
|
|
shadowFontDefinition._stroke._strokeAlpha = shadowFontDefinition._fontAlpha;
|
|
|
|
|
|
|
|
auto texture = new (std::nothrow) Texture2D;
|
|
|
|
texture->initWithString(_originalUTF8String.c_str(), shadowFontDefinition);
|
|
|
|
_shadowNode = Sprite::createWithTexture(texture);
|
|
|
|
texture->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
if (_blendFuncDirty)
|
|
|
|
{
|
|
|
|
_shadowNode->setBlendFunc(_blendFunc);
|
|
|
|
}
|
2015-04-13 18:22:05 +08:00
|
|
|
_shadowNode->setCameraMask(getCameraMask());
|
2015-04-07 11:17:01 +08:00
|
|
|
_shadowNode->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
|
|
|
_shadowNode->setPosition(_shadowOffset.width, _shadowOffset.height);
|
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
_shadowNode->retain();
|
2015-04-07 11:17:01 +08:00
|
|
|
_shadowNode->updateDisplayedColor(_displayedColor);
|
|
|
|
_shadowNode->updateDisplayedOpacity(_displayedOpacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setCameraMask(unsigned short mask, bool applyChildren)
|
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
Node::setCameraMask(mask, applyChildren);
|
2015-04-07 11:17:01 +08:00
|
|
|
|
|
|
|
if (_textSprite)
|
|
|
|
{
|
|
|
|
_textSprite->setCameraMask(mask, applyChildren);
|
|
|
|
}
|
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->setCameraMask(mask, applyChildren);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
void Label::setFontDefinition(const FontDefinition& textDefinition)
|
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
_systemFont = textDefinition._fontName;
|
|
|
|
_systemFontSize = textDefinition._fontSize;
|
|
|
|
_hAlignment = textDefinition._alignment;
|
|
|
|
_vAlignment = textDefinition._vertAlignment;
|
|
|
|
setDimensions(textDefinition._dimensions.width, textDefinition._dimensions.height);
|
|
|
|
Color4B textColor = Color4B(textDefinition._fontFillColor);
|
|
|
|
textColor.a = textDefinition._fontAlpha;
|
|
|
|
setTextColor(textColor);
|
|
|
|
|
2014-04-09 21:35:08 +08:00
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
|
2015-07-14 11:30:56 +08:00
|
|
|
if (textDefinition._stroke._strokeEnabled)
|
2014-04-09 21:35:08 +08:00
|
|
|
{
|
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
|
|
|
}
|
2015-07-14 11:30:56 +08:00
|
|
|
_outlineSize = 0.f;
|
|
|
|
#else
|
|
|
|
if (textDefinition._stroke._strokeEnabled && textDefinition._stroke._strokeSize > 0.f)
|
|
|
|
{
|
|
|
|
Color4B outlineColor = Color4B(textDefinition._stroke._strokeColor);
|
|
|
|
outlineColor.a = textDefinition._stroke._strokeAlpha;
|
|
|
|
enableOutline(outlineColor, textDefinition._stroke._strokeSize);
|
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
#endif
|
2015-07-14 11:30:56 +08:00
|
|
|
|
|
|
|
if (textDefinition._shadow._shadowEnabled)
|
2014-04-09 21:35:08 +08:00
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
enableShadow(Color4B(0, 0, 0, 255 * textDefinition._shadow._shadowOpacity),
|
|
|
|
textDefinition._shadow._shadowOffset, textDefinition._shadow._shadowBlur);
|
2014-04-09 21:35:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
void Label::updateContent()
|
|
|
|
{
|
2015-04-07 11:17:01 +08:00
|
|
|
if (_systemFontDirty)
|
2014-05-12 14:34:25 +08:00
|
|
|
{
|
2015-04-07 11:17:01 +08:00
|
|
|
if (_fontAtlas)
|
|
|
|
{
|
|
|
|
_batchNodes.clear();
|
2014-05-12 14:34:25 +08:00
|
|
|
|
2015-04-07 11:17:01 +08:00
|
|
|
FontAtlasCache::releaseFontAtlas(_fontAtlas);
|
|
|
|
_fontAtlas = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
_systemFontDirty = false;
|
2014-05-08 11:12:59 +08:00
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_textSprite);
|
|
|
|
CC_SAFE_RELEASE_NULL(_shadowNode);
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_fontAtlas)
|
|
|
|
{
|
2015-04-07 11:17:01 +08:00
|
|
|
std::u16string utf16String;
|
|
|
|
if (StringUtils::UTF8ToUTF16(_originalUTF8String, utf16String))
|
2014-04-14 11:33:46 +08:00
|
|
|
{
|
2015-04-07 11:17:01 +08:00
|
|
|
_currentUTF16String = utf16String;
|
2014-04-14 11:33:46 +08:00
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2015-04-07 11:17:01 +08:00
|
|
|
computeStringNumLines();
|
|
|
|
computeHorizontalKernings(_currentUTF16String);
|
|
|
|
alignText();
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
2015-04-07 11:17:01 +08:00
|
|
|
else
|
2014-03-26 11:09:16 +08:00
|
|
|
{
|
2015-07-14 13:47:35 +08:00
|
|
|
auto fontDef = _getFontDefinition();
|
2015-07-14 11:30:56 +08:00
|
|
|
createSpriteForSystemFont(fontDef);
|
2015-04-07 11:17:01 +08:00
|
|
|
if (_shadowEnabled)
|
2014-03-31 15:45:06 +08:00
|
|
|
{
|
2015-07-14 11:30:56 +08:00
|
|
|
createShadowSpriteForSystemFont(fontDef);
|
2014-03-31 15:45:06 +08:00
|
|
|
}
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
2015-04-07 11:17:01 +08:00
|
|
|
_contentDirty = false;
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 17:59:24 +08:00
|
|
|
void Label::onDrawShadow(GLProgram* glProgram)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
2015-07-08 17:59:24 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
2015-07-08 17:59:24 +08:00
|
|
|
glProgram->setUniformLocationWith4f(_uniformTextColor,
|
|
|
|
_shadowColor4F.r, _shadowColor4F.g, _shadowColor4F.b, _shadowColor4F.a);
|
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE || _currLabelEffect == LabelEffect::GLOW)
|
|
|
|
{
|
|
|
|
glProgram->setUniformLocationWith4f(_uniformEffectColor,
|
|
|
|
_shadowColor4F.r, _shadowColor4F.g, _shadowColor4F.b, _shadowColor4F.a);
|
|
|
|
}
|
|
|
|
|
|
|
|
glProgram->setUniformsForBuiltins(_shadowTransform);
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-08 17:59:24 +08:00
|
|
|
{
|
|
|
|
it.second->updateTransform();
|
|
|
|
}
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& batchNode : _batchNodes)
|
2015-07-08 17:59:24 +08:00
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->drawQuads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Color3B oldColor = _realColor;
|
|
|
|
GLubyte oldOPacity = _displayedOpacity;
|
|
|
|
_displayedOpacity = _shadowOpacity;
|
|
|
|
setColor(_shadowColor3B);
|
|
|
|
|
|
|
|
glProgram->setUniformsForBuiltins(_shadowTransform);
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-08 17:59:24 +08:00
|
|
|
{
|
|
|
|
it.second->updateTransform();
|
|
|
|
}
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& batchNode : _batchNodes)
|
2015-07-08 17:59:24 +08:00
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->drawQuads();
|
|
|
|
}
|
|
|
|
|
|
|
|
_displayedOpacity = oldOPacity;
|
|
|
|
setColor(oldColor);
|
2015-07-07 14:20:23 +08:00
|
|
|
}
|
2015-07-08 17:59:24 +08:00
|
|
|
}
|
2015-07-07 14:20:23 +08:00
|
|
|
|
2015-07-08 17:59:24 +08:00
|
|
|
void Label::onDraw(const Mat4& transform, bool transformUpdated)
|
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
auto glprogram = getGLProgram();
|
|
|
|
glprogram->use();
|
|
|
|
GL::blendFunc(_blendFunc.src, _blendFunc.dst);
|
|
|
|
|
|
|
|
if (_shadowEnabled)
|
|
|
|
{
|
2015-07-08 17:59:24 +08:00
|
|
|
onDrawShadow(glprogram);
|
2015-07-07 14:20:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
glprogram->setUniformsForBuiltins(transform);
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
it.second->updateTransform();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
|
|
|
switch (_currLabelEffect) {
|
|
|
|
case LabelEffect::OUTLINE:
|
|
|
|
//draw text with outline
|
|
|
|
glprogram->setUniformLocationWith4f(_uniformTextColor,
|
|
|
|
_textColorF.r, _textColorF.g, _textColorF.b, _textColorF.a);
|
|
|
|
glprogram->setUniformLocationWith4f(_uniformEffectColor,
|
|
|
|
_effectColorF.r, _effectColorF.g, _effectColorF.b, _effectColorF.a);
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& batchNode : _batchNodes)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->drawQuads();
|
|
|
|
}
|
|
|
|
|
|
|
|
//draw text without outline
|
|
|
|
glprogram->setUniformLocationWith4f(_uniformEffectColor,
|
|
|
|
_effectColorF.r, _effectColorF.g, _effectColorF.b, 0.f);
|
|
|
|
break;
|
|
|
|
case LabelEffect::GLOW:
|
|
|
|
glprogram->setUniformLocationWith4f(_uniformEffectColor,
|
|
|
|
_effectColorF.r, _effectColorF.g, _effectColorF.b, _effectColorF.a);
|
|
|
|
case LabelEffect::NORMAL:
|
|
|
|
glprogram->setUniformLocationWith4f(_uniformTextColor,
|
|
|
|
_textColorF.r, _textColorF.g, _textColorF.b, _textColorF.a);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& batchNode : _batchNodes)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
batchNode->getTextureAtlas()->drawQuads();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
|
|
|
{
|
2015-07-08 17:59:24 +08:00
|
|
|
if (_batchNodes.empty() || _limitShowCount <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-07-07 14:20:23 +08:00
|
|
|
// Don't do calculate the culling if the transform was not updated
|
|
|
|
bool transformUpdated = flags & FLAGS_TRANSFORM_DIRTY;
|
|
|
|
#if CC_USE_CULLING
|
|
|
|
_insideBounds = transformUpdated ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
|
|
|
|
|
|
|
|
if (_insideBounds)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
|
|
|
_customCommand.func = CC_CALLBACK_0(Label::onDraw, this, transform, transformUpdated);
|
|
|
|
|
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Label::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
if (! _visible || (_originalUTF8String.empty() && _children.empty()) )
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-04-07 11:17:01 +08:00
|
|
|
|
|
|
|
if (_systemFontDirty || _contentDirty)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
|
|
|
updateContent();
|
|
|
|
}
|
2014-03-01 03:20:53 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
2014-03-31 18:16:08 +08:00
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
if (!_originalUTF8String.empty() && _shadowEnabled && (_shadowDirty || (flags & FLAGS_DIRTY_MASK)))
|
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
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
bool visibleByCamera = isVisitableByVisitingCamera();
|
|
|
|
if (_children.empty() && !_textSprite && !visibleByCamera)
|
2015-03-05 11:27:53 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-31 18:16:08 +08:00
|
|
|
// IMPORTANT:
|
2014-05-15 01:07:09 +08:00
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
2014-03-31 18:16:08 +08:00
|
|
|
// but it is deprecated and your code should not rely on it
|
2015-04-13 18:22:05 +08:00
|
|
|
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2014-04-03 14:32:49 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
if (!_children.empty())
|
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
// draw children zOrder < 0
|
|
|
|
for (; i < _children.size(); i++)
|
|
|
|
{
|
|
|
|
auto node = _children.at(i);
|
|
|
|
|
|
|
|
if (node && node->getLocalZOrder() < 0)
|
|
|
|
node->visit(renderer, _modelViewTransform, flags);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// self draw
|
|
|
|
if (visibleByCamera)
|
|
|
|
this->drawSelf(renderer, flags);
|
|
|
|
|
|
|
|
for (auto it = _children.cbegin() + i; it != _children.cend(); ++it)
|
|
|
|
{
|
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (visibleByCamera)
|
|
|
|
{
|
|
|
|
this->drawSelf(renderer, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::drawSelf(Renderer* renderer, uint32_t flags)
|
|
|
|
{
|
2014-03-31 18:16:08 +08:00
|
|
|
if (_textSprite)
|
|
|
|
{
|
2015-04-07 11:17:01 +08:00
|
|
|
if (_shadowNode)
|
|
|
|
{
|
2015-04-13 16:28:55 +08:00
|
|
|
_shadowNode->visit(renderer, _modelViewTransform, flags);
|
2015-04-07 11:17:01 +08:00
|
|
|
}
|
2015-04-13 16:28:55 +08:00
|
|
|
_textSprite->visit(renderer, _modelViewTransform, flags);
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2014-03-31 18:16:08 +08:00
|
|
|
else
|
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
draw(renderer, _modelViewTransform, flags);
|
2014-03-31 18:16:08 +08:00
|
|
|
}
|
2014-02-18 14:30:51 +08:00
|
|
|
}
|
|
|
|
|
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-05-08 21:30:12 +08:00
|
|
|
_systemFontDirty = 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-05-08 21:30:12 +08:00
|
|
|
_systemFontDirty = 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
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
Sprite* letter = nullptr;
|
|
|
|
do
|
2014-03-17 18:33:15 +08:00
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
if (_systemFontDirty || _currentLabelType == LabelType::STRING_TEXTURE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-04-09 23:31:05 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
auto contentDirty = _contentDirty;
|
|
|
|
if (contentDirty)
|
|
|
|
{
|
|
|
|
updateContent();
|
|
|
|
}
|
2014-03-17 18:33:15 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
if (_textSprite == nullptr && letterIndex < _limitShowCount)
|
|
|
|
{
|
|
|
|
const auto &letterInfo = _lettersInfo[letterIndex];
|
|
|
|
if (!letterInfo.def.validDefinition)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
if (_letters.find(letterIndex) != _letters.end())
|
|
|
|
{
|
|
|
|
letter = _letters[letterIndex];
|
|
|
|
}
|
2013-10-29 20:25:03 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
auto textureID = letterInfo.def.textureID;
|
2013-10-29 20:25:03 +08:00
|
|
|
Rect uvRect;
|
2015-07-07 14:20:23 +08:00
|
|
|
uvRect.size.height = letterInfo.def.height;
|
|
|
|
uvRect.size.width = letterInfo.def.width;
|
|
|
|
uvRect.origin.x = letterInfo.def.U;
|
|
|
|
uvRect.origin.y = letterInfo.def.V;
|
|
|
|
|
|
|
|
if (letter == nullptr)
|
|
|
|
{
|
|
|
|
letter = LabelLetter::createWithTexture(_fontAtlas->getTexture(textureID), uvRect);
|
|
|
|
letter->setTextureAtlas(_batchNodes.at(textureID)->getTextureAtlas());
|
|
|
|
letter->setAtlasIndex(letterInfo.atlasIndex);
|
|
|
|
|
|
|
|
letter->setPosition(letterInfo.position.x + uvRect.size.width / 2,
|
|
|
|
letterInfo.position.y - uvRect.size.height / 2);
|
|
|
|
letter->setOpacity(_realOpacity);
|
|
|
|
addChild(letter);
|
|
|
|
|
|
|
|
_letters[letterIndex] = letter;
|
|
|
|
}
|
|
|
|
else if (contentDirty)
|
|
|
|
{
|
|
|
|
letter->setTexture(_fontAtlas->getTexture(textureID));
|
|
|
|
letter->setTextureRect(uvRect, false, uvRect.size);
|
|
|
|
letter->setTextureAtlas(_batchNodes.at(textureID)->getTextureAtlas());
|
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
2015-07-07 14:20:23 +08:00
|
|
|
} while (false);
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
return letter;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 12:01:15 +08:00
|
|
|
void Label::setLineHeight(float height)
|
|
|
|
{
|
2014-06-12 14:41:01 +08:00
|
|
|
CCASSERT(_currentLabelType != LabelType::STRING_TEXTURE, "Not supported system font!");
|
|
|
|
|
2014-06-12 12:01:15 +08:00
|
|
|
if (_commonLineHeight != height)
|
|
|
|
{
|
|
|
|
_commonLineHeight = height;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getLineHeight() const
|
|
|
|
{
|
2014-06-12 14:41:01 +08:00
|
|
|
CCASSERT(_currentLabelType != LabelType::STRING_TEXTURE, "Not supported system font!");
|
2014-06-12 12:01:15 +08:00
|
|
|
return _textSprite ? 0.0f : _commonLineHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setAdditionalKerning(float space)
|
|
|
|
{
|
2014-06-12 14:41:01 +08:00
|
|
|
CCASSERT(_currentLabelType != LabelType::STRING_TEXTURE, "Not supported system font!");
|
2014-06-12 12:01:15 +08:00
|
|
|
if (_additionalKerning != space)
|
|
|
|
{
|
|
|
|
_additionalKerning = space;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getAdditionalKerning() const
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-06-12 14:41:01 +08:00
|
|
|
CCASSERT(_currentLabelType != LabelType::STRING_TEXTURE, "Not supported system font!");
|
|
|
|
|
2014-06-12 12:01:15 +08:00
|
|
|
return _additionalKerning;
|
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-05-08 11:12:59 +08:00
|
|
|
if (_currentUTF16String.empty())
|
2014-02-10 11:21:54 +08:00
|
|
|
{
|
2014-05-08 11:12:59 +08:00
|
|
|
_currNumLines = 0;
|
2014-02-10 11:21:54 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2013-08-08 07:36:04 +08:00
|
|
|
// count number of lines
|
2014-05-08 11:12:59 +08:00
|
|
|
size_t stringLen = _currentUTF16String.length();
|
|
|
|
for (size_t 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-09-25 00:46:17 +08:00
|
|
|
int Label::getStringNumLines() const {
|
|
|
|
if (_contentDirty)
|
|
|
|
{
|
|
|
|
const_cast<Label*>(this)->updateContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _currNumLines;
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:58:44 +08:00
|
|
|
int Label::getStringLength() const
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2014-05-11 11:24:01 +08:00
|
|
|
return static_cast<int>(_currentUTF16String.length());
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RGBA protocol
|
|
|
|
void Label::setOpacityModifyRGB(bool isOpacityModifyRGB)
|
|
|
|
{
|
2014-10-09 10:54:35 +08:00
|
|
|
if (isOpacityModifyRGB != _isOpacityModifyRGB)
|
|
|
|
{
|
|
|
|
_isOpacityModifyRGB = isOpacityModifyRGB;
|
|
|
|
updateColor();
|
2013-12-19 10:33:04 +08:00
|
|
|
}
|
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
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
Node::updateDisplayedColor(parentColor);
|
2014-03-19 10:10:47 +08:00
|
|
|
|
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
|
|
|
}
|
2015-07-15 20:17:05 +08:00
|
|
|
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-15 20:17:05 +08:00
|
|
|
{
|
|
|
|
it.second->updateDisplayedColor(_displayedColor);;
|
|
|
|
}
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
void Label::updateDisplayedOpacity(GLubyte parentOpacity)
|
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
Node::updateDisplayedOpacity(parentOpacity);
|
2014-03-20 20:56:10 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-07-15 20:17:05 +08:00
|
|
|
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-15 20:17:05 +08:00
|
|
|
{
|
|
|
|
it.second->updateDisplayedOpacity(_displayedOpacity);;
|
|
|
|
}
|
2014-03-20 20:56:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setTextColor(const Color4B &color)
|
|
|
|
{
|
2014-06-12 14:41:01 +08:00
|
|
|
CCASSERT(_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::STRING_TEXTURE, "Only supported system font and ttf!");
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
if (_currentLabelType == LabelType::STRING_TEXTURE && _textColor != color)
|
|
|
|
{
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:56:10 +08:00
|
|
|
_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
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
if (_batchNodes.empty())
|
2014-03-05 14:41:59 +08:00
|
|
|
{
|
|
|
|
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;
|
2015-07-16 14:08:59 +08:00
|
|
|
for (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
|
|
|
|
{
|
2014-05-08 11:12:59 +08:00
|
|
|
std::string utf8str;
|
|
|
|
StringUtils::UTF16ToUTF8(_currentUTF16String, utf8str);
|
|
|
|
return StringUtils::format("<Label | Tag = %d, Label = '%s'>", _tag, utf8str.c_str());
|
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
|
|
|
{
|
2015-04-07 11:17:01 +08:00
|
|
|
if (_systemFontDirty || _contentDirty)
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
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
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
void Label::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
|
|
|
Node::removeAllChildrenWithCleanup(cleanup);
|
|
|
|
_letters.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::removeChild(Node* child, bool cleanup /* = true */)
|
|
|
|
{
|
|
|
|
Node::removeChild(child, cleanup);
|
2015-07-16 14:08:59 +08:00
|
|
|
for (auto&& it : _letters)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
if (it.second == child)
|
|
|
|
{
|
|
|
|
_letters.erase(it.first);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 11:30:56 +08:00
|
|
|
FontDefinition Label::_getFontDefinition() const
|
|
|
|
{
|
|
|
|
FontDefinition systemFontDef;
|
|
|
|
systemFontDef._fontName = _systemFont;
|
|
|
|
systemFontDef._fontSize = _systemFontSize;
|
|
|
|
systemFontDef._alignment = _hAlignment;
|
|
|
|
systemFontDef._vertAlignment = _vAlignment;
|
|
|
|
systemFontDef._dimensions.width = _labelWidth;
|
|
|
|
systemFontDef._dimensions.height = _labelHeight;
|
|
|
|
systemFontDef._fontFillColor.r = _textColor.r;
|
|
|
|
systemFontDef._fontFillColor.g = _textColor.g;
|
|
|
|
systemFontDef._fontFillColor.b = _textColor.b;
|
|
|
|
systemFontDef._fontAlpha = _textColor.a;
|
|
|
|
systemFontDef._shadow._shadowEnabled = false;
|
|
|
|
|
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE && _outlineSize > 0.f)
|
|
|
|
{
|
|
|
|
systemFontDef._stroke._strokeEnabled = true;
|
|
|
|
systemFontDef._stroke._strokeSize = _outlineSize;
|
|
|
|
systemFontDef._stroke._strokeColor.r = _effectColorF.r * 255;
|
|
|
|
systemFontDef._stroke._strokeColor.g = _effectColorF.g * 255;
|
|
|
|
systemFontDef._stroke._strokeColor.b = _effectColorF.b * 255;
|
|
|
|
systemFontDef._stroke._strokeAlpha = _effectColorF.a * 255;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
systemFontDef._stroke._strokeEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS)
|
|
|
|
if (systemFontDef._stroke._strokeEnabled)
|
|
|
|
{
|
|
|
|
CCLOGERROR("Currently only supported on iOS and Android!");
|
|
|
|
}
|
|
|
|
systemFontDef._stroke._strokeEnabled = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return systemFontDef;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:16:26 +08:00
|
|
|
NS_CC_END
|