UITextAtlas.cpp 5.7 KB

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