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</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 /** 40 * Gets the grid object. 41 * @returns {cc.GridBase} 42 */ 43 getGrid: function () { 44 return this.grid; 45 }, 46 47 /** 48 * Set the grid object. 49 * @param {cc.GridBase} grid 50 */ 51 setGrid: function (grid) { 52 this.grid = grid; 53 }, 54 55 /** 56 * Set the target 57 * @param {cc.Node} target 58 */ 59 setTarget: function (target) { 60 //var self = this; 61 //self._target && self.removeChild(self._target); 62 this._target = target; 63 //self.addChild(self._target); 64 }, 65 66 /** <p>"add" logic MUST only be in this method <br/> </p> 67 * 68 * <p>If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.</p> 69 * @function 70 * @param {cc.Node} child A child node 71 * @param {Number} [zOrder=] Z order for drawing priority. Please refer to setZOrder(int) 72 * @param {Number} [tag=] A integer to identify the node easily. Please refer to setTag(int) 73 */ 74 addChild: function (child, zOrder, tag) { 75 cc.Node.prototype.addChild.call(this, child, zOrder, tag); 76 77 if (child && !this._target) 78 this._target = child; 79 }, 80 81 /** 82 * Recursive method that visit its children and draw them 83 */ 84 visit: function () { 85 var self = this; 86 // quick return if not visible 87 if (!self._visible) 88 return; 89 90 var isWebGL = cc._renderType == cc._RENDER_TYPE_WEBGL; 91 var locGrid = self.grid; 92 if (isWebGL && locGrid && locGrid._active) 93 locGrid.beforeDraw(); 94 95 self.transform(); 96 97 var locChildren = this._children; 98 if (locChildren && locChildren.length > 0) { 99 var childLen = locChildren.length; 100 this.sortAllChildren(); 101 // draw children 102 for (i = 0; i < childLen; i++) { 103 var child = locChildren[i]; 104 child && child.visit(); 105 } 106 } 107 108 if (isWebGL && locGrid && locGrid._active) 109 locGrid.afterDraw(self._target); 110 }, 111 112 _transformForWebGL: function () { 113 //optimize performance for javascript 114 var t4x4 = this._transform4x4, topMat4 = cc.current_stack.top; 115 116 // Convert 3x3 into 4x4 matrix 117 //cc.CGAffineToGL(this.nodeToParentTransform(), this._transform4x4.mat); 118 var trans = this.nodeToParentTransform(); 119 var t4x4Mat = t4x4.mat; 120 t4x4Mat[0] = trans.a; 121 t4x4Mat[4] = trans.c; 122 t4x4Mat[12] = trans.tx; 123 t4x4Mat[1] = trans.b; 124 t4x4Mat[5] = trans.d; 125 t4x4Mat[13] = trans.ty; 126 127 // Update Z vertex manually 128 //this._transform4x4.mat[14] = this._vertexZ; 129 t4x4Mat[14] = this._vertexZ; 130 131 //optimize performance for Javascript 132 cc.kmMat4Multiply(topMat4, topMat4, t4x4); // = cc.kmGLMultMatrix(this._transform4x4); 133 134 // XXX: Expensive calls. Camera should be integrated into the cached affine matrix 135 if (this._camera != null && !(this.grid && this.grid.isActive())) { 136 var apx = this._anchorPointInPoints.x, apy = this._anchorPointInPoints.y; 137 var translate = (apx !== 0.0 || apy !== 0.0); 138 if (translate) { 139 if(!cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) { 140 apx = 0 | apx; 141 apy = 0 | apy; 142 } 143 cc.kmGLTranslatef(apx, apy, 0); 144 this._camera.locate(); 145 cc.kmGLTranslatef(-apx, -apy, 0); 146 } else { 147 this._camera.locate(); 148 } 149 } 150 } 151 }); 152 153 var _p = cc.NodeGrid.prototype; 154 if (cc._renderType === cc._RENDER_TYPE_WEBGL) { 155 _p.transform = _p._transformForWebGL; 156 //The parent class method directly from canvas model 157 } 158 // Extended property 159 /** @expose */ 160 _p.grid; 161 /** @expose */ 162 _p.target; 163 cc.defineGetterSetter(_p, "target", null, _p.setTarget); 164 165 166 /** 167 * Creates a NodeGrid. <br /> 168 * Implementation cc.NodeGrid 169 * @deprecated since v3.0 please new cc.NodeGrid instead. 170 * @return {cc.NodeGrid} 171 */ 172 cc.NodeGrid.create = function () { 173 return new cc.NodeGrid(); 174 };