Skip to content

Commit 64838a3

Browse files
committed
New Color component.
1 parent bfd391b commit 64838a3

1 file changed

Lines changed: 209 additions & 0 deletions

File tree

src/components/Color.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2016 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Color Component allows you to control the alpha, blend mode, tint and background color
9+
* of a Game Object.
10+
*
11+
* @class
12+
*/
13+
Phaser.Component.Color = function (gameObject)
14+
{
15+
this.gameObject = gameObject;
16+
17+
this.game = gameObject.game;
18+
19+
this._dirty = false;
20+
21+
this._alpha = 1;
22+
this._worldAlpha = 1;
23+
24+
this._blendMode = 0;
25+
26+
this._tint = [];
27+
this._hasTint = false;
28+
29+
this._r = 0;
30+
this._g = 0;
31+
this._b = 0;
32+
this._a = 1;
33+
this._rgba = '';
34+
this._hasBackground = false;
35+
};
36+
37+
Phaser.Component.Color.prototype.constructor = Phaser.Component.Color;
38+
39+
Phaser.Component.Color.prototype = {
40+
41+
setBackground: function (red, green, blue, alpha)
42+
{
43+
if (red === undefined)
44+
{
45+
this._hasBackground = false;
46+
}
47+
else
48+
{
49+
this._hasBackground = true;
50+
this._r = red;
51+
this._g = (green) ? green : 0;
52+
this._b = (blue) ? blue : 0;
53+
this._a = (alpha) ? alpha : 1;
54+
}
55+
56+
this.setDirty();
57+
},
58+
59+
getBlendMode: function ()
60+
{
61+
return this._blendMode;
62+
},
63+
64+
setBlendMode: function (blendMode)
65+
{
66+
if (blendMode >= 0 && blendMode <= 16)
67+
{
68+
this._blendMode = blendMode;
69+
this.setDirty();
70+
}
71+
},
72+
73+
// Called by the Dirty Manager
74+
update: function ()
75+
{
76+
this._dirty = false;
77+
78+
if (this._hasBackground)
79+
{
80+
this._rgba = 'rgba(' + this._r + ',' + this._g + ',' + this._b + ',' + this._a + ')';
81+
}
82+
},
83+
84+
setDirty: function ()
85+
{
86+
if (!this._dirty)
87+
{
88+
this.game.updates.add(this);
89+
}
90+
91+
this._dirty = true;
92+
},
93+
94+
destroy: function ()
95+
{
96+
this.gameObject = null;
97+
this.game = null;
98+
this._tint = [];
99+
}
100+
101+
};
102+
103+
Object.defineProperties(Phaser.Component.Color.prototype, {
104+
105+
alpha: {
106+
107+
enumerable: true,
108+
109+
get: function ()
110+
{
111+
return this._alpha;
112+
},
113+
114+
set: function (value)
115+
{
116+
if (value !== this._alpha)
117+
{
118+
this._alpha = value;
119+
this.setDirty();
120+
}
121+
}
122+
123+
},
124+
125+
backgroundAlpha: {
126+
127+
enumerable: true,
128+
129+
get: function ()
130+
{
131+
return this._a;
132+
},
133+
134+
set: function (value)
135+
{
136+
if (value !== this._a)
137+
{
138+
this._a = value;
139+
this._hasBackground = true;
140+
this.setDirty();
141+
}
142+
}
143+
144+
},
145+
146+
red: {
147+
148+
enumerable: true,
149+
150+
get: function ()
151+
{
152+
return this._r;
153+
},
154+
155+
set: function (value)
156+
{
157+
if (value !== this._r)
158+
{
159+
this._r = value | 0;
160+
this._hasBackground = true;
161+
this.setDirty();
162+
}
163+
}
164+
165+
},
166+
167+
green: {
168+
169+
enumerable: true,
170+
171+
get: function ()
172+
{
173+
return this._g;
174+
},
175+
176+
set: function (value)
177+
{
178+
if (value !== this._g)
179+
{
180+
this._g = value | 0;
181+
this._hasBackground = true;
182+
this.setDirty();
183+
}
184+
}
185+
186+
},
187+
188+
blue: {
189+
190+
enumerable: true,
191+
192+
get: function ()
193+
{
194+
return this._b;
195+
},
196+
197+
set: function (value)
198+
{
199+
if (value !== this._b)
200+
{
201+
this._b = value | 0;
202+
this._hasBackground = true;
203+
this.setDirty();
204+
}
205+
}
206+
207+
}
208+
209+
});

0 commit comments

Comments
 (0)