UITextBMFont.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/UITextBMFont.h"
  22. #include "2d/CCLabel.h"
  23. #include "editor-support/cocostudio/CocosStudioExtension.h"
  24. NS_CC_BEGIN
  25. namespace ui {
  26. static const int LABELBMFONT_RENDERER_Z = (-1);
  27. IMPLEMENT_CLASS_GUI_INFO(TextBMFont)
  28. TextBMFont::TextBMFont():
  29. _labelBMFontRenderer(nullptr),
  30. _fntFileName(""),
  31. _stringValue(""),
  32. _labelBMFontRendererAdaptDirty(true)
  33. {
  34. }
  35. TextBMFont::~TextBMFont()
  36. {
  37. }
  38. TextBMFont* TextBMFont::create()
  39. {
  40. TextBMFont* widget = new (std::nothrow) TextBMFont();
  41. if (widget && widget->init())
  42. {
  43. widget->autorelease();
  44. return widget;
  45. }
  46. CC_SAFE_DELETE(widget);
  47. return nullptr;
  48. }
  49. TextBMFont* TextBMFont::create(const std::string &text, const std::string &filename)
  50. {
  51. TextBMFont* widget = new (std::nothrow) TextBMFont();
  52. if (widget && widget->init())
  53. {
  54. widget->setFntFile(filename);
  55. widget->setString(text);
  56. widget->autorelease();
  57. return widget;
  58. }
  59. CC_SAFE_DELETE(widget);
  60. return nullptr;
  61. }
  62. void TextBMFont::initRenderer()
  63. {
  64. _labelBMFontRenderer = cocos2d::Label::create();
  65. addProtectedChild(_labelBMFontRenderer, LABELBMFONT_RENDERER_Z, -1);
  66. }
  67. void TextBMFont::setFntFile(const std::string& fileName)
  68. {
  69. if (fileName.empty())
  70. {
  71. return;
  72. }
  73. _fntFileName = fileName;
  74. _labelBMFontRenderer->setBMFontFilePath(fileName);
  75. updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize());
  76. _labelBMFontRendererAdaptDirty = true;
  77. }
  78. void TextBMFont::setString(const std::string& value)
  79. {
  80. if (value == _labelBMFontRenderer->getString())
  81. {
  82. return;
  83. }
  84. _stringValue = value;
  85. _labelBMFontRenderer->setString(value);
  86. updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize());
  87. _labelBMFontRendererAdaptDirty = true;
  88. }
  89. const std::string& TextBMFont::getString()const
  90. {
  91. return _stringValue;
  92. }
  93. ssize_t TextBMFont::getStringLength()const
  94. {
  95. return _labelBMFontRenderer->getStringLength();
  96. }
  97. void TextBMFont::onSizeChanged()
  98. {
  99. Widget::onSizeChanged();
  100. _labelBMFontRendererAdaptDirty = true;
  101. }
  102. void TextBMFont::adaptRenderers()
  103. {
  104. if (_labelBMFontRendererAdaptDirty)
  105. {
  106. labelBMFontScaleChangedWithSize();
  107. _labelBMFontRendererAdaptDirty = false;
  108. }
  109. }
  110. Size TextBMFont::getVirtualRendererSize() const
  111. {
  112. return _labelBMFontRenderer->getContentSize();
  113. }
  114. Node* TextBMFont::getVirtualRenderer()
  115. {
  116. return _labelBMFontRenderer;
  117. }
  118. void TextBMFont::labelBMFontScaleChangedWithSize()
  119. {
  120. if (_ignoreSize)
  121. {
  122. _labelBMFontRenderer->setScale(1.0f);
  123. }
  124. else
  125. {
  126. Size textureSize = _labelBMFontRenderer->getContentSize();
  127. if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
  128. {
  129. _labelBMFontRenderer->setScale(1.0f);
  130. return;
  131. }
  132. float scaleX = _contentSize.width / textureSize.width;
  133. float scaleY = _contentSize.height / textureSize.height;
  134. _labelBMFontRenderer->setScaleX(scaleX);
  135. _labelBMFontRenderer->setScaleY(scaleY);
  136. }
  137. _labelBMFontRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  138. }
  139. std::string TextBMFont::getDescription() const
  140. {
  141. return "TextBMFont";
  142. }
  143. Widget* TextBMFont::createCloneInstance()
  144. {
  145. return TextBMFont::create();
  146. }
  147. void TextBMFont::copySpecialProperties(Widget *widget)
  148. {
  149. TextBMFont* labelBMFont = dynamic_cast<TextBMFont*>(widget);
  150. if (labelBMFont)
  151. {
  152. setFntFile(labelBMFont->_fntFileName);
  153. setString(labelBMFont->_stringValue);
  154. }
  155. }
  156. ResourceData TextBMFont::getRenderFile()
  157. {
  158. ResourceData rData;
  159. rData.type = 0;
  160. rData.file = _fntFileName;
  161. return rData;
  162. }
  163. void TextBMFont::resetRender()
  164. {
  165. this->removeProtectedChild(_labelBMFontRenderer);
  166. this->initRenderer();
  167. }
  168. }
  169. NS_CC_END