phaser/wip/components/Color.js

391 lines
7.2 KiB
JavaScript
Raw Normal View History

2016-10-14 07:58:35 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Color Component allows you to control the alpha, blend mode, tint and background color
* of a Game Object.
*
* @class
*/
Phaser.Component.Color = function (gameObject)
{
this.gameObject = gameObject;
2016-11-10 00:10:48 +00:00
this.state = gameObject.state;
2016-10-14 07:58:35 +00:00
this._dirty = false;
this._alpha = 1;
this._worldAlpha = 1;
this._blendMode = 0;
this._tint = { topLeft: 0xffffff, topRight: 0xffffff, bottomLeft: 0xffffff, bottomRight: 0xffffff };
this._glTint = { topLeft: 16777215, topRight: 16777215, bottomLeft: 16777215, bottomRight: 16777215 };
2016-10-14 07:58:35 +00:00
this._hasTint = false;
// Between 0 and 255
2016-10-14 07:58:35 +00:00
this._r = 0;
this._g = 0;
this._b = 0;
// Between 0 and 1
2016-10-14 07:58:35 +00:00
this._a = 1;
// String version of RGBA
2016-10-14 07:58:35 +00:00
this._rgba = '';
2016-10-19 02:58:25 +00:00
// 32-bit version of ARGB
this._glBg = 0;
2016-10-14 07:58:35 +00:00
this._hasBackground = false;
};
Phaser.Component.Color.prototype.constructor = Phaser.Component.Color;
Phaser.Component.Color.prototype = {
setBackground: function (red, green, blue, alpha)
{
if (red === undefined)
{
this._hasBackground = false;
2016-10-19 02:58:25 +00:00
this._glBg = 0;
2016-10-14 07:58:35 +00:00
}
else
{
this._hasBackground = true;
this._r = red;
this._g = (green) ? green : 0;
this._b = (blue) ? blue : 0;
this._a = (alpha) ? alpha : 1;
}
this.dirty = true;
2016-10-14 07:58:35 +00:00
},
2016-10-16 22:51:54 +00:00
clearTint: function ()
{
this.setTint(0xffffff);
2016-10-16 22:51:54 +00:00
this._hasTint = false;
2016-10-16 22:51:54 +00:00
},
setTint: function (topLeft, topRight, bottomLeft, bottomRight)
{
if (topRight === undefined)
{
topRight = topLeft;
bottomLeft = topLeft;
bottomRight = topLeft;
}
this.tintTopLeft = topLeft;
this.tintTopRight = topRight;
this.tintBottomLeft = bottomLeft;
this.tintBottomRight = bottomRight;
2016-10-16 22:51:54 +00:00
this._hasTint = true;
this.dirty = true;
2016-10-14 07:58:35 +00:00
},
// Called by the Dirty Manager
update: function ()
{
this._dirty = false;
if (this._hasBackground)
{
this._rgba = 'rgba(' + this._r + ',' + this._g + ',' + this._b + ',' + this._a + ')';
this._glBg = this.getColor32(this._r, this._g, this._b, this._a);
2016-10-14 07:58:35 +00:00
}
2016-10-19 02:58:25 +00:00
// Tint mults?
2016-10-14 07:58:35 +00:00
},
getColor: function (value)
{
return (value >> 16) + (value & 0xff00) + ((value & 0xff) << 16);
},
getColor32: function (r, g, b, a)
{
a *= 255;
return ((a << 24) | (b << 16) | (g << 8) | r) >>> 0;
},
2016-10-14 07:58:35 +00:00
destroy: function ()
{
this.gameObject = null;
2016-11-10 00:10:48 +00:00
this.state = null;
2016-10-14 07:58:35 +00:00
this._tint = [];
}
};
Object.defineProperties(Phaser.Component.Color.prototype, {
dirty: {
enumerable: true,
get: function ()
{
return this._dirty;
},
set: function (value)
{
if (value)
{
if (!this._dirty)
{
2016-11-10 00:10:48 +00:00
this.state.sys.updates.add(this);
}
this._dirty = true;
}
else
{
this._dirty = false;
}
}
},
tintTopLeft: {
2016-10-16 22:51:54 +00:00
enumerable: true,
get: function ()
{
return this._tint.topLeft;
2016-10-16 22:51:54 +00:00
},
set: function (value)
{
this._tint.topLeft = value;
this._glTint.topLeft = this.getColor(value);
this.dirty = true;
}
},
2016-10-16 22:51:54 +00:00
tintTopRight: {
enumerable: true,
get: function ()
{
return this._tint.topRight;
},
set: function (value)
{
this._tint.topRight = value;
this._glTint.topRight = this.getColor(value);
this.dirty = true;
2016-10-16 22:51:54 +00:00
}
},
tintBottomLeft: {
enumerable: true,
get: function ()
{
return this._tint.bottomLeft;
},
set: function (value)
{
this._tint.bottomLeft = value;
this._glTint.bottomLeft = this.getColor(value);
this.dirty = true;
}
},
tintBottomRight: {
enumerable: true,
get: function ()
{
return this._tint.bottomRight;
},
set: function (value)
{
this._tint.bottomRight = value;
this._glTint.bottomRight = this.getColor(value);
this.dirty = true;
}
},
tint: {
enumerable: true,
get: function ()
{
return this._tint;
},
set: function (value)
{
this.setTint(value, value, value, value);
}
},
2016-10-14 07:58:35 +00:00
alpha: {
enumerable: true,
get: function ()
{
return this._alpha;
},
set: function (value)
{
if (value !== this._alpha)
{
this._alpha = value;
this.dirty = true;
}
}
},
blendMode: {
enumerable: true,
get: function ()
{
return this._blendMode;
},
set: function (value)
{
if (value !== this._blendMode && value >= 0 && value <= 16)
{
this._blendMode = value;
this.dirty = true;
2016-10-14 07:58:35 +00:00
}
}
},
worldAlpha: {
enumerable: true,
get: function ()
{
return this._worldAlpha;
},
set: function (value)
{
this._worldAlpha = this._alpha * value;
}
},
2016-10-14 07:58:35 +00:00
backgroundAlpha: {
enumerable: true,
get: function ()
{
return this._a;
},
set: function (value)
{
if (value !== this._a)
{
this._a = value;
this._hasBackground = true;
this.dirty = true;
2016-10-14 07:58:35 +00:00
}
}
},
red: {
enumerable: true,
get: function ()
{
return this._r;
},
set: function (value)
{
if (value !== this._r)
{
this._r = value | 0;
this._hasBackground = true;
this.dirty = true;
2016-10-14 07:58:35 +00:00
}
}
},
green: {
enumerable: true,
get: function ()
{
return this._g;
},
set: function (value)
{
if (value !== this._g)
{
this._g = value | 0;
this._hasBackground = true;
this.dirty = true;
2016-10-14 07:58:35 +00:00
}
}
},
blue: {
enumerable: true,
get: function ()
{
return this._b;
},
set: function (value)
{
if (value !== this._b)
{
this._b = value | 0;
this._hasBackground = true;
this.dirty = true;
2016-10-14 07:58:35 +00:00
}
}
}
});