CCLabelTTF.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCLabelTTF.h"
  24. #include "2d/CCLabel.h"
  25. #include "base/ccUTF8.h"
  26. NS_CC_BEGIN
  27. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  28. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  29. #elif _MSC_VER >= 1400 //vs 2005 or higher
  30. #pragma warning (push)
  31. #pragma warning (disable: 4996)
  32. #endif
  33. LabelTTF::LabelTTF()
  34. {
  35. _renderLabel = Label::create();
  36. _renderLabel->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
  37. this->addChild(_renderLabel);
  38. this->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
  39. _contentDirty = false;
  40. _cascadeColorEnabled = true;
  41. _cascadeOpacityEnabled = true;
  42. }
  43. LabelTTF::~LabelTTF()
  44. {
  45. }
  46. LabelTTF * LabelTTF::create()
  47. {
  48. LabelTTF * ret = new (std::nothrow) LabelTTF();
  49. if (ret)
  50. {
  51. ret->autorelease();
  52. }
  53. else
  54. {
  55. CC_SAFE_DELETE(ret);
  56. }
  57. return ret;
  58. }
  59. LabelTTF* LabelTTF::create(const std::string& string, const std::string& fontName, float fontSize,
  60. const Size &dimensions, TextHAlignment hAlignment,
  61. TextVAlignment vAlignment)
  62. {
  63. LabelTTF *ret = new (std::nothrow) LabelTTF();
  64. if(ret && ret->initWithString(string, fontName, fontSize, dimensions, hAlignment, vAlignment))
  65. {
  66. ret->autorelease();
  67. return ret;
  68. }
  69. CC_SAFE_DELETE(ret);
  70. return nullptr;
  71. }
  72. LabelTTF * LabelTTF::createWithFontDefinition(const std::string& string, FontDefinition &textDefinition)
  73. {
  74. LabelTTF *ret = new (std::nothrow) LabelTTF();
  75. if(ret && ret->initWithStringAndTextDefinition(string, textDefinition))
  76. {
  77. ret->autorelease();
  78. return ret;
  79. }
  80. CC_SAFE_DELETE(ret);
  81. return nullptr;
  82. }
  83. bool LabelTTF::initWithString(const std::string& string, const std::string& fontName, float fontSize,
  84. const cocos2d::Size &dimensions, TextHAlignment hAlignment,
  85. TextVAlignment vAlignment)
  86. {
  87. _renderLabel->setString(string);
  88. _renderLabel->setSystemFontSize(fontSize);
  89. _renderLabel->setDimensions(dimensions.width,dimensions.height);
  90. _renderLabel->setAlignment(hAlignment,vAlignment);
  91. _renderLabel->setSystemFontName(fontName);
  92. _contentDirty = true;
  93. return true;
  94. }
  95. bool LabelTTF::initWithStringAndTextDefinition(const std::string& string, FontDefinition &textDefinition)
  96. {
  97. _renderLabel->setFontDefinition(textDefinition);
  98. _renderLabel->setString(string);
  99. _contentDirty = true;
  100. return true;
  101. }
  102. void LabelTTF::setString(const std::string &string)
  103. {
  104. _renderLabel->setString(string);
  105. _contentDirty = true;
  106. }
  107. const std::string& LabelTTF::getString() const
  108. {
  109. return _renderLabel->getString();
  110. }
  111. std::string LabelTTF::getDescription() const
  112. {
  113. return StringUtils::format("<LabelTTF | FontName = %s, FontSize = %f, Label = '%s'>", _renderLabel->getSystemFontName().c_str(), _renderLabel->getSystemFontSize(), _renderLabel->getString().c_str());
  114. }
  115. TextHAlignment LabelTTF::getHorizontalAlignment() const
  116. {
  117. return _renderLabel->getHorizontalAlignment();
  118. }
  119. void LabelTTF::setHorizontalAlignment(TextHAlignment alignment)
  120. {
  121. _renderLabel->setHorizontalAlignment(alignment);
  122. _contentDirty = true;
  123. }
  124. TextVAlignment LabelTTF::getVerticalAlignment() const
  125. {
  126. return _renderLabel->getVerticalAlignment();
  127. }
  128. void LabelTTF::setVerticalAlignment(TextVAlignment verticalAlignment)
  129. {
  130. _renderLabel->setVerticalAlignment(verticalAlignment);
  131. _contentDirty = true;
  132. }
  133. const Size& LabelTTF::getDimensions() const
  134. {
  135. return _renderLabel->getDimensions();
  136. }
  137. void LabelTTF::setDimensions(const Size &dim)
  138. {
  139. _renderLabel->setDimensions(dim.width,dim.height);
  140. _contentDirty = true;
  141. }
  142. float LabelTTF::getFontSize() const
  143. {
  144. return _renderLabel->getSystemFontSize();
  145. }
  146. void LabelTTF::setFontSize(float fontSize)
  147. {
  148. _renderLabel->setSystemFontSize(fontSize);
  149. _contentDirty = true;
  150. }
  151. const std::string& LabelTTF::getFontName() const
  152. {
  153. return _renderLabel->getSystemFontName();
  154. }
  155. void LabelTTF::setFontName(const std::string& fontName)
  156. {
  157. _renderLabel->setSystemFontName(fontName);
  158. _contentDirty = true;
  159. }
  160. void LabelTTF::enableShadow(const Size &shadowOffset, float shadowOpacity, float shadowBlur, bool /*updateTexture*/)
  161. {
  162. Color4B temp(Color3B::BLACK);
  163. temp.a = 255 * shadowOpacity;
  164. _renderLabel->enableShadow(temp,shadowOffset,shadowBlur);
  165. _contentDirty = true;
  166. }
  167. void LabelTTF::disableShadow(bool /*updateTexture*/)
  168. {
  169. _renderLabel->disableEffect();
  170. _contentDirty = true;
  171. }
  172. void LabelTTF::enableStroke(const Color3B &strokeColor, float strokeSize, bool /*updateTexture*/)
  173. {
  174. _renderLabel->enableOutline(Color4B(strokeColor),strokeSize);
  175. _contentDirty = true;
  176. }
  177. void LabelTTF::disableStroke(bool /*updateTexture*/)
  178. {
  179. _renderLabel->disableEffect();
  180. _contentDirty = true;
  181. }
  182. void LabelTTF::setFontFillColor(const Color3B &tintColor, bool /*updateTexture*/)
  183. {
  184. _renderLabel->setTextColor(Color4B(tintColor));
  185. }
  186. void LabelTTF::setTextDefinition(const FontDefinition& theDefinition)
  187. {
  188. _renderLabel->setFontDefinition(theDefinition);
  189. _contentDirty = true;
  190. }
  191. const FontDefinition& LabelTTF::getTextDefinition()
  192. {
  193. auto fontDef = _renderLabel->getFontDefinition();
  194. _fontDef = fontDef;
  195. return _fontDef;
  196. }
  197. void LabelTTF::setBlendFunc(const BlendFunc &blendFunc)
  198. {
  199. _renderLabel->setBlendFunc(blendFunc);
  200. }
  201. const BlendFunc &LabelTTF::getBlendFunc() const
  202. {
  203. return _renderLabel->getBlendFunc();
  204. }
  205. void LabelTTF::setFlippedX(bool flippedX)
  206. {
  207. if (flippedX)
  208. {
  209. _renderLabel->setScaleX(-1.0f);
  210. }
  211. else
  212. {
  213. _renderLabel->setScaleX(1.0f);
  214. }
  215. }
  216. void LabelTTF::setFlippedY(bool flippedY)
  217. {
  218. if (flippedY)
  219. {
  220. _renderLabel->setScaleY(-1.0f);
  221. }
  222. else
  223. {
  224. _renderLabel->setScaleY(1.0f);
  225. }
  226. }
  227. void LabelTTF::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  228. {
  229. if (_contentDirty)
  230. {
  231. this->setContentSize(_renderLabel->getContentSize());
  232. _contentDirty = false;
  233. }
  234. Node::visit(renderer,parentTransform, parentFlags);
  235. }
  236. const Size& LabelTTF::getContentSize() const
  237. {
  238. const_cast<LabelTTF*>(this)->setContentSize(_renderLabel->getContentSize());
  239. return _contentSize;
  240. }
  241. Rect LabelTTF::getBoundingBox() const
  242. {
  243. const_cast<LabelTTF*>(this)->setContentSize(_renderLabel->getContentSize());
  244. return Node::getBoundingBox();
  245. }
  246. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  247. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  248. #elif _MSC_VER >= 1400 //vs 2005 or higher
  249. #pragma warning (pop)
  250. #endif
  251. NS_CC_END