CCNodeGrid.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * <p>NodeGrid class is a class serves as a decorator of cc.Node,<br/>
  24. * Grid node can run grid actions over all its children</p>
  25. * @type {Class}
  26. *
  27. * @property {cc.GridBase} grid - Grid object that is used when applying effects
  28. * @property {cc.Node} target - <@writeonly>Target
  29. */
  30. cc.NodeGrid = cc.Node.extend({
  31. grid: null,
  32. _target: null,
  33. /**
  34. * Gets the grid object.
  35. * @returns {cc.GridBase}
  36. */
  37. getGrid: function () {
  38. return this.grid;
  39. },
  40. /**
  41. * Set the grid object.
  42. * @param {cc.GridBase} grid
  43. */
  44. setGrid: function (grid) {
  45. this.grid = grid;
  46. },
  47. /**
  48. * Set the target
  49. * @param {cc.Node} target
  50. */
  51. setTarget: function (target) {
  52. //var self = this;
  53. //self._target && self.removeChild(self._target);
  54. this._target = target;
  55. //self.addChild(self._target);
  56. },
  57. /** <p>"add" logic MUST only be in this method <br/> </p>
  58. *
  59. * <p>If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.</p>
  60. * @function
  61. * @param {cc.Node} child A child node
  62. * @param {Number} [zOrder=] Z order for drawing priority. Please refer to setZOrder(int)
  63. * @param {Number} [tag=] A integer to identify the node easily. Please refer to setTag(int)
  64. */
  65. addChild: function (child, zOrder, tag) {
  66. cc.Node.prototype.addChild.call(this, child, zOrder, tag);
  67. if (child && !this._target)
  68. this._target = child;
  69. },
  70. /**
  71. * Recursive method that visit its children and draw them
  72. */
  73. visit: function () {
  74. var self = this;
  75. // quick return if not visible
  76. if (!self._visible)
  77. return;
  78. var isWebGL = cc._renderType == cc._RENDER_TYPE_WEBGL;
  79. var locGrid = self.grid;
  80. if (isWebGL && locGrid && locGrid._active)
  81. locGrid.beforeDraw();
  82. self.transform();
  83. var locChildren = this._children;
  84. if (locChildren && locChildren.length > 0) {
  85. var childLen = locChildren.length;
  86. this.sortAllChildren();
  87. // draw children
  88. for (i = 0; i < childLen; i++) {
  89. var child = locChildren[i];
  90. child && child.visit();
  91. }
  92. }
  93. if (isWebGL && locGrid && locGrid._active)
  94. locGrid.afterDraw(self._target);
  95. },
  96. _transformForWebGL: function () {
  97. //optimize performance for javascript
  98. var t4x4 = this._transform4x4, topMat4 = cc.current_stack.top;
  99. // Convert 3x3 into 4x4 matrix
  100. //cc.CGAffineToGL(this.nodeToParentTransform(), this._transform4x4.mat);
  101. var trans = this.nodeToParentTransform();
  102. var t4x4Mat = t4x4.mat;
  103. t4x4Mat[0] = trans.a;
  104. t4x4Mat[4] = trans.c;
  105. t4x4Mat[12] = trans.tx;
  106. t4x4Mat[1] = trans.b;
  107. t4x4Mat[5] = trans.d;
  108. t4x4Mat[13] = trans.ty;
  109. // Update Z vertex manually
  110. //this._transform4x4.mat[14] = this._vertexZ;
  111. t4x4Mat[14] = this._vertexZ;
  112. //optimize performance for Javascript
  113. cc.kmMat4Multiply(topMat4, topMat4, t4x4); // = cc.kmGLMultMatrix(this._transform4x4);
  114. // XXX: Expensive calls. Camera should be integrated into the cached affine matrix
  115. if (this._camera != null && !(this.grid && this.grid.isActive())) {
  116. var apx = this._anchorPointInPoints.x, apy = this._anchorPointInPoints.y;
  117. var translate = (apx !== 0.0 || apy !== 0.0);
  118. if (translate) {
  119. if(!cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) {
  120. apx = 0 | apx;
  121. apy = 0 | apy;
  122. }
  123. cc.kmGLTranslatef(apx, apy, 0);
  124. this._camera.locate();
  125. cc.kmGLTranslatef(-apx, -apy, 0);
  126. } else {
  127. this._camera.locate();
  128. }
  129. }
  130. }
  131. });
  132. var _p = cc.NodeGrid.prototype;
  133. if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
  134. _p.transform = _p._transformForWebGL;
  135. //The parent class method directly from canvas model
  136. }
  137. // Extended property
  138. /** @expose */
  139. _p.grid;
  140. /** @expose */
  141. _p.target;
  142. cc.defineGetterSetter(_p, "target", null, _p.setTarget);
  143. /**
  144. * Creates a NodeGrid. <br />
  145. * Implementation cc.NodeGrid
  146. * @deprecated since v3.0 please new cc.NodeGrid instead.
  147. * @return {cc.NodeGrid}
  148. */
  149. cc.NodeGrid.create = function () {
  150. return new cc.NodeGrid();
  151. };