forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlpha.js
More file actions
289 lines (244 loc) · 6.94 KB
/
Copy pathAlpha.js
File metadata and controls
289 lines (244 loc) · 6.94 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Clamp = require('../../math/Clamp');
// bitmask flag for GameObject.renderMask
var _FLAG = 2; // 0010
/**
* Provides methods used for setting the alpha properties of a Game Object.
* Should be applied as a mixin and not used directly.
*
* @namespace Phaser.GameObjects.Components.Alpha
* @since 3.0.0
*/
var Alpha = {
/**
* Private internal value. Holds the global alpha value.
*
* @name Phaser.GameObjects.Components.Alpha#_alpha
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
_alpha: 1,
/**
* Private internal value. Holds the top-left alpha value.
*
* @name Phaser.GameObjects.Components.Alpha#_alphaTL
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
_alphaTL: 1,
/**
* Private internal value. Holds the top-right alpha value.
*
* @name Phaser.GameObjects.Components.Alpha#_alphaTR
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
_alphaTR: 1,
/**
* Private internal value. Holds the bottom-left alpha value.
*
* @name Phaser.GameObjects.Components.Alpha#_alphaBL
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
_alphaBL: 1,
/**
* Private internal value. Holds the bottom-right alpha value.
*
* @name Phaser.GameObjects.Components.Alpha#_alphaBR
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
_alphaBR: 1,
/**
* Clears all alpha values associated with this Game Object.
*
* Immediately sets the alpha levels back to 1 (fully opaque).
*
* @method Phaser.GameObjects.Components.Alpha#clearAlpha
* @since 3.0.0
*
* @return {this} This Game Object instance.
*/
clearAlpha: function ()
{
return this.setAlpha(1);
},
/**
* Set the Alpha level of this Game Object. The alpha controls the opacity of the Game Object as it renders.
* Alpha values are provided as a float between 0, fully transparent, and 1, fully opaque.
*
* If your game is running under WebGL you can optionally specify four different alpha values, each of which
* correspond to the four corners of the Game Object. Under Canvas only the `topLeft` value given is used.
*
* @method Phaser.GameObjects.Components.Alpha#setAlpha
* @since 3.0.0
*
* @param {number} [topLeft=1] - The alpha value used for the top-left of the Game Object. If this is the only value given it's applied across the whole Game Object.
* @param {number} [topRight] - The alpha value used for the top-right of the Game Object. WebGL only.
* @param {number} [bottomLeft] - The alpha value used for the bottom-left of the Game Object. WebGL only.
* @param {number} [bottomRight] - The alpha value used for the bottom-right of the Game Object. WebGL only.
*
* @return {this} This Game Object instance.
*/
setAlpha: function (topLeft, topRight, bottomLeft, bottomRight)
{
if (topLeft === undefined) { topLeft = 1; }
// Treat as if there is only one alpha value for the whole Game Object
if (topRight === undefined)
{
this.alpha = topLeft;
}
else
{
this._alphaTL = Clamp(topLeft, 0, 1);
this._alphaTR = Clamp(topRight, 0, 1);
this._alphaBL = Clamp(bottomLeft, 0, 1);
this._alphaBR = Clamp(bottomRight, 0, 1);
}
return this;
},
/**
* The alpha value of the Game Object.
*
* This is a global value, impacting the entire Game Object, not just a region of it.
*
* @name Phaser.GameObjects.Components.Alpha#alpha
* @type {number}
* @since 3.0.0
*/
alpha: {
get: function ()
{
return this._alpha;
},
set: function (value)
{
var v = Clamp(value, 0, 1);
this._alpha = v;
this._alphaTL = v;
this._alphaTR = v;
this._alphaBL = v;
this._alphaBR = v;
if (v === 0)
{
this.renderFlags &= ~_FLAG;
}
else
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The alpha value starting from the top-left of the Game Object.
* This value is interpolated from the corner to the center of the Game Object.
*
* @name Phaser.GameObjects.Components.Alpha#alphaTopLeft
* @type {number}
* @webglOnly
* @since 3.0.0
*/
alphaTopLeft: {
get: function ()
{
return this._alphaTL;
},
set: function (value)
{
var v = Clamp(value, 0, 1);
this._alphaTL = v;
if (v !== 0)
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The alpha value starting from the top-right of the Game Object.
* This value is interpolated from the corner to the center of the Game Object.
*
* @name Phaser.GameObjects.Components.Alpha#alphaTopRight
* @type {number}
* @webglOnly
* @since 3.0.0
*/
alphaTopRight: {
get: function ()
{
return this._alphaTR;
},
set: function (value)
{
var v = Clamp(value, 0, 1);
this._alphaTR = v;
if (v !== 0)
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The alpha value starting from the bottom-left of the Game Object.
* This value is interpolated from the corner to the center of the Game Object.
*
* @name Phaser.GameObjects.Components.Alpha#alphaBottomLeft
* @type {number}
* @webglOnly
* @since 3.0.0
*/
alphaBottomLeft: {
get: function ()
{
return this._alphaBL;
},
set: function (value)
{
var v = Clamp(value, 0, 1);
this._alphaBL = v;
if (v !== 0)
{
this.renderFlags |= _FLAG;
}
}
},
/**
* The alpha value starting from the bottom-right of the Game Object.
* This value is interpolated from the corner to the center of the Game Object.
*
* @name Phaser.GameObjects.Components.Alpha#alphaBottomRight
* @type {number}
* @webglOnly
* @since 3.0.0
*/
alphaBottomRight: {
get: function ()
{
return this._alphaBR;
},
set: function (value)
{
var v = Clamp(value, 0, 1);
this._alphaBR = v;
if (v !== 0)
{
this.renderFlags |= _FLAG;
}
}
}
};
module.exports = Alpha;