UIText.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "ui/UIText.h"
  22. #include "2d/CCLabel.h"
  23. #include "platform/CCFileUtils.h"
  24. NS_CC_BEGIN
  25. namespace ui {
  26. static const int LABEL_RENDERER_Z = (-1);
  27. IMPLEMENT_CLASS_GUI_INFO(Text)
  28. Text::Text():
  29. _touchScaleChangeEnabled(false),
  30. _normalScaleValueX(1.0f),
  31. _normalScaleValueY(1.0f),
  32. _fontName("Thonburi"),
  33. _fontSize(10.f),
  34. _onSelectedScaleOffset(0.5),
  35. _labelRenderer(nullptr),
  36. _labelRendererAdaptDirty(true),
  37. _type(Type::SYSTEM)
  38. {
  39. }
  40. Text::~Text()
  41. {
  42. }
  43. Text* Text::create()
  44. {
  45. Text* widget = new (std::nothrow) Text();
  46. if (widget && widget->init())
  47. {
  48. widget->autorelease();
  49. return widget;
  50. }
  51. CC_SAFE_DELETE(widget);
  52. return nullptr;
  53. }
  54. bool Text::init()
  55. {
  56. if (Widget::init())
  57. {
  58. return true;
  59. }
  60. return false;
  61. }
  62. Text* Text::create(const std::string &textContent, const std::string &fontName, float fontSize)
  63. {
  64. Text *text = new (std::nothrow) Text;
  65. if (text && text->init(textContent, fontName, fontSize))
  66. {
  67. text->autorelease();
  68. return text;
  69. }
  70. CC_SAFE_DELETE(text);
  71. return nullptr;
  72. }
  73. bool Text::init(const std::string &textContent, const std::string &fontName, float fontSize)
  74. {
  75. bool ret = true;
  76. do
  77. {
  78. if (!Widget::init())
  79. {
  80. ret = false;
  81. break;
  82. }
  83. this->setFontName(fontName);
  84. this->setFontSize(fontSize);
  85. this->setString(textContent);
  86. } while (0);
  87. return ret;
  88. }
  89. void Text::initRenderer()
  90. {
  91. _labelRenderer = Label::create();
  92. addProtectedChild(_labelRenderer, LABEL_RENDERER_Z, -1);
  93. }
  94. void Text::setString(const std::string &text)
  95. {
  96. if (text == _labelRenderer->getString())
  97. {
  98. return;
  99. }
  100. _labelRenderer->setString(text);
  101. updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
  102. _labelRendererAdaptDirty = true;
  103. }
  104. const std::string& Text::getString() const
  105. {
  106. return _labelRenderer->getString();
  107. }
  108. ssize_t Text::getStringLength()const
  109. {
  110. return _labelRenderer->getStringLength();
  111. }
  112. void Text::setFontSize(float size)
  113. {
  114. if (_type == Type::SYSTEM)
  115. {
  116. _labelRenderer->setSystemFontSize(size);
  117. }
  118. else
  119. {
  120. TTFConfig config = _labelRenderer->getTTFConfig();
  121. config.fontSize = size;
  122. _labelRenderer->setTTFConfig(config);
  123. }
  124. _fontSize = size;
  125. updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
  126. _labelRendererAdaptDirty = true;
  127. }
  128. float Text::getFontSize()const
  129. {
  130. return _fontSize;
  131. }
  132. void Text::setFontName(const std::string& name)
  133. {
  134. if(FileUtils::getInstance()->isFileExist(name))
  135. {
  136. TTFConfig config = _labelRenderer->getTTFConfig();
  137. config.fontFilePath = name;
  138. config.fontSize = _fontSize;
  139. _labelRenderer->setTTFConfig(config);
  140. _type = Type::TTF;
  141. }
  142. else
  143. {
  144. _labelRenderer->setSystemFontName(name);
  145. if (_type == Type::TTF)
  146. {
  147. _labelRenderer->requestSystemFontRefresh();
  148. }
  149. _type = Type::SYSTEM;
  150. }
  151. _fontName = name;
  152. updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
  153. _labelRendererAdaptDirty = true;
  154. }
  155. const std::string& Text::getFontName()const
  156. {
  157. return _fontName;
  158. }
  159. Text::Type Text::getType() const
  160. {
  161. return _type;
  162. }
  163. void Text::setTextAreaSize(const Size &size)
  164. {
  165. _labelRenderer->setDimensions(size.width,size.height);
  166. if (!_ignoreSize)
  167. {
  168. _customSize=size;
  169. }
  170. updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
  171. _labelRendererAdaptDirty = true;
  172. }
  173. const Size& Text::getTextAreaSize()const
  174. {
  175. return _labelRenderer->getDimensions();
  176. }
  177. void Text::setTextHorizontalAlignment(TextHAlignment alignment)
  178. {
  179. _labelRenderer->setHorizontalAlignment(alignment);
  180. }
  181. TextHAlignment Text::getTextHorizontalAlignment()const
  182. {
  183. return _labelRenderer->getHorizontalAlignment();
  184. }
  185. void Text::setTextVerticalAlignment(TextVAlignment alignment)
  186. {
  187. _labelRenderer->setVerticalAlignment(alignment);
  188. }
  189. TextVAlignment Text::getTextVerticalAlignment()const
  190. {
  191. return _labelRenderer->getVerticalAlignment();
  192. }
  193. void Text::setTextColor(const Color4B color)
  194. {
  195. _labelRenderer->setTextColor(color);
  196. }
  197. const Color4B& Text::getTextColor() const
  198. {
  199. return _labelRenderer->getTextColor();
  200. }
  201. void Text::setTouchScaleChangeEnabled(bool enable)
  202. {
  203. _touchScaleChangeEnabled = enable;
  204. }
  205. bool Text::isTouchScaleChangeEnabled()const
  206. {
  207. return _touchScaleChangeEnabled;
  208. }
  209. void Text::onPressStateChangedToNormal()
  210. {
  211. if (!_touchScaleChangeEnabled)
  212. {
  213. return;
  214. }
  215. _labelRenderer->setScaleX(_normalScaleValueX);
  216. _labelRenderer->setScaleY(_normalScaleValueY);
  217. }
  218. void Text::onPressStateChangedToPressed()
  219. {
  220. if (!_touchScaleChangeEnabled)
  221. {
  222. return;
  223. }
  224. _labelRenderer->setScaleX(_normalScaleValueX + _onSelectedScaleOffset);
  225. _labelRenderer->setScaleY(_normalScaleValueY + _onSelectedScaleOffset);
  226. }
  227. void Text::onPressStateChangedToDisabled()
  228. {
  229. }
  230. void Text::onSizeChanged()
  231. {
  232. Widget::onSizeChanged();
  233. _labelRendererAdaptDirty = true;
  234. }
  235. void Text::adaptRenderers()
  236. {
  237. if (_labelRendererAdaptDirty)
  238. {
  239. labelScaleChangedWithSize();
  240. _labelRendererAdaptDirty = false;
  241. }
  242. }
  243. Size Text::getVirtualRendererSize() const
  244. {
  245. return _labelRenderer->getContentSize();
  246. }
  247. Size Text::getAutoRenderSize()
  248. {
  249. Size virtualSize = _labelRenderer->getContentSize();
  250. if (!_ignoreSize)
  251. {
  252. _labelRenderer->setDimensions(0, 0);
  253. virtualSize = _labelRenderer->getContentSize();
  254. _labelRenderer->setDimensions(_contentSize.width, _contentSize.height);
  255. }
  256. return virtualSize;
  257. }
  258. Node* Text::getVirtualRenderer()
  259. {
  260. return _labelRenderer;
  261. }
  262. void Text::labelScaleChangedWithSize()
  263. {
  264. if (_ignoreSize)
  265. {
  266. _labelRenderer->setScale(1.0f);
  267. _normalScaleValueX = _normalScaleValueY = 1.0f;
  268. }
  269. else
  270. {
  271. _labelRenderer->setDimensions(_contentSize.width,_contentSize.height);
  272. Size textureSize = _labelRenderer->getContentSize();
  273. if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
  274. {
  275. _labelRenderer->setScale(1.0f);
  276. return;
  277. }
  278. float scaleX = _contentSize.width / textureSize.width;
  279. float scaleY = _contentSize.height / textureSize.height;
  280. _labelRenderer->setScaleX(scaleX);
  281. _labelRenderer->setScaleY(scaleY);
  282. _normalScaleValueX = scaleX;
  283. _normalScaleValueY = scaleY;
  284. }
  285. _labelRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  286. }
  287. std::string Text::getDescription() const
  288. {
  289. return "Label";
  290. }
  291. void Text::enableShadow(const Color4B& shadowColor,const Size &offset, int blurRadius)
  292. {
  293. _labelRenderer->enableShadow(shadowColor, offset, blurRadius);
  294. }
  295. void Text::enableOutline(const Color4B& outlineColor,int outlineSize)
  296. {
  297. _labelRenderer->enableOutline(outlineColor, outlineSize);
  298. updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
  299. _labelRendererAdaptDirty = true;
  300. }
  301. void Text::enableGlow(const Color4B& glowColor)
  302. {
  303. if (_type == Type::TTF)
  304. _labelRenderer->enableGlow(glowColor);
  305. }
  306. void Text::disableEffect()
  307. {
  308. _labelRenderer->disableEffect();
  309. updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
  310. _labelRendererAdaptDirty = true;
  311. }
  312. void Text::disableEffect(LabelEffect effect)
  313. {
  314. _labelRenderer->disableEffect(effect);
  315. //only outline effect will affect the content size of label
  316. if(LabelEffect::OUTLINE == effect)
  317. {
  318. updateContentSizeWithTextureSize(_labelRenderer->getContentSize());
  319. _labelRendererAdaptDirty = true;
  320. }
  321. }
  322. bool Text::isShadowEnabled() const
  323. {
  324. return _labelRenderer->isShadowEnabled();
  325. }
  326. Size Text::getShadowOffset() const
  327. {
  328. return _labelRenderer->getShadowOffset();
  329. }
  330. float Text::getShadowBlurRadius() const
  331. {
  332. return _labelRenderer->getShadowBlurRadius();
  333. }
  334. Color4B Text::getShadowColor() const
  335. {
  336. Color4F effect = _labelRenderer->getShadowColor();
  337. return Color4B(effect.r * 255, effect.g * 255, effect.b * 255, effect.a * 255);
  338. }
  339. int Text::getOutlineSize() const
  340. {
  341. return _labelRenderer->getOutlineSize();
  342. }
  343. LabelEffect Text::getLabelEffectType() const
  344. {
  345. return _labelRenderer->getLabelEffectType();
  346. }
  347. Color4B Text::getEffectColor() const
  348. {
  349. Color4F effect = _labelRenderer->getEffectColor();
  350. return Color4B(effect.r * 255, effect.g * 255, effect.b * 255, effect.a * 255);
  351. }
  352. Sprite * Text::getLetter(int lettetIndex)
  353. {
  354. return _labelRenderer->getLetter(lettetIndex);
  355. }
  356. void Text::setBlendFunc(const BlendFunc &blendFunc)
  357. {
  358. _labelRenderer->setBlendFunc(blendFunc);
  359. }
  360. const BlendFunc& Text::getBlendFunc() const
  361. {
  362. return _labelRenderer->getBlendFunc();
  363. }
  364. Widget* Text::createCloneInstance()
  365. {
  366. return Text::create();
  367. }
  368. void Text::copySpecialProperties(Widget *widget)
  369. {
  370. Text* label = dynamic_cast<Text*>(widget);
  371. if (label)
  372. {
  373. setFontName(label->_fontName);
  374. setFontSize(label->getFontSize());
  375. setTextColor(label->getTextColor());
  376. setString(label->getString());
  377. setTouchScaleChangeEnabled(label->_touchScaleChangeEnabled);
  378. setTextHorizontalAlignment(label->_labelRenderer->getHorizontalAlignment());
  379. setTextVerticalAlignment(label->_labelRenderer->getVerticalAlignment());
  380. setTextAreaSize(label->_labelRenderer->getDimensions());
  381. setContentSize(label->getContentSize());
  382. LabelEffect effectType = label->getLabelEffectType();
  383. if (effectType == LabelEffect::GLOW)
  384. {
  385. enableGlow(label->getEffectColor());
  386. }
  387. else if (effectType == LabelEffect::OUTLINE)
  388. {
  389. enableOutline(label->getEffectColor(),label->getOutlineSize());
  390. }
  391. if (label->isShadowEnabled())
  392. {
  393. enableShadow(label->getShadowColor(),label->getShadowOffset(),label->getShadowBlurRadius());
  394. }
  395. }
  396. }
  397. }
  398. NS_CC_END