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"
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
#include "2d/CCFont.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCFontAtlasCache.h"
|
2015-07-24 18:38:45 +08:00
|
|
|
#include "2d/CCFontAtlas.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"
|
2015-07-24 18:38:45 +08:00
|
|
|
#include "2d/CCDrawNode.h"
|
2015-07-30 13:59:50 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "platform/CCFileUtils.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"
|
2015-11-19 16:06:13 +08:00
|
|
|
#include "2d/CCFontFNT.h"
|
2014-05-10 12:29:24 +08:00
|
|
|
|
2013-07-23 02:17:51 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
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;
|
2015-12-16 14:59:33 +08:00
|
|
|
_letterVisible = true;
|
2015-07-07 14:20:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static LabelLetter* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated = false)
|
|
|
|
{
|
|
|
|
auto letter = new (std::nothrow) LabelLetter();
|
|
|
|
if (letter && letter->initWithTexture(texture, rect, rotated))
|
|
|
|
{
|
2015-12-16 14:59:33 +08:00
|
|
|
letter->Sprite::setVisible(false);
|
2015-07-07 14:20:23 +08:00
|
|
|
letter->autorelease();
|
|
|
|
return letter;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(letter);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-10 16:50:59 +08:00
|
|
|
|
|
|
|
CREATE_FUNC(LabelLetter);
|
2015-07-07 14:20:23 +08:00
|
|
|
|
|
|
|
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;
|
2015-08-11 10:06:14 +08:00
|
|
|
if (_flippedX)
|
|
|
|
{
|
|
|
|
std::swap(x1, x2);
|
|
|
|
}
|
|
|
|
if (_flippedY)
|
|
|
|
{
|
|
|
|
std::swap(y1, y2);
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-10-02 16:59:12 +08:00
|
|
|
virtual void updateColor() override
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
if (_textureAtlas == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:59:33 +08:00
|
|
|
auto displayedOpacity = _displayedOpacity;
|
|
|
|
if(!_letterVisible)
|
|
|
|
{
|
|
|
|
displayedOpacity = 0.0f;
|
|
|
|
}
|
|
|
|
Color4B color4(_displayedColor.r, _displayedColor.g, _displayedColor.b, displayedOpacity);
|
2015-07-07 14:20:23 +08:00
|
|
|
// special opacity for premultiplied textures
|
|
|
|
if (_opacityModifyRGB)
|
|
|
|
{
|
2015-12-16 14:59:33 +08:00
|
|
|
color4.r *= displayedOpacity / 255.0f;
|
|
|
|
color4.g *= displayedOpacity / 255.0f;
|
|
|
|
color4.b *= displayedOpacity / 255.0f;
|
2015-07-07 14:20:23 +08:00
|
|
|
}
|
|
|
|
_quad.bl.colors = color4;
|
|
|
|
_quad.br.colors = color4;
|
|
|
|
_quad.tl.colors = color4;
|
|
|
|
_quad.tr.colors = color4;
|
|
|
|
|
|
|
|
_textureAtlas->updateQuad(&_quad, _atlasIndex);
|
|
|
|
}
|
2015-08-18 15:42:05 +08:00
|
|
|
|
2015-12-16 14:59:33 +08:00
|
|
|
void setVisible(bool visible) override
|
|
|
|
{
|
|
|
|
_letterVisible = visible;
|
|
|
|
updateColor();
|
|
|
|
}
|
|
|
|
|
2015-08-18 15:42:05 +08:00
|
|
|
//LabelLetter doesn't need to draw directly.
|
2015-10-02 16:59:12 +08:00
|
|
|
void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override
|
2015-08-18 15:42:05 +08:00
|
|
|
{
|
|
|
|
}
|
2015-12-16 14:59:33 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool _letterVisible;
|
2015-07-07 14:20:23 +08:00
|
|
|
};
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
Label* Label::create()
|
2013-07-23 02:17:51 +08:00
|
|
|
{
|
2015-12-03 09:23:23 +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
|
|
|
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
|
|
|
|
2015-12-03 09:23:23 +08:00
|
|
|
if (ret && ret->initWithTTF(text, fontFile, fontSize, dimensions, hAlignment, vAlignment))
|
2014-03-10 19:42:43 +08:00
|
|
|
{
|
2015-12-03 09:23:23 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2014-04-09 21:35:08 +08:00
|
|
|
}
|
2014-03-10 19:42:43 +08:00
|
|
|
|
2015-12-03 09:23:23 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2014-04-09 21:35:08 +08:00
|
|
|
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
|
|
|
|
2015-12-03 09:23:23 +08:00
|
|
|
if (ret && ret->initWithTTF(ttfConfig, text, hAlignment, maxLineWidth))
|
2013-07-26 08:58:13 +08:00
|
|
|
{
|
2014-04-09 21:35:08 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-23 02:17:51 +08:00
|
|
|
|
2015-12-03 09:23:23 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2014-04-09 21:35:08 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-03 09:23:23 +08:00
|
|
|
|
|
|
|
bool Label::initWithTTF(const std::string& text, const std::string& fontFilePath, float fontSize,
|
|
|
|
const Size& dimensions, TextHAlignment hAlignment, TextVAlignment vAlignment)
|
|
|
|
{
|
|
|
|
if (FileUtils::getInstance()->isFileExist(fontFilePath))
|
|
|
|
{
|
|
|
|
TTFConfig ttfConfig(fontFilePath, fontSize, GlyphCollection::DYNAMIC);
|
|
|
|
if (setTTFConfig(ttfConfig))
|
|
|
|
{
|
|
|
|
setDimensions(dimensions.width, dimensions.height);
|
|
|
|
setString(text);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::initWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment hAlignment, int maxLineWidth)
|
|
|
|
{
|
|
|
|
if (FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) && setTTFConfig(ttfConfig))
|
|
|
|
{
|
|
|
|
setMaxLineWidth(maxLineWidth);
|
|
|
|
setString(text);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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)
|
2016-01-20 12:41:31 +08:00
|
|
|
, _boldEnabled(false)
|
|
|
|
, _underlineNode(nullptr)
|
|
|
|
, _strikethroughEnabled(false)
|
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-24 18:38:45 +08:00
|
|
|
#if CC_LABEL_DEBUG_DRAW
|
|
|
|
_debugDrawNode = DrawNode::create();
|
|
|
|
addChild(_debugDrawNode);
|
|
|
|
#endif
|
|
|
|
|
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;
|
2015-07-24 18:38:45 +08:00
|
|
|
_numberOfLines = 0;
|
|
|
|
_lengthOfString = 0;
|
|
|
|
_utf16Text.clear();
|
|
|
|
_utf8Text.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;
|
2015-07-24 18:38:45 +08:00
|
|
|
_lineHeight = 0.f;
|
2015-09-29 00:02:17 +08:00
|
|
|
_lineSpacing = 0.f;
|
2015-07-14 11:30:56 +08:00
|
|
|
_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;
|
|
|
|
|
|
|
|
_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;
|
2015-11-19 16:06:13 +08:00
|
|
|
_enableWrap = true;
|
|
|
|
_bmFontSize = -1;
|
|
|
|
_bmfontScale = 1.0f;
|
2016-01-04 21:43:37 +08:00
|
|
|
_overflow = Overflow::NONE;
|
2015-11-19 16:06:13 +08:00
|
|
|
_originalFontSize = 0.0f;
|
2016-01-20 12:41:31 +08:00
|
|
|
_boldEnabled = false;
|
|
|
|
if (_underlineNode)
|
|
|
|
{
|
|
|
|
removeChild(_underlineNode);
|
|
|
|
_underlineNode = nullptr;
|
|
|
|
}
|
|
|
|
_strikethroughEnabled = false;
|
|
|
|
setRotationSkewX(0); // reverse italics
|
2014-01-11 22:33:07 +08:00
|
|
|
}
|
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
// ETC1 ALPHA supports, for LabelType::BMFONT & LabelType::CHARMAP
|
|
|
|
static Texture2D* _getTexture(Label* label)
|
|
|
|
{
|
|
|
|
struct _FontAtlasPub : public FontAtlas
|
|
|
|
{
|
|
|
|
Texture2D* getTexture()
|
|
|
|
{
|
|
|
|
if (!_atlasTextures.empty())
|
|
|
|
return _atlasTextures.begin()->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto fontAtlas = label->getFontAtlas();
|
|
|
|
Texture2D* texture = nullptr;
|
|
|
|
if (fontAtlas != nullptr)
|
|
|
|
texture = ((_FontAtlasPub*)(fontAtlas))->getTexture();
|
|
|
|
return texture;
|
|
|
|
}
|
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));
|
2015-08-05 00:21:06 +08:00
|
|
|
else if (_shadowEnabled)
|
2016-07-25 17:31:54 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, _getTexture(this)));
|
2015-08-05 00:21:06 +08:00
|
|
|
else
|
2016-07-25 17:31:54 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, _getTexture(this)));
|
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
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
if(atlas)
|
|
|
|
{
|
|
|
|
_systemFontDirty = false;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_lineHeight = _fontAtlas->getLineHeight();
|
2014-03-10 19:42:43 +08:00
|
|
|
_contentDirty = true;
|
2016-03-22 14:46:55 +08:00
|
|
|
_systemFontDirty = false;
|
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
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
_originalFontSize = ttfConfig.fontSize;
|
|
|
|
return setTTFConfigInternal(ttfConfig);
|
|
|
|
}
|
2014-01-11 22:33:07 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
bool Label::setBMFontFilePath(const std::string& bmfontFilePath, const Vec2& imageOffset, float fontSize)
|
|
|
|
{
|
|
|
|
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasFNT(bmfontFilePath,imageOffset);
|
|
|
|
|
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
|
|
|
|
2015-12-09 00:45:39 +08:00
|
|
|
//assign the default fontSize
|
2016-07-12 03:13:56 +08:00
|
|
|
if (std::abs(fontSize) < FLT_EPSILON) {
|
2015-11-19 16:06:13 +08:00
|
|
|
FontFNT *bmFont = (FontFNT*)newAtlas->getFont();
|
|
|
|
if (bmFont) {
|
|
|
|
float originalFontSize = bmFont->getOriginalFontSize();
|
2015-12-13 13:59:42 +08:00
|
|
|
_bmFontSize = originalFontSize / CC_CONTENT_SCALE_FACTOR();
|
2015-11-19 16:06:13 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
2014-03-13 18:46:35 +08:00
|
|
|
|
2015-11-26 12:03:18 +08:00
|
|
|
if(fontSize > 0.0f){
|
2015-11-19 16:06:13 +08:00
|
|
|
_bmFontSize = fontSize;
|
2014-03-10 19:42:43 +08:00
|
|
|
}
|
2015-11-19 16:06:13 +08:00
|
|
|
|
2014-03-17 18:33:15 +08:00
|
|
|
_bmFontPath = bmfontFilePath;
|
2015-11-19 16:06:13 +08:00
|
|
|
|
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
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
if (text.compare(_utf8Text))
|
2014-05-08 21:30:12 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_utf8Text = text;
|
2014-05-14 16:41:26 +08:00
|
|
|
_contentDirty = true;
|
|
|
|
|
|
|
|
std::u16string utf16String;
|
2015-07-24 18:38:45 +08:00
|
|
|
if (StringUtils::UTF8ToUTF16(_utf8Text, utf16String))
|
2014-05-14 16:41:26 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_utf16Text = utf16String;
|
2014-05-14 16:41:26 +08:00
|
|
|
}
|
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
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
if(_overflow == Overflow::RESIZE_HEIGHT){
|
|
|
|
height = 0;
|
|
|
|
}
|
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;
|
2015-11-19 16:06:13 +08:00
|
|
|
|
|
|
|
if(_overflow == Overflow::SHRINK){
|
|
|
|
if (_originalFontSize > 0) {
|
|
|
|
this->restoreFontSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::restoreFontSize()
|
|
|
|
{
|
|
|
|
if(_currentLabelType == LabelType::TTF){
|
|
|
|
auto ttfConfig = this->getTTFConfig();
|
|
|
|
ttfConfig.fontSize = _originalFontSize;
|
|
|
|
this->setTTFConfigInternal(ttfConfig);
|
|
|
|
}else if(_currentLabelType == LabelType::BMFONT){
|
|
|
|
this->setBMFontSizeInternal(_originalFontSize);
|
|
|
|
}else if(_currentLabelType == LabelType::STRING_TEXTURE){
|
|
|
|
this->setSystemFontSize(_originalFontSize);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 10:29:58 +08:00
|
|
|
void Label::updateLabelLetters()
|
|
|
|
{
|
|
|
|
if (!_letters.empty())
|
|
|
|
{
|
|
|
|
Rect uvRect;
|
|
|
|
LabelLetter* letterSprite;
|
|
|
|
int letterIndex;
|
|
|
|
|
|
|
|
for (auto it = _letters.begin(); it != _letters.end();)
|
|
|
|
{
|
|
|
|
letterIndex = it->first;
|
|
|
|
letterSprite = (LabelLetter*)it->second;
|
|
|
|
|
|
|
|
if (letterIndex >= _lengthOfString)
|
|
|
|
{
|
|
|
|
Node::removeChild(letterSprite, true);
|
|
|
|
it = _letters.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto& letterInfo = _lettersInfo[letterIndex];
|
|
|
|
auto& letterDef = _fontAtlas->_letterDefinitions[letterInfo.utf16Char];
|
|
|
|
uvRect.size.height = letterDef.height;
|
|
|
|
uvRect.size.width = letterDef.width;
|
|
|
|
uvRect.origin.x = letterDef.U;
|
|
|
|
uvRect.origin.y = letterDef.V;
|
|
|
|
|
2015-11-20 16:39:48 +08:00
|
|
|
auto batchNode = _batchNodes.at(letterDef.textureID);
|
|
|
|
letterSprite->setTextureAtlas(batchNode->getTextureAtlas());
|
2015-08-13 10:29:58 +08:00
|
|
|
letterSprite->setTexture(_fontAtlas->getTexture(letterDef.textureID));
|
|
|
|
if (letterDef.width <= 0.f || letterDef.height <= 0.f)
|
|
|
|
{
|
|
|
|
letterSprite->setTextureAtlas(nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
letterSprite->setTextureRect(uvRect, false, uvRect.size);
|
|
|
|
letterSprite->setTextureAtlas(_batchNodes.at(letterDef.textureID)->getTextureAtlas());
|
|
|
|
letterSprite->setAtlasIndex(_lettersInfo[letterIndex].atlasIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto px = letterInfo.positionX + letterDef.width / 2 + _linesOffsetX[letterInfo.lineIndex];
|
|
|
|
auto py = letterInfo.positionY - letterDef.height / 2 + _letterOffsetY;
|
|
|
|
letterSprite->setPosition(px, py);
|
|
|
|
|
2015-11-20 16:39:48 +08:00
|
|
|
this->updateLetterSpriteScale(letterSprite);
|
2015-08-13 10:29:58 +08:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
bool Label::alignText()
|
2014-01-23 13:42:27 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_fontAtlas == nullptr || _utf16Text.empty())
|
2014-02-19 20:26:14 +08:00
|
|
|
{
|
2014-12-15 14:36:57 +08:00
|
|
|
setContentSize(Size::ZERO);
|
2015-11-19 16:06:13 +08:00
|
|
|
return true;
|
2014-02-19 20:26:14 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
bool ret = true;
|
|
|
|
do {
|
|
|
|
_fontAtlas->prepareLetterDefinitions(_utf16Text);
|
|
|
|
auto& textures = _fontAtlas->getTextures();
|
2016-04-26 14:50:39 +08:00
|
|
|
if (textures.size() > static_cast<size_t>(_batchNodes.size()))
|
2014-02-18 14:30:51 +08:00
|
|
|
{
|
2016-04-26 14:50:39 +08:00
|
|
|
for (auto index = static_cast<size_t>(_batchNodes.size()); index < textures.size(); ++index)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
auto batchNode = SpriteBatchNode::createWithTexture(textures.at(index));
|
|
|
|
if (batchNode)
|
|
|
|
{
|
|
|
|
_isOpacityModifyRGB = batchNode->getTexture()->hasPremultipliedAlpha();
|
|
|
|
_blendFunc = batchNode->getBlendFunc();
|
|
|
|
batchNode->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
|
|
|
batchNode->setPosition(Vec2::ZERO);
|
|
|
|
_batchNodes.pushBack(batchNode);
|
|
|
|
}
|
2015-07-07 14:20:23 +08:00
|
|
|
}
|
2014-02-18 14:30:51 +08:00
|
|
|
}
|
2015-11-19 16:06:13 +08:00
|
|
|
if (_batchNodes.empty())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_reusedLetter->setBatchNode(_batchNodes.at(0));
|
|
|
|
|
|
|
|
_lengthOfString = 0;
|
|
|
|
_textDesiredHeight = 0.f;
|
|
|
|
_linesWidth.clear();
|
|
|
|
if (_maxLineWidth > 0.f && !_lineBreakWithoutSpaces)
|
|
|
|
{
|
|
|
|
multilineTextWrapByWord();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
multilineTextWrapByChar();
|
|
|
|
}
|
|
|
|
computeAlignmentOffset();
|
|
|
|
|
2015-11-20 16:39:48 +08:00
|
|
|
if(_overflow == Overflow::SHRINK){
|
2015-11-20 10:54:05 +08:00
|
|
|
float fontSize = this->getRenderingFontSize();
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
if(fontSize > 0 && isVerticalClamp()){
|
|
|
|
this->shrinkLabelToContentSize(CC_CALLBACK_0(Label::isVerticalClamp, this));
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 16:27:28 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
if(!updateQuads()){
|
|
|
|
ret = false;
|
2015-12-07 18:13:03 +08:00
|
|
|
if(_overflow == Overflow::SHRINK){
|
2015-11-19 16:06:13 +08:00
|
|
|
this->shrinkLabelToContentSize(CC_CALLBACK_0(Label::isHorizontalClamp, this));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLabelLetters();
|
|
|
|
|
|
|
|
updateColor();
|
|
|
|
}while (0);
|
2015-08-13 10:29:58 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
return ret;
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:13:03 +08:00
|
|
|
bool Label::isHorizontalClamped(float letterPositionX, int lineIndex)
|
|
|
|
{
|
|
|
|
auto wordWidth = this->_linesWidth[lineIndex];
|
|
|
|
bool letterOverClamp = (letterPositionX > _contentSize.width || letterPositionX < 0);
|
|
|
|
if (!_enableWrap) {
|
|
|
|
return letterOverClamp;
|
|
|
|
}else{
|
|
|
|
return (wordWidth > this->_contentSize.width && letterOverClamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
bool Label::updateQuads()
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
bool ret = true;
|
2015-07-24 18:38:45 +08:00
|
|
|
for (auto&& batchNode : _batchNodes)
|
2014-03-17 18:33:15 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
batchNode->getTextureAtlas()->removeAllQuads();
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
bool letterClamp = false;
|
2015-07-24 18:38:45 +08:00
|
|
|
for (int ctr = 0; ctr < _lengthOfString; ++ctr)
|
|
|
|
{
|
2015-08-10 16:50:59 +08:00
|
|
|
if (_lettersInfo[ctr].valid)
|
2014-03-17 18:33:15 +08:00
|
|
|
{
|
2015-08-10 16:50:59 +08:00
|
|
|
auto& letterDef = _fontAtlas->_letterDefinitions[_lettersInfo[ctr].utf16Char];
|
|
|
|
|
2014-03-17 18:33:15 +08:00
|
|
|
_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
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
auto py = _lettersInfo[ctr].positionY + _letterOffsetY;
|
2015-08-10 16:50:59 +08:00
|
|
|
if (_labelHeight > 0.f) {
|
|
|
|
if (py > _tailoredTopY)
|
|
|
|
{
|
|
|
|
auto clipTop = py - _tailoredTopY;
|
|
|
|
_reusedRect.origin.y += clipTop;
|
|
|
|
_reusedRect.size.height -= clipTop;
|
|
|
|
py -= clipTop;
|
|
|
|
}
|
2015-12-02 13:48:20 +08:00
|
|
|
if (py - letterDef.height * _bmfontScale < _tailoredBottomY)
|
2015-08-10 16:50:59 +08:00
|
|
|
{
|
|
|
|
_reusedRect.size.height = (py < _tailoredBottomY) ? 0.f : (py - _tailoredBottomY);
|
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
}
|
2014-03-17 18:33:15 +08:00
|
|
|
|
2015-12-07 18:13:03 +08:00
|
|
|
auto lineIndex = _lettersInfo[ctr].lineIndex;
|
|
|
|
auto px = _lettersInfo[ctr].positionX + letterDef.width/2 * _bmfontScale + _linesOffsetX[lineIndex];
|
|
|
|
|
|
|
|
if(_labelWidth > 0.f){
|
|
|
|
if (this->isHorizontalClamped(px, lineIndex)) {
|
|
|
|
if(_overflow == Overflow::CLAMP){
|
|
|
|
_reusedRect.size.width = 0;
|
|
|
|
}else if(_overflow == Overflow::SHRINK){
|
|
|
|
if (_contentSize.width > letterDef.width) {
|
|
|
|
letterClamp = true;
|
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}else{
|
2015-11-19 16:06:13 +08:00
|
|
|
_reusedRect.size.width = 0;
|
|
|
|
}
|
2015-12-07 18:13:03 +08:00
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:13:03 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_reusedRect.size.height > 0.f && _reusedRect.size.width > 0.f)
|
|
|
|
{
|
|
|
|
_reusedLetter->setTextureRect(_reusedRect, false, _reusedRect.size);
|
2015-11-19 18:14:46 +08:00
|
|
|
float letterPositionX = _lettersInfo[ctr].positionX + _linesOffsetX[_lettersInfo[ctr].lineIndex];
|
|
|
|
_reusedLetter->setPosition(letterPositionX, py);
|
2015-07-24 18:38:45 +08:00
|
|
|
auto index = static_cast<int>(_batchNodes.at(letterDef.textureID)->getTextureAtlas()->getTotalQuads());
|
|
|
|
_lettersInfo[ctr].atlasIndex = index;
|
2015-12-07 18:13:03 +08:00
|
|
|
|
2015-11-20 16:39:48 +08:00
|
|
|
this->updateLetterSpriteScale(_reusedLetter);
|
2015-11-19 18:14:46 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
_batchNodes.at(letterDef.textureID)->insertQuadFromSprite(_reusedLetter, index);
|
|
|
|
}
|
2014-03-17 18:33:15 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-19 16:06:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::setTTFConfigInternal(const TTFConfig& ttfConfig)
|
|
|
|
{
|
|
|
|
FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(&ttfConfig);
|
|
|
|
|
|
|
|
if (!newAtlas)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_currentLabelType = LabelType::TTF;
|
|
|
|
setFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true);
|
|
|
|
|
|
|
|
_fontConfig = ttfConfig;
|
|
|
|
|
|
|
|
if (_fontConfig.outlineSize > 0)
|
|
|
|
{
|
|
|
|
_fontConfig.distanceFieldEnabled = false;
|
|
|
|
_useDistanceField = false;
|
|
|
|
_useA8Shader = false;
|
|
|
|
_currLabelEffect = LabelEffect::OUTLINE;
|
|
|
|
updateShaderProgram();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
|
|
|
updateShaderProgram();
|
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
if (_fontConfig.italics)
|
|
|
|
this->enableItalics();
|
|
|
|
if (_fontConfig.bold)
|
|
|
|
this->enableBold();
|
|
|
|
if (_fontConfig.underline)
|
|
|
|
this->enableUnderline();
|
|
|
|
if (_fontConfig.strikethrough)
|
|
|
|
this->enableStrikethrough();
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setBMFontSizeInternal(float fontSize)
|
|
|
|
{
|
|
|
|
if(_currentLabelType == LabelType::BMFONT){
|
|
|
|
this->setBMFontFilePath(_bmFontPath, Vec2::ZERO, fontSize);
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::scaleFontSizeDown(float fontSize)
|
|
|
|
{
|
|
|
|
bool shouldUpdateContent = true;
|
|
|
|
if(_currentLabelType == LabelType::TTF){
|
|
|
|
auto ttfConfig = this->getTTFConfig();
|
|
|
|
ttfConfig.fontSize = fontSize;
|
|
|
|
this->setTTFConfigInternal(ttfConfig);
|
|
|
|
}else if(_currentLabelType == LabelType::BMFONT){
|
2016-07-12 03:13:56 +08:00
|
|
|
if (std::abs(fontSize) < FLT_EPSILON) {
|
2015-11-19 16:06:13 +08:00
|
|
|
fontSize = 0.1f;
|
|
|
|
shouldUpdateContent = false;
|
|
|
|
}
|
|
|
|
this->setBMFontSizeInternal(fontSize);
|
|
|
|
}else if (_currentLabelType == LabelType::STRING_TEXTURE){
|
|
|
|
this->setSystemFontSize(fontSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldUpdateContent) {
|
|
|
|
this->updateContent();
|
|
|
|
}
|
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
|
|
|
_currLabelEffect = LabelEffect::OUTLINE;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
2015-12-10 13:17:31 +08:00
|
|
|
_outlineSize = outlineSize;
|
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
|
|
|
|
2015-09-28 01:12:50 +08:00
|
|
|
_shadowOffset.width = offset.width;
|
|
|
|
_shadowOffset.height = offset.height;
|
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;
|
2015-08-05 00:21:06 +08:00
|
|
|
|
|
|
|
if (_currentLabelType == LabelType::BMFONT || _currentLabelType == LabelType::CHARMAP)
|
|
|
|
{
|
2016-07-25 17:31:54 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(_shadowEnabled ? GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR : GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, _getTexture(this)));
|
2015-08-05 00:21:06 +08:00
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
}
|
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
void Label::enableItalics()
|
|
|
|
{
|
|
|
|
setRotationSkewX(12);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::enableBold()
|
|
|
|
{
|
|
|
|
if (!_boldEnabled)
|
|
|
|
{
|
|
|
|
// bold is implemented with outline
|
2016-04-20 02:54:32 +08:00
|
|
|
enableShadow(Color4B::WHITE, Size(0.9f, 0), 0);
|
2016-01-20 12:41:31 +08:00
|
|
|
// add one to kerning
|
|
|
|
setAdditionalKerning(_additionalKerning+1);
|
|
|
|
_boldEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::enableUnderline()
|
|
|
|
{
|
|
|
|
// remove it, just in case to prevent adding two or more
|
|
|
|
if (!_underlineNode)
|
|
|
|
{
|
|
|
|
_underlineNode = DrawNode::create();
|
|
|
|
addChild(_underlineNode, 100000);
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::enableStrikethrough()
|
|
|
|
{
|
|
|
|
if (!_strikethroughEnabled)
|
|
|
|
{
|
|
|
|
enableUnderline();
|
|
|
|
_strikethroughEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 15:54:40 +08:00
|
|
|
void Label::disableEffect()
|
|
|
|
{
|
2016-01-20 12:41:31 +08:00
|
|
|
disableEffect(LabelEffect::ALL);
|
2015-04-07 15:51:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Label::disableEffect(LabelEffect effect)
|
|
|
|
{
|
|
|
|
switch (effect)
|
2014-03-26 11:09:16 +08:00
|
|
|
{
|
2016-01-20 12:41:31 +08:00
|
|
|
case cocos2d::LabelEffect::NORMAL:
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::OUTLINE:
|
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE)
|
2015-04-07 15:51:57 +08:00
|
|
|
{
|
2016-01-20 12:41:31 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
|
|
|
_fontConfig.outlineSize = 0;
|
|
|
|
setTTFConfig(_fontConfig);
|
|
|
|
}
|
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
|
|
|
_contentDirty = true;
|
2015-04-07 15:51:57 +08:00
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::SHADOW:
|
|
|
|
if (_shadowEnabled)
|
|
|
|
{
|
|
|
|
_shadowEnabled = false;
|
|
|
|
CC_SAFE_RELEASE_NULL(_shadowNode);
|
2016-01-21 11:08:31 +08:00
|
|
|
updateShaderProgram();
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::GLOW:
|
|
|
|
if (_currLabelEffect == LabelEffect::GLOW)
|
|
|
|
{
|
|
|
|
_currLabelEffect = LabelEffect::NORMAL;
|
|
|
|
updateShaderProgram();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::ITALICS:
|
|
|
|
setRotationSkewX(0);
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::BOLD:
|
|
|
|
_boldEnabled = false;
|
|
|
|
_additionalKerning -= 1;
|
|
|
|
disableEffect(LabelEffect::SHADOW);
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::UNDERLINE:
|
|
|
|
if (_underlineNode) {
|
|
|
|
removeChild(_underlineNode);
|
|
|
|
_underlineNode = nullptr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case cocos2d::LabelEffect::STRIKETHROUGH:
|
|
|
|
_strikethroughEnabled = false;
|
|
|
|
// since it is based on underline, disable it as well
|
|
|
|
disableEffect(LabelEffect::UNDERLINE);
|
|
|
|
break;
|
|
|
|
case LabelEffect::ALL:
|
2015-04-17 14:39:52 +08:00
|
|
|
{
|
|
|
|
disableEffect(LabelEffect::SHADOW);
|
|
|
|
disableEffect(LabelEffect::GLOW);
|
|
|
|
disableEffect(LabelEffect::OUTLINE);
|
2016-01-20 12:41:31 +08:00
|
|
|
disableEffect(LabelEffect::ITALICS);
|
|
|
|
disableEffect(LabelEffect::BOLD);
|
|
|
|
disableEffect(LabelEffect::UNDERLINE);
|
|
|
|
disableEffect(LabelEffect::STRIKETHROUGH);
|
2015-04-17 14:39:52 +08:00
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
break;
|
|
|
|
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::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-24 18:38:45 +08:00
|
|
|
texture->initWithString(_utf8Text.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());
|
2015-08-19 10:22:48 +08:00
|
|
|
_textSprite->setGlobalZOrder(getGlobalZOrder());
|
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;
|
2015-07-24 18:38:45 +08:00
|
|
|
texture->initWithString(_utf8Text.c_str(), shadowFontDefinition);
|
2015-04-07 11:17:01 +08:00
|
|
|
_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-08-19 10:22:48 +08:00
|
|
|
_shadowNode->setGlobalZOrder(getGlobalZOrder());
|
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
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
CCLOGERROR("Stroke Currently only supported on iOS and Android!");
|
2014-04-09 21:35:08 +08:00
|
|
|
}
|
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);
|
2015-11-19 16:06:13 +08:00
|
|
|
bool updateFinished = true;
|
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;
|
2015-07-24 18:38:45 +08:00
|
|
|
if (StringUtils::UTF8ToUTF16(_utf8Text, utf16String))
|
2014-04-14 11:33:46 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_utf16Text = utf16String;
|
2014-04-14 11:33:46 +08:00
|
|
|
}
|
2014-04-09 21:35:08 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
computeHorizontalKernings(_utf16Text);
|
2015-11-19 16:06:13 +08:00
|
|
|
updateFinished = 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
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
if (_underlineNode)
|
|
|
|
{
|
|
|
|
_underlineNode->clear();
|
|
|
|
|
2016-01-21 10:41:59 +08:00
|
|
|
if (_numberOfLines)
|
2016-01-20 12:41:31 +08:00
|
|
|
{
|
2016-01-21 10:41:59 +08:00
|
|
|
const float charheight = (_textDesiredHeight / _numberOfLines);
|
|
|
|
_underlineNode->setLineWidth(charheight/6);
|
|
|
|
|
|
|
|
// atlas font
|
|
|
|
for (int i=0; i<_numberOfLines; ++i)
|
|
|
|
{
|
|
|
|
float offsety = 0;
|
|
|
|
if (_strikethroughEnabled)
|
|
|
|
offsety += charheight / 2;
|
|
|
|
// FIXME: Might not work with different vertical alignments
|
|
|
|
float y = (_numberOfLines - i - 1) * charheight + offsety;
|
|
|
|
_underlineNode->drawLine(Vec2(_linesOffsetX[i],y), Vec2(_linesWidth[i] + _linesOffsetX[i],y), _textColorF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_textSprite)
|
|
|
|
{
|
|
|
|
// system font
|
|
|
|
float y = 0;
|
|
|
|
const auto spriteSize = _textSprite->getContentSize();
|
|
|
|
_underlineNode->setLineWidth(spriteSize.height/6);
|
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
if (_strikethroughEnabled)
|
2016-01-21 10:41:59 +08:00
|
|
|
// FIXME: system fonts don't report the height of the font correctly. only the size of the texture, which is POT
|
|
|
|
y += spriteSize.height / 2;
|
2016-01-20 12:41:31 +08:00
|
|
|
// FIXME: Might not work with different vertical alignments
|
2016-01-21 10:41:59 +08:00
|
|
|
_underlineNode->drawLine(Vec2(0,y), Vec2(spriteSize.width,y), Color4F(_textSprite->getDisplayedColor()));
|
2016-01-20 12:41:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
if(updateFinished){
|
|
|
|
_contentDirty = false;
|
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
|
|
|
|
#if CC_LABEL_DEBUG_DRAW
|
|
|
|
_debugDrawNode->clear();
|
|
|
|
Vec2 vertices[4] =
|
|
|
|
{
|
|
|
|
Vec2::ZERO,
|
|
|
|
Vec2(_contentSize.width, 0),
|
|
|
|
Vec2(_contentSize.width, _contentSize.height),
|
|
|
|
Vec2(0, _contentSize.height)
|
|
|
|
};
|
|
|
|
_debugDrawNode->drawPoly(vertices, 4, true, Color4F::WHITE);
|
|
|
|
#endif
|
2014-03-26 11:09:16 +08:00
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
void Label::setBMFontSize(float fontSize)
|
|
|
|
{
|
|
|
|
this->setBMFontSizeInternal(fontSize);
|
|
|
|
_originalFontSize = fontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getBMFontSize()const
|
|
|
|
{
|
|
|
|
return _bmFontSize;
|
|
|
|
}
|
|
|
|
|
2016-01-20 12:41:31 +08:00
|
|
|
void Label::onDrawShadow(GLProgram* glProgram, const Color4F& shadowColor)
|
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,
|
2016-01-20 12:41:31 +08:00
|
|
|
shadowColor.r, shadowColor.g, shadowColor.b, shadowColor.a);
|
2015-07-08 17:59:24 +08:00
|
|
|
if (_currLabelEffect == LabelEffect::OUTLINE || _currLabelEffect == LabelEffect::GLOW)
|
|
|
|
{
|
|
|
|
glProgram->setUniformLocationWith4f(_uniformEffectColor,
|
2016-01-20 12:41:31 +08:00
|
|
|
shadowColor.r, shadowColor.g, shadowColor.b, shadowColor.a);
|
2015-07-08 17:59:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-01-20 12:41:31 +08:00
|
|
|
_displayedOpacity = shadowColor.a * 255;
|
|
|
|
setColor(Color3B(shadowColor));
|
2015-07-08 17:59:24 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-01-20 12:41:31 +08:00
|
|
|
if (_boldEnabled)
|
|
|
|
onDrawShadow(glprogram, _textColorF);
|
|
|
|
else
|
|
|
|
onDrawShadow(glprogram, _shadowColor4F);
|
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-24 18:38:45 +08:00
|
|
|
if (_batchNodes.empty() || _lengthOfString <= 0)
|
2015-07-08 17:59:24 +08:00
|
|
|
{
|
|
|
|
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
|
2015-07-30 13:59:50 +08:00
|
|
|
auto visitingCamera = Camera::getVisitingCamera();
|
|
|
|
auto defaultCamera = Camera::getDefaultCamera();
|
|
|
|
if (visitingCamera == defaultCamera) {
|
|
|
|
_insideBounds = (transformUpdated || visitingCamera->isViewProjectionUpdated()) ? renderer->checkVisibility(transform, _contentSize) : _insideBounds;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_insideBounds = renderer->checkVisibility(transform, _contentSize);
|
|
|
|
}
|
2015-07-07 14:20:23 +08:00
|
|
|
|
|
|
|
if (_insideBounds)
|
|
|
|
#endif
|
|
|
|
{
|
2015-08-04 21:49:14 +08:00
|
|
|
if (!_shadowEnabled && (_currentLabelType == LabelType::BMFONT || _currentLabelType == LabelType::CHARMAP))
|
|
|
|
{
|
2015-08-05 00:21:06 +08:00
|
|
|
for (auto&& it : _letters)
|
|
|
|
{
|
|
|
|
it.second->updateTransform();
|
|
|
|
}
|
2016-07-25 17:31:54 +08:00
|
|
|
// ETC1 ALPHA supports for BMFONT & CHARMAP
|
2015-08-04 21:49:14 +08:00
|
|
|
auto textureAtlas = _batchNodes.at(0)->getTextureAtlas();
|
2016-07-25 17:31:54 +08:00
|
|
|
auto texture = textureAtlas->getTexture();
|
|
|
|
_quadCommand.init(_globalZOrder, texture, getGLProgramState(),
|
2015-08-04 21:49:14 +08:00
|
|
|
_blendFunc, textureAtlas->getQuads(), textureAtlas->getTotalQuads(), transform, flags);
|
|
|
|
renderer->addCommand(&_quadCommand);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
|
|
|
_customCommand.func = CC_CALLBACK_0(Label::onDraw, this, transform, transformUpdated);
|
2015-07-07 14:20:23 +08:00
|
|
|
|
2015-08-04 21:49:14 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
}
|
2015-07-07 14:20:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-24 18:38:45 +08:00
|
|
|
if (! _visible || (_utf8Text.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();
|
|
|
|
}
|
2015-07-17 17:45:11 +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-24 18:38:45 +08:00
|
|
|
if (!_utf8Text.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;
|
|
|
|
}
|
2015-07-17 17:45:11 +08:00
|
|
|
|
|
|
|
this->drawSelf(visibleByCamera, renderer, flags);
|
2015-07-07 14:20:23 +08:00
|
|
|
|
|
|
|
for (auto it = _children.cbegin() + i; it != _children.cend(); ++it)
|
|
|
|
{
|
|
|
|
(*it)->visit(renderer, _modelViewTransform, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-07-17 17:45:11 +08:00
|
|
|
else
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
2015-07-17 17:45:11 +08:00
|
|
|
this->drawSelf(visibleByCamera, renderer, flags);
|
2015-07-07 14:20:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
}
|
|
|
|
|
2015-07-17 17:45:11 +08:00
|
|
|
void Label::drawSelf(bool visibleByCamera, Renderer* renderer, uint32_t flags)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
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
|
|
|
}
|
2015-07-24 18:38:45 +08:00
|
|
|
else if (visibleByCamera && !_utf8Text.empty())
|
2014-03-31 18:16:08 +08:00
|
|
|
{
|
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;
|
2016-06-15 10:43:15 +08:00
|
|
|
_currentLabelType = LabelType::STRING_TEXTURE;
|
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;
|
2015-11-19 16:06:13 +08:00
|
|
|
_originalFontSize = fontSize;
|
2016-06-15 10:43:15 +08:00
|
|
|
_currentLabelType = LabelType::STRING_TEXTURE;
|
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
|
2015-08-10 16:50:59 +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;
|
2015-08-10 16:50:59 +08:00
|
|
|
do
|
2014-03-17 18:33:15 +08:00
|
|
|
{
|
2015-07-07 14:20:23 +08:00
|
|
|
if (_systemFontDirty || _currentLabelType == LabelType::STRING_TEXTURE)
|
|
|
|
{
|
|
|
|
break;
|
2015-08-13 10:29:58 +08:00
|
|
|
}
|
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-24 18:38:45 +08:00
|
|
|
if (_textSprite == nullptr && letterIndex < _lengthOfString)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
const auto &letterInfo = _lettersInfo[letterIndex];
|
2015-07-24 18:38:45 +08:00
|
|
|
if (!letterInfo.valid)
|
2015-07-07 14:20:23 +08:00
|
|
|
{
|
|
|
|
break;
|
2015-07-24 18:38:45 +08:00
|
|
|
}
|
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
|
|
|
if (letter == nullptr)
|
|
|
|
{
|
2015-08-13 10:29:58 +08:00
|
|
|
auto& letterDef = _fontAtlas->_letterDefinitions[letterInfo.utf16Char];
|
|
|
|
auto textureID = letterDef.textureID;
|
|
|
|
Rect uvRect;
|
|
|
|
uvRect.size.height = letterDef.height;
|
|
|
|
uvRect.size.width = letterDef.width;
|
|
|
|
uvRect.origin.x = letterDef.U;
|
|
|
|
uvRect.origin.y = letterDef.V;
|
|
|
|
|
2015-08-10 16:50:59 +08:00
|
|
|
if (letterDef.width <= 0.f || letterDef.height <= 0.f)
|
|
|
|
{
|
|
|
|
letter = LabelLetter::create();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
letter = LabelLetter::createWithTexture(_fontAtlas->getTexture(textureID), uvRect);
|
|
|
|
letter->setTextureAtlas(_batchNodes.at(textureID)->getTextureAtlas());
|
|
|
|
letter->setAtlasIndex(letterInfo.atlasIndex);
|
|
|
|
auto px = letterInfo.positionX + uvRect.size.width / 2 + _linesOffsetX[letterInfo.lineIndex];
|
|
|
|
auto py = letterInfo.positionY - uvRect.size.height / 2 + _letterOffsetY;
|
|
|
|
letter->setPosition(px,py);
|
|
|
|
letter->setOpacity(_realOpacity);
|
|
|
|
}
|
|
|
|
|
2015-07-07 14:20:23 +08:00
|
|
|
addChild(letter);
|
|
|
|
_letters[letterIndex] = letter;
|
|
|
|
}
|
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!");
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_lineHeight != height)
|
2014-06-12 12:01:15 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_lineHeight = height;
|
2014-06-12 12:01:15 +08:00
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getLineHeight() const
|
|
|
|
{
|
2014-06-12 14:41:01 +08:00
|
|
|
CCASSERT(_currentLabelType != LabelType::STRING_TEXTURE, "Not supported system font!");
|
2015-11-20 16:39:48 +08:00
|
|
|
return _textSprite ? 0.0f : _lineHeight * _bmfontScale;
|
2014-06-12 12:01:15 +08:00
|
|
|
}
|
|
|
|
|
2015-09-29 00:02:17 +08:00
|
|
|
void Label::setLineSpacing(float height)
|
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
if (_lineSpacing != height)
|
|
|
|
{
|
|
|
|
_lineSpacing = height;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
2015-09-29 00:02:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getLineSpacing() const
|
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
return _lineSpacing;
|
2015-09-29 00:02:17 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 12:01:15 +08:00
|
|
|
void Label::setAdditionalKerning(float space)
|
|
|
|
{
|
2016-01-20 12:41:31 +08:00
|
|
|
|
|
|
|
if (_currentLabelType != LabelType::STRING_TEXTURE)
|
2014-06-12 12:01:15 +08:00
|
|
|
{
|
2016-01-20 12:41:31 +08:00
|
|
|
if (_additionalKerning != space)
|
|
|
|
{
|
|
|
|
_additionalKerning = space;
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
2014-06-12 12:01:15 +08:00
|
|
|
}
|
2016-01-20 12:41:31 +08:00
|
|
|
else
|
|
|
|
CCLOG("Label::setAdditionalKerning not supported on LabelType::STRING_TEXTURE");
|
2014-06-12 12:01:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float Label::getAdditionalKerning() const
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2016-06-15 10:43:15 +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
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_utf16Text.empty())
|
2014-02-10 11:21:54 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_numberOfLines = 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
|
2015-07-24 18:38:45 +08:00
|
|
|
size_t stringLen = _utf16Text.length();
|
|
|
|
for (size_t i = 0; i < stringLen - 1; ++i)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2016-03-07 19:07:36 +08:00
|
|
|
if (_utf16Text[i] == (char16_t)TextFormatter::NewLine)
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
|
|
|
quantityOfLines++;
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 15:54:40 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
_numberOfLines = quantityOfLines;
|
2013-08-08 07:36:04 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
int Label::getStringNumLines()
|
|
|
|
{
|
2014-09-25 00:46:17 +08:00
|
|
|
if (_contentDirty)
|
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
updateContent();
|
2014-09-25 00:46:17 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
if (_currentLabelType == LabelType::STRING_TEXTURE)
|
|
|
|
{
|
|
|
|
computeStringNumLines();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _numberOfLines;
|
2014-09-25 00:46:17 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
int Label::getStringLength()
|
2013-08-08 07:36:04 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
_lengthOfString = static_cast<int>(_utf16Text.length());
|
|
|
|
return _lengthOfString;
|
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
|
|
|
|
2016-06-18 08:23:59 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::STRING_TEXTURE)
|
|
|
|
setTextColor(Color4B(_displayedColor));
|
|
|
|
|
2014-03-10 19:42:43 +08:00
|
|
|
if (_textSprite)
|
|
|
|
{
|
2014-03-19 10:10:47 +08:00
|
|
|
_textSprite->updateDisplayedColor(_displayedColor);
|
2016-06-18 08:23:59 +08:00
|
|
|
}
|
2016-01-21 10:41:59 +08:00
|
|
|
|
2016-06-18 08:23:59 +08:00
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->updateDisplayedColor(_displayedColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_underlineNode)
|
|
|
|
{
|
|
|
|
_contentDirty = true;
|
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
|
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
char tmp[50];
|
|
|
|
sprintf(tmp, "<Label | Tag = %d, Label = >", _tag);
|
|
|
|
std::string ret = tmp;
|
|
|
|
ret += _utf8Text;
|
|
|
|
|
|
|
|
return ret;
|
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;
|
2016-06-15 10:43:15 +08:00
|
|
|
systemFontDef._enableWrap = _enableWrap;
|
|
|
|
systemFontDef._overflow = (int)_overflow;
|
2015-07-14 11:30:56 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
CCLOGERROR("Stroke Currently only supported on iOS and Android!");
|
2015-07-14 11:30:56 +08:00
|
|
|
}
|
|
|
|
systemFontDef._stroke._strokeEnabled = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return systemFontDef;
|
|
|
|
}
|
|
|
|
|
2015-08-19 10:22:48 +08:00
|
|
|
void Label::setGlobalZOrder(float globalZOrder)
|
|
|
|
{
|
|
|
|
Node::setGlobalZOrder(globalZOrder);
|
|
|
|
if (_textSprite)
|
|
|
|
{
|
|
|
|
_textSprite->setGlobalZOrder(globalZOrder);
|
|
|
|
if (_shadowNode)
|
|
|
|
{
|
|
|
|
_shadowNode->setGlobalZOrder(globalZOrder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 16:06:13 +08:00
|
|
|
float Label::getRenderingFontSize()const
|
|
|
|
{
|
|
|
|
float fontSize;
|
|
|
|
if (_currentLabelType == LabelType::BMFONT) {
|
|
|
|
fontSize = _bmFontSize;
|
|
|
|
}else if(_currentLabelType == LabelType::TTF){
|
|
|
|
fontSize = this->getTTFConfig().fontSize;
|
|
|
|
}else if(_currentLabelType == LabelType::STRING_TEXTURE){
|
|
|
|
fontSize = _systemFontSize;
|
2015-12-09 00:45:39 +08:00
|
|
|
}else{ //FIXME: find a way to calculate char map font size
|
2015-11-19 16:06:13 +08:00
|
|
|
fontSize = this->getLineHeight();
|
|
|
|
}
|
|
|
|
return fontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::enableWrap(bool enable)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
if(enable == _enableWrap || _overflow == Overflow::RESIZE_HEIGHT){
|
2015-11-19 16:06:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->_enableWrap = enable;
|
2015-11-20 16:39:48 +08:00
|
|
|
|
|
|
|
this->rescaleWithOriginalFontSize();
|
2015-11-19 16:06:13 +08:00
|
|
|
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Label::isWrapEnabled()const
|
|
|
|
{
|
|
|
|
return this->_enableWrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::setOverflow(Overflow overflow)
|
|
|
|
{
|
2015-11-20 10:54:05 +08:00
|
|
|
if(_overflow == overflow){
|
2015-11-19 16:06:13 +08:00
|
|
|
return;
|
|
|
|
}
|
2015-11-20 10:54:05 +08:00
|
|
|
|
|
|
|
if (_currentLabelType == LabelType::CHARMAP) {
|
|
|
|
if (overflow == Overflow::SHRINK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-11-19 16:06:13 +08:00
|
|
|
|
|
|
|
if(overflow == Overflow::RESIZE_HEIGHT){
|
|
|
|
this->setDimensions(_labelDimensions.width,0);
|
|
|
|
this->enableWrap(true);
|
|
|
|
}
|
|
|
|
_overflow = overflow;
|
|
|
|
|
2015-11-20 16:39:48 +08:00
|
|
|
this->rescaleWithOriginalFontSize();
|
|
|
|
|
|
|
|
_contentDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Label::rescaleWithOriginalFontSize()
|
|
|
|
{
|
2015-11-19 16:06:13 +08:00
|
|
|
auto renderingFontSize = this->getRenderingFontSize();
|
2015-11-20 16:39:48 +08:00
|
|
|
if (_originalFontSize - renderingFontSize >= 1) {
|
2015-11-19 16:06:13 +08:00
|
|
|
this->scaleFontSizeDown(_originalFontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Label::Overflow Label::getOverflow()const
|
|
|
|
{
|
|
|
|
return _overflow;
|
|
|
|
}
|
|
|
|
|
2015-11-20 16:39:48 +08:00
|
|
|
void Label::updateLetterSpriteScale(Sprite* sprite)
|
2015-11-19 18:14:46 +08:00
|
|
|
{
|
|
|
|
if (_currentLabelType == LabelType::BMFONT && _bmFontSize > 0)
|
|
|
|
{
|
2015-11-20 16:39:48 +08:00
|
|
|
sprite->setScale(_bmfontScale);
|
2015-11-19 18:14:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-12 03:13:56 +08:00
|
|
|
if (std::abs(_bmFontSize) < FLT_EPSILON)
|
2015-11-19 18:14:46 +08:00
|
|
|
{
|
2015-11-20 16:39:48 +08:00
|
|
|
sprite->setScale(0);
|
2015-11-19 18:14:46 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-20 16:39:48 +08:00
|
|
|
sprite->setScale(1.0);
|
2015-11-19 18:14:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:16:26 +08:00
|
|
|
NS_CC_END
|