CCLabelBMFont.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. Use any of these editors to generate BMFonts:
  24. http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
  25. http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
  26. http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
  27. http://www.angelcode.com/products/bmfont/ (Free, Windows only)
  28. ****************************************************************************/
  29. #include "2d/CCLabelBMFont.h"
  30. #include "base/ccUTF8.h"
  31. #include "2d/CCSprite.h"
  32. #if CC_LABELBMFONT_DEBUG_DRAW
  33. #include "renderer/CCRenderer.h"
  34. #include "base/CCDirector.h"
  35. #endif
  36. using namespace std;
  37. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  38. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  39. #elif _MSC_VER >= 1400 //vs 2005 or higher
  40. #pragma warning (push)
  41. #pragma warning (disable: 4996)
  42. #endif
  43. NS_CC_BEGIN
  44. LabelBMFont * LabelBMFont::create()
  45. {
  46. LabelBMFont * pRet = new (std::nothrow) LabelBMFont();
  47. if (pRet)
  48. {
  49. pRet->autorelease();
  50. return pRet;
  51. }
  52. CC_SAFE_DELETE(pRet);
  53. return nullptr;
  54. }
  55. //LabelBMFont - Creation & Init
  56. LabelBMFont *LabelBMFont::create(const std::string& str, const std::string& fntFile, float width /* = 0 */, TextHAlignment alignment /* = TextHAlignment::LEFT */,const Vec2& imageOffset /* = Vec2::ZERO */)
  57. {
  58. LabelBMFont *ret = new (std::nothrow) LabelBMFont();
  59. if(ret && ret->initWithString(str, fntFile, width, alignment,imageOffset))
  60. {
  61. ret->autorelease();
  62. return ret;
  63. }
  64. CC_SAFE_DELETE(ret);
  65. return nullptr;
  66. }
  67. bool LabelBMFont::initWithString(const std::string& str, const std::string& fntFile, float width /* = 0 */, TextHAlignment alignment /* = TextHAlignment::LEFT */,const Vec2& imageOffset /* = Vec2::ZERO */)
  68. {
  69. if (_label->setBMFontFilePath(fntFile,imageOffset))
  70. {
  71. _fntFile = fntFile;
  72. _label->setMaxLineWidth(width);
  73. _label->setAlignment(alignment);
  74. _label->setString(str);
  75. this->setContentSize(_label->getContentSize());
  76. return true;
  77. }
  78. return false;
  79. }
  80. LabelBMFont::LabelBMFont()
  81. {
  82. _label = Label::create();
  83. _label->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
  84. this->addChild(_label);
  85. this->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
  86. _cascadeOpacityEnabled = true;
  87. #if CC_LABELBMFONT_DEBUG_DRAW
  88. _debugDrawNode = DrawNode::create();
  89. addChild(_debugDrawNode);
  90. #endif
  91. }
  92. LabelBMFont::~LabelBMFont()
  93. {
  94. }
  95. //LabelBMFont - LabelProtocol protocol
  96. void LabelBMFont::setString(const std::string &newString)
  97. {
  98. _label->setString(newString);
  99. this->setContentSize(_label->getContentSize());
  100. }
  101. const std::string& LabelBMFont::getString() const
  102. {
  103. return _label->getString();
  104. }
  105. /** Override synthesized setOpacity to recurse items */
  106. void LabelBMFont::setOpacityModifyRGB(bool var)
  107. {
  108. _label->setOpacityModifyRGB(var);
  109. for(const auto &child : _children) {
  110. child->setOpacityModifyRGB(var);
  111. }
  112. }
  113. bool LabelBMFont::isOpacityModifyRGB() const
  114. {
  115. return _label->isOpacityModifyRGB();
  116. }
  117. // LabelBMFont - Alignment
  118. void LabelBMFont::setAlignment(TextHAlignment alignment)
  119. {
  120. _label->setAlignment(alignment);
  121. this->setContentSize(_label->getContentSize());
  122. }
  123. void LabelBMFont::setWidth(float width)
  124. {
  125. _label->setMaxLineWidth(width);
  126. this->setContentSize(_label->getContentSize());
  127. }
  128. void LabelBMFont::setLineBreakWithoutSpace( bool breakWithoutSpace )
  129. {
  130. _label->setLineBreakWithoutSpace(breakWithoutSpace);
  131. this->setContentSize(_label->getContentSize());
  132. }
  133. // LabelBMFont - FntFile
  134. void LabelBMFont::setFntFile(const std::string& fntFile, const Vec2& imageOffset /* = Vec2::ZERO */)
  135. {
  136. if (_fntFile.compare(fntFile) != 0)
  137. {
  138. _fntFile = fntFile;
  139. _label->setBMFontFilePath(fntFile,imageOffset);
  140. }
  141. }
  142. const std::string& LabelBMFont::getFntFile() const
  143. {
  144. return _fntFile;
  145. }
  146. std::string LabelBMFont::getDescription() const
  147. {
  148. return StringUtils::format("<LabelBMFont | Tag = %d, Label = '%s'>", _tag, _label->getString().c_str());
  149. }
  150. void LabelBMFont::setBlendFunc(const BlendFunc &blendFunc)
  151. {
  152. _label->setBlendFunc(blendFunc);
  153. }
  154. const BlendFunc &LabelBMFont::getBlendFunc() const
  155. {
  156. return _label->getBlendFunc();
  157. }
  158. Node* LabelBMFont::getChildByTag(int tag) const
  159. {
  160. return _label->getLetter(tag);
  161. }
  162. Sprite* LabelBMFont::getLetter(int ID)
  163. {
  164. return _label->getLetter(ID);
  165. }
  166. void LabelBMFont::setColor(const Color3B& color)
  167. {
  168. _label->setColor(color);
  169. }
  170. const Size& LabelBMFont::getContentSize() const
  171. {
  172. const_cast<LabelBMFont*>(this)->setContentSize(_label->getContentSize());
  173. return _contentSize;
  174. }
  175. Rect LabelBMFont::getBoundingBox() const
  176. {
  177. return Node::getBoundingBox();
  178. }
  179. #if CC_LABELBMFONT_DEBUG_DRAW
  180. void LabelBMFont::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  181. {
  182. Node::draw(renderer, transform, _transformUpdated);
  183. _debugDrawNode->clear();
  184. auto size = getContentSize();
  185. Vec2 vertices[4]=
  186. {
  187. Vec2::ZERO,
  188. Vec2(size.width, 0),
  189. Vec2(size.width, size.height),
  190. Vec2(0, size.height)
  191. };
  192. _debugDrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
  193. }
  194. #endif
  195. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  196. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  197. #elif _MSC_VER >= 1400 //vs 2005 or higher
  198. #pragma warning (pop)
  199. #endif
  200. NS_CC_END