UILoadingBar.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/UILoadingBar.h"
  22. #include "ui/UIHelper.h"
  23. #include "ui/UIScale9Sprite.h"
  24. #include "2d/CCSprite.h"
  25. #include "editor-support/cocostudio/CocosStudioExtension.h"
  26. NS_CC_BEGIN
  27. /* FIXME:
  28. Code could be simplified by using Sprite's setContentSize feature.
  29. Instead of scaling the sprite, set call setContentSize both in scale9 and non-scale9 modes
  30. */
  31. namespace ui {
  32. static const int BAR_RENDERER_Z = (-1);
  33. IMPLEMENT_CLASS_GUI_INFO(LoadingBar)
  34. LoadingBar::LoadingBar():
  35. _direction(Direction::LEFT),
  36. _percent(100.0),
  37. _totalLength(0),
  38. _textureFile(""),
  39. _barRenderer(nullptr),
  40. _renderBarTexType(TextureResType::LOCAL),
  41. _barRendererTextureSize(Size::ZERO),
  42. _originalRect(Rect::ZERO),
  43. _scale9Enabled(false),
  44. _prevIgnoreSize(true),
  45. _capInsets(Rect::ZERO),
  46. _barRendererAdaptDirty(true)
  47. {
  48. }
  49. LoadingBar::~LoadingBar()
  50. {
  51. }
  52. LoadingBar* LoadingBar::create()
  53. {
  54. LoadingBar* widget = new (std::nothrow) LoadingBar();
  55. if (widget && widget->init())
  56. {
  57. widget->autorelease();
  58. return widget;
  59. }
  60. CC_SAFE_DELETE(widget);
  61. return nullptr;
  62. }
  63. LoadingBar* LoadingBar::create(const std::string &textureName, float percentage)
  64. {
  65. return LoadingBar::create(textureName, TextureResType::LOCAL, percentage);
  66. }
  67. LoadingBar* LoadingBar::create(const std::string &textureName,
  68. TextureResType texType,
  69. float percentage)
  70. {
  71. LoadingBar* widget = new (std::nothrow) LoadingBar;
  72. if (widget && widget->init())
  73. {
  74. widget->autorelease();
  75. widget->loadTexture(textureName,texType);
  76. widget->setPercent(percentage);
  77. return widget;
  78. }
  79. CC_SAFE_DELETE(widget);
  80. return nullptr;
  81. }
  82. void LoadingBar::initRenderer()
  83. {
  84. _barRenderer = Scale9Sprite::create();
  85. _barRenderer->setScale9Enabled(false);
  86. addProtectedChild(_barRenderer, BAR_RENDERER_Z, -1);
  87. _barRenderer->setAnchorPoint(Vec2(0.0,0.5));
  88. }
  89. void LoadingBar::setDirection(cocos2d::ui::LoadingBar::Direction direction)
  90. {
  91. if (_direction == direction)
  92. {
  93. return;
  94. }
  95. _direction = direction;
  96. switch (_direction)
  97. {
  98. case Direction::LEFT:
  99. _barRenderer->setAnchorPoint(Vec2(0.0f,0.5f));
  100. _barRenderer->setPosition(Vec2(0,_contentSize.height*0.5f));
  101. break;
  102. case Direction::RIGHT:
  103. _barRenderer->setAnchorPoint(Vec2(1.0f,0.5f));
  104. _barRenderer->setPosition(Vec2(_totalLength,_contentSize.height*0.5f));
  105. break;
  106. }
  107. this->handleSpriteFlipX();
  108. }
  109. LoadingBar::Direction LoadingBar::getDirection()const
  110. {
  111. return _direction;
  112. }
  113. void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
  114. {
  115. if (texture.empty())
  116. {
  117. return;
  118. }
  119. _textureFile = texture;
  120. _renderBarTexType = texType;
  121. switch (_renderBarTexType)
  122. {
  123. case TextureResType::LOCAL:
  124. _barRenderer->setTexture(texture);
  125. break;
  126. case TextureResType::PLIST:
  127. _barRenderer->setSpriteFrame(texture);
  128. break;
  129. default:
  130. break;
  131. }
  132. //FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
  133. if (!_ignoreSize && _customSize.equals(Size::ZERO)) {
  134. _customSize = _barRenderer->getContentSize();
  135. }
  136. this->setupTexture();
  137. }
  138. void LoadingBar::loadTexture(SpriteFrame* spriteframe)
  139. {
  140. this->_barRenderer->initWithSpriteFrame(spriteframe);
  141. this->setupTexture();
  142. }
  143. void LoadingBar::setupTexture()
  144. {
  145. _barRendererTextureSize = _barRenderer->getContentSize();
  146. _originalRect = _barRenderer->getTextureRect();
  147. switch (_direction)
  148. {
  149. case Direction::LEFT:
  150. _barRenderer->setAnchorPoint(Vec2(0.0f,0.5f));
  151. break;
  152. case Direction::RIGHT:
  153. _barRenderer->setAnchorPoint(Vec2(1.0f,0.5f));
  154. break;
  155. }
  156. this->handleSpriteFlipX();
  157. _barRenderer->setCapInsets(_capInsets);
  158. this->updateChildrenDisplayedRGBA();
  159. barRendererScaleChangedWithSize();
  160. updateContentSizeWithTextureSize(_barRendererTextureSize);
  161. this->updateProgressBar();
  162. _barRendererAdaptDirty = true;
  163. }
  164. void LoadingBar::handleSpriteFlipX()
  165. {
  166. if (_direction == Direction::LEFT)
  167. {
  168. if (!_scale9Enabled)
  169. {
  170. _barRenderer->setFlippedX(false);
  171. }
  172. }
  173. else
  174. {
  175. if (!_scale9Enabled)
  176. {
  177. _barRenderer->setFlippedX(true);
  178. }
  179. }
  180. }
  181. void LoadingBar::setScale9Enabled(bool enabled)
  182. {
  183. if (_scale9Enabled == enabled)
  184. {
  185. return;
  186. }
  187. _scale9Enabled = enabled;
  188. _barRenderer->setScale9Enabled(_scale9Enabled);
  189. if (_scale9Enabled)
  190. {
  191. bool ignoreBefore = _ignoreSize;
  192. ignoreContentAdaptWithSize(false);
  193. _prevIgnoreSize = ignoreBefore;
  194. }
  195. else
  196. {
  197. ignoreContentAdaptWithSize(_prevIgnoreSize);
  198. }
  199. setCapInsets(_capInsets);
  200. updateProgressBar();
  201. _barRendererAdaptDirty = true;
  202. }
  203. bool LoadingBar::isScale9Enabled()const
  204. {
  205. return _scale9Enabled;
  206. }
  207. void LoadingBar::setCapInsets(const Rect &capInsets)
  208. {
  209. _capInsets = ui::Helper::restrictCapInsetRect(capInsets, _barRendererTextureSize);
  210. if (!_scale9Enabled)
  211. {
  212. return;
  213. }
  214. // textureRect should be restored in order to calculate the scale9 correctly
  215. // https://github.com/cocos2d/cocos2d-x/issues/16930
  216. _barRenderer->setTextureRect(_originalRect, _barRenderer->isTextureRectRotated(), _barRendererTextureSize);
  217. _barRenderer->setCapInsets(_capInsets);
  218. }
  219. const Rect& LoadingBar::getCapInsets()const
  220. {
  221. return _capInsets;
  222. }
  223. void LoadingBar::setPercent(float percent)
  224. {
  225. if (percent > 100)
  226. {
  227. percent = 100;
  228. }
  229. if (percent < 0)
  230. {
  231. percent = 0;
  232. }
  233. if (_percent == percent)
  234. {
  235. return;
  236. }
  237. _percent = percent;
  238. if (_totalLength <= 0)
  239. {
  240. return;
  241. }
  242. this->updateProgressBar();
  243. }
  244. void LoadingBar::updateProgressBar()
  245. {
  246. if (_scale9Enabled)
  247. {
  248. setScale9Scale();
  249. }
  250. else
  251. {
  252. float res = _percent / 100.0f;
  253. Rect rect = _barRenderer->getTextureRect();
  254. rect.size.width = _barRendererTextureSize.width * res;
  255. _barRenderer->setTextureRect(rect, _barRenderer->isTextureRectRotated(), rect.size);
  256. }
  257. }
  258. float LoadingBar::getPercent() const
  259. {
  260. return _percent;
  261. }
  262. void LoadingBar::onSizeChanged()
  263. {
  264. Widget::onSizeChanged();
  265. _barRendererAdaptDirty = true;
  266. }
  267. void LoadingBar::adaptRenderers()
  268. {
  269. if (_barRendererAdaptDirty)
  270. {
  271. barRendererScaleChangedWithSize();
  272. _barRendererAdaptDirty = false;
  273. }
  274. }
  275. void LoadingBar::ignoreContentAdaptWithSize(bool ignore)
  276. {
  277. if (!_scale9Enabled || (_scale9Enabled && !ignore))
  278. {
  279. Widget::ignoreContentAdaptWithSize(ignore);
  280. _prevIgnoreSize = ignore;
  281. }
  282. }
  283. Size LoadingBar::getVirtualRendererSize() const
  284. {
  285. return _barRendererTextureSize;
  286. }
  287. Node* LoadingBar::getVirtualRenderer()
  288. {
  289. return _barRenderer;
  290. }
  291. void LoadingBar::barRendererScaleChangedWithSize()
  292. {
  293. if (_unifySize)
  294. {
  295. //_barRenderer->setPreferredSize(_contentSize);
  296. _totalLength = _contentSize.width;
  297. this->setPercent(_percent);
  298. }
  299. else if (_ignoreSize)
  300. {
  301. if (!_scale9Enabled)
  302. {
  303. _totalLength = _barRendererTextureSize.width;
  304. _barRenderer->setScale(1.0f);
  305. }
  306. }
  307. else
  308. {
  309. _totalLength = _contentSize.width;
  310. if (_scale9Enabled)
  311. {
  312. this->setScale9Scale();
  313. _barRenderer->setScale(1.0f);
  314. }
  315. else
  316. {
  317. Size textureSize = _barRendererTextureSize;
  318. if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
  319. {
  320. _barRenderer->setScale(1.0f);
  321. return;
  322. }
  323. float scaleX = _contentSize.width / textureSize.width;
  324. float scaleY = _contentSize.height / textureSize.height;
  325. _barRenderer->setScaleX(scaleX);
  326. _barRenderer->setScaleY(scaleY);
  327. }
  328. }
  329. switch (_direction)
  330. {
  331. case Direction::LEFT:
  332. _barRenderer->setPosition(Vec2(0.0f,_contentSize.height*0.5f));
  333. break;
  334. case Direction::RIGHT:
  335. _barRenderer->setPosition(Vec2(_totalLength,_contentSize.height*0.5f));
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. void LoadingBar::setScale9Scale()
  342. {
  343. float width = (float)(_percent) / 100.0f * _totalLength;
  344. _barRenderer->setPreferredSize(Size(width, _contentSize.height));
  345. }
  346. std::string LoadingBar::getDescription() const
  347. {
  348. return "LoadingBar";
  349. }
  350. Widget* LoadingBar::createCloneInstance()
  351. {
  352. return LoadingBar::create();
  353. }
  354. void LoadingBar::copySpecialProperties(Widget *widget)
  355. {
  356. LoadingBar* loadingBar = dynamic_cast<LoadingBar*>(widget);
  357. if (loadingBar)
  358. {
  359. _prevIgnoreSize = loadingBar->_prevIgnoreSize;
  360. setScale9Enabled(loadingBar->_scale9Enabled);
  361. // clone the inner sprite: https://github.com/cocos2d/cocos2d-x/issues/16930
  362. loadingBar->_barRenderer->copyTo(_barRenderer);
  363. setupTexture();
  364. setCapInsets(loadingBar->_capInsets);
  365. setPercent(loadingBar->_percent);
  366. setDirection(loadingBar->_direction);
  367. _textureFile = loadingBar->_textureFile;
  368. _totalLength = loadingBar->_totalLength;
  369. _barRendererTextureSize = loadingBar->_barRendererTextureSize;
  370. }
  371. }
  372. ResourceData LoadingBar::getRenderFile()
  373. {
  374. ResourceData rData;
  375. rData.type = (int)_renderBarTexType;
  376. rData.file = _textureFile;
  377. return rData;
  378. }
  379. }
  380. NS_CC_END