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>NodeGrid class is a class serves as a decorator of cc.Node,<br/>
 29  * Grid node can run grid actions over all its children   (WebGL only)
 30  * </p>
 31  * @type {Class}
 32  *
 33  * @property {cc.GridBase}  grid    - Grid object that is used when applying effects
 34  * @property {cc.Node}      target  - <@writeonly>Target
 35  */
 36 cc.NodeGrid = cc.Node.extend({
 37     grid: null,
 38     _target: null,
 39 
 40     /**
 41      * Gets the grid object.
 42      * @returns {cc.GridBase}
 43      */
 44     getGrid: function () {
 45         return this.grid;
 46     },
 47 
 48     /**
 49      * Set the grid object.
 50      * @param {cc.GridBase} grid
 51      */
 52     setGrid: function (grid) {
 53         this.grid = grid;
 54     },
 55 
 56     /**
 57      * Set the target
 58      * @param {cc.Node} target
 59      */
 60     setTarget: function (target) {
 61         this._target = target;
 62     },
 63 
 64     _transformForWebGL: function () {
 65         //optimize performance for javascript
 66         var t4x4 = this._transform4x4, topMat4 = cc.current_stack.top;
 67 
 68         // Convert 3x3 into 4x4 matrix
 69         var trans = this.getNodeToParentTransform();
 70         var t4x4Mat = t4x4.mat;
 71         t4x4Mat[0] = trans.a;
 72         t4x4Mat[4] = trans.c;
 73         t4x4Mat[12] = trans.tx;
 74         t4x4Mat[1] = trans.b;
 75         t4x4Mat[5] = trans.d;
 76         t4x4Mat[13] = trans.ty;
 77 
 78         // Update Z vertex manually
 79         //this._transform4x4.mat[14] = this._vertexZ;
 80         t4x4Mat[14] = this._vertexZ;
 81 
 82         //optimize performance for Javascript
 83         topMat4.multiply(t4x4) ; // = cc.kmGLMultMatrix(this._transform4x4);
 84 
 85         // XXX: Expensive calls. Camera should be integrated into the cached affine matrix
 86         if (this._camera !== null && !(this.grid && this.grid.isActive())) {
 87             var app = this._renderCmd._anchorPointInPoints,
 88                 apx = app.x, apy = app.y,
 89                 translate = (apx !== 0.0 || apy !== 0.0);
 90             if (translate) {
 91                 if(!cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) {
 92                     apx = 0 | apx;
 93                     apy = 0 | apy;
 94                 }
 95                 cc.kmGLTranslatef(apx, apy, 0);
 96                 this._camera.locate();
 97                 cc.kmGLTranslatef(-apx, -apy, 0);
 98             } else {
 99                 this._camera.locate();
100             }
101         }
102     },
103 
104     _createRenderCmd: function(){
105         if (cc._renderType === cc._RENDER_TYPE_WEBGL)
106             return new cc.NodeGrid.WebGLRenderCmd(this);
107         else
108             return new cc.Node.CanvasRenderCmd(this);            // cc.NodeGrid doesn't support Canvas mode.
109     }
110 });
111 
112 var _p = cc.NodeGrid.prototype;
113 // Extended property
114 /** @expose */
115 _p.grid;
116 /** @expose */
117 _p.target;
118 cc.defineGetterSetter(_p, "target", null, _p.setTarget);
119 
120 
121 /**
122  * Creates a NodeGrid. <br />
123  * Implementation cc.NodeGrid
124  * @deprecated since v3.0 please new cc.NodeGrid instead.
125  * @return {cc.NodeGrid}
126  */
127 cc.NodeGrid.create = function () {
128     return new cc.NodeGrid();
129 };