1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * tag for scene redial
 29  * @constant
 30  * @type Number
 31  */
 32 cc.SCENE_RADIAL = 0xc001;
 33 
 34 /**
 35  * cc.TransitionProgress transition.
 36  * @class
 37  * @extends cc.TransitionScene
 38  * @param {Number} t time
 39  * @param {cc.Scene} scene
 40  * @example
 41  * var trans = new cc.TransitionProgress(time,scene);
 42  */
 43 cc.TransitionProgress = cc.TransitionScene.extend(/** @lends cc.TransitionProgress# */{
 44     _to:0,
 45     _from:0,
 46     _sceneToBeModified:null,
 47     _className:"TransitionProgress",
 48 
 49     /**
 50      * @param {Number} t time
 51      * @param {cc.Scene} scene
 52      */
 53     ctor:function (t, scene) {
 54         cc.TransitionScene.prototype.ctor.call(this);
 55         scene && this.initWithDuration(t, scene);
 56     },
 57 
 58 	_setAttrs: function(node, x, y) {
 59 		node.attr({
 60 			x: x,
 61 			y: y,
 62 			anchorX: 0.5,
 63 			anchorY: 0.5
 64 		});
 65 	},
 66 
 67     /**
 68      * @override
 69      * custom on enter
 70      */
 71     onEnter:function () {
 72         cc.TransitionScene.prototype.onEnter.call(this);
 73         this._setupTransition();
 74 
 75         // create a transparent color layer
 76         // in which we are going to add our rendertextures
 77         var winSize = cc.director.getWinSize();
 78 
 79         // create the second render texture for outScene
 80         var texture = cc.RenderTexture.create(winSize.width, winSize.height);
 81         texture.sprite.anchorX = 0.5;
 82 	    texture.sprite.anchorY = 0.5;
 83         this._setAttrs(texture, winSize.width / 2, winSize.height / 2);
 84 
 85         // render outScene to its texturebuffer
 86         texture.clear(0, 0, 0, 1);
 87         texture.begin();
 88         this._sceneToBeModified.visit();
 89         texture.end();
 90 
 91         //    Since we've passed the outScene to the texture we don't need it.
 92         if (this._sceneToBeModified == this._outScene)
 93             this.hideOutShowIn();
 94 
 95         //    We need the texture in RenderTexture.
 96         var pNode = this._progressTimerNodeWithRenderTexture(texture);
 97 
 98         // create the blend action
 99         var layerAction = cc.sequence(
100             cc.progressFromTo(this._duration, this._from, this._to),
101             cc.callFunc(this.finish, this));
102         // run the blend action
103         pNode.runAction(layerAction);
104 
105         // add the layer (which contains our two rendertextures) to the scene
106         this.addChild(pNode, 2, cc.SCENE_RADIAL);
107     },
108 
109     /**
110      * @override
111      * custom on exit
112      */
113     onExit:function () {
114         // remove our layer and release all containing objects
115         this.removeChildByTag(cc.SCENE_RADIAL, true);
116         cc.TransitionScene.prototype.onExit.call(this);
117     },
118 
119     _setupTransition:function () {
120         this._sceneToBeModified = this._outScene;
121         this._from = 100;
122         this._to = 0;
123     },
124 
125     _progressTimerNodeWithRenderTexture:function (texture) {
126         cc.log("cc.TransitionProgress._progressTimerNodeWithRenderTexture(): should be overridden in subclass");
127         return null;
128     },
129 
130     _sceneOrder:function () {
131         this._isInSceneOnTop = false;
132     }
133 });
134 
135 /**
136  * create a cc.TransitionProgress object
137  * @deprecated since v3.0,please use new cc.TransitionProgress(t, scene) instead.
138  * @function
139  * @param {Number} t time
140  * @param {cc.Scene} scene
141  * @return {cc.TransitionProgress}
142  * @example
143  * var trans = cc.TransitionProgress.create(time,scene);
144  */
145 cc.TransitionProgress.create = function (t, scene) {
146     return new cc.TransitionProgress(t, scene);
147 };
148 
149 /**
150  *  cc.TransitionRadialCCW transition.<br/>
151  *  A counter clock-wise radial transition to the next scene
152  * @class
153  * @extends cc.TransitionProgress
154  * @param {Number} t time
155  * @param {cc.Scene} scene
156  * @example
157  * var trans = new cc.TransitionProgressRadialCCW(t, scene);
158  */
159 cc.TransitionProgressRadialCCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCCW# */{
160 
161     /**
162      * @param {Number} t time
163      * @param {cc.Scene} scene
164      */
165     ctor:function (t, scene) {
166         cc.TransitionProgress.prototype.ctor.call(this);
167         scene && this.initWithDuration(t, scene);
168     },
169 
170     _progressTimerNodeWithRenderTexture:function (texture) {
171         var size = cc.director.getWinSize();
172 
173         var pNode = cc.ProgressTimer.create(texture.sprite);
174 
175         // but it is flipped upside down so we flip the sprite
176         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
177             pNode.sprite.flippedY = true;
178         pNode.type = cc.ProgressTimer.TYPE_RADIAL;
179 
180         //    Return the radial type that we want to use
181         pNode.reverseDir = false;
182         pNode.percentage = 100;
183         this._setAttrs(pNode, size.width / 2, size.height / 2);
184 
185         return pNode;
186     }
187 });
188 
189 /**
190  * create a cc.TransitionProgressRadialCCW object
191  * @deprecated since v3.0,please use new cc.TransitionProgressRadialCCW(t, scene) instead.
192  * @param {Number} t time
193  * @param {cc.Scene} scene
194  * @return {cc.TransitionProgressRadialCCW}
195  * @example
196  * var trans = cc.TransitionProgressRadialCCW.create(time,scene);
197  */
198 cc.TransitionProgressRadialCCW.create = function (t, scene) {
199     return new cc.TransitionProgressRadialCCW(t, scene);
200 };
201 
202 /**
203  * cc.TransitionRadialCW transition.<br/>
204  * A counter colock-wise radial transition to the next scene
205  * @class
206  * @extends cc.TransitionProgress
207  * @param {Number} t time
208  * @param {cc.Scene} scene
209  * @example
210  * var trans = new cc.TransitionProgressRadialCW(t, scene);
211  */
212 cc.TransitionProgressRadialCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCW# */{
213     /**
214      * @param {Number} t time
215      * @param {cc.Scene} scene
216      */
217     ctor:function (t, scene) {
218         cc.TransitionProgress.prototype.ctor.call(this);
219         scene && this.initWithDuration(t, scene);
220     },
221 
222     _progressTimerNodeWithRenderTexture:function (texture) {
223         var size = cc.director.getWinSize();
224 
225         var pNode = cc.ProgressTimer.create(texture.sprite);
226 
227         // but it is flipped upside down so we flip the sprite
228         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
229             pNode.sprite.flippedY = true;
230         pNode.type = cc.ProgressTimer.TYPE_RADIAL;
231 
232         //    Return the radial type that we want to use
233         pNode.reverseDir = true;
234         pNode.percentage = 100;
235         this._setAttrs(pNode, size.width / 2, size.height / 2);
236 
237         return pNode;
238     }
239 });
240 
241 /**
242  * create a cc.TransitionProgressRadialCW object
243  * @deprecated since v3.0,please use cc.TransitionProgressRadialCW(t, scene) instead.
244  * @param {Number} t time
245  * @param {cc.Scene} scene
246  * @return {cc.TransitionProgressRadialCW}
247  * @example
248  * var trans = cc.TransitionProgressRadialCW.create(time,scene);
249  */
250 cc.TransitionProgressRadialCW.create = function (t, scene) {
251     var tempScene = new cc.TransitionProgressRadialCW();
252     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
253         return tempScene;
254     }
255     return new cc.TransitionProgressRadialCW(t, scene);
256 };
257 
258 /**
259  * cc.TransitionProgressHorizontal transition.<br/>
260  * A  colock-wise radial transition to the next scene
261  * @class
262  * @extends cc.TransitionProgress
263  * @param {Number} t time
264  * @param {cc.Scene} scene
265  * @example
266  * var trans = new cc.TransitionProgressHorizontal(t, scene);
267  */
268 cc.TransitionProgressHorizontal = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressHorizontal# */{
269     /**
270      * @param {Number} t time
271      * @param {cc.Scene} scene
272      */
273     ctor:function (t, scene) {
274         cc.TransitionProgress.prototype.ctor.call(this);
275         scene && this.initWithDuration(t, scene);
276     },
277 
278     _progressTimerNodeWithRenderTexture:function (texture) {
279         var size = cc.director.getWinSize();
280 
281         var pNode = cc.ProgressTimer.create(texture.sprite);
282 
283         // but it is flipped upside down so we flip the sprite
284         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
285             pNode.sprite.flippedY = true;
286         pNode.type = cc.ProgressTimer.TYPE_BAR;
287 
288         pNode.midPoint = cc.p(1, 0);
289         pNode.barChangeRate = cc.p(1, 0);
290 
291         pNode.percentage = 100;
292         this._setAttrs(pNode, size.width / 2, size.height / 2);
293 
294         return pNode;
295     }
296 });
297 
298 /**
299  * create a cc.TransitionProgressHorizontal object
300  * @deprecated since v3.0,please use new cc.TransitionProgressHorizontal(t, scene) instead.
301  * @param {Number} t time
302  * @param {cc.Scene} scene
303  * @return {cc.TransitionProgressHorizontal}
304  * @example
305  * var trans = cc.TransitionProgressHorizontal.create(time,scene);
306  */
307 cc.TransitionProgressHorizontal.create = function (t, scene) {
308     return new cc.TransitionProgressHorizontal(t, scene);
309 };
310 
311 /**
312  * cc.TransitionProgressVertical transition.
313  * @class
314  * @extends cc.TransitionProgress
315  * @param {Number} t time
316  * @param {cc.Scene} scene
317  * @example
318  * var trans = new cc.TransitionProgressVertical(t, scene);
319  */
320 cc.TransitionProgressVertical = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressVertical# */{
321 
322     /**
323      * @param {Number} t time
324      * @param {cc.Scene} scene
325      */
326     ctor:function (t, scene) {
327         cc.TransitionProgress.prototype.ctor.call(this);
328         scene && this.initWithDuration(t, scene);
329     },
330 
331     _progressTimerNodeWithRenderTexture:function (texture) {
332         var size = cc.director.getWinSize();
333 
334         var pNode = cc.ProgressTimer.create(texture.sprite);
335 
336         // but it is flipped upside down so we flip the sprite
337         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
338             pNode.sprite.flippedY = true;
339         pNode.type = cc.ProgressTimer.TYPE_BAR;
340 
341         pNode.midPoint = cc.p(0, 0);
342         pNode.barChangeRate = cc.p(0, 1);
343 
344         pNode.percentage = 100;
345         this._setAttrs(pNode, size.width / 2, size.height / 2);
346 
347         return pNode;
348     }
349 });
350 
351 /**
352  * create a cc.TransitionProgressVertical object
353  * @deprecated since v3.0,please use new cc.TransitionProgressVertical(t, scene) instead.
354  * @param {Number} t time
355  * @param {cc.Scene} scene
356  * @return {cc.TransitionProgressVertical}
357  * @example
358  * var trans = cc.TransitionProgressVertical.create(time,scene);
359  */
360 cc.TransitionProgressVertical.create = function (t, scene) {
361     return new cc.TransitionProgressVertical(t, scene);
362 };
363 
364 /**
365  * cc.TransitionProgressInOut transition.
366  * @class
367  * @extends cc.TransitionProgress
368  */
369 cc.TransitionProgressInOut = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressInOut# */{
370 
371     /**
372      * The constructor of cc.TransitionProgressInOut. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
373      * @param {Number} t time
374      * @param {cc.Scene} scene
375      */
376     ctor:function (t, scene) {
377         cc.TransitionProgress.prototype.ctor.call(this);
378         scene && this.initWithDuration(t, scene);
379     },
380 
381     _progressTimerNodeWithRenderTexture:function (texture) {
382         var size = cc.director.getWinSize();
383         var pNode = cc.ProgressTimer.create(texture.sprite);
384 
385         // but it is flipped upside down so we flip the sprite
386         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
387             pNode.sprite.flippedY = true;
388         pNode.type = cc.ProgressTimer.TYPE_BAR;
389 
390         pNode.midPoint = cc.p(0.5, 0.5);
391         pNode.barChangeRate = cc.p(1, 1);
392 
393         pNode.percentage = 0;
394         this._setAttrs(pNode, size.width / 2, size.height / 2);
395 
396         return pNode;
397     },
398     _sceneOrder:function () {
399         this._isInSceneOnTop = false;
400     },
401     _setupTransition:function () {
402         this._sceneToBeModified = this._inScene;
403         this._from = 0;
404         this._to = 100;
405     }
406 });
407 
408 /**
409  * create a cc.TransitionProgressInOut object
410  * @function
411  * @deprecated
412  * @param {Number} t time
413  * @param {cc.Scene} scene
414  * @return {cc.TransitionProgressInOut}
415  */
416 cc.TransitionProgressInOut.create = function (t, scene) {
417     return new cc.TransitionProgressInOut(t, scene);
418 };
419 
420 /**
421  * cc.TransitionProgressOutIn transition.
422  * @class
423  * @extends cc.TransitionProgress
424  */
425 cc.TransitionProgressOutIn = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressOutIn# */{
426 
427     /**
428      * The constructor of cc.TransitionProgressOutIn. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
429      * @param {Number} t time
430      * @param {cc.Scene} scene
431      */
432     ctor:function (t, scene) {
433         cc.TransitionProgress.prototype.ctor.call(this);
434         scene && this.initWithDuration(t, scene);
435     },
436     
437     _progressTimerNodeWithRenderTexture:function (texture) {
438         var size = cc.director.getWinSize();
439         var pNode = cc.ProgressTimer.create(texture.sprite);
440 
441         // but it is flipped upside down so we flip the sprite
442         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
443             pNode.sprite.flippedY = true;
444         pNode.type = cc.ProgressTimer.TYPE_BAR;
445 
446         pNode.midPoint = cc.p(0.5, 0.5);
447         pNode.barChangeRate = cc.p(1, 1);
448 
449         pNode.percentage = 100;
450         this._setAttrs(pNode, size.width / 2, size.height / 2);
451 
452         return pNode;
453     }
454 });
455 
456 /**
457  * create a cc.TransitionProgressOutIn object
458  * @function
459  * @deprecated
460  * @param {Number} t time
461  * @param {cc.Scene} scene
462  * @return {cc.TransitionProgressOutIn}
463  */
464 cc.TransitionProgressOutIn.create = function (t, scene) {
465     return new cc.TransitionProgressOutIn(t, scene);
466 };
467