1 /****************************************************************************
  2  Copyright (c) 2010-2014 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>NodeGrid class is a class serves as a decorator of cc.Node,<br/>
 29  * Grid node can run grid actions over all its children</p>
 30  * @type {Class}
 31  *
 32  * @property {cc.GridBase}  grid    - Grid object that is used when applying effects
 33  * @property {cc.Node}      target  - <@writeonly>Target
 34  */
 35 cc.NodeGrid = cc.Node.extend({
 36     grid: null,
 37     _target: null,
 38 
 39     getGrid: function () {
 40         return this.grid;
 41     },
 42 
 43     setGrid: function (grid) {
 44         this.grid = grid;
 45     },
 46 
 47     setTarget: function (target) {
 48         //var self = this;
 49         //self._target && self.removeChild(self._target);
 50         this._target = target;
 51         //self.addChild(self._target);
 52     },
 53 
 54     addChild: function (child, zOrder, tag) {
 55         cc.Node.prototype.addChild.call(this, child, zOrder, tag);
 56 
 57         if (child && !this._target)
 58             this._target = child;
 59     },
 60 
 61     visit: function () {
 62         var self = this;
 63         // quick return if not visible
 64         if (!self._visible)
 65             return;
 66 
 67         var isWebGL = cc._renderType == cc._RENDER_TYPE_WEBGL;
 68         var locGrid = self.grid;
 69         if (isWebGL && locGrid && locGrid._active)
 70             locGrid.beforeDraw();
 71 
 72         self.transform();
 73 
 74         var locChildren = this._children;
 75         if (locChildren && locChildren.length > 0) {
 76             var childLen = locChildren.length;
 77             this.sortAllChildren();
 78             // draw children
 79             for (i = 0; i < childLen; i++) {
 80                 var child = locChildren[i];
 81                 child && child.visit();
 82             }
 83         }
 84 
 85         if (isWebGL && locGrid && locGrid._active)
 86             locGrid.afterDraw(self._target);
 87     },
 88 
 89     _transformForWebGL: function () {
 90         //optimize performance for javascript
 91         var t4x4 = this._transform4x4, topMat4 = cc.current_stack.top;
 92 
 93         // Convert 3x3 into 4x4 matrix
 94         //cc.CGAffineToGL(this.nodeToParentTransform(), this._transform4x4.mat);
 95         var trans = this.nodeToParentTransform();
 96         var t4x4Mat = t4x4.mat;
 97         t4x4Mat[0] = trans.a;
 98         t4x4Mat[4] = trans.c;
 99         t4x4Mat[12] = trans.tx;
100         t4x4Mat[1] = trans.b;
101         t4x4Mat[5] = trans.d;
102         t4x4Mat[13] = trans.ty;
103 
104         // Update Z vertex manually
105         //this._transform4x4.mat[14] = this._vertexZ;
106         t4x4Mat[14] = this._vertexZ;
107 
108         //optimize performance for Javascript
109         cc.kmMat4Multiply(topMat4, topMat4, t4x4); // = cc.kmGLMultMatrix(this._transform4x4);
110 
111         // XXX: Expensive calls. Camera should be integrated into the cached affine matrix
112         if (this._camera != null && !(this.grid && this.grid.isActive())) {
113             var apx = this._anchorPointInPoints.x, apy = this._anchorPointInPoints.y;
114             var translate = (apx !== 0.0 || apy !== 0.0);
115             if (translate) {
116                 if(!cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) {
117                     apx = 0 | apx;
118                     apy = 0 | apy;
119                 }
120                 cc.kmGLTranslatef(apx, apy, 0);
121                 this._camera.locate();
122                 cc.kmGLTranslatef(-apx, -apy, 0);
123             } else {
124                 this._camera.locate();
125             }
126         }
127     }
128 });
129 
130 var _p = cc.NodeGrid.prototype;
131 if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
132     _p.transform = _p._transformForWebGL;
133     //The parent class method directly from canvas model
134 }
135 // Extended property
136 /** @expose */
137 _p.grid;
138 /** @expose */
139 _p.target;
140 cc.defineGetterSetter(_p, "target", null, _p.setTarget);
141 
142 
143 /**
144  * Creates a NodeGrid
145  * Implementation cc.NodeGrid
146  * @return {cc.NodeGrid|null}
147  */
148 cc.NodeGrid.create = function () {
149     return new cc.NodeGrid();
150 };