From ab603ffa928a6f3eacf24edaee7fc810221160e9 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Wed, 5 Jun 2019 10:04:14 -0400 Subject: [PATCH] push skin alteration down from renderwebgl --- src/Drawable.js | 9 --------- src/RenderWebGL.js | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Drawable.js b/src/Drawable.js index 1d90a29..9426ebf 100644 --- a/src/Drawable.js +++ b/src/Drawable.js @@ -3,7 +3,6 @@ const twgl = require('twgl.js'); const Rectangle = require('./Rectangle'); const RenderConstants = require('./RenderConstants'); const ShaderManager = require('./ShaderManager'); -const Skin = require('./Skin'); const EffectTransform = require('./EffectTransform'); /** @@ -101,8 +100,6 @@ 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); } /** @@ -141,13 +138,7 @@ class Drawable { */ set skin (newSkin) { if (this._skin !== newSkin) { - if (this._skin) { - this._skin.removeListener(Skin.Events.WasAltered, this._skinWasAltered); - } this._skin = newSkin; - if (this._skin) { - this._skin.addListener(Skin.Events.WasAltered, this._skinWasAltered); - } this._skinWasAltered(); } } diff --git a/src/RenderWebGL.js b/src/RenderWebGL.js index 4782455..b0f2145 100644 --- a/src/RenderWebGL.js +++ b/src/RenderWebGL.js @@ -3,6 +3,7 @@ const EventEmitter = require('events'); const hull = require('hull.js'); const twgl = require('twgl.js'); +const Skin = require('./Skin'); const BitmapSkin = require('./BitmapSkin'); const Drawable = require('./Drawable'); const Rectangle = require('./Rectangle'); @@ -290,6 +291,20 @@ class RenderWebGL extends EventEmitter { this.emit(RenderConstants.Events.NativeSizeChanged, {newSize: this._nativeSize}); } + /** + * Notify Drawables whose skin is the skin that changed. + * @param {Skin} skin - the skin that changed. + * @private + */ + _skinWasAltered (skin) { + for (let i = 0; i < this._allDrawables.length; i++) { + const drawable = this._allDrawables[i]; + if (drawable && drawable._skin === skin) { + drawable._skinWasAltered(); + } + } + } + /** * Create a new bitmap skin from a snapshot of the provided bitmap data. * @param {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement} bitmapData - new contents for this skin. @@ -302,6 +317,7 @@ class RenderWebGL extends EventEmitter { const skinId = this._nextSkinId++; const newSkin = new BitmapSkin(skinId, this); newSkin.setBitmap(bitmapData, costumeResolution, rotationCenter); + newSkin.addListener(Skin.Events.WasAltered, this._skinWasAltered.bind(this, newSkin)); this._allSkins[skinId] = newSkin; return skinId; } @@ -317,6 +333,7 @@ class RenderWebGL extends EventEmitter { const skinId = this._nextSkinId++; const newSkin = new SVGSkin(skinId, this); newSkin.setSVG(svgData, rotationCenter); + newSkin.addListener(Skin.Events.WasAltered, this._skinWasAltered.bind(this, newSkin)); this._allSkins[skinId] = newSkin; return skinId; } @@ -328,6 +345,7 @@ class RenderWebGL extends EventEmitter { createPenSkin () { const skinId = this._nextSkinId++; const newSkin = new PenSkin(skinId, this); + newSkin.addListener(Skin.Events.WasAltered, this._skinWasAltered.bind(this, newSkin)); this._allSkins[skinId] = newSkin; return skinId; } @@ -344,6 +362,7 @@ class RenderWebGL extends EventEmitter { const skinId = this._nextSkinId++; const newSkin = new TextBubbleSkin(skinId, this); newSkin.setTextBubble(type, text, pointsLeft); + newSkin.addListener(Skin.Events.WasAltered, this._skinWasAltered.bind(this, newSkin)); this._allSkins[skinId] = newSkin; return skinId; }