CCLabelAtlas.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCLabelAtlas.h"
  25. #include "renderer/CCTextureAtlas.h"
  26. #include "platform/CCFileUtils.h"
  27. #include "base/CCDirector.h"
  28. #include "base/ccUTF8.h"
  29. #include "renderer/CCTextureCache.h"
  30. #if CC_LABELATLAS_DEBUG_DRAW
  31. #include "renderer/CCRenderer.h"
  32. #endif
  33. NS_CC_BEGIN
  34. //CCLabelAtlas - Creation & Init
  35. LabelAtlas* LabelAtlas::create()
  36. {
  37. LabelAtlas* ret = new (std::nothrow) LabelAtlas();
  38. if (ret)
  39. {
  40. ret->autorelease();
  41. }
  42. else
  43. {
  44. CC_SAFE_RELEASE_NULL(ret);
  45. }
  46. return ret;
  47. }
  48. LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
  49. {
  50. LabelAtlas* ret = new (std::nothrow) LabelAtlas();
  51. if(ret && ret->initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap))
  52. {
  53. ret->autorelease();
  54. return ret;
  55. }
  56. CC_SAFE_DELETE(ret);
  57. return nullptr;
  58. }
  59. bool LabelAtlas::initWithString(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
  60. {
  61. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(charMapFile);
  62. return initWithString(string, texture, itemWidth, itemHeight, startCharMap);
  63. }
  64. bool LabelAtlas::initWithString(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
  65. {
  66. if (AtlasNode::initWithTexture(texture, itemWidth, itemHeight, static_cast<int>(string.size())))
  67. {
  68. _mapStartChar = startCharMap;
  69. this->setString(string);
  70. return true;
  71. }
  72. return false;
  73. }
  74. LabelAtlas* LabelAtlas::create(const std::string& string, const std::string& fntFile)
  75. {
  76. LabelAtlas *ret = new (std::nothrow) LabelAtlas();
  77. if (ret)
  78. {
  79. if (ret->initWithString(string, fntFile))
  80. {
  81. ret->autorelease();
  82. }
  83. else
  84. {
  85. CC_SAFE_RELEASE_NULL(ret);
  86. }
  87. }
  88. return ret;
  89. }
  90. bool LabelAtlas::initWithString(const std::string& theString, const std::string& fntFile)
  91. {
  92. std::string pathStr = FileUtils::getInstance()->fullPathForFilename(fntFile);
  93. std::string relPathStr = pathStr.substr(0, pathStr.find_last_of("/"))+"/";
  94. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr);
  95. CCASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version");
  96. std::string textureFilename = relPathStr + dict["textureFilename"].asString();
  97. unsigned int width = dict["itemWidth"].asInt() / CC_CONTENT_SCALE_FACTOR();
  98. unsigned int height = dict["itemHeight"].asInt() / CC_CONTENT_SCALE_FACTOR();
  99. unsigned int startChar = dict["firstChar"].asInt();
  100. this->initWithString(theString, textureFilename, width, height, startChar);
  101. return true;
  102. }
  103. //CCLabelAtlas - Atlas generation
  104. void LabelAtlas::updateAtlasValues()
  105. {
  106. if(_itemsPerRow == 0)
  107. {
  108. return;
  109. }
  110. ssize_t n = _string.length();
  111. const unsigned char *s = (unsigned char*)_string.c_str();
  112. Texture2D *texture = _textureAtlas->getTexture();
  113. float textureWide = (float) texture->getPixelsWide();
  114. float textureHigh = (float) texture->getPixelsHigh();
  115. float itemWidthInPixels = _itemWidth * CC_CONTENT_SCALE_FACTOR();
  116. float itemHeightInPixels = _itemHeight * CC_CONTENT_SCALE_FACTOR();
  117. if (_ignoreContentScaleFactor)
  118. {
  119. itemWidthInPixels = _itemWidth;
  120. itemHeightInPixels = _itemHeight;
  121. }
  122. CCASSERT(n <= _textureAtlas->getCapacity(), "updateAtlasValues: Invalid String length");
  123. V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
  124. for(ssize_t i = 0; i < n; i++) {
  125. unsigned char a = s[i] - _mapStartChar;
  126. float row = (float) (a % _itemsPerRow);
  127. float col = (float) (a / _itemsPerRow);
  128. #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
  129. // Issue #938. Don't use texStepX & texStepY
  130. float left = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
  131. float right = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
  132. float top = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
  133. float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
  134. #else
  135. float left = row * itemWidthInPixels / textureWide;
  136. float right = left + itemWidthInPixels / textureWide;
  137. float top = col * itemHeightInPixels / textureHigh;
  138. float bottom = top + itemHeightInPixels / textureHigh;
  139. #endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
  140. quads[i].tl.texCoords.u = left;
  141. quads[i].tl.texCoords.v = top;
  142. quads[i].tr.texCoords.u = right;
  143. quads[i].tr.texCoords.v = top;
  144. quads[i].bl.texCoords.u = left;
  145. quads[i].bl.texCoords.v = bottom;
  146. quads[i].br.texCoords.u = right;
  147. quads[i].br.texCoords.v = bottom;
  148. quads[i].bl.vertices.x = (float) (i * _itemWidth);
  149. quads[i].bl.vertices.y = 0;
  150. quads[i].bl.vertices.z = 0.0f;
  151. quads[i].br.vertices.x = (float)(i * _itemWidth + _itemWidth);
  152. quads[i].br.vertices.y = 0;
  153. quads[i].br.vertices.z = 0.0f;
  154. quads[i].tl.vertices.x = (float)(i * _itemWidth);
  155. quads[i].tl.vertices.y = (float)(_itemHeight);
  156. quads[i].tl.vertices.z = 0.0f;
  157. quads[i].tr.vertices.x = (float)(i * _itemWidth + _itemWidth);
  158. quads[i].tr.vertices.y = (float)(_itemHeight);
  159. quads[i].tr.vertices.z = 0.0f;
  160. Color4B c(_displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity);
  161. quads[i].tl.colors = c;
  162. quads[i].tr.colors = c;
  163. quads[i].bl.colors = c;
  164. quads[i].br.colors = c;
  165. }
  166. if (n > 0 ){
  167. _textureAtlas->setDirty(true);
  168. ssize_t totalQuads = _textureAtlas->getTotalQuads();
  169. if (n > totalQuads) {
  170. _textureAtlas->increaseTotalQuadsWith(static_cast<int>(n - totalQuads));
  171. }
  172. }
  173. }
  174. //CCLabelAtlas - LabelProtocol
  175. void LabelAtlas::setString(const std::string &label)
  176. {
  177. ssize_t len = label.size();
  178. if (len > _textureAtlas->getTotalQuads())
  179. {
  180. _textureAtlas->resizeCapacity(len);
  181. }
  182. _string.clear();
  183. _string = label;
  184. this->updateAtlasValues();
  185. Size s = Size(len * _itemWidth, _itemHeight);
  186. this->setContentSize(s);
  187. _quadsToDraw = len;
  188. }
  189. const std::string& LabelAtlas::getString(void) const
  190. {
  191. return _string;
  192. }
  193. void LabelAtlas::updateColor()
  194. {
  195. if (_textureAtlas)
  196. {
  197. Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity );
  198. if (_isOpacityModifyRGB)
  199. {
  200. color4.r *= _displayedOpacity/255.0f;
  201. color4.g *= _displayedOpacity/255.0f;
  202. color4.b *= _displayedOpacity/255.0f;
  203. }
  204. auto quads = _textureAtlas->getQuads();
  205. ssize_t length = _string.length();
  206. for (int index = 0; index < length; index++)
  207. {
  208. quads[index].bl.colors = color4;
  209. quads[index].br.colors = color4;
  210. quads[index].tl.colors = color4;
  211. quads[index].tr.colors = color4;
  212. _textureAtlas->updateQuad(&quads[index], index);
  213. }
  214. }
  215. }
  216. //CCLabelAtlas - draw
  217. #if CC_LABELATLAS_DEBUG_DRAW
  218. void LabelAtlas::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  219. {
  220. AtlasNode::draw(renderer, transform, _transformUpdated);
  221. _debugDrawNode->clear();
  222. auto size = getContentSize();
  223. Vec2 vertices[4]=
  224. {
  225. Vec2::ZERO,
  226. Vec2(size.width, 0),
  227. Vec2(size.width, size.height),
  228. Vec2(0, size.height)
  229. };
  230. _debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
  231. }
  232. #endif
  233. std::string LabelAtlas::getDescription() const
  234. {
  235. return StringUtils::format("<LabelAtlas | Tag = %d, Label = '%s'>", _tag, _string.c_str());
  236. }
  237. NS_CC_END