UIImageView.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/UIImageView.h"
  22. #include "ui/UIScale9Sprite.h"
  23. #include "ui/UIHelper.h"
  24. #include "2d/CCSprite.h"
  25. #include "editor-support/cocostudio/CocosStudioExtension.h"
  26. NS_CC_BEGIN
  27. namespace ui {
  28. static const int IMAGE_RENDERER_Z = (-1);
  29. IMPLEMENT_CLASS_GUI_INFO(ImageView)
  30. ImageView::ImageView():
  31. _scale9Enabled(false),
  32. _prevIgnoreSize(true),
  33. _capInsets(Rect::ZERO),
  34. _textureFile(""),
  35. _imageRenderer(nullptr),
  36. _imageTexType(TextureResType::LOCAL),
  37. _imageTextureSize(_contentSize),
  38. _imageRendererAdaptDirty(true)
  39. {
  40. }
  41. ImageView::~ImageView()
  42. {
  43. }
  44. ImageView* ImageView::create(const std::string &imageFileName, TextureResType texType)
  45. {
  46. ImageView *widget = new (std::nothrow) ImageView;
  47. if (widget && widget->init(imageFileName, texType))
  48. {
  49. widget->autorelease();
  50. return widget;
  51. }
  52. CC_SAFE_DELETE(widget);
  53. return nullptr;
  54. }
  55. ImageView* ImageView::create()
  56. {
  57. ImageView* widget = new (std::nothrow) ImageView();
  58. if (widget && widget->init())
  59. {
  60. widget->autorelease();
  61. return widget;
  62. }
  63. CC_SAFE_DELETE(widget);
  64. return nullptr;
  65. }
  66. bool ImageView::init()
  67. {
  68. bool ret = true;
  69. do
  70. {
  71. if (!Widget::init())
  72. {
  73. ret = false;
  74. break;
  75. }
  76. _imageTexType = TextureResType::LOCAL;
  77. } while (0);
  78. return ret;
  79. }
  80. bool ImageView::init(const std::string &imageFileName, TextureResType texType)
  81. {
  82. bool bRet = true;
  83. do
  84. {
  85. if (!Widget::init())
  86. {
  87. bRet = false;
  88. break;
  89. }
  90. this->loadTexture(imageFileName, texType);
  91. } while (0);
  92. return bRet;
  93. }
  94. void ImageView::initRenderer()
  95. {
  96. _imageRenderer = Scale9Sprite::create();
  97. _imageRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  98. addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1);
  99. }
  100. void ImageView::loadTexture(const std::string& fileName, TextureResType texType)
  101. {
  102. if (fileName.empty())
  103. {
  104. return;
  105. }
  106. if (texType != _imageTexType)
  107. {
  108. _imageRenderer->resetRender();
  109. }
  110. _textureFile = fileName;
  111. _imageTexType = texType;
  112. switch (_imageTexType)
  113. {
  114. case TextureResType::LOCAL:
  115. _imageRenderer->initWithFile(fileName);
  116. break;
  117. case TextureResType::PLIST:
  118. _imageRenderer->initWithSpriteFrameName(fileName);
  119. break;
  120. default:
  121. break;
  122. }
  123. //FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
  124. if (!_ignoreSize && _customSize.equals(Size::ZERO)) {
  125. _customSize = _imageRenderer->getContentSize();
  126. }
  127. this->setupTexture();
  128. }
  129. void ImageView::loadTexture(SpriteFrame* spriteframe)
  130. {
  131. _imageRenderer->initWithSpriteFrame(spriteframe);
  132. this->setupTexture();
  133. }
  134. void ImageView::setupTexture()
  135. {
  136. _imageTextureSize = _imageRenderer->getContentSize();
  137. this->updateChildrenDisplayedRGBA();
  138. updateContentSizeWithTextureSize(_imageTextureSize);
  139. _imageRendererAdaptDirty = true;
  140. }
  141. void ImageView::setTextureRect(const Rect &rect)
  142. {
  143. //This API should be refactor
  144. if (_scale9Enabled)
  145. {
  146. }
  147. else
  148. {
  149. auto sprite = _imageRenderer->getSprite();
  150. if (sprite)
  151. {
  152. sprite->setTextureRect(rect);
  153. }
  154. else
  155. {
  156. CCLOG("Warning!! you should load texture before set the texture's rect!");
  157. }
  158. }
  159. }
  160. void ImageView::setScale9Enabled(bool able)
  161. {
  162. if (_scale9Enabled == able)
  163. {
  164. return;
  165. }
  166. _scale9Enabled = able;
  167. if (_scale9Enabled) {
  168. _imageRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
  169. }else{
  170. _imageRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
  171. }
  172. if (_scale9Enabled)
  173. {
  174. bool ignoreBefore = _ignoreSize;
  175. ignoreContentAdaptWithSize(false);
  176. _prevIgnoreSize = ignoreBefore;
  177. }
  178. else
  179. {
  180. ignoreContentAdaptWithSize(_prevIgnoreSize);
  181. }
  182. setCapInsets(_capInsets);
  183. _imageRendererAdaptDirty = true;
  184. }
  185. bool ImageView::isScale9Enabled()const
  186. {
  187. return _scale9Enabled;
  188. }
  189. void ImageView::ignoreContentAdaptWithSize(bool ignore)
  190. {
  191. if (!_scale9Enabled || (_scale9Enabled && !ignore))
  192. {
  193. Widget::ignoreContentAdaptWithSize(ignore);
  194. _prevIgnoreSize = ignore;
  195. }
  196. }
  197. void ImageView::setCapInsets(const Rect &capInsets)
  198. {
  199. _capInsets = ui::Helper::restrictCapInsetRect(capInsets, _imageTextureSize);
  200. if (!_scale9Enabled)
  201. {
  202. return;
  203. }
  204. _imageRenderer->setCapInsets(_capInsets);
  205. }
  206. const Rect& ImageView::getCapInsets()const
  207. {
  208. return _capInsets;
  209. }
  210. void ImageView::onSizeChanged()
  211. {
  212. Widget::onSizeChanged();
  213. _imageRendererAdaptDirty = true;
  214. }
  215. void ImageView::adaptRenderers()
  216. {
  217. if (_imageRendererAdaptDirty)
  218. {
  219. imageTextureScaleChangedWithSize();
  220. _imageRendererAdaptDirty = false;
  221. }
  222. }
  223. Size ImageView::getVirtualRendererSize() const
  224. {
  225. return _imageTextureSize;
  226. }
  227. Node* ImageView::getVirtualRenderer()
  228. {
  229. return _imageRenderer;
  230. }
  231. void ImageView::imageTextureScaleChangedWithSize()
  232. {
  233. _imageRenderer->setPreferredSize(_contentSize);
  234. _imageRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  235. }
  236. std::string ImageView::getDescription() const
  237. {
  238. return "ImageView";
  239. }
  240. Widget* ImageView::createCloneInstance()
  241. {
  242. return ImageView::create();
  243. }
  244. void ImageView::copySpecialProperties(Widget *widget)
  245. {
  246. ImageView* imageView = dynamic_cast<ImageView*>(widget);
  247. if (imageView)
  248. {
  249. _prevIgnoreSize = imageView->_prevIgnoreSize;
  250. setScale9Enabled(imageView->_scale9Enabled);
  251. auto imageSprite = imageView->_imageRenderer->getSprite();
  252. if(nullptr != imageSprite)
  253. {
  254. loadTexture(imageSprite->getSpriteFrame());
  255. }
  256. setCapInsets(imageView->_capInsets);
  257. }
  258. }
  259. ResourceData ImageView::getRenderFile()
  260. {
  261. ResourceData rData;
  262. rData.type = (int)_imageTexType;
  263. rData.file = _textureFile;
  264. return rData;
  265. }
  266. void ImageView::setGLProgram(GLProgram* glProgram)
  267. {
  268. Widget::setGLProgram(glProgram);
  269. _imageRenderer->setGLProgram(glProgram);
  270. }
  271. void ImageView::setGLProgramState(cocos2d::GLProgramState* glProgramState)
  272. {
  273. Widget::setGLProgramState(glProgramState);
  274. _imageRenderer->setGLProgramState(glProgramState);
  275. }
  276. void ImageView::setBlendFunc(const BlendFunc &blendFunc)
  277. {
  278. _imageRenderer->setBlendFunc(blendFunc);
  279. }
  280. const BlendFunc& ImageView::getBlendFunc() const
  281. {
  282. return _imageRenderer->getBlendFunc();
  283. }
  284. }
  285. NS_CC_END