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  * <p>
 29  *     This action simulates a page turn from the bottom right hand corner of the screen.     <br/>
 30  *     It's not much use by itself but is used by the PageTurnTransition.                     <br/>
 31  *                                                                                            <br/>
 32  *     Based on an original paper by L Hong et al.                                            <br/>
 33  *     http://www.parc.com/publication/1638/turning-pages-of-3d-electronic-books.html
 34  * </p>
 35  * @class
 36  * @extends cc.Grid3DAction
 37  */
 38 cc.PageTurn3D = cc.Grid3DAction.extend(/** @lends cc.PageTurn3D# */{
 39     /**
 40      * Update each tick                                         <br/>
 41      * Time is the percentage of the way through the duration
 42      */
 43     update:function (time) {
 44         var tt = Math.max(0, time - 0.25);
 45         var deltaAy = (tt * tt * 500);
 46         var ay = -100 - deltaAy;
 47 
 48         var deltaTheta = -Math.PI / 2 * Math.sqrt(time);
 49         var theta = /*0.01f */ +Math.PI / 2 + deltaTheta;
 50 
 51         var sinTheta = Math.sin(theta);
 52         var cosTheta = Math.cos(theta);
 53 
 54         var locGridSize = this._gridSize;
 55         var locVer = cc.p(0, 0);
 56         for (var i = 0; i <= locGridSize.width; ++i) {
 57             for (var j = 0; j <= locGridSize.height; ++j) {
 58                 locVer.x = i;
 59                 locVer.y = j;
 60                 // Get original vertex
 61                 var p = this.originalVertex(locVer);
 62 
 63                 var R = Math.sqrt((p.x * p.x) + ((p.y - ay) * (p.y - ay)));
 64                 var r = R * sinTheta;
 65                 var alpha = Math.asin(p.x / R);
 66                 var beta = alpha / sinTheta;
 67                 var cosBeta = Math.cos(beta);
 68 
 69                 // If beta > PI then we've wrapped around the cone
 70                 // Reduce the radius to stop these points interfering with others
 71                 if (beta <= Math.PI)
 72                     p.x = ( r * Math.sin(beta));
 73                 else
 74                     p.x = 0;     //Force X = 0 to stop wrapped points
 75 
 76                 p.y = ( R + ay - ( r * (1 - cosBeta) * sinTheta));
 77 
 78                 // We scale z here to avoid the animation being
 79                 // too much bigger than the screen due to perspectve transform
 80                 p.z = (r * ( 1 - cosBeta ) * cosTheta) / 7;// "100" didn't work for
 81 
 82                 //	Stop z coord from dropping beneath underlying page in a transition
 83                 // issue #751
 84                 if (p.z < 0.5)
 85                     p.z = 0.5;
 86 
 87                 // Set new coords
 88                 this.setVertex(locVer, p);
 89             }
 90         }
 91     }
 92 });
 93 
 94 /**
 95  * create PageTurn3D action
 96  * @function
 97  * @param {Number} duration
 98  * @param {cc.Size} gridSize
 99  * @return {cc.PageTurn3D}
100  */
101 cc.pageTurn3D = function (duration, gridSize) {
102     return new cc.PageTurn3D(duration, gridSize);
103 };
104 /**
105  * Please use cc.pageTurn3D instead
106  * create PageTurn3D action
107  * @param {Number} duration
108  * @param {cc.Size} gridSize
109  * @return {cc.PageTurn3D}
110  * @static
111  * @deprecated
112  */
113 cc.PageTurn3D.create = cc.pageTurn3D;