1
0

CCLabelTTF.cpp 7.9 KB

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