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     getGrid: function(){
 40         var result = new cc.Grid3D(this._gridSize, undefined, undefined, this._gridNodeTarget.getGridRect());
 41         result.setNeedDepthTestForBlit(true);
 42         return result;
 43     },
 44 
 45     clone: function(){
 46        var ret = new cc.PageTurn3D();
 47         ret.initWithDuration(this._duration, this._gridSize);
 48         return ret;
 49     },
 50 
 51     /**
 52      * Update each tick                                         <br/>
 53      * Time is the percentage of the way through the duration
 54      */
 55     update:function (time) {
 56         var tt = Math.max(0, time - 0.25);
 57         var deltaAy = (tt * tt * 500);
 58         var ay = -100 - deltaAy;
 59 
 60         var deltaTheta = Math.sqrt(time);
 61         var theta = deltaTheta>0.5?Math.PI/2 *deltaTheta : Math.PI/2*(1-deltaTheta);
 62         var rotateByYAxis = (2-time)*Math.PI;
 63 
 64         var sinTheta = Math.sin(theta);
 65         var cosTheta = Math.cos(theta);
 66 
 67         var locGridSize = this._gridSize;
 68         var locVer = cc.p(0, 0);
 69         for (var i = 0; i <= locGridSize.width; ++i) {
 70             for (var j = 0; j <= locGridSize.height; ++j) {
 71                 locVer.x = i;
 72                 locVer.y = j;
 73                 // Get original vertex
 74                 var p = this.getOriginalVertex(locVer);
 75 
 76                 p.x -= this.getGridRect().x;
 77                 var R = Math.sqrt((p.x * p.x) + ((p.y - ay) * (p.y - ay)));
 78                 var r = R * sinTheta;
 79                 var alpha = Math.asin(p.x / R);
 80                 var beta = alpha / sinTheta;
 81                 var cosBeta = Math.cos(beta);
 82 
 83                 // If beta > PI then we've wrapped around the cone
 84                 // Reduce the radius to stop these points interfering with others
 85                 if (beta <= Math.PI)
 86                     p.x = ( r * Math.sin(beta));
 87                 else
 88                     p.x = 0;     //Force X = 0 to stop wrapped points
 89 
 90                 p.y = ( R + ay - ( r * (1 - cosBeta) * sinTheta));
 91 
 92                 // We scale z here to avoid the animation being
 93                 // too much bigger than the screen due to perspectve transform
 94                 p.z = (r * ( 1 - cosBeta ) * cosTheta);// "100" didn't work for
 95                 p.x = p.z * Math.sin(rotateByYAxis) + p.x * Math.cos(rotateByYAxis);
 96                 p.z = p.z * Math.cos(rotateByYAxis) - p.x * Math.cos(rotateByYAxis);
 97                 p.z/= 7;
 98                 //	Stop z coord from dropping beneath underlying page in a transition
 99                 // issue #751
100                 if (p.z < 0.5)
101                     p.z = 0.5;
102 
103                 // Set new coords
104                 p.x+= this.getGridRect().x;
105                 this.setVertex(locVer, p);
106             }
107         }
108     }
109 });
110 
111 /**
112  * create PageTurn3D action
113  * @function
114  * @param {Number} duration
115  * @param {cc.Size} gridSize
116  * @return {cc.PageTurn3D}
117  */
118 cc.pageTurn3D = function (duration, gridSize) {
119     return new cc.PageTurn3D(duration, gridSize);
120 };
121 /**
122  * Please use cc.pageTurn3D instead
123  * create PageTurn3D action
124  * @param {Number} duration
125  * @param {cc.Size} gridSize
126  * @return {cc.PageTurn3D}
127  * @static
128  * @deprecated since v3.0 please use cc.pageTurn3D instead.
129  */
130 cc.PageTurn3D.create = cc.pageTurn3D;