1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga 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  */
 39 cc.TransitionProgress = cc.TransitionScene.extend(/** @lends cc.TransitionProgress# */{
 40     _to:0,
 41     _from:0,
 42     _sceneToBeModified:null,
 43     _className:"TransitionProgress",
 44 
 45 	_setAttrs: function(node, x, y) {
 46 		node.attr({
 47 			x: x,
 48 			y: y,
 49 			anchorX: 0.5,
 50 			anchorY: 0.5
 51 		});
 52 	},
 53 
 54     /**
 55      * @override
 56      */
 57     onEnter:function () {
 58         cc.TransitionScene.prototype.onEnter.call(this);
 59         this._setupTransition();
 60 
 61         // create a transparent color layer
 62         // in which we are going to add our rendertextures
 63         var winSize = cc.director.getWinSize();
 64 
 65         // create the second render texture for outScene
 66         var texture = cc.RenderTexture.create(winSize.width, winSize.height);
 67         texture.sprite.anchorX = 0.5;
 68 	    texture.sprite.anchorY = 0.5;
 69         this._setAttrs(texture, winSize.width / 2, winSize.height / 2);
 70 
 71         // render outScene to its texturebuffer
 72         texture.clear(0, 0, 0, 1);
 73         texture.begin();
 74         this._sceneToBeModified.visit();
 75         texture.end();
 76 
 77         //    Since we've passed the outScene to the texture we don't need it.
 78         if (this._sceneToBeModified == this._outScene)
 79             this.hideOutShowIn();
 80 
 81         //    We need the texture in RenderTexture.
 82         var pNode = this._progressTimerNodeWithRenderTexture(texture);
 83 
 84         // create the blend action
 85         var layerAction = cc.Sequence.create(
 86             cc.ProgressFromTo.create(this._duration, this._from, this._to),
 87             cc.CallFunc.create(this.finish, this));
 88         // run the blend action
 89         pNode.runAction(layerAction);
 90 
 91         // add the layer (which contains our two rendertextures) to the scene
 92         this.addChild(pNode, 2, cc.SCENE_RADIAL);
 93     },
 94 
 95     /**
 96      * @override
 97      */
 98     onExit:function () {
 99         // remove our layer and release all containing objects
100         this.removeChildByTag(cc.SCENE_RADIAL, true);
101         cc.TransitionScene.prototype.onExit.call(this);
102     },
103 
104     _setupTransition:function () {
105         this._sceneToBeModified = this._outScene;
106         this._from = 100;
107         this._to = 0;
108     },
109 
110     _progressTimerNodeWithRenderTexture:function (texture) {
111         cc.log("cc.TransitionProgress._progressTimerNodeWithRenderTexture(): should be overridden in subclass");
112         return null;
113     },
114 
115     _sceneOrder:function () {
116         this._isInSceneOnTop = false;
117     }
118 });
119 
120 /**
121  * create a cc.TransitionProgress object
122  * @function
123  * @param {Number} t time
124  * @param {cc.Scene} scene
125  * @return {cc.TransitionProgress}
126  */
127 cc.TransitionProgress.create = function (t, scene) {
128     var tempScene = new cc.TransitionProgress();
129     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
130         return tempScene;
131     }
132     return null;
133 };
134 
135 /**
136  *  cc.TransitionRadialCCW transition.<br/>
137  *  A counter clock-wise radial transition to the next scene
138  * @class
139  * @extends cc.TransitionProgress
140  */
141 cc.TransitionProgressRadialCCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCCW# */{
142     _progressTimerNodeWithRenderTexture:function (texture) {
143         var size = cc.director.getWinSize();
144 
145         var pNode = cc.ProgressTimer.create(texture.sprite);
146 
147         // but it is flipped upside down so we flip the sprite
148         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
149             pNode.sprite.flippedY = true;
150         pNode.type = cc.PROGRESS_TIMER_TYPE_RADIAL;
151 
152         //    Return the radial type that we want to use
153         pNode.reverseDir = false;
154         pNode.percentage = 100;
155         this._setAttrs(pNode, size.width / 2, size.height / 2);
156 
157         return pNode;
158     }
159 });
160 
161 /**
162  * create a cc.TransitionProgressRadialCCW object
163  * @function
164  * @param {Number} t time
165  * @param {cc.Scene} scene
166  * @return {cc.TransitionProgressRadialCCW}
167  */
168 cc.TransitionProgressRadialCCW.create = function (t, scene) {
169     var tempScene = new cc.TransitionProgressRadialCCW();
170     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
171         return tempScene;
172     }
173     return null;
174 };
175 
176 /**
177  * cc.TransitionRadialCW transition.<br/>
178  * A counter colock-wise radial transition to the next scene
179  * @class
180  * @extends cc.TransitionProgress
181  */
182 cc.TransitionProgressRadialCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCW# */{
183     _progressTimerNodeWithRenderTexture:function (texture) {
184         var size = cc.director.getWinSize();
185 
186         var pNode = cc.ProgressTimer.create(texture.sprite);
187 
188         // but it is flipped upside down so we flip the sprite
189         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
190             pNode.sprite.flippedY = true;
191         pNode.type = cc.PROGRESS_TIMER_TYPE_RADIAL;
192 
193         //    Return the radial type that we want to use
194         pNode.reverseDir = true;
195         pNode.percentage = 100;
196         this._setAttrs(pNode, size.width / 2, size.height / 2);
197 
198         return pNode;
199     }
200 });
201 
202 /**
203  * create a cc.TransitionProgressRadialCW object
204  * @function
205  * @param {Number} t time
206  * @param {cc.Scene} scene
207  * @return {cc.TransitionProgressRadialCW}
208  */
209 cc.TransitionProgressRadialCW.create = function (t, scene) {
210     var tempScene = new cc.TransitionProgressRadialCW();
211     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
212         return tempScene;
213     }
214     return null;
215 };
216 
217 /**
218  * cc.TransitionProgressHorizontal transition.<br/>
219  * A  colock-wise radial transition to the next scene
220  * @class
221  * @extends cc.TransitionProgress
222  */
223 cc.TransitionProgressHorizontal = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressHorizontal# */{
224     _progressTimerNodeWithRenderTexture:function (texture) {
225         var size = cc.director.getWinSize();
226 
227         var pNode = cc.ProgressTimer.create(texture.sprite);
228 
229         // but it is flipped upside down so we flip the sprite
230         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
231             pNode.sprite.flippedY = true;
232         pNode.type = cc.PROGRESS_TIMER_TYPE_BAR;
233 
234         pNode.midPoint = cc.p(1, 0);
235         pNode.barChangeRate = cc.p(1, 0);
236 
237         pNode.percentage = 100;
238         this._setAttrs(pNode, size.width / 2, size.height / 2);
239 
240         return pNode;
241     }
242 });
243 
244 /**
245  * create a cc.TransitionProgressHorizontal object
246  * @function
247  * @param {Number} t time
248  * @param {cc.Scene} scene
249  * @return {cc.TransitionProgressHorizontal}
250  */
251 cc.TransitionProgressHorizontal.create = function (t, scene) {
252     var tempScene = new cc.TransitionProgressHorizontal();
253     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
254         return tempScene;
255     }
256     return null;
257 };
258 
259 /**
260  * cc.TransitionProgressVertical transition.
261  * @class
262  * @extends cc.TransitionProgress
263  */
264 cc.TransitionProgressVertical = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressVertical# */{
265     _progressTimerNodeWithRenderTexture:function (texture) {
266         var size = cc.director.getWinSize();
267 
268         var pNode = cc.ProgressTimer.create(texture.sprite);
269 
270         // but it is flipped upside down so we flip the sprite
271         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
272             pNode.sprite.flippedY = true;
273         pNode.type = cc.PROGRESS_TIMER_TYPE_BAR;
274 
275         pNode.midPoint = cc.p(0, 0);
276         pNode.barChangeRate = cc.p(0, 1);
277 
278         pNode.percentage = 100;
279         this._setAttrs(pNode, size.width / 2, size.height / 2);
280 
281         return pNode;
282     }
283 });
284 
285 /**
286  * create a cc.TransitionProgressVertical object
287  * @function
288  * @param {Number} t time
289  * @param {cc.Scene} scene
290  * @return {cc.TransitionProgressVertical}
291  */
292 cc.TransitionProgressVertical.create = function (t, scene) {
293     var tempScene = new cc.TransitionProgressVertical();
294     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
295         return tempScene;
296     }
297     return null;
298 };
299 
300 /**
301  * cc.TransitionProgressInOut transition.
302  * @class
303  * @extends cc.TransitionProgress
304  */
305 cc.TransitionProgressInOut = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressInOut# */{
306     _progressTimerNodeWithRenderTexture:function (texture) {
307         var size = cc.director.getWinSize();
308         var pNode = cc.ProgressTimer.create(texture.sprite);
309 
310         // but it is flipped upside down so we flip the sprite
311         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
312             pNode.sprite.flippedY = true;
313         pNode.type = cc.PROGRESS_TIMER_TYPE_BAR;
314 
315         pNode.midPoint = cc.p(0.5, 0.5);
316         pNode.barChangeRate = cc.p(1, 1);
317 
318         pNode.percentage = 0;
319         this._setAttrs(pNode, size.width / 2, size.height / 2);
320 
321         return pNode;
322     },
323     _sceneOrder:function () {
324         this._isInSceneOnTop = false;
325     },
326     _setupTransition:function () {
327         this._sceneToBeModified = this._inScene;
328         this._from = 0;
329         this._to = 100;
330     }
331 });
332 
333 /**
334  * create a cc.TransitionProgressInOut object
335  * @function
336  * @param {Number} t time
337  * @param {cc.Scene} scene
338  * @return {cc.TransitionProgressInOut}
339  */
340 cc.TransitionProgressInOut.create = function (t, scene) {
341     var tempScene = new cc.TransitionProgressInOut();
342     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
343         return tempScene;
344     }
345     return null;
346 };
347 
348 /**
349  * cc.TransitionProgressOutIn transition.
350  * @class
351  * @extends cc.TransitionProgress
352  */
353 cc.TransitionProgressOutIn = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressOutIn# */{
354     _progressTimerNodeWithRenderTexture:function (texture) {
355         var size = cc.director.getWinSize();
356         var pNode = cc.ProgressTimer.create(texture.sprite);
357 
358         // but it is flipped upside down so we flip the sprite
359         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
360             pNode.sprite.flippedY = true;
361         pNode.type = cc.PROGRESS_TIMER_TYPE_BAR;
362 
363         pNode.midPoint = cc.p(0.5, 0.5);
364         pNode.barChangeRate = cc.p(1, 1);
365 
366         pNode.percentage = 100;
367         this._setAttrs(pNode, winSize.width / 2, winSize.height / 2);
368 
369         return pNode;
370     }
371 });
372 
373 /**
374  * create a cc.TransitionProgressOutIn object
375  * @function
376  * @param {Number} t time
377  * @param {cc.Scene} scene
378  * @return {cc.TransitionProgressOutIn}
379  */
380 cc.TransitionProgressOutIn.create = function (t, scene) {
381     var tempScene = new cc.TransitionProgressOutIn();
382     if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
383         return tempScene;
384     }
385     return null;
386 };
387