1
0

UITextAtlas.cpp 5.7 KB

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