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 = new cc.RenderTexture(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  */
143 cc.TransitionProgress.create = function (t, scene) {
144     return new cc.TransitionProgress(t, scene);
145 };
146 
147 /**
148  *  cc.TransitionRadialCCW transition.<br/>
149  *  A counter clock-wise radial transition to the next scene
150  * @class
151  * @extends cc.TransitionProgress
152  * @param {Number} t time
153  * @param {cc.Scene} scene
154  * @example
155  * var trans = new cc.TransitionProgressRadialCCW(t, scene);
156  */
157 cc.TransitionProgressRadialCCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCCW# */{
158 
159     /**
160      * @param {Number} t time
161      * @param {cc.Scene} scene
162      */
163     ctor:function (t, scene) {
164         cc.TransitionProgress.prototype.ctor.call(this);
165         scene && this.initWithDuration(t, scene);
166     },
167 
168     _progressTimerNodeWithRenderTexture:function (texture) {
169         var size = cc.director.getWinSize();
170 
171         var pNode = new cc.ProgressTimer(texture.sprite);
172 
173         // but it is flipped upside down so we flip the sprite
174         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
175             pNode.sprite.flippedY = true;
176         pNode.type = cc.ProgressTimer.TYPE_RADIAL;
177 
178         //    Return the radial type that we want to use
179         pNode.reverseDir = false;
180         pNode.percentage = 100;
181         this._setAttrs(pNode, size.width / 2, size.height / 2);
182 
183         return pNode;
184     }
185 });
186 
187 /**
188  * create a cc.TransitionProgressRadialCCW object
189  * @deprecated since v3.0,please use new cc.TransitionProgressRadialCCW(t, scene) instead.
190  * @param {Number} t time
191  * @param {cc.Scene} scene
192  * @return {cc.TransitionProgressRadialCCW}
193  * @example
194  * var trans = new cc.TransitionProgressRadialCCW(time,scene);
195  */
196 cc.TransitionProgressRadialCCW.create = function (t, scene) {
197     return new cc.TransitionProgressRadialCCW(t, scene);
198 };
199 
200 /**
201  * cc.TransitionRadialCW transition.<br/>
202  * A counter colock-wise radial transition to the next scene
203  * @class
204  * @extends cc.TransitionProgress
205  * @param {Number} t time
206  * @param {cc.Scene} scene
207  * @example
208  * var trans = new cc.TransitionProgressRadialCW(t, scene);
209  */
210 cc.TransitionProgressRadialCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCW# */{
211     /**
212      * @param {Number} t time
213      * @param {cc.Scene} scene
214      */
215     ctor:function (t, scene) {
216         cc.TransitionProgress.prototype.ctor.call(this);
217         scene && this.initWithDuration(t, scene);
218     },
219 
220     _progressTimerNodeWithRenderTexture:function (texture) {
221         var size = cc.director.getWinSize();
222 
223         var pNode = new cc.ProgressTimer(texture.sprite);
224 
225         // but it is flipped upside down so we flip the sprite
226         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
227             pNode.sprite.flippedY = true;
228         pNode.type = cc.ProgressTimer.TYPE_RADIAL;
229 
230         //    Return the radial type that we want to use
231         pNode.reverseDir = true;
232         pNode.percentage = 100;
233         this._setAttrs(pNode, size.width / 2, size.height / 2);
234 
235         return pNode;
236     }
237 });
238 
239 /**
240  * create a cc.TransitionProgressRadialCW object
241  * @deprecated since v3.0,please use cc.TransitionProgressRadialCW(t, scene) instead.
242  * @param {Number} t time
243  * @param {cc.Scene} scene
244  * @return {cc.TransitionProgressRadialCW}
245  */
246 cc.TransitionProgressRadialCW.create = function (t, scene) {
247     var tempScene = new cc.TransitionProgressRadialCW();
248     if ((tempScene !== null) && (tempScene.initWithDuration(t, scene))) {
249         return tempScene;
250     }
251     return new cc.TransitionProgressRadialCW(t, scene);
252 };
253 
254 /**
255  * cc.TransitionProgressHorizontal transition.<br/>
256  * A  colock-wise radial transition to the next scene
257  * @class
258  * @extends cc.TransitionProgress
259  * @param {Number} t time
260  * @param {cc.Scene} scene
261  * @example
262  * var trans = new cc.TransitionProgressHorizontal(t, scene);
263  */
264 cc.TransitionProgressHorizontal = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressHorizontal# */{
265     /**
266      * @param {Number} t time
267      * @param {cc.Scene} scene
268      */
269     ctor:function (t, scene) {
270         cc.TransitionProgress.prototype.ctor.call(this);
271         scene && this.initWithDuration(t, scene);
272     },
273 
274     _progressTimerNodeWithRenderTexture:function (texture) {
275         var size = cc.director.getWinSize();
276 
277         var pNode = new cc.ProgressTimer(texture.sprite);
278 
279         // but it is flipped upside down so we flip the sprite
280         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
281             pNode.sprite.flippedY = true;
282         pNode.type = cc.ProgressTimer.TYPE_BAR;
283 
284         pNode.midPoint = cc.p(1, 0);
285         pNode.barChangeRate = cc.p(1, 0);
286 
287         pNode.percentage = 100;
288         this._setAttrs(pNode, size.width / 2, size.height / 2);
289 
290         return pNode;
291     }
292 });
293 
294 /**
295  * create a cc.TransitionProgressHorizontal object
296  * @deprecated since v3.0,please use new cc.TransitionProgressHorizontal(t, scene) instead.
297  * @param {Number} t time
298  * @param {cc.Scene} scene
299  * @return {cc.TransitionProgressHorizontal}
300  */
301 cc.TransitionProgressHorizontal.create = function (t, scene) {
302     return new cc.TransitionProgressHorizontal(t, scene);
303 };
304 
305 /**
306  * cc.TransitionProgressVertical transition.
307  * @class
308  * @extends cc.TransitionProgress
309  * @param {Number} t time
310  * @param {cc.Scene} scene
311  * @example
312  * var trans = new cc.TransitionProgressVertical(t, scene);
313  */
314 cc.TransitionProgressVertical = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressVertical# */{
315 
316     /**
317      * @param {Number} t time
318      * @param {cc.Scene} scene
319      */
320     ctor:function (t, scene) {
321         cc.TransitionProgress.prototype.ctor.call(this);
322         scene && this.initWithDuration(t, scene);
323     },
324 
325     _progressTimerNodeWithRenderTexture:function (texture) {
326         var size = cc.director.getWinSize();
327 
328         var pNode = new cc.ProgressTimer(texture.sprite);
329 
330         // but it is flipped upside down so we flip the sprite
331         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
332             pNode.sprite.flippedY = true;
333         pNode.type = cc.ProgressTimer.TYPE_BAR;
334 
335         pNode.midPoint = cc.p(0, 0);
336         pNode.barChangeRate = cc.p(0, 1);
337 
338         pNode.percentage = 100;
339         this._setAttrs(pNode, size.width / 2, size.height / 2);
340 
341         return pNode;
342     }
343 });
344 
345 /**
346  * create a cc.TransitionProgressVertical object
347  * @deprecated since v3.0,please use new cc.TransitionProgressVertical(t, scene) instead.
348  * @param {Number} t time
349  * @param {cc.Scene} scene
350  * @return {cc.TransitionProgressVertical}
351  */
352 cc.TransitionProgressVertical.create = function (t, scene) {
353     return new cc.TransitionProgressVertical(t, scene);
354 };
355 
356 /**
357  * cc.TransitionProgressInOut transition.
358  * @class
359  * @extends cc.TransitionProgress
360  */
361 cc.TransitionProgressInOut = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressInOut# */{
362 
363     /**
364      * The constructor of cc.TransitionProgressInOut. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
365      * @param {Number} t time
366      * @param {cc.Scene} scene
367      */
368     ctor:function (t, scene) {
369         cc.TransitionProgress.prototype.ctor.call(this);
370         scene && this.initWithDuration(t, scene);
371     },
372 
373     _progressTimerNodeWithRenderTexture:function (texture) {
374         var size = cc.director.getWinSize();
375         var pNode = new cc.ProgressTimer(texture.sprite);
376 
377         // but it is flipped upside down so we flip the sprite
378         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
379             pNode.sprite.flippedY = true;
380         pNode.type = cc.ProgressTimer.TYPE_BAR;
381 
382         pNode.midPoint = cc.p(0.5, 0.5);
383         pNode.barChangeRate = cc.p(1, 1);
384 
385         pNode.percentage = 0;
386         this._setAttrs(pNode, size.width / 2, size.height / 2);
387 
388         return pNode;
389     },
390     _sceneOrder:function () {
391         this._isInSceneOnTop = false;
392     },
393     _setupTransition:function () {
394         this._sceneToBeModified = this._inScene;
395         this._from = 0;
396         this._to = 100;
397     }
398 });
399 
400 /**
401  * create a cc.TransitionProgressInOut object
402  * @function
403  * @deprecated
404  * @param {Number} t time
405  * @param {cc.Scene} scene
406  * @return {cc.TransitionProgressInOut}
407  */
408 cc.TransitionProgressInOut.create = function (t, scene) {
409     return new cc.TransitionProgressInOut(t, scene);
410 };
411 
412 /**
413  * cc.TransitionProgressOutIn transition.
414  * @class
415  * @extends cc.TransitionProgress
416  */
417 cc.TransitionProgressOutIn = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressOutIn# */{
418 
419     /**
420      * The constructor of cc.TransitionProgressOutIn. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
421      * @param {Number} t time
422      * @param {cc.Scene} scene
423      */
424     ctor:function (t, scene) {
425         cc.TransitionProgress.prototype.ctor.call(this);
426         scene && this.initWithDuration(t, scene);
427     },
428     
429     _progressTimerNodeWithRenderTexture:function (texture) {
430         var size = cc.director.getWinSize();
431         var pNode = new cc.ProgressTimer(texture.sprite);
432 
433         // but it is flipped upside down so we flip the sprite
434         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
435             pNode.sprite.flippedY = true;
436         pNode.type = cc.ProgressTimer.TYPE_BAR;
437 
438         pNode.midPoint = cc.p(0.5, 0.5);
439         pNode.barChangeRate = cc.p(1, 1);
440 
441         pNode.percentage = 100;
442         this._setAttrs(pNode, size.width / 2, size.height / 2);
443 
444         return pNode;
445     }
446 });
447 
448 /**
449  * create a cc.TransitionProgressOutIn object
450  * @function
451  * @deprecated
452  * @param {Number} t time
453  * @param {cc.Scene} scene
454  * @return {cc.TransitionProgressOutIn}
455  */
456 cc.TransitionProgressOutIn.create = function (t, scene) {
457     return new cc.TransitionProgressOutIn(t, scene);
458 };
459