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     _gridRect:null,
 40 
 41     ctor: function (rect) {
 42         cc.Node.prototype.ctor.call(this);
 43         if(rect === undefined) rect = cc.rect();
 44         this._gridRect = rect;
 45     },
 46     /**
 47      * Gets the grid object.
 48      * @returns {cc.GridBase}
 49      */
 50     getGrid: function () {
 51         return this.grid;
 52     },
 53 
 54     /**
 55      * Set the grid object.
 56      * @param {cc.GridBase} grid
 57      */
 58     setGrid: function (grid) {
 59         this.grid = grid;
 60     },
 61 
 62     /**
 63      * @brief Set the effect grid rect.
 64      * @param {cc.rect} rect.
 65      */
 66     setGridRect: function (rect) {
 67         this._gridRect = rect;
 68     },
 69     /**
 70      * @brief Get the effect grid rect.
 71      * @return {cc.rect} rect.
 72     */
 73     getGridRect: function () {
 74         return this._gridRect;
 75     },
 76 
 77     /**
 78      * Set the target
 79      * @param {cc.Node} target
 80      */
 81     setTarget: function (target) {
 82         this._target = target;
 83     },
 84 
 85     _transformForWebGL: function () {
 86         //optimize performance for javascript
 87         var t4x4 = this._transform4x4, topMat4 = cc.current_stack.top;
 88 
 89         // Convert 3x3 into 4x4 matrix
 90         var trans = this.getNodeToParentTransform();
 91         var t4x4Mat = t4x4.mat;
 92         t4x4Mat[0] = trans.a;
 93         t4x4Mat[4] = trans.c;
 94         t4x4Mat[12] = trans.tx;
 95         t4x4Mat[1] = trans.b;
 96         t4x4Mat[5] = trans.d;
 97         t4x4Mat[13] = trans.ty;
 98 
 99         // Update Z vertex manually
100         //this._transform4x4.mat[14] = this._vertexZ;
101         t4x4Mat[14] = this._vertexZ;
102 
103         //optimize performance for Javascript
104         topMat4.multiply(t4x4) ; // = cc.kmGLMultMatrix(this._transform4x4);
105 
106         // XXX: Expensive calls. Camera should be integrated into the cached affine matrix
107         if (this._camera !== null && !(this.grid && this.grid.isActive())) {
108             var app = this._renderCmd._anchorPointInPoints,
109                 apx = app.x, apy = app.y,
110                 translate = (apx !== 0.0 || apy !== 0.0);
111             if (translate) {
112                 if(!cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) {
113                     apx = 0 | apx;
114                     apy = 0 | apy;
115                 }
116                 cc.kmGLTranslatef(apx, apy, 0);
117                 this._camera.locate();
118                 cc.kmGLTranslatef(-apx, -apy, 0);
119             } else {
120                 this._camera.locate();
121             }
122         }
123     },
124 
125     _createRenderCmd: function(){
126         if (cc._renderType === cc.game.RENDER_TYPE_WEBGL)
127             return new cc.NodeGrid.WebGLRenderCmd(this);
128         else
129             return new cc.Node.CanvasRenderCmd(this);            // cc.NodeGrid doesn't support Canvas mode.
130     }
131 });
132 
133 var _p = cc.NodeGrid.prototype;
134 // Extended property
135 /** @expose */
136 _p.grid;
137 /** @expose */
138 _p.target;
139 cc.defineGetterSetter(_p, "target", null, _p.setTarget);
140 
141 
142 /**
143  * Creates a NodeGrid. <br />
144  * Implementation cc.NodeGrid
145  * @deprecated since v3.0 please new cc.NodeGrid instead.
146  * @return {cc.NodeGrid}
147  */
148 cc.NodeGrid.create = function () {
149     return new cc.NodeGrid();
150 };