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 Copyright (c) 2012 Pierre-David Bélanger 6 7 http://www.cocos2d-x.org 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 THE SOFTWARE. 26 ****************************************************************************/ 27 28 /** 29 * the value of stencil bits. 30 * @type Number 31 */ 32 cc.stencilBits = -1; 33 34 /** 35 * <p> 36 * cc.ClippingNode is a subclass of cc.Node. <br/> 37 * It draws its content (children) clipped using a stencil. <br/> 38 * The stencil is an other cc.Node that will not be drawn. <br/> 39 * The clipping is done using the alpha part of the stencil (adjusted with an alphaThreshold). 40 * </p> 41 * @class 42 * @extends cc.Node 43 * @param {cc.Node} [stencil=null] 44 * 45 * @property {Number} alphaThreshold - Threshold for alpha value. 46 * @property {Boolean} inverted - Indicate whether in inverted mode. 47 * @property {cc.Node} stencil - he cc.Node to use as a stencil to do the clipping. 48 */ 49 cc.ClippingNode = cc.Node.extend(/** @lends cc.ClippingNode# */{ 50 alphaThreshold: 0, 51 inverted: false, 52 53 _stencil: null, 54 _className: "ClippingNode", 55 56 /** 57 * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. 58 * @param {cc.Node} [stencil=null] 59 */ 60 ctor: function (stencil) { 61 stencil = stencil || null; 62 cc.Node.prototype.ctor.call(this); 63 this._stencil = stencil; 64 this.alphaThreshold = 1; 65 this.inverted = false; 66 this._renderCmd.initStencilBits(); 67 }, 68 69 /** 70 * Initialization of the node, please do not call this function by yourself, you should pass the parameters to constructor to initialize it . 71 * @function 72 * @param {cc.Node} [stencil=null] 73 */ 74 init: function (stencil) { 75 this._stencil = stencil; 76 this.alphaThreshold = 1; 77 this.inverted = false; 78 this._renderCmd.initStencilBits(); 79 return true; 80 }, 81 82 /** 83 * <p> 84 * Event callback that is invoked every time when node enters the 'stage'. <br/> 85 * If the CCNode enters the 'stage' with a transition, this event is called when the transition starts. <br/> 86 * During onEnter you can't access a "sister/brother" node. <br/> 87 * If you override onEnter, you must call its parent's onEnter function with this._super(). 88 * </p> 89 * @function 90 */ 91 onEnter: function () { 92 cc.Node.prototype.onEnter.call(this); 93 this._stencil.onEnter(); 94 }, 95 96 /** 97 * <p> 98 * Event callback that is invoked when the node enters in the 'stage'. <br/> 99 * If the node enters the 'stage' with a transition, this event is called when the transition finishes. <br/> 100 * If you override onEnterTransitionDidFinish, you shall call its parent's onEnterTransitionDidFinish with this._super() 101 * </p> 102 * @function 103 */ 104 onEnterTransitionDidFinish: function () { 105 cc.Node.prototype.onEnterTransitionDidFinish.call(this); 106 this._stencil.onEnterTransitionDidFinish(); 107 }, 108 109 /** 110 * <p> 111 * callback that is called every time the node leaves the 'stage'. <br/> 112 * If the node leaves the 'stage' with a transition, this callback is called when the transition starts. <br/> 113 * If you override onExitTransitionDidStart, you shall call its parent's onExitTransitionDidStart with this._super() 114 * </p> 115 * @function 116 */ 117 onExitTransitionDidStart: function () { 118 this._stencil.onExitTransitionDidStart(); 119 cc.Node.prototype.onExitTransitionDidStart.call(this); 120 }, 121 122 /** 123 * <p> 124 * callback that is called every time the node leaves the 'stage'. <br/> 125 * If the node leaves the 'stage' with a transition, this callback is called when the transition finishes. <br/> 126 * During onExit you can't access a sibling node. <br/> 127 * If you override onExit, you shall call its parent's onExit with this._super(). 128 * </p> 129 * @function 130 */ 131 onExit: function () { 132 this._stencil.onExit(); 133 cc.Node.prototype.onExit.call(this); 134 }, 135 136 /** 137 * <p> 138 * The alpha threshold. <br/> 139 * The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold. <br/> 140 * Should be a float between 0 and 1. <br/> 141 * This default to 1 (so alpha test is disabled). 142 * </P> 143 * @return {Number} 144 */ 145 getAlphaThreshold: function () { 146 return this.alphaThreshold; 147 }, 148 149 /** 150 * set alpha threshold. 151 * @param {Number} alphaThreshold 152 */ 153 setAlphaThreshold: function (alphaThreshold) { 154 this.alphaThreshold = alphaThreshold; 155 }, 156 157 /** 158 * <p> 159 * Inverted. If this is set to YES, <br/> 160 * the stencil is inverted, so the content is drawn where the stencil is NOT drawn. <br/> 161 * This default to NO. 162 * </p> 163 * @return {Boolean} 164 */ 165 isInverted: function () { 166 return this.inverted; 167 }, 168 169 /** 170 * set whether or not invert of stencil 171 * @param {Boolean} inverted 172 */ 173 setInverted: function (inverted) { 174 this.inverted = inverted; 175 }, 176 177 /** 178 * The cc.Node to use as a stencil to do the clipping. <br/> 179 * The stencil node will be retained. This default to nil. 180 * @return {cc.Node} 181 */ 182 getStencil: function () { 183 return this._stencil; 184 }, 185 186 /** 187 * Set stencil. 188 * @function 189 * @param {cc.Node} stencil 190 */ 191 setStencil: function (stencil) { 192 if(this._stencil === stencil) 193 return; 194 this._renderCmd.setStencil(stencil); 195 }, 196 197 _createRenderCmd: function(){ 198 if(cc._renderType === cc.game.RENDER_TYPE_CANVAS) 199 return new cc.ClippingNode.CanvasRenderCmd(this); 200 else 201 return new cc.ClippingNode.WebGLRenderCmd(this); 202 } 203 }); 204 205 var _p = cc.ClippingNode.prototype; 206 207 // Extended properties 208 cc.defineGetterSetter(_p, "stencil", _p.getStencil, _p.setStencil); 209 /** @expose */ 210 _p.stencil; 211 212 /** 213 * Creates and initializes a clipping node with an other node as its stencil. <br/> 214 * The stencil node will be retained. 215 * @deprecated since v3.0, please use "new cc.ClippingNode(stencil)" instead 216 * @param {cc.Node} [stencil=null] 217 * @return {cc.ClippingNode} 218 * @example 219 * //example 220 * new cc.ClippingNode(stencil); 221 */ 222 cc.ClippingNode.create = function (stencil) { 223 return new cc.ClippingNode(stencil); 224 }; 225