1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3 
  4  http://www.cocos2d-x.org
  5 
  6  Permission is hereby granted, free of charge, to any person obtaining a copy
  7  of this software and associated documentation files (the "Software"), to deal
  8  in the Software without restriction, including without limitation the rights
  9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  copies of the Software, and to permit persons to whom the Software is
 11  furnished to do so, subject to the following conditions:
 12 
 13  The above copyright notice and this permission notice shall be included in
 14  all copies or substantial portions of the Software.
 15 
 16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22  THE SOFTWARE.
 23  ****************************************************************************/
 24 
 25 /**
 26  * loadingBar type
 27  * @type {Object}
 28  */
 29 ccs.LoadingBarType = { left: 0, right: 1};
 30 
 31 /**
 32  * Base class for ccs.UILoadingBar
 33  * @class
 34  * @extends ccs.UIWidget
 35  */
 36 ccs.UILoadingBar = ccs.UIWidget.extend(/** @lends ccs.UILoadingBar# */{
 37     _barType: null,
 38     _percent: 100,
 39     _totalLength: 0,
 40     _barRenderer: null,
 41     _renderBarTexType: null,
 42     _barRendererTextureSize: null,
 43     _scale9Enabled: false,
 44     _prevIgnoreSize: true,
 45     _capInsets: null,
 46     _textureFile: "",
 47     ctor: function () {
 48         ccs.UIWidget.prototype.ctor.call(this);
 49         this._barType = ccs.LoadingBarType.left;
 50         this._percent = 100;
 51         this._totalLength = 0;
 52         this._barRenderer = null;
 53         this._renderBarTexType = ccs.TextureResType.local;
 54         this._barRendererTextureSize = cc.size(0, 0);
 55         this._scale9Enabled = false;
 56         this._prevIgnoreSize = true;
 57         this._capInsets = cc.rect(0, 0, 0, 0);
 58         this._textureFile = "";
 59     },
 60 
 61     initRenderer: function () {
 62         ccs.UIWidget.prototype.initRenderer.call(this);
 63         this._barRenderer = cc.Sprite.create();
 64         this._renderer.addChild(this._barRenderer);
 65         this._barRenderer.setAnchorPoint(cc.p(0.0, 0.5));
 66     },
 67 
 68     /**
 69      * Changes the progress direction of loadingbar.
 70      * LoadingBarTypeLeft means progress left to right, LoadingBarTypeRight otherwise.
 71      * @param {ccs.LoadingBarType} dir
 72      */
 73     setDirection: function (dir) {
 74         if (this._barType == dir) {
 75             return;
 76         }
 77         this._barType = dir;
 78 
 79         switch (this._barType) {
 80             case ccs.LoadingBarType.left:
 81                 this._barRenderer.setAnchorPoint(cc.p(0.0, 0.5));
 82                 this._barRenderer.setPosition(cc.p(-this._totalLength * 0.5, 0.0));
 83                 if (!this._scale9Enabled) {
 84                     this._barRenderer.setFlippedX(false);
 85                 }
 86                 break;
 87             case ccs.LoadingBarType.right:
 88                 this._barRenderer.setAnchorPoint(cc.p(1.0, 0.5));
 89                 this._barRenderer.setPosition(cc.p(this._totalLength * 0.5, 0.0));
 90                 if (!this._scale9Enabled) {
 91                     this._barRenderer.setFlippedX(true);
 92                 }
 93                 break;
 94         }
 95     },
 96 
 97     /**
 98      * Gets the progress direction of loadingbar.
 99      * LoadingBarTypeLeft means progress left to right, LoadingBarTypeRight otherwise.
100      * @returns {ccs.LoadingBarType}
101      */
102     getDirection: function () {
103         return this._barType;
104     },
105 
106     /**
107      * Load texture for loadingbar.
108      * @param {String} texture
109      * @param {ccs.TextureResType} texType
110      */
111     loadTexture: function (texture, texType) {
112         if (!texture) {
113             return;
114         }
115         texType = texType || ccs.TextureResType.local;
116         this._renderBarTexType = texType;
117         this._textureFile = texture;
118         switch (this._renderBarTexType) {
119             case ccs.TextureResType.local:
120                 if (this._scale9Enabled){
121                     this._barRenderer.initWithFile(texture);
122                     this._barRenderer.setCapInsets(this._capInsets);
123                 }
124                 else
125                     this._barRenderer.initWithFile(texture);
126                 break;
127             case ccs.TextureResType.plist:
128                 if (this._scale9Enabled){
129                     this._barRenderer.initWithSpriteFrameName(texture);
130                     this._barRenderer.setCapInsets(this._capInsets);
131                 }
132                 else
133                     this._barRenderer.initWithSpriteFrameName(texture);
134                 break;
135             default:
136                 break;
137         }
138         if (this._scale9Enabled) {
139             this._barRenderer.setColor(this.getColor());
140             this._barRenderer.setOpacity(this.getOpacity());
141         }
142         else {
143             this._barRenderer.setColor(this.getColor());
144             this._barRenderer.setOpacity(this.getOpacity());
145         }
146         this._barRendererTextureSize.width = this._barRenderer.getContentSize().width;
147         this._barRendererTextureSize.height = this._barRenderer.getContentSize().height;
148 
149         switch (this._barType) {
150             case ccs.LoadingBarType.left:
151                 this._barRenderer.setAnchorPoint(cc.p(0.0, 0.5));
152                 if (!this._scale9Enabled) {
153                     this._barRenderer.setFlippedX(false);
154                 }
155                 break;
156             case ccs.LoadingBarType.right:
157                 this._barRenderer.setAnchorPoint(cc.p(1.0, 0.5));
158                 if (!this._scale9Enabled) {
159                     this._barRenderer.setFlippedX(true);
160                 }
161                 break;
162         }
163         this.barRendererScaleChangedWithSize();
164     },
165 
166     /**
167      * Sets if loadingbar is using scale9 renderer.
168      * @param {Boolean} enabled
169      */
170     setScale9Enabled: function (enabled) {
171         if (this._scale9Enabled == enabled) {
172             return;
173         }
174         this._scale9Enabled = enabled;
175         this._renderer.removeChild(this._barRenderer, true);
176         this._barRenderer = null;
177         if (this._scale9Enabled) {
178             this._barRenderer = cc.Scale9Sprite.create();
179         }
180         else {
181             this._barRenderer = cc.Sprite.create();
182         }
183         this.loadTexture(this._textureFile, this._renderBarTexType);
184         this._renderer.addChild(this._barRenderer);
185         if (this._scale9Enabled) {
186             var ignoreBefore = this._ignoreSize;
187             this.ignoreContentAdaptWithSize(false);
188             this._prevIgnoreSize = ignoreBefore;
189         }
190         else {
191             this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
192         }
193         this.setCapInsets(this._capInsets);
194     },
195 
196     /**
197      * Sets capinsets for loadingbar, if loadingbar is using scale9 renderer.
198      * @param {cc.Rect} capInsets
199      */
200     setCapInsets: function (capInsets) {
201         this._capInsets = capInsets;
202         if (!this._scale9Enabled) {
203             return;
204         }
205         this._barRenderer.setCapInsets(capInsets);
206     },
207 
208     /**
209      * Changes the progress direction of loadingbar.
210      * @param {number} percent
211      */
212     setPercent: function (percent) {
213         if (percent < 0 || percent > 100) {
214             return;
215         }
216         if (this._totalLength <= 0) {
217             return;
218         }
219         this._percent = percent;
220         var res = this._percent / 100.0;
221 
222         var x = 0, y = 0;
223         switch (this._renderBarTexType) {
224             case ccs.TextureResType.plist:
225                 var barNode = this._barRenderer;
226                 if (barNode) {
227                     var to = barNode.getTextureRect().origin;
228                     x = to.x;
229                     y = to.y;
230                 }
231                 break;
232             default:
233                 break;
234         }
235         if (this._scale9Enabled)
236             this.setScale9Scale();
237         else
238             this._barRenderer.setTextureRect(cc.rect(x, y, this._barRendererTextureSize.width * res, this._barRendererTextureSize.height));
239     },
240 
241     /**
242      * Gets the progress direction of loadingbar.
243      * @returns {number}
244      */
245     getPercent: function () {
246         return this._percent;
247     },
248 
249     onSizeChanged: function () {
250         this.barRendererScaleChangedWithSize();
251     },
252 
253     /**
254      * override "ignoreContentAdaptWithSize" method of widget.
255      * @param {Boolean}ignore
256      */
257     ignoreContentAdaptWithSize: function (ignore) {
258         if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) {
259             ccs.UIWidget.prototype.ignoreContentAdaptWithSize.call(this, ignore);
260             this._prevIgnoreSize = ignore;
261         }
262     },
263 
264     /**
265      * override "getContentSize" method of widget.
266      * @returns {cc.Size}
267      */
268     getContentSize: function () {
269         return this._barRendererTextureSize;
270     },
271 
272     /**
273      * override "getContentSize" method of widget.
274      * @returns {cc.Node}
275      */
276     getVirtualRenderer: function () {
277         return this._barRenderer;
278     },
279 
280     barRendererScaleChangedWithSize: function () {
281         if (this._ignoreSize) {
282             if (!this._scale9Enabled) {
283                 this._totalLength = this._barRendererTextureSize.width;
284                 this._barRenderer.setScale(1.0);
285                 this._size = this._barRendererTextureSize;
286             }
287         }
288         else {
289             this._totalLength = this._size.width;
290             if (this._scale9Enabled) {
291                 this.setScale9Scale();
292             }
293             else {
294 
295                 var textureSize = this._barRendererTextureSize;
296                 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
297                     this._barRenderer.setScale(1.0);
298                     return;
299                 }
300                 var scaleX = this._size.width / textureSize.width;
301                 var scaleY = this._size.height / textureSize.height;
302                 this._barRenderer.setScaleX(scaleX);
303                 this._barRenderer.setScaleY(scaleY);
304             }
305         }
306         switch (this._barType) {
307             case ccs.LoadingBarType.left:
308                 this._barRenderer.setPosition(cc.p(-this._totalLength * 0.5, 0.0));
309                 break;
310             case ccs.LoadingBarType.right:
311                 this._barRenderer.setPosition(cc.p(this._totalLength * 0.5, 0.0));
312                 break;
313             default:
314                 break;
315         }
316     },
317 
318     setScale9Scale: function () {
319         var width = (this._percent) / 100 * this._totalLength;
320         this._barRenderer.setPreferredSize(cc.size(width, this._size.height));
321     },
322 
323     /**
324      * Returns the "class name" of widget.
325      * @returns {string}
326      */
327     getDescription: function () {
328         return "LoadingBar";
329     },
330 
331     createCloneInstance: function () {
332         return ccs.UILoadingBar.create();
333     },
334 
335     copySpecialProperties: function (loadingBar) {
336         this._prevIgnoreSize = loadingBar._prevIgnoreSize;
337         this.setScale9Enabled(loadingBar._scale9Enabled);
338         this.loadTexture(loadingBar._textureFile, loadingBar._renderBarTexType);
339         this.setCapInsets(loadingBar._capInsets);
340         this.setPercent(loadingBar._percent);
341     }
342 });
343 /**
344  * allocates and initializes a UILoadingBar.
345  * @constructs
346  * @return {ccs.UILoadingBar}
347  * @example
348  * // example
349  * var uiLoadingBar = ccs.UILoadingBar.create();
350  */
351 ccs.UILoadingBar.create = function () {
352     var uiLoadingBar = new ccs.UILoadingBar();
353     if (uiLoadingBar && uiLoadingBar.init()) {
354         return uiLoadingBar;
355     }
356     return null;
357 };