UILoadingBar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  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. /**
  22. * The LoadingBar control of Cocos UI.
  23. * @class
  24. * @extends ccui.Widget
  25. *
  26. * @property {ccui.LoadingBar.TYPE_LEFT | ccui.LoadingBar.TYPE_RIGHT} direction - The progress direction of loadingbar
  27. * @property {Number} percent - The current progress of loadingbar
  28. */
  29. ccui.LoadingBar = ccui.Widget.extend(/** @lends ccui.LoadingBar# */{
  30. _direction: null,
  31. _percent: 100,
  32. _totalLength: 0,
  33. _barRenderer: null,
  34. _renderBarTexType: ccui.Widget.LOCAL_TEXTURE,
  35. _barRendererTextureSize: null,
  36. _scale9Enabled: false,
  37. _prevIgnoreSize: true,
  38. _capInsets: null,
  39. _textureFile: "",
  40. _isTextureLoaded: false,
  41. _className: "LoadingBar",
  42. _barRendererAdaptDirty: true,
  43. /**
  44. * allocates and initializes a UILoadingBar. <br/>
  45. * Constructor of ccui.LoadingBar, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  46. * @param {string} textureName
  47. * @param {Number} percentage
  48. * @example
  49. * // example
  50. * var uiLoadingBar = new ccui.LoadingBar;
  51. */
  52. ctor: function (textureName, percentage) {
  53. this._direction = ccui.LoadingBar.TYPE_LEFT;
  54. this._barRendererTextureSize = cc.size(0, 0);
  55. this._capInsets = cc.rect(0, 0, 0, 0);
  56. ccui.Widget.prototype.ctor.call(this);
  57. if(textureName !== undefined)
  58. this.loadTexture(textureName);
  59. if(percentage !== undefined)
  60. this.setPercent(percentage);
  61. },
  62. _initRenderer: function () {
  63. this._barRenderer = cc.Sprite.create();
  64. cc.Node.prototype.addChild.call(this, this._barRenderer, ccui.LoadingBar.RENDERER_ZORDER, -1);
  65. this._barRenderer.setAnchorPoint(0.0, 0.5);
  66. },
  67. /**
  68. * Changes the progress direction of LoadingBar. <br/>
  69. * LoadingBarTypeLeft means progress left to right, LoadingBarTypeRight otherwise.
  70. * @param {ccui.LoadingBar.TYPE_LEFT | ccui.LoadingBar.TYPE_RIGHT} dir
  71. */
  72. setDirection: function (dir) {
  73. if (this._direction == dir)
  74. return;
  75. this._direction = dir;
  76. switch (this._direction) {
  77. case ccui.LoadingBar.TYPE_LEFT:
  78. this._barRenderer.setAnchorPoint(0.0, 0.5);
  79. this._barRenderer.setPosition(-this._totalLength * 0.5, 0.0);
  80. if (!this._scale9Enabled)
  81. this._barRenderer.setFlippedX(false);
  82. break;
  83. case ccui.LoadingBar.TYPE_RIGHT:
  84. this._barRenderer.setAnchorPoint(1.0, 0.5);
  85. this._barRenderer.setPosition(this._totalLength * 0.5, 0.0);
  86. if (!this._scale9Enabled)
  87. this._barRenderer.setFlippedX(true);
  88. break;
  89. }
  90. },
  91. /**
  92. * Returns the progress direction of LoadingBar. <br/>
  93. * LoadingBarTypeLeft means progress left to right, LoadingBarTypeRight otherwise.
  94. * @returns {ccui.LoadingBar.TYPE_LEFT | ccui.LoadingBar.TYPE_RIGHT}
  95. */
  96. getDirection: function () {
  97. return this._direction;
  98. },
  99. /**
  100. * Loads texture for LoadingBar.
  101. * @param {String} texture
  102. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  103. */
  104. loadTexture: function (texture, texType) {
  105. if (!texture)
  106. return;
  107. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  108. this._renderBarTexType = texType;
  109. this._textureFile = texture;
  110. var barRenderer = this._barRenderer;
  111. var self = this;
  112. if(!barRenderer.texture || !barRenderer.texture.isLoaded()){
  113. barRenderer.addLoadedEventListener(function(){
  114. self._findLayout();
  115. var bz = barRenderer.getContentSize();
  116. self._barRendererTextureSize.width = bz.width;
  117. self._barRendererTextureSize.height = bz.height;
  118. switch (self._direction) {
  119. case ccui.LoadingBar.TYPE_LEFT:
  120. barRenderer.setAnchorPoint(0.0,0.5);
  121. if (!self._scale9Enabled)
  122. barRenderer.setFlippedX(false);
  123. break;
  124. case ccui.LoadingBar.TYPE_RIGHT:
  125. barRenderer.setAnchorPoint(1.0,0.5);
  126. if (!self._scale9Enabled)
  127. barRenderer.setFlippedX(true);
  128. break;
  129. }
  130. self._updateChildrenDisplayedRGBA();
  131. self._barRendererScaleChangedWithSize();
  132. self._updateContentSizeWithTextureSize(self._barRendererTextureSize);
  133. self._barRendererAdaptDirty = true;
  134. });
  135. }
  136. switch (this._renderBarTexType) {
  137. case ccui.Widget.LOCAL_TEXTURE:
  138. if (this._scale9Enabled){
  139. barRenderer.initWithFile(texture);
  140. barRenderer.setCapInsets(this._capInsets);
  141. } else
  142. //SetTexture cannot load resource
  143. barRenderer.initWithFile(texture);
  144. break;
  145. case ccui.Widget.PLIST_TEXTURE:
  146. if (this._scale9Enabled) {
  147. barRenderer.initWithSpriteFrameName(texture);
  148. barRenderer.setCapInsets(this._capInsets);
  149. } else
  150. //SetTexture cannot load resource
  151. barRenderer.initWithSpriteFrameName(texture);
  152. break;
  153. default:
  154. break;
  155. }
  156. var bz = barRenderer.getContentSize();
  157. this._barRendererTextureSize.width = bz.width;
  158. this._barRendererTextureSize.height = bz.height;
  159. switch (this._direction) {
  160. case ccui.LoadingBar.TYPE_LEFT:
  161. barRenderer.setAnchorPoint(0.0,0.5);
  162. if (!this._scale9Enabled)
  163. barRenderer.setFlippedX(false);
  164. break;
  165. case ccui.LoadingBar.TYPE_RIGHT:
  166. barRenderer.setAnchorPoint(1.0,0.5);
  167. if (!this._scale9Enabled)
  168. barRenderer.setFlippedX(true);
  169. break;
  170. }
  171. this._updateChildrenDisplayedRGBA();
  172. this._barRendererScaleChangedWithSize();
  173. this._updateContentSizeWithTextureSize(this._barRendererTextureSize);
  174. this._barRendererAdaptDirty = true;
  175. },
  176. /**
  177. * Sets if LoadingBar is using scale9 renderer.
  178. * @param {Boolean} enabled
  179. */
  180. setScale9Enabled: function (enabled) {
  181. if (this._scale9Enabled == enabled)
  182. return;
  183. this._scale9Enabled = enabled;
  184. this.removeProtectedChild(this._barRenderer);
  185. this._barRenderer = this._scale9Enabled ? new ccui.Scale9Sprite() : cc.Sprite.create();
  186. this.loadTexture(this._textureFile, this._renderBarTexType);
  187. this.addProtectedChild(this._barRenderer, ccui.LoadingBar.RENDERER_ZORDER, -1);
  188. if (this._scale9Enabled) {
  189. var ignoreBefore = this._ignoreSize;
  190. this.ignoreContentAdaptWithSize(false);
  191. this._prevIgnoreSize = ignoreBefore;
  192. } else
  193. this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
  194. this.setCapInsets(this._capInsets);
  195. this.setPercent(this._percent);
  196. },
  197. /**
  198. * Returns LoadingBar is using scale9 renderer or not..
  199. * @returns {Boolean}
  200. */
  201. isScale9Enabled: function () {
  202. return this._scale9Enabled;
  203. },
  204. /**
  205. * Sets capinsets for LoadingBar, if LoadingBar is using scale9 renderer.
  206. * @param {cc.Rect} capInsets
  207. */
  208. setCapInsets: function (capInsets) {
  209. if(!capInsets)
  210. return;
  211. var locInsets = this._capInsets;
  212. locInsets.x = capInsets.x;
  213. locInsets.y = capInsets.y;
  214. locInsets.width = capInsets.width;
  215. locInsets.height = capInsets.height;
  216. if (this._scale9Enabled)
  217. this._barRenderer.setCapInsets(capInsets);
  218. },
  219. /**
  220. * Returns cap insets for loadingBar.
  221. * @returns {cc.Rect}
  222. */
  223. getCapInsets: function () {
  224. return cc.rect(this._capInsets);
  225. },
  226. /**
  227. * The current progress of loadingBar
  228. * @param {number} percent percent value from 1 to 100.
  229. */
  230. setPercent: function (percent) {
  231. if (percent < 0 || percent > 100)
  232. return;
  233. this._percent = percent;
  234. if (this._totalLength <= 0)
  235. return;
  236. var res = this._percent / 100.0;
  237. if (this._scale9Enabled)
  238. this._setScale9Scale();
  239. else {
  240. var spriteRenderer = this._barRenderer;
  241. var rect = spriteRenderer.getTextureRect();
  242. rect.width = this._barRendererTextureSize.width * res;
  243. this._barRenderer.setTextureRect(
  244. cc.rect(
  245. rect.x,
  246. rect.y,
  247. this._barRendererTextureSize.width * res,
  248. this._barRendererTextureSize.height
  249. )
  250. );
  251. }
  252. },
  253. /**
  254. * Sets the contentSize of ccui.LoadingBar
  255. * @override
  256. * @param {Number|cc.Size} contentSize
  257. * @param {Number} [height]
  258. */
  259. setContentSize: function(contentSize, height){
  260. ccui.Widget.prototype.setContentSize.call(this, contentSize, height);
  261. this._totalLength = (height === undefined) ? contentSize.width : contentSize;;
  262. },
  263. /**
  264. * Returns the progress direction of LoadingBar.
  265. * @returns {number} percent value from 1 to 100.
  266. */
  267. getPercent: function () {
  268. return this._percent;
  269. },
  270. _onSizeChanged: function () {
  271. ccui.Widget.prototype._onSizeChanged.call(this);
  272. this._barRendererAdaptDirty = true;
  273. },
  274. _adaptRenderers: function(){
  275. if (this._barRendererAdaptDirty){
  276. this._barRendererScaleChangedWithSize();
  277. this._barRendererAdaptDirty = false;
  278. }
  279. },
  280. /**
  281. * Ignore the LoadingBar's custom size, if ignore is true that LoadingBar will ignore it's custom size, use renderer's content size, false otherwise.
  282. * @override
  283. * @param {Boolean}ignore
  284. */
  285. ignoreContentAdaptWithSize: function (ignore) {
  286. if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) {
  287. ccui.Widget.prototype.ignoreContentAdaptWithSize.call(this, ignore);
  288. this._prevIgnoreSize = ignore;
  289. }
  290. },
  291. /**
  292. * Returns the texture size of renderer.
  293. * @returns {cc.Size|*}
  294. */
  295. getVirtualRendererSize:function(){
  296. return cc.size(this._barRendererTextureSize);
  297. },
  298. /**
  299. * Returns the renderer of ccui.LoadingBar
  300. * @override
  301. * @returns {cc.Node}
  302. */
  303. getVirtualRenderer: function () {
  304. return this._barRenderer;
  305. },
  306. _barRendererScaleChangedWithSize: function () {
  307. var locBarRender = this._barRenderer, locContentSize = this._contentSize;
  308. if (this._ignoreSize) {
  309. if (!this._scale9Enabled) {
  310. this._totalLength = this._barRendererTextureSize.width;
  311. locBarRender.setScale(1.0);
  312. }
  313. } else {
  314. this._totalLength = locContentSize.width;
  315. if (this._scale9Enabled)
  316. this._setScale9Scale();
  317. else {
  318. var textureSize = this._barRendererTextureSize;
  319. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  320. locBarRender.setScale(1.0);
  321. return;
  322. }
  323. var scaleX = locContentSize.width / textureSize.width;
  324. var scaleY = locContentSize.height / textureSize.height;
  325. locBarRender.setScaleX(scaleX);
  326. locBarRender.setScaleY(scaleY);
  327. }
  328. }
  329. switch (this._direction) {
  330. case ccui.LoadingBar.TYPE_LEFT:
  331. locBarRender.setPosition(0, locContentSize.height * 0.5);
  332. break;
  333. case ccui.LoadingBar.TYPE_RIGHT:
  334. locBarRender.setPosition(this._totalLength, locContentSize.height * 0.5);
  335. break;
  336. default:
  337. break;
  338. }
  339. },
  340. _setScale9Scale: function () {
  341. var width = (this._percent) / 100 * this._totalLength;
  342. this._barRenderer.setPreferredSize(cc.size(width, this._contentSize.height));
  343. },
  344. /**
  345. * Returns the "class name" of widget.
  346. * @returns {string}
  347. */
  348. getDescription: function () {
  349. return "LoadingBar";
  350. },
  351. _createCloneInstance: function () {
  352. return ccui.LoadingBar.create();
  353. },
  354. _copySpecialProperties: function (loadingBar) {
  355. if(loadingBar instanceof ccui.LoadingBar){
  356. this._prevIgnoreSize = loadingBar._prevIgnoreSize;
  357. this.setScale9Enabled(loadingBar._scale9Enabled);
  358. this.loadTexture(loadingBar._textureFile, loadingBar._renderBarTexType);
  359. this.setCapInsets(loadingBar._capInsets);
  360. this.setPercent(loadingBar._percent);
  361. this.setDirection(loadingBar._direction);
  362. }
  363. }
  364. });
  365. var _p = ccui.LoadingBar.prototype;
  366. // Extended properties
  367. /** @expose */
  368. _p.direction;
  369. cc.defineGetterSetter(_p, "direction", _p.getDirection, _p.setDirection);
  370. /** @expose */
  371. _p.percent;
  372. cc.defineGetterSetter(_p, "percent", _p.getPercent, _p.setPercent);
  373. _p = null;
  374. /**
  375. * Allocates and initializes a UILoadingBar.
  376. * @deprecated since v3.0, please use new ccui.LoadingBar() instead.
  377. * @param {string} textureName
  378. * @param {Number} percentage
  379. * @return {ccui.LoadingBar}
  380. * @example
  381. * // example
  382. * var uiLoadingBar = ccui.LoadingBar.create();
  383. */
  384. ccui.LoadingBar.create = function (textureName, percentage) {
  385. return new ccui.LoadingBar(textureName, percentage);
  386. };
  387. // Constants
  388. //loadingBar Type
  389. /**
  390. * The left direction of ccui.LoadingBar.
  391. * @constant
  392. * @type {number}
  393. */
  394. ccui.LoadingBar.TYPE_LEFT = 0;
  395. /**
  396. * The right direction of ccui.LoadingBar.
  397. * @constant
  398. * @type {number}
  399. */
  400. ccui.LoadingBar.TYPE_RIGHT = 1;
  401. /**
  402. * The zOrder value of ccui.LoadingBar's renderer.
  403. * @constant
  404. * @type {number}
  405. */
  406. ccui.LoadingBar.RENDERER_ZORDER = -1;