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