2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +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-08-27 13:39:50 +08:00
|
|
|
#include "2d/CCLabelAtlas.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureAtlas.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/CCDirector.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
|
2014-04-22 23:18:23 +08:00
|
|
|
#if CC_LABELATLAS_DEBUG_DRAW
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-04-22 23:18:23 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_BEGIN
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
//CCLabelAtlas - Creation & Init
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2014-01-09 12:40:51 +08:00
|
|
|
LabelAtlas* LabelAtlas::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LabelAtlas* ret = new (std::nothrow) LabelAtlas();
|
2014-01-09 12:40:51 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LabelAtlas* ret = new (std::nothrow) LabelAtlas();
|
2014-01-09 14:26:34 +08:00
|
|
|
if(ret && ret->initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-01-09 14:26:34 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-01-09 14:26:34 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
bool LabelAtlas::initWithString(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(charMapFile);
|
2015-01-08 10:22:45 +08:00
|
|
|
return initWithString(string, texture, itemWidth, itemHeight, startCharMap);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
bool LabelAtlas::initWithString(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
if (AtlasNode::initWithTexture(texture, itemWidth, itemHeight, static_cast<int>(string.size())))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_mapStartChar = startCharMap;
|
2012-07-23 22:49:11 +08:00
|
|
|
this->setString(string);
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& fntFile)
|
2012-06-08 15:29:47 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
LabelAtlas *ret = new (std::nothrow) LabelAtlas();
|
2012-06-08 15:29:47 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (ret->initWithString(string, fntFile))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
bool LabelAtlas::initWithString(const std::string& theString, const std::string& fntFile)
|
2012-06-08 15:29:47 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
std::string pathStr = FileUtils::getInstance()->fullPathForFilename(fntFile);
|
2019-04-22 09:44:19 +08:00
|
|
|
std::string relPathStr = pathStr.substr(0, pathStr.find_last_of('/'))+"/";
|
2013-12-11 17:15:27 +08:00
|
|
|
|
2016-02-03 23:12:37 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr);
|
2013-12-11 17:15:27 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
CCASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version");
|
2013-11-07 09:05:13 +08:00
|
|
|
|
2013-12-11 17:15:27 +08:00
|
|
|
std::string textureFilename = relPathStr + dict["textureFilename"].asString();
|
2013-11-07 09:05:13 +08:00
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
unsigned int width = dict["itemWidth"].asInt() / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
unsigned int height = dict["itemHeight"].asInt() / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
unsigned int startChar = dict["firstChar"].asInt();
|
2013-11-07 09:05:13 +08:00
|
|
|
|
|
|
|
|
2016-02-03 23:12:37 +08:00
|
|
|
this->initWithString(theString, textureFilename, width, height, startChar);
|
2013-11-07 09:05:13 +08:00
|
|
|
|
|
|
|
return true;
|
2012-06-08 15:29:47 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
//CCLabelAtlas - Atlas generation
|
2013-06-20 14:13:12 +08:00
|
|
|
void LabelAtlas::updateAtlasValues()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-07-08 02:17:05 +08:00
|
|
|
if(_itemsPerRow == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-27 15:06:16 +08:00
|
|
|
ssize_t n = _string.length();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
const unsigned char *s = (unsigned char*)_string.c_str();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D *texture = _textureAtlas->getTexture();
|
2012-04-19 14:35:52 +08:00
|
|
|
float textureWide = (float) texture->getPixelsWide();
|
2012-04-18 18:43:45 +08:00
|
|
|
float textureHigh = (float) texture->getPixelsHigh();
|
2013-06-15 14:03:30 +08:00
|
|
|
float itemWidthInPixels = _itemWidth * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
float itemHeightInPixels = _itemHeight * CC_CONTENT_SCALE_FACTOR();
|
|
|
|
if (_ignoreContentScaleFactor)
|
2013-05-23 15:06:57 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
itemWidthInPixels = _itemWidth;
|
|
|
|
itemHeightInPixels = _itemHeight;
|
2013-05-23 15:06:57 +08:00
|
|
|
}
|
2013-05-28 10:23:47 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
CCASSERT(n <= _textureAtlas->getCapacity(), "updateAtlasValues: Invalid String length");
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
|
2013-12-27 15:06:16 +08:00
|
|
|
for(ssize_t i = 0; i < n; i++) {
|
2013-05-29 19:18:46 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned char a = s[i] - _mapStartChar;
|
|
|
|
float row = (float) (a % _itemsPerRow);
|
|
|
|
float col = (float) (a / _itemsPerRow);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
|
|
|
// Issue #938. Don't use texStepX & texStepY
|
|
|
|
float left = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
|
|
|
|
float right = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
|
|
|
|
float top = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
|
|
|
|
float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
|
|
|
|
#else
|
|
|
|
float left = row * itemWidthInPixels / textureWide;
|
|
|
|
float right = left + itemWidthInPixels / textureWide;
|
|
|
|
float top = col * itemHeightInPixels / textureHigh;
|
|
|
|
float bottom = top + itemHeightInPixels / textureHigh;
|
2010-12-24 17:07:31 +08:00
|
|
|
#endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
|
|
|
|
|
2013-05-28 10:23:47 +08:00
|
|
|
quads[i].tl.texCoords.u = left;
|
|
|
|
quads[i].tl.texCoords.v = top;
|
|
|
|
quads[i].tr.texCoords.u = right;
|
|
|
|
quads[i].tr.texCoords.v = top;
|
|
|
|
quads[i].bl.texCoords.u = left;
|
|
|
|
quads[i].bl.texCoords.v = bottom;
|
|
|
|
quads[i].br.texCoords.u = right;
|
|
|
|
quads[i].br.texCoords.v = bottom;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
quads[i].bl.vertices.x = (float) (i * _itemWidth);
|
2013-05-28 10:23:47 +08:00
|
|
|
quads[i].bl.vertices.y = 0;
|
|
|
|
quads[i].bl.vertices.z = 0.0f;
|
2013-06-15 14:03:30 +08:00
|
|
|
quads[i].br.vertices.x = (float)(i * _itemWidth + _itemWidth);
|
2013-05-28 10:23:47 +08:00
|
|
|
quads[i].br.vertices.y = 0;
|
|
|
|
quads[i].br.vertices.z = 0.0f;
|
2013-06-15 14:03:30 +08:00
|
|
|
quads[i].tl.vertices.x = (float)(i * _itemWidth);
|
|
|
|
quads[i].tl.vertices.y = (float)(_itemHeight);
|
2013-05-28 10:23:47 +08:00
|
|
|
quads[i].tl.vertices.z = 0.0f;
|
2013-06-15 14:03:30 +08:00
|
|
|
quads[i].tr.vertices.x = (float)(i * _itemWidth + _itemWidth);
|
|
|
|
quads[i].tr.vertices.y = (float)(_itemHeight);
|
2013-05-28 10:23:47 +08:00
|
|
|
quads[i].tr.vertices.z = 0.0f;
|
2013-07-07 21:08:14 +08:00
|
|
|
Color4B c(_displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity);
|
2013-05-28 10:23:47 +08:00
|
|
|
quads[i].tl.colors = c;
|
|
|
|
quads[i].tr.colors = c;
|
|
|
|
quads[i].bl.colors = c;
|
|
|
|
quads[i].br.colors = c;
|
|
|
|
}
|
2013-05-29 10:45:47 +08:00
|
|
|
if (n > 0 ){
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->setDirty(true);
|
2013-12-27 15:06:16 +08:00
|
|
|
ssize_t totalQuads = _textureAtlas->getTotalQuads();
|
2013-12-05 17:19:01 +08:00
|
|
|
if (n > totalQuads) {
|
2013-12-06 16:32:06 +08:00
|
|
|
_textureAtlas->increaseTotalQuadsWith(static_cast<int>(n - totalQuads));
|
2013-05-29 10:45:47 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
//CCLabelAtlas - LabelProtocol
|
2013-11-06 11:02:03 +08:00
|
|
|
void LabelAtlas::setString(const std::string &label)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-12-27 15:06:16 +08:00
|
|
|
ssize_t len = label.size();
|
2013-12-05 17:19:01 +08:00
|
|
|
if (len > _textureAtlas->getTotalQuads())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-27 15:06:16 +08:00
|
|
|
_textureAtlas->resizeCapacity(len);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_string.clear();
|
|
|
|
_string = label;
|
2012-04-19 14:35:52 +08:00
|
|
|
this->updateAtlasValues();
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
Size s = Size(len * _itemWidth, _itemHeight);
|
2011-07-12 11:20:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(s);
|
2010-08-11 18:09:10 +08:00
|
|
|
|
2013-12-27 15:06:16 +08:00
|
|
|
_quadsToDraw = len;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-28 15:05:55 +08:00
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
const std::string& LabelAtlas::getString() const
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-11-08 04:42:16 +08:00
|
|
|
return _string;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 17:36:05 +08:00
|
|
|
void LabelAtlas::updateColor()
|
|
|
|
{
|
|
|
|
if (_textureAtlas)
|
|
|
|
{
|
|
|
|
Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity );
|
2014-10-08 18:28:24 +08:00
|
|
|
if (_isOpacityModifyRGB)
|
|
|
|
{
|
|
|
|
color4.r *= _displayedOpacity/255.0f;
|
|
|
|
color4.g *= _displayedOpacity/255.0f;
|
|
|
|
color4.b *= _displayedOpacity/255.0f;
|
|
|
|
}
|
2014-03-14 17:36:05 +08:00
|
|
|
auto quads = _textureAtlas->getQuads();
|
|
|
|
ssize_t length = _string.length();
|
|
|
|
for (int index = 0; index < length; 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
//CCLabelAtlas - draw
|
2014-04-23 15:05:02 +08:00
|
|
|
#if CC_LABELATLAS_DEBUG_DRAW
|
2014-05-31 07:42:05 +08:00
|
|
|
void LabelAtlas::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-11-24 17:21:14 +08:00
|
|
|
AtlasNode::draw(renderer, transform, _transformUpdated);
|
2014-04-23 15:05:02 +08:00
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
_debugDrawNode->clear();
|
2014-04-22 23:18:23 +08:00
|
|
|
auto size = getContentSize();
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 vertices[4]=
|
2014-04-22 23:18:23 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2::ZERO,
|
|
|
|
Vec2(size.width, 0),
|
|
|
|
Vec2(size.width, size.height),
|
2019-09-24 11:31:35 +08:00
|
|
|
Vec2(0.0f, size.height)
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
2014-09-11 15:39:56 +08:00
|
|
|
_debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2011-07-12 11:20:41 +08:00
|
|
|
#endif
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string LabelAtlas::getDescription() const
|
|
|
|
{
|
|
|
|
return StringUtils::format("<LabelAtlas | Tag = %d, Label = '%s'>", _tag, _string.c_str());
|
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|