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  *<p> A transition which peels back the bottom right hand corner of a scene<br/>
 29  * to transition to the scene beneath it simulating a page turn.<br/></p>
 30  *
 31  * <p>This uses a 3DAction so it's strongly recommended that depth buffering<br/>
 32  * is turned on in cc.Director using:</p>
 33  *
 34  * <p>cc.Director.getInstance().setDepthBufferFormat(kDepthBuffer16);</p>
 35  * @class
 36  * @extends cc.TransitionScene
 37  */
 38 cc.TransitionPageTurn = cc.TransitionScene.extend(/** @lends cc.TransitionPageTurn# */{
 39     /**
 40      * @type Boolean
 41      */
 42     _back:true,
 43 
 44     /**
 45      * Creates a base transition with duration and incoming scene.<br/>
 46      * If back is true then the effect is reversed to appear as if the incoming<br/>
 47      * scene is being turned from left over the outgoing scene.
 48      * @param {Number} t time in seconds
 49      * @param {cc.Scene} scene
 50      * @param {Boolean} backwards
 51      * @return {Boolean}
 52      */
 53     initWithDuration:function (t, scene, backwards) {
 54         // XXX: needed before [super init]
 55         this._back = backwards;
 56 
 57         if (cc.TransitionScene.prototype.initWithDuration.call(this, t, scene)) {
 58             // do something
 59         }
 60         return true;
 61     },
 62 
 63     /**
 64      * @param {cc.Size} vector
 65      * @return {cc.ReverseTime|cc.TransitionScene}
 66      */
 67     actionWithSize:function (vector) {
 68         if (this._back)
 69             return cc.ReverseTime.create(cc.PageTurn3D.create(this._duration, vector));        // Get hold of the PageTurn3DAction
 70         else
 71             return cc.PageTurn3D.create(this._duration, vector);     // Get hold of the PageTurn3DAction
 72     },
 73 
 74     /**
 75      * custom on enter
 76      */
 77     onEnter:function () {
 78         cc.TransitionScene.prototype.onEnter.call(this);
 79         var winSize = cc.Director.getInstance().getWinSize();
 80         var x, y;
 81         if (winSize.width > winSize.height) {
 82             x = 16;
 83             y = 12;
 84         } else {
 85             x = 12;
 86             y = 16;
 87         }
 88 
 89         var action = this.actionWithSize(cc.size(x, y));
 90 
 91         if (!this._back) {
 92             this._outScene.runAction( cc.Sequence.create(action,cc.CallFunc.create(this.finish, this),cc.StopGrid.create()));
 93         } else {
 94             // to prevent initial flicker
 95             this._inScene.setVisible(false);
 96             this._inScene.runAction(
 97                 cc.Sequence.create(cc.Show.create(),action, cc.CallFunc.create(this.finish, this), cc.StopGrid.create())
 98             );
 99         }
100     },
101 
102     _sceneOrder:function () {
103         this._isInSceneOnTop = this._back;
104     }
105 });
106 
107 /**
108  * Creates a base transition with duration and incoming scene.<br/>
109  * If back is true then the effect is reversed to appear as if the incoming<br/>
110  * scene is being turned from left over the outgoing scene.
111  * @param {Number} t time in seconds
112  * @param {cc.Scene} scene
113  * @param {Boolean} backwards
114  * @return {cc.TransitionPageTurn}
115  * @example
116  * // Example
117  * var myTransition = cc.TransitionPageTurn.create(1.5, nextScene, true)//true means backwards
118  */
119 cc.TransitionPageTurn.create = function (t, scene, backwards) {
120     var transition = new cc.TransitionPageTurn();
121     transition.initWithDuration(t, scene, backwards);
122     return transition;
123 };
124