Make a Skin
emit an event when it is altered
This allows any `Drawable` using the `Skin` to update its transform as necessary.
This commit is contained in:
parent
ba5deb9936
commit
6a0798062b
@ -77,6 +77,8 @@ class BitmapSkin extends Skin {
|
||||
// Do these last in case any of the above throws an exception
|
||||
this._costumeResolution = costumeResolution || 1;
|
||||
this._textureSize = BitmapSkin._getBitmapSize(bitmapData);
|
||||
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,7 @@ const twgl = require('twgl.js');
|
||||
const Rectangle = require('./Rectangle');
|
||||
const RenderConstants = require('./RenderConstants');
|
||||
const ShaderManager = require('./ShaderManager');
|
||||
const Skin = require('./Skin');
|
||||
|
||||
/**
|
||||
* @callback Drawable~idFilterFunc
|
||||
@ -60,6 +61,8 @@ class Drawable {
|
||||
// TODO: move convex hull functionality, maybe bounds functionality overall, to Skin classes
|
||||
this._convexHullPoints = null;
|
||||
this._convexHullDirty = true;
|
||||
|
||||
this._skinWasAltered = this._skinWasAltered.bind(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,9 +101,14 @@ class Drawable {
|
||||
*/
|
||||
set skin (newSkin) {
|
||||
if (this._skin !== newSkin) {
|
||||
if (this._skin) {
|
||||
this._skin.removeListener(Skin.Events.WasAltered, this._skinWasAltered);
|
||||
}
|
||||
this._skin = newSkin;
|
||||
this.setConvexHullDirty();
|
||||
this.setTransformDirty();
|
||||
if (this._skin) {
|
||||
this._skin.addListener(Skin.Events.WasAltered, this._skinWasAltered);
|
||||
}
|
||||
this._skinWasAltered();
|
||||
}
|
||||
}
|
||||
|
||||
@ -312,6 +320,15 @@ class Drawable {
|
||||
return this.getAABB();
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond to an internal change in the current Skin.
|
||||
* @private
|
||||
*/
|
||||
_skinWasAltered () {
|
||||
this.setConvexHullDirty();
|
||||
this.setTransformDirty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate a color to represent the given ID number. At least one component of
|
||||
* the resulting color will be non-zero if the ID is not RenderConstants.ID_NONE.
|
||||
|
@ -672,10 +672,8 @@ class RenderWebGL {
|
||||
drawable.skin = this._allSkins[properties.skinId];
|
||||
}
|
||||
if ('rotationCenter' in properties) {
|
||||
const centerChanged = drawable.skin.setRotationCenter(properties.rotationCenter);
|
||||
if (centerChanged) {
|
||||
drawable.setTransformDirty();
|
||||
}
|
||||
const newRotationCenter = properties.rotationCenter;
|
||||
drawable.skin.setRotationCenter(newRotationCenter[0], newRotationCenter[1]);
|
||||
}
|
||||
drawable.updateProperties(properties);
|
||||
}
|
||||
|
@ -71,6 +71,7 @@ class SVGSkin extends Skin {
|
||||
|
||||
this._texture = twgl.createTexture(gl, textureOptions);
|
||||
}
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
21
src/Skin.js
21
src/Skin.js
@ -1,13 +1,17 @@
|
||||
const EventEmitter = require('events');
|
||||
|
||||
const twgl = require('twgl.js');
|
||||
|
||||
const RenderConstants = require('./RenderConstants');
|
||||
|
||||
class Skin {
|
||||
class Skin extends EventEmitter {
|
||||
/**
|
||||
* Create a Skin, which stores and/or generates textures for use in rendering.
|
||||
* @param {int} id - The unique ID for this Skin.
|
||||
*/
|
||||
constructor (id) {
|
||||
super();
|
||||
|
||||
/** @type {int} */
|
||||
this._id = id;
|
||||
|
||||
@ -68,15 +72,13 @@ class Skin {
|
||||
* Set the origin, in object space, about which this Skin should rotate.
|
||||
* @param {number} x - The x coordinate of the new rotation center.
|
||||
* @param {number} y - The y coordinate of the new rotation center.
|
||||
* @returns {boolean} true iff the new rotation center differs from the old one.
|
||||
*/
|
||||
setRotationCenter (x, y) {
|
||||
if (x !== this._rotationCenter[0] || y !== this._rotationCenter[1]) {
|
||||
this._rotationCenter[0] = x;
|
||||
this._rotationCenter[1] = y;
|
||||
return true;
|
||||
this.emit(Skin.Events.WasAltered);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,4 +103,15 @@ class Skin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* These are the events which can be emitted by instances of this class.
|
||||
* @type {object.<string,string>}
|
||||
*/
|
||||
Skin.Events = {
|
||||
/**
|
||||
* Emitted when anything about the Skin has been altered, such as the appearance or rotation center.
|
||||
*/
|
||||
WasAltered: 'WasAltered'
|
||||
};
|
||||
|
||||
module.exports = Skin;
|
||||
|
Loading…
x
Reference in New Issue
Block a user