CCProtectedNode.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /****************************************************************************
  2. Copyright (c) 2013-2014 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. /**
  21. * A class inhert from cc.Node, use for saving some protected children in other list.
  22. * @class
  23. * @extends cc.Node
  24. */
  25. cc.ProtectedNode = cc.Node.extend(/** @lends cc.ProtectedNode# */{
  26. _protectedChildren: null,
  27. _reorderProtectedChildDirty: false,
  28. _insertProtectedChild: function(child, z){
  29. this._reorderProtectedChildDirty = true;
  30. this._protectedChildren.push(child);
  31. child._setLocalZOrder(z);
  32. },
  33. /**
  34. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  35. * @function
  36. */
  37. ctor: function(){
  38. cc.Node.prototype.ctor.call(this);
  39. this._protectedChildren = [];
  40. },
  41. /**
  42. * <p>
  43. * Adds a child to the container with z order and tag <br/>
  44. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately. <br/>
  45. * </p>
  46. * @param {cc.Node} child A child node
  47. * @param {Number} [localZOrder] Z order for drawing priority. Please refer to `setLocalZOrder(int)`
  48. * @param {Number} [tag] An integer to identify the node easily. Please refer to `setTag(int)`
  49. */
  50. addProtectedChild: function(child, localZOrder, tag){
  51. cc.assert(child != null, "child must be non-nil");
  52. cc.assert(!child.parent, "child already added. It can't be added again");
  53. localZOrder = localZOrder || child.getLocalZOrder();
  54. if(tag)
  55. child.setTag(tag);
  56. this._insertProtectedChild(child, localZOrder);
  57. child.setParent(this);
  58. child.setOrderOfArrival(cc.s_globalOrderOfArrival);
  59. //TODO USE PHYSICS
  60. if(this._running){
  61. child.onEnter();
  62. // prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
  63. if(this._isTransitionFinished)
  64. child.onEnterTransitionDidFinish();
  65. }
  66. if(this._cascadeColorEnabled)
  67. this._enableCascadeColor();
  68. if (this._cascadeOpacityEnabled)
  69. this._enableCascadeOpacity();
  70. },
  71. /**
  72. * Gets a child from the container with its tag
  73. * @param {Number} tag An identifier to find the child node.
  74. * @return {cc.Node} a Node object whose tag equals to the input parameter
  75. */
  76. getProtectedChildByTag: function(tag){
  77. cc.assert(tag != cc.NODE_TAG_INVALID, "Invalid tag");
  78. var locChildren = this._protectedChildren;
  79. for(var i = 0, len = locChildren.length; i < len; i++)
  80. if(locChildren.getTag() == tag)
  81. return locChildren[i];
  82. return null;
  83. },
  84. /**
  85. * Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
  86. * @param {cc.Node} child The child node which will be removed.
  87. * @param {Boolean} [cleanup=true] true if all running actions and callbacks on the child node will be cleanup, false otherwise.
  88. */
  89. removeProtectedChild: function(child, cleanup){
  90. if(cleanup == null)
  91. cleanup = true;
  92. var locChildren = this._protectedChildren;
  93. if(locChildren.length === 0)
  94. return;
  95. var idx = locChildren.indexOf(child);
  96. if(idx > -1){
  97. if(this._running){
  98. child.onExitTransitionDidStart();
  99. child.onExit();
  100. }
  101. //TODO USE PHYSICS
  102. // If you don't do cleanup, the child's actions will not get removed and the
  103. // its scheduledSelectors_ dict will not get released!
  104. if (cleanup)
  105. child.cleanup();
  106. // set parent nil at the end
  107. child.setParent(null);
  108. locChildren.splice(idx, 1);
  109. }
  110. },
  111. /**
  112. * Removes a child from the container by tag value. <br/>
  113. * It will also cleanup all running actions depending on the cleanup parameter
  114. * @param {Number} tag
  115. * @param {Boolean} [cleanup=true]
  116. */
  117. removeProtectedChildByTag: function(tag, cleanup){
  118. cc.assert( tag != cc.NODE_TAG_INVALID, "Invalid tag");
  119. if(cleanup == null)
  120. cleanup = true;
  121. var child = this.getProtectedChildByTag(tag);
  122. if (child == null)
  123. cc.log("cocos2d: removeChildByTag(tag = %d): child not found!", tag);
  124. else
  125. this.removeProtectedChild(child, cleanup);
  126. },
  127. /**
  128. * Removes all children from the container with a cleanup.
  129. * @see cc.ProtectedNode#removeAllProtectedChildrenWithCleanup
  130. */
  131. removeAllProtectedChildren: function(){
  132. this.removeAllProtectedChildrenWithCleanup(true);
  133. },
  134. /**
  135. * Removes all children from the container, and do a cleanup to all running actions depending on the cleanup parameter.
  136. * @param {Boolean} [cleanup=true] true if all running actions on all children nodes should be cleanup, false otherwise.
  137. */
  138. removeAllProtectedChildrenWithCleanup: function(cleanup){
  139. if(cleanup == null)
  140. cleanup = true;
  141. var locChildren = this._protectedChildren;
  142. // not using detachChild improves speed here
  143. for (var i = 0, len = locChildren.length; i< len; i++) {
  144. var child = locChildren[i];
  145. // IMPORTANT:
  146. // -1st do onExit
  147. // -2nd cleanup
  148. if(this._running){
  149. child.onExitTransitionDidStart();
  150. child.onExit();
  151. }
  152. //TODO USE PHYSICS
  153. if (cleanup)
  154. child.cleanup();
  155. // set parent nil at the end
  156. child.setParent(null);
  157. }
  158. locChildren.length = 0;
  159. },
  160. /**
  161. * Reorders a child according to a new z value.
  162. * @param {cc.Node} child An already added child node. It MUST be already added.
  163. * @param {Number} localZOrder Z order for drawing priority. Please refer to setLocalZOrder(int)
  164. */
  165. reorderProtectedChild: function(child, localZOrder){
  166. cc.assert( child != null, "Child must be non-nil");
  167. this._reorderProtectedChildDirty = true;
  168. child.setOrderOfArrival(cc.s_globalOrderOfArrival++);
  169. child._setLocalZOrder(localZOrder);
  170. },
  171. /**
  172. * <p>
  173. * Sorts the children array once before drawing, instead of every time when a child is added or reordered. <br/>
  174. * This approach can improves the performance massively. <br/>
  175. * @note Don't call this manually unless a child added needs to be removed in the same frame
  176. * </p>
  177. */
  178. sortAllProtectedChildren: function(){
  179. if (this._reorderProtectedChildDirty) {
  180. var _children = this._protectedChildren;
  181. // insertion sort
  182. var len = _children.length, i, j, tmp;
  183. for(i=1; i<len; i++){
  184. tmp = _children[i];
  185. j = i - 1;
  186. //continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
  187. while(j >= 0){
  188. if(tmp._localZOrder < _children[j]._localZOrder){
  189. _children[j+1] = _children[j];
  190. }else if(tmp._localZOrder === _children[j]._localZOrder && tmp.arrivalOrder < _children[j].arrivalOrder){
  191. _children[j+1] = _children[j];
  192. }else
  193. break;
  194. j--;
  195. }
  196. _children[j+1] = tmp;
  197. }
  198. //don't need to check children recursively, that's done in visit of each child
  199. this._reorderProtectedChildDirty = false;
  200. }
  201. },
  202. /**
  203. * transforms and draws itself, and visit its children and protected children.
  204. * @override
  205. * @function
  206. * @param {CanvasRenderingContext2D|WebGLRenderingContext} ctx context of renderer
  207. */
  208. visit: null,
  209. _visitForCanvas: function(ctx){
  210. var _t = this;
  211. // quick return if not visible
  212. if (!_t._visible)
  213. return;
  214. //visit for canvas
  215. var context = ctx || cc._renderContext, i, j;
  216. var children = _t._children, child;
  217. var locChildren = _t._children, locProtectedChildren = this._protectedChildren;
  218. var childLen = locChildren.length, pLen = locProtectedChildren.length;
  219. context.save();
  220. _t.transform(context);
  221. _t.sortAllChildren();
  222. _t.sortAllProtectedChildren();
  223. // draw children zOrder < 0
  224. for (i = 0; i < childLen; i++) {
  225. child = children[i];
  226. if (child._localZOrder < 0)
  227. child.visit(context);
  228. else
  229. break;
  230. }
  231. for (j = 0; j < pLen; j++) {
  232. child = locProtectedChildren[j];
  233. if (child._localZOrder < 0)
  234. child.visit(context);
  235. else
  236. break;
  237. }
  238. _t.draw(context);
  239. for (; i < childLen; i++)
  240. children[i] && children[i].visit(context);
  241. for (; j < pLen; j++)
  242. locProtectedChildren[j] && locProtectedChildren[j].visit(context);
  243. this._cacheDirty = false;
  244. _t.arrivalOrder = 0;
  245. context.restore();
  246. },
  247. _visitForWebGL: function(){
  248. var _t = this;
  249. // quick return if not visible
  250. if (!_t._visible)
  251. return;
  252. var context = cc._renderContext, i, currentStack = cc.current_stack, j;
  253. //optimize performance for javascript
  254. currentStack.stack.push(currentStack.top);
  255. cc.kmMat4Assign(_t._stackMatrix, currentStack.top);
  256. currentStack.top = _t._stackMatrix;
  257. var locGrid = _t.grid;
  258. if (locGrid && locGrid._active)
  259. locGrid.beforeDraw();
  260. _t.transform();
  261. var locChildren = _t._children, locProtectedChildren = this._protectedChildren;
  262. var childLen = locChildren.length, pLen = locProtectedChildren.length;
  263. _t.sortAllChildren();
  264. _t.sortAllProtectedChildren();
  265. // draw children zOrder < 0
  266. for (i = 0; i < childLen; i++) {
  267. if (locChildren[i] && locChildren[i]._localZOrder < 0)
  268. locChildren[i].visit();
  269. else
  270. break;
  271. }
  272. for(j = 0; j < pLen; j++){
  273. if (locProtectedChildren[j] && locProtectedChildren[j]._localZOrder < 0)
  274. locProtectedChildren[j].visit();
  275. else
  276. break;
  277. }
  278. _t.draw(context);
  279. // draw children zOrder >= 0
  280. for (; i < childLen; i++) {
  281. locChildren[i] && locChildren[i].visit();
  282. }
  283. for (; j < pLen; j++) {
  284. locProtectedChildren[j] && locProtectedChildren[j].visit();
  285. }
  286. _t.arrivalOrder = 0;
  287. if (locGrid && locGrid._active)
  288. locGrid.afterDraw(_t);
  289. //optimize performance for javascript
  290. currentStack.top = currentStack.stack.pop();
  291. },
  292. /**
  293. * Stops itself and its children and protected children's all running actions and schedulers
  294. * @override
  295. */
  296. cleanup: function(){
  297. cc.Node.prototype.cleanup.call(this);
  298. var locChildren = this._protectedChildren;
  299. for(var i = 0 , len = locChildren.length; i < len; i++)
  300. locChildren[i].cleanup();
  301. },
  302. /**
  303. * Calls its parent's onEnter and calls its protected children's onEnter
  304. * @override
  305. */
  306. onEnter: function(){
  307. cc.Node.prototype.onEnter.call(this);
  308. var locChildren = this._protectedChildren;
  309. for(var i = 0, len = locChildren.length;i< len;i++)
  310. locChildren[i].onEnter();
  311. },
  312. /**
  313. * <p>
  314. * Event callback that is invoked when the Node enters in the 'stage'. <br/>
  315. * If the Node enters the 'stage' with a transition, this event is called when the transition finishes. <br/>
  316. * If you override onEnterTransitionDidFinish, you shall call its parent's one, e.g. Node::onEnterTransitionDidFinish()
  317. * </p>
  318. * @override
  319. */
  320. onEnterTransitionDidFinish: function(){
  321. cc.Node.prototype.onEnterTransitionDidFinish.call(this);
  322. var locChildren = this._protectedChildren;
  323. for(var i = 0, len = locChildren.length;i< len;i++)
  324. locChildren[i].onEnterTransitionDidFinish();
  325. },
  326. /**
  327. * Calls its parent's onExit and calls its protected children's onExit
  328. * @override
  329. */
  330. onExit:function(){
  331. cc.Node.prototype.onExit.call(this);
  332. var locChildren = this._protectedChildren;
  333. for(var i = 0, len = locChildren.length;i< len;i++)
  334. locChildren[i].onExit();
  335. },
  336. /**
  337. * <p>
  338. * Event callback that is called every time the Node leaves the 'stage'. <br/>
  339. * If the Node leaves the 'stage' with a transition, this callback is called when the transition starts.
  340. * </p>
  341. */
  342. onExitTransitionDidStart: function(){
  343. cc.Node.prototype.onExitTransitionDidStart.call(this);
  344. var locChildren = this._protectedChildren;
  345. for(var i = 0, len = locChildren.length;i< len;i++)
  346. locChildren[i].onExitTransitionDidStart();
  347. },
  348. /**
  349. * Updates itself and its protected children displayed opacity, if opacity cascade is enable, its children also update.
  350. * @param {Number} parentOpacity
  351. * @override
  352. */
  353. updateDisplayedOpacity: function(parentOpacity){
  354. this._displayedOpacity = this._realOpacity * parentOpacity/255.0;
  355. this._updateColor();
  356. var i,len, locChildren, _opacity = this._displayedOpacity;
  357. if (this._cascadeOpacityEnabled){
  358. locChildren = this._children;
  359. for(i = 0, len = locChildren.length;i < len; i++){
  360. if(locChildren[i].updateDisplayedOpacity)
  361. locChildren[i].updateDisplayedOpacity(_opacity);
  362. }
  363. }
  364. locChildren = this._protectedChildren;
  365. for(i = 0, len = locChildren.length;i < len; i++){
  366. if(locChildren[i])
  367. locChildren[i].updateDisplayedOpacity(_opacity);
  368. }
  369. },
  370. /**
  371. * Updates itself and its protected children displayed color, if opacity cascade is enable, its children also update.
  372. * @param {cc.Color} parentColor
  373. * @override
  374. */
  375. updateDisplayedColor: function(parentColor){
  376. var displayedColor = this._displayedColor, realColor = this._realColor;
  377. displayedColor.r = realColor.r * parentColor.r/255.0;
  378. displayedColor.g = realColor.g * parentColor.g/255.0;
  379. displayedColor.b = realColor.b * parentColor.b/255.0;
  380. this._updateColor();
  381. var i, len, locChildren;
  382. if (this._cascadeColorEnabled){
  383. locChildren = this._children;
  384. for(i = 0, len = locChildren.length; i < len; i++){
  385. if(locChildren[i].updateDisplayedColor)
  386. locChildren[i].updateDisplayedColor(displayedColor);
  387. }
  388. }
  389. locChildren = this._protectedChildren;
  390. for(i =0, len = locChildren.length; i < len; i++) {
  391. if (locChildren[i])
  392. locChildren[i].updateDisplayedColor(displayedColor);
  393. }
  394. },
  395. _disableCascadeOpacity: function () {
  396. this._displayedOpacity = this._realOpacity;
  397. var selChildren = this._children, i, item;
  398. for (i = 0; i < selChildren.length; i++) {
  399. item = selChildren[i];
  400. if (item)
  401. item.updateDisplayedOpacity(255);
  402. }
  403. selChildren = this._protectedChildren;
  404. for (i = 0; i < selChildren.length; i++) {
  405. item = selChildren[i];
  406. if (item)
  407. item.updateDisplayedOpacity(255);
  408. }
  409. },
  410. _disableCascadeColor: function(){
  411. var white = cc.color.WHITE;
  412. var i, len, locChildren = this._children;
  413. for(i = 0, len = locChildren.length; i < len; i++)
  414. locChildren[i].updateDisplayedColor(white);
  415. locChildren = this._protectedChildren;
  416. for(i =0, len = locChildren.length; i < len; i++)
  417. locChildren[i].setColor(white);
  418. }
  419. });
  420. if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
  421. cc.ProtectedNode.prototype.visit = cc.ProtectedNode.prototype._visitForCanvas;
  422. }else{
  423. cc.ProtectedNode.prototype.visit = cc.ProtectedNode.prototype._visitForWebGL;
  424. }
  425. /**
  426. * create a cc.ProtectedNode object;
  427. * @deprecated since v3.0, please use new cc.ProtectedNode() instead.
  428. * @return cc.ProtectedNode
  429. */
  430. cc.ProtectedNode.create = function(){
  431. return new cc.ProtectedNode();
  432. };