1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 /**
 26  * <p>cc.LoaderScene is a scene that you can load it when you loading files</p>
 27  * <p>cc.LoaderScene can present thedownload progress </p>
 28  * @class
 29  * @extends cc.Scene
 30  * @example
 31  * var lc = new cc.LoaderScene();
 32  */
 33 cc.LoaderScene = cc.Scene.extend({
 34     _interval : null,
 35     _label : null,
 36     _className:"LoaderScene",
 37     cb: null,
 38     target: null,
 39     /**
 40      * Contructor of cc.LoaderScene
 41      * @returns {boolean}
 42      */
 43     init : function(){
 44         var self = this;
 45 
 46         //logo
 47         var logoWidth = 160;
 48         var logoHeight = 200;
 49 
 50         // bg
 51         var bgLayer = self._bgLayer = new cc.LayerColor(cc.color(32, 32, 32, 255));
 52         self.addChild(bgLayer, 0);
 53 
 54         //image move to CCSceneFile.js
 55         var fontSize = 24, lblHeight =  -logoHeight / 2 + 100;
 56         if(cc._loaderImage){
 57             //loading logo
 58             cc.loader.loadImg(cc._loaderImage, {isCrossOrigin : false }, function(err, img){
 59                 logoWidth = img.width;
 60                 logoHeight = img.height;
 61                 self._initStage(img, cc.visibleRect.center);
 62             });
 63             fontSize = 14;
 64             lblHeight = -logoHeight / 2 - 10;
 65         }
 66         //loading percent
 67         var label = self._label = new cc.LabelTTF("Loading... 0%", "Arial", fontSize);
 68         label.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(0, lblHeight)));
 69         label.setColor(cc.color(180, 180, 180));
 70         bgLayer.addChild(this._label, 10);
 71         return true;
 72     },
 73 
 74     _initStage: function (img, centerPos) {
 75         var self = this;
 76         var texture2d = self._texture2d = new cc.Texture2D();
 77         texture2d.initWithElement(img);
 78         texture2d.handleLoadedTexture();
 79         var logo = self._logo = new cc.Sprite(texture2d);
 80         logo.setScale(cc.contentScaleFactor());
 81         logo.x = centerPos.x;
 82         logo.y = centerPos.y;
 83         self._bgLayer.addChild(logo, 10);
 84     },
 85     /**
 86      * custom onEnter
 87      */
 88     onEnter: function () {
 89         var self = this;
 90         cc.Node.prototype.onEnter.call(self);
 91         self.schedule(self._startLoading, 0.3);
 92     },
 93     /**
 94      * custom onExit
 95      */
 96     onExit: function () {
 97         cc.Node.prototype.onExit.call(this);
 98         var tmpStr = "Loading... 0%";
 99         this._label.setString(tmpStr);
100     },
101 
102     /**
103      * init with resources
104      * @param {Array} resources
105      * @param {Function|String} cb
106      * @param {Object} target
107      */
108     initWithResources: function (resources, cb, target) {
109         if(cc.isString(resources))
110             resources = [resources];
111         this.resources = resources || [];
112         this.cb = cb;
113         this.target = target;
114     },
115 
116     _startLoading: function () {
117         var self = this;
118         self.unschedule(self._startLoading);
119         var res = self.resources;
120         cc.loader.load(res,
121             function (result, count, loadedCount) {
122                 var percent = (loadedCount / count * 100) | 0;
123                 percent = Math.min(percent, 100);
124                 self._label.setString("Loading... " + percent + "%");
125             }, function () {
126                 if (self.cb)
127                     self.cb.call(self.target);
128             });
129     },
130 
131     _updateTransform: function(){
132         this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
133         this._bgLayer._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
134         this._label._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
135         this._logo._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty);
136     }
137 });
138 /**
139  * <p>cc.LoaderScene.preload can present a loaderScene with download progress.</p>
140  * <p>when all the resource are downloaded it will invoke call function</p>
141  * @param resources
142  * @param cb
143  * @param target
144  * @returns {cc.LoaderScene|*}
145  * @example
146  * //Example
147  * cc.LoaderScene.preload(g_resources, function () {
148         cc.director.runScene(new HelloWorldScene());
149     }, this);
150  */
151 cc.LoaderScene.preload = function(resources, cb, target){
152     var _cc = cc;
153     if(!_cc.loaderScene) {
154         _cc.loaderScene = new cc.LoaderScene();
155         _cc.loaderScene.init();
156         cc.eventManager.addCustomListener(cc.Director.EVENT_PROJECTION_CHANGED, function(){
157             _cc.loaderScene._updateTransform();
158         });
159     }
160     _cc.loaderScene.initWithResources(resources, cb, target);
161 
162     cc.director.runScene(_cc.loaderScene);
163     return _cc.loaderScene;
164 };