1
0

UILoadingBar.cpp 11 KB

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