1
0

UITextBMFont.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "ui/UITextBMFont.h"
  21. #include "2d/CCLabel.h"
  22. #include "editor-support/cocostudio/CocosStudioExtension.h"
  23. NS_CC_BEGIN
  24. namespace ui {
  25. static const int LABELBMFONT_RENDERER_Z = (-1);
  26. IMPLEMENT_CLASS_GUI_INFO(TextBMFont)
  27. TextBMFont::TextBMFont():
  28. _labelBMFontRenderer(nullptr),
  29. _fntFileName(""),
  30. _stringValue(""),
  31. _labelBMFontRendererAdaptDirty(true)
  32. {
  33. }
  34. TextBMFont::~TextBMFont()
  35. {
  36. }
  37. TextBMFont* TextBMFont::create()
  38. {
  39. TextBMFont* widget = new (std::nothrow) TextBMFont();
  40. if (widget && widget->init())
  41. {
  42. widget->autorelease();
  43. return widget;
  44. }
  45. CC_SAFE_DELETE(widget);
  46. return nullptr;
  47. }
  48. TextBMFont* TextBMFont::create(const std::string &text, const std::string &filename)
  49. {
  50. TextBMFont* widget = new (std::nothrow) TextBMFont();
  51. if (widget && widget->init())
  52. {
  53. widget->setFntFile(filename);
  54. widget->setString(text);
  55. widget->autorelease();
  56. return widget;
  57. }
  58. CC_SAFE_DELETE(widget);
  59. return nullptr;
  60. }
  61. void TextBMFont::initRenderer()
  62. {
  63. _labelBMFontRenderer = cocos2d::Label::create();
  64. addProtectedChild(_labelBMFontRenderer, LABELBMFONT_RENDERER_Z, -1);
  65. }
  66. void TextBMFont::setFntFile(const std::string& fileName)
  67. {
  68. if (fileName.empty())
  69. {
  70. return;
  71. }
  72. _fntFileName = fileName;
  73. _labelBMFontRenderer->setBMFontFilePath(fileName);
  74. updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize());
  75. _labelBMFontRendererAdaptDirty = true;
  76. }
  77. void TextBMFont::setString(const std::string& value)
  78. {
  79. if (value == _labelBMFontRenderer->getString())
  80. {
  81. return;
  82. }
  83. _stringValue = value;
  84. _labelBMFontRenderer->setString(value);
  85. updateContentSizeWithTextureSize(_labelBMFontRenderer->getContentSize());
  86. _labelBMFontRendererAdaptDirty = true;
  87. }
  88. const std::string& TextBMFont::getString()const
  89. {
  90. return _stringValue;
  91. }
  92. ssize_t TextBMFont::getStringLength()const
  93. {
  94. return _labelBMFontRenderer->getStringLength();
  95. }
  96. void TextBMFont::onSizeChanged()
  97. {
  98. Widget::onSizeChanged();
  99. _labelBMFontRendererAdaptDirty = true;
  100. }
  101. void TextBMFont::adaptRenderers()
  102. {
  103. if (_labelBMFontRendererAdaptDirty)
  104. {
  105. labelBMFontScaleChangedWithSize();
  106. _labelBMFontRendererAdaptDirty = false;
  107. }
  108. }
  109. Size TextBMFont::getVirtualRendererSize() const
  110. {
  111. return _labelBMFontRenderer->getContentSize();
  112. }
  113. Node* TextBMFont::getVirtualRenderer()
  114. {
  115. return _labelBMFontRenderer;
  116. }
  117. void TextBMFont::labelBMFontScaleChangedWithSize()
  118. {
  119. if (_ignoreSize)
  120. {
  121. _labelBMFontRenderer->setScale(1.0f);
  122. }
  123. else
  124. {
  125. Size textureSize = _labelBMFontRenderer->getContentSize();
  126. if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
  127. {
  128. _labelBMFontRenderer->setScale(1.0f);
  129. return;
  130. }
  131. float scaleX = _contentSize.width / textureSize.width;
  132. float scaleY = _contentSize.height / textureSize.height;
  133. _labelBMFontRenderer->setScaleX(scaleX);
  134. _labelBMFontRenderer->setScaleY(scaleY);
  135. }
  136. _labelBMFontRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  137. }
  138. std::string TextBMFont::getDescription() const
  139. {
  140. return "TextBMFont";
  141. }
  142. Widget* TextBMFont::createCloneInstance()
  143. {
  144. return TextBMFont::create();
  145. }
  146. void TextBMFont::copySpecialProperties(Widget *widget)
  147. {
  148. TextBMFont* labelBMFont = dynamic_cast<TextBMFont*>(widget);
  149. if (labelBMFont)
  150. {
  151. setFntFile(labelBMFont->_fntFileName);
  152. setString(labelBMFont->_stringValue);
  153. }
  154. }
  155. ResourceData TextBMFont::getRenderFile()
  156. {
  157. ResourceData rData;
  158. rData.type = 0;
  159. rData.file = _fntFileName;
  160. return rData;
  161. }
  162. void TextBMFont::resetRender()
  163. {
  164. this->removeProtectedChild(_labelBMFontRenderer);
  165. this->initRenderer();
  166. }
  167. }
  168. NS_CC_END