diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/canvas/CanvasRenderer.js b/packages/core/src/renderers/canvas/CanvasRenderer.js index 37ffdce..38a7a44 100644 --- a/packages/core/src/renderers/canvas/CanvasRenderer.js +++ b/packages/core/src/renderers/canvas/CanvasRenderer.js @@ -2,7 +2,6 @@ import CanvasMaskManager from './utils/CanvasMaskManager'; import CanvasRenderTarget from './utils/CanvasRenderTarget'; import mapCanvasBlendModesToPixi from './utils/mapCanvasBlendModesToPixi'; -import { pluginTarget } from '@pixi/utils'; import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants'; import { settings } from '@pixi/settings'; @@ -98,7 +97,7 @@ } } - this.initPlugins(); + this.initPlugins(CanvasRenderer.__plugins); this.blendModes = mapCanvasBlendModesToPixi(); this._activeBlendMode = null; @@ -288,8 +287,6 @@ */ destroy(removeView) { - this.destroyPlugins(); - // call the base destroy super.destroy(removeView); @@ -330,27 +327,30 @@ { this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation); } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.CanvasRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + CanvasRenderer.__plugins = CanvasRenderer.__plugins || {}; + CanvasRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.CanvasRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.CanvasRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(CanvasRenderer); diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/canvas/CanvasRenderer.js b/packages/core/src/renderers/canvas/CanvasRenderer.js index 37ffdce..38a7a44 100644 --- a/packages/core/src/renderers/canvas/CanvasRenderer.js +++ b/packages/core/src/renderers/canvas/CanvasRenderer.js @@ -2,7 +2,6 @@ import CanvasMaskManager from './utils/CanvasMaskManager'; import CanvasRenderTarget from './utils/CanvasRenderTarget'; import mapCanvasBlendModesToPixi from './utils/mapCanvasBlendModesToPixi'; -import { pluginTarget } from '@pixi/utils'; import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants'; import { settings } from '@pixi/settings'; @@ -98,7 +97,7 @@ } } - this.initPlugins(); + this.initPlugins(CanvasRenderer.__plugins); this.blendModes = mapCanvasBlendModesToPixi(); this._activeBlendMode = null; @@ -288,8 +287,6 @@ */ destroy(removeView) { - this.destroyPlugins(); - // call the base destroy super.destroy(removeView); @@ -330,27 +327,30 @@ { this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation); } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.CanvasRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + CanvasRenderer.__plugins = CanvasRenderer.__plugins || {}; + CanvasRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.CanvasRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.CanvasRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(CanvasRenderer); diff --git a/packages/core/src/renderers/webgl/WebGLRenderer.js b/packages/core/src/renderers/webgl/WebGLRenderer.js index c36e138..f57f6ba 100644 --- a/packages/core/src/renderers/webgl/WebGLRenderer.js +++ b/packages/core/src/renderers/webgl/WebGLRenderer.js @@ -1,5 +1,5 @@ import SystemRenderer from '../SystemRenderer'; -import { sayHello, pluginTarget } from '@pixi/utils'; +import { sayHello } from '@pixi/utils'; import MaskSystem from './systems/MaskSystem'; import StencilSystem from './systems/StencilSystem'; import FilterSystem from './systems/FilterSystem'; @@ -107,7 +107,8 @@ .addSystem(RenderTextureSystem, 'renderTexture') .addSystem(BatchSystem, 'batch'); - this.initPlugins(); + this.initPlugins(WebGLRenderer.__plugins); + /** * The options passed in to create a new webgl context. * @@ -292,27 +293,30 @@ // TODO nullify all the managers.. this.gl = null; } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.WebGLRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + WebGLRenderer.__plugins = WebGLRenderer.__plugins || {}; + WebGLRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.WebGLRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.WebGLRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(WebGLRenderer); diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/canvas/CanvasRenderer.js b/packages/core/src/renderers/canvas/CanvasRenderer.js index 37ffdce..38a7a44 100644 --- a/packages/core/src/renderers/canvas/CanvasRenderer.js +++ b/packages/core/src/renderers/canvas/CanvasRenderer.js @@ -2,7 +2,6 @@ import CanvasMaskManager from './utils/CanvasMaskManager'; import CanvasRenderTarget from './utils/CanvasRenderTarget'; import mapCanvasBlendModesToPixi from './utils/mapCanvasBlendModesToPixi'; -import { pluginTarget } from '@pixi/utils'; import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants'; import { settings } from '@pixi/settings'; @@ -98,7 +97,7 @@ } } - this.initPlugins(); + this.initPlugins(CanvasRenderer.__plugins); this.blendModes = mapCanvasBlendModesToPixi(); this._activeBlendMode = null; @@ -288,8 +287,6 @@ */ destroy(removeView) { - this.destroyPlugins(); - // call the base destroy super.destroy(removeView); @@ -330,27 +327,30 @@ { this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation); } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.CanvasRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + CanvasRenderer.__plugins = CanvasRenderer.__plugins || {}; + CanvasRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.CanvasRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.CanvasRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(CanvasRenderer); diff --git a/packages/core/src/renderers/webgl/WebGLRenderer.js b/packages/core/src/renderers/webgl/WebGLRenderer.js index c36e138..f57f6ba 100644 --- a/packages/core/src/renderers/webgl/WebGLRenderer.js +++ b/packages/core/src/renderers/webgl/WebGLRenderer.js @@ -1,5 +1,5 @@ import SystemRenderer from '../SystemRenderer'; -import { sayHello, pluginTarget } from '@pixi/utils'; +import { sayHello } from '@pixi/utils'; import MaskSystem from './systems/MaskSystem'; import StencilSystem from './systems/StencilSystem'; import FilterSystem from './systems/FilterSystem'; @@ -107,7 +107,8 @@ .addSystem(RenderTextureSystem, 'renderTexture') .addSystem(BatchSystem, 'batch'); - this.initPlugins(); + this.initPlugins(WebGLRenderer.__plugins); + /** * The options passed in to create a new webgl context. * @@ -292,27 +293,30 @@ // TODO nullify all the managers.. this.gl = null; } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.WebGLRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + WebGLRenderer.__plugins = WebGLRenderer.__plugins || {}; + WebGLRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.WebGLRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.WebGLRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(WebGLRenderer); diff --git a/packages/utils/src/index.js b/packages/utils/src/index.js index 9e794c5..9152ec1 100644 --- a/packages/utils/src/index.js +++ b/packages/utils/src/index.js @@ -47,12 +47,17 @@ import EventEmitter from 'eventemitter3'; export { EventEmitter }; +/** + * @namespace PIXI.utils.mixins + */ +import * as mixins from './mixins'; +export { mixins }; + export * from './browser'; export * from './color'; export * from './data'; export * from './media'; export * from './network'; -export * from './plugin'; export * from './const'; export * from './deprecation'; diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/canvas/CanvasRenderer.js b/packages/core/src/renderers/canvas/CanvasRenderer.js index 37ffdce..38a7a44 100644 --- a/packages/core/src/renderers/canvas/CanvasRenderer.js +++ b/packages/core/src/renderers/canvas/CanvasRenderer.js @@ -2,7 +2,6 @@ import CanvasMaskManager from './utils/CanvasMaskManager'; import CanvasRenderTarget from './utils/CanvasRenderTarget'; import mapCanvasBlendModesToPixi from './utils/mapCanvasBlendModesToPixi'; -import { pluginTarget } from '@pixi/utils'; import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants'; import { settings } from '@pixi/settings'; @@ -98,7 +97,7 @@ } } - this.initPlugins(); + this.initPlugins(CanvasRenderer.__plugins); this.blendModes = mapCanvasBlendModesToPixi(); this._activeBlendMode = null; @@ -288,8 +287,6 @@ */ destroy(removeView) { - this.destroyPlugins(); - // call the base destroy super.destroy(removeView); @@ -330,27 +327,30 @@ { this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation); } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.CanvasRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + CanvasRenderer.__plugins = CanvasRenderer.__plugins || {}; + CanvasRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.CanvasRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.CanvasRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(CanvasRenderer); diff --git a/packages/core/src/renderers/webgl/WebGLRenderer.js b/packages/core/src/renderers/webgl/WebGLRenderer.js index c36e138..f57f6ba 100644 --- a/packages/core/src/renderers/webgl/WebGLRenderer.js +++ b/packages/core/src/renderers/webgl/WebGLRenderer.js @@ -1,5 +1,5 @@ import SystemRenderer from '../SystemRenderer'; -import { sayHello, pluginTarget } from '@pixi/utils'; +import { sayHello } from '@pixi/utils'; import MaskSystem from './systems/MaskSystem'; import StencilSystem from './systems/StencilSystem'; import FilterSystem from './systems/FilterSystem'; @@ -107,7 +107,8 @@ .addSystem(RenderTextureSystem, 'renderTexture') .addSystem(BatchSystem, 'batch'); - this.initPlugins(); + this.initPlugins(WebGLRenderer.__plugins); + /** * The options passed in to create a new webgl context. * @@ -292,27 +293,30 @@ // TODO nullify all the managers.. this.gl = null; } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.WebGLRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + WebGLRenderer.__plugins = WebGLRenderer.__plugins || {}; + WebGLRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.WebGLRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.WebGLRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(WebGLRenderer); diff --git a/packages/utils/src/index.js b/packages/utils/src/index.js index 9e794c5..9152ec1 100644 --- a/packages/utils/src/index.js +++ b/packages/utils/src/index.js @@ -47,12 +47,17 @@ import EventEmitter from 'eventemitter3'; export { EventEmitter }; +/** + * @namespace PIXI.utils.mixins + */ +import * as mixins from './mixins'; +export { mixins }; + export * from './browser'; export * from './color'; export * from './data'; export * from './media'; export * from './network'; -export * from './plugin'; export * from './const'; export * from './deprecation'; diff --git a/packages/utils/src/mixins.js b/packages/utils/src/mixins.js new file mode 100644 index 0000000..335c54f --- /dev/null +++ b/packages/utils/src/mixins.js @@ -0,0 +1,57 @@ +/** + * Mixes all enumerable properties and methods from a source object to a target object. + * + * @memberof PIXI.utils.mixins + * @function mixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function mixin(target, source) +{ + if (!target || !source) return; + // in ES8/ES2017, this would be really easy: + // Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); + + // get all the enumerable property keys + const keys = Object.keys(source); + + // loop through properties + for (let i = 0; i < keys.length; ++i) + { + const propertyName = keys[i]; + + // Set the property using the property descriptor - this works for accessors and normal value properties + Object.defineProperty(target, propertyName, Object.getOwnPropertyDescriptor(source, propertyName)); + } +} + +const mixins = []; + +/** + * Queues a mixin to be handled towards the end of the initialization of PIXI, so that deprecation + * can take effect. + * + * @memberof PIXI.utils.mixins + * @function delayMixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function delayMixin(target, source) +{ + mixins.push(target, source); +} + +/** + * Handles all mixins queued via delayMixin(). + * + * @memberof PIXI.utils.mixins + * @function performMixins + */ +export function performMixins() +{ + for (let i = 0; i < mixins.length; i += 2) + { + mixin(mixins[i], mixins[i + 1]); + } + mixins.length = 0; +} diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/canvas/CanvasRenderer.js b/packages/core/src/renderers/canvas/CanvasRenderer.js index 37ffdce..38a7a44 100644 --- a/packages/core/src/renderers/canvas/CanvasRenderer.js +++ b/packages/core/src/renderers/canvas/CanvasRenderer.js @@ -2,7 +2,6 @@ import CanvasMaskManager from './utils/CanvasMaskManager'; import CanvasRenderTarget from './utils/CanvasRenderTarget'; import mapCanvasBlendModesToPixi from './utils/mapCanvasBlendModesToPixi'; -import { pluginTarget } from '@pixi/utils'; import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants'; import { settings } from '@pixi/settings'; @@ -98,7 +97,7 @@ } } - this.initPlugins(); + this.initPlugins(CanvasRenderer.__plugins); this.blendModes = mapCanvasBlendModesToPixi(); this._activeBlendMode = null; @@ -288,8 +287,6 @@ */ destroy(removeView) { - this.destroyPlugins(); - // call the base destroy super.destroy(removeView); @@ -330,27 +327,30 @@ { this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation); } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.CanvasRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + CanvasRenderer.__plugins = CanvasRenderer.__plugins || {}; + CanvasRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.CanvasRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.CanvasRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(CanvasRenderer); diff --git a/packages/core/src/renderers/webgl/WebGLRenderer.js b/packages/core/src/renderers/webgl/WebGLRenderer.js index c36e138..f57f6ba 100644 --- a/packages/core/src/renderers/webgl/WebGLRenderer.js +++ b/packages/core/src/renderers/webgl/WebGLRenderer.js @@ -1,5 +1,5 @@ import SystemRenderer from '../SystemRenderer'; -import { sayHello, pluginTarget } from '@pixi/utils'; +import { sayHello } from '@pixi/utils'; import MaskSystem from './systems/MaskSystem'; import StencilSystem from './systems/StencilSystem'; import FilterSystem from './systems/FilterSystem'; @@ -107,7 +107,8 @@ .addSystem(RenderTextureSystem, 'renderTexture') .addSystem(BatchSystem, 'batch'); - this.initPlugins(); + this.initPlugins(WebGLRenderer.__plugins); + /** * The options passed in to create a new webgl context. * @@ -292,27 +293,30 @@ // TODO nullify all the managers.. this.gl = null; } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.WebGLRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + WebGLRenderer.__plugins = WebGLRenderer.__plugins || {}; + WebGLRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.WebGLRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.WebGLRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(WebGLRenderer); diff --git a/packages/utils/src/index.js b/packages/utils/src/index.js index 9e794c5..9152ec1 100644 --- a/packages/utils/src/index.js +++ b/packages/utils/src/index.js @@ -47,12 +47,17 @@ import EventEmitter from 'eventemitter3'; export { EventEmitter }; +/** + * @namespace PIXI.utils.mixins + */ +import * as mixins from './mixins'; +export { mixins }; + export * from './browser'; export * from './color'; export * from './data'; export * from './media'; export * from './network'; -export * from './plugin'; export * from './const'; export * from './deprecation'; diff --git a/packages/utils/src/mixins.js b/packages/utils/src/mixins.js new file mode 100644 index 0000000..335c54f --- /dev/null +++ b/packages/utils/src/mixins.js @@ -0,0 +1,57 @@ +/** + * Mixes all enumerable properties and methods from a source object to a target object. + * + * @memberof PIXI.utils.mixins + * @function mixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function mixin(target, source) +{ + if (!target || !source) return; + // in ES8/ES2017, this would be really easy: + // Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); + + // get all the enumerable property keys + const keys = Object.keys(source); + + // loop through properties + for (let i = 0; i < keys.length; ++i) + { + const propertyName = keys[i]; + + // Set the property using the property descriptor - this works for accessors and normal value properties + Object.defineProperty(target, propertyName, Object.getOwnPropertyDescriptor(source, propertyName)); + } +} + +const mixins = []; + +/** + * Queues a mixin to be handled towards the end of the initialization of PIXI, so that deprecation + * can take effect. + * + * @memberof PIXI.utils.mixins + * @function delayMixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function delayMixin(target, source) +{ + mixins.push(target, source); +} + +/** + * Handles all mixins queued via delayMixin(). + * + * @memberof PIXI.utils.mixins + * @function performMixins + */ +export function performMixins() +{ + for (let i = 0; i < mixins.length; i += 2) + { + mixin(mixins[i], mixins[i + 1]); + } + mixins.length = 0; +} diff --git a/packages/utils/src/plugin/index.js b/packages/utils/src/plugin/index.js deleted file mode 100644 index abc456d..0000000 --- a/packages/utils/src/plugin/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export { default as pluginTarget } from './pluginTarget'; -import * as mixins from './mixins'; -export { mixins }; diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/canvas/CanvasRenderer.js b/packages/core/src/renderers/canvas/CanvasRenderer.js index 37ffdce..38a7a44 100644 --- a/packages/core/src/renderers/canvas/CanvasRenderer.js +++ b/packages/core/src/renderers/canvas/CanvasRenderer.js @@ -2,7 +2,6 @@ import CanvasMaskManager from './utils/CanvasMaskManager'; import CanvasRenderTarget from './utils/CanvasRenderTarget'; import mapCanvasBlendModesToPixi from './utils/mapCanvasBlendModesToPixi'; -import { pluginTarget } from '@pixi/utils'; import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants'; import { settings } from '@pixi/settings'; @@ -98,7 +97,7 @@ } } - this.initPlugins(); + this.initPlugins(CanvasRenderer.__plugins); this.blendModes = mapCanvasBlendModesToPixi(); this._activeBlendMode = null; @@ -288,8 +287,6 @@ */ destroy(removeView) { - this.destroyPlugins(); - // call the base destroy super.destroy(removeView); @@ -330,27 +327,30 @@ { this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation); } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.CanvasRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + CanvasRenderer.__plugins = CanvasRenderer.__plugins || {}; + CanvasRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.CanvasRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.CanvasRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(CanvasRenderer); diff --git a/packages/core/src/renderers/webgl/WebGLRenderer.js b/packages/core/src/renderers/webgl/WebGLRenderer.js index c36e138..f57f6ba 100644 --- a/packages/core/src/renderers/webgl/WebGLRenderer.js +++ b/packages/core/src/renderers/webgl/WebGLRenderer.js @@ -1,5 +1,5 @@ import SystemRenderer from '../SystemRenderer'; -import { sayHello, pluginTarget } from '@pixi/utils'; +import { sayHello } from '@pixi/utils'; import MaskSystem from './systems/MaskSystem'; import StencilSystem from './systems/StencilSystem'; import FilterSystem from './systems/FilterSystem'; @@ -107,7 +107,8 @@ .addSystem(RenderTextureSystem, 'renderTexture') .addSystem(BatchSystem, 'batch'); - this.initPlugins(); + this.initPlugins(WebGLRenderer.__plugins); + /** * The options passed in to create a new webgl context. * @@ -292,27 +293,30 @@ // TODO nullify all the managers.. this.gl = null; } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.WebGLRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + WebGLRenderer.__plugins = WebGLRenderer.__plugins || {}; + WebGLRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.WebGLRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.WebGLRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(WebGLRenderer); diff --git a/packages/utils/src/index.js b/packages/utils/src/index.js index 9e794c5..9152ec1 100644 --- a/packages/utils/src/index.js +++ b/packages/utils/src/index.js @@ -47,12 +47,17 @@ import EventEmitter from 'eventemitter3'; export { EventEmitter }; +/** + * @namespace PIXI.utils.mixins + */ +import * as mixins from './mixins'; +export { mixins }; + export * from './browser'; export * from './color'; export * from './data'; export * from './media'; export * from './network'; -export * from './plugin'; export * from './const'; export * from './deprecation'; diff --git a/packages/utils/src/mixins.js b/packages/utils/src/mixins.js new file mode 100644 index 0000000..335c54f --- /dev/null +++ b/packages/utils/src/mixins.js @@ -0,0 +1,57 @@ +/** + * Mixes all enumerable properties and methods from a source object to a target object. + * + * @memberof PIXI.utils.mixins + * @function mixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function mixin(target, source) +{ + if (!target || !source) return; + // in ES8/ES2017, this would be really easy: + // Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); + + // get all the enumerable property keys + const keys = Object.keys(source); + + // loop through properties + for (let i = 0; i < keys.length; ++i) + { + const propertyName = keys[i]; + + // Set the property using the property descriptor - this works for accessors and normal value properties + Object.defineProperty(target, propertyName, Object.getOwnPropertyDescriptor(source, propertyName)); + } +} + +const mixins = []; + +/** + * Queues a mixin to be handled towards the end of the initialization of PIXI, so that deprecation + * can take effect. + * + * @memberof PIXI.utils.mixins + * @function delayMixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function delayMixin(target, source) +{ + mixins.push(target, source); +} + +/** + * Handles all mixins queued via delayMixin(). + * + * @memberof PIXI.utils.mixins + * @function performMixins + */ +export function performMixins() +{ + for (let i = 0; i < mixins.length; i += 2) + { + mixin(mixins[i], mixins[i + 1]); + } + mixins.length = 0; +} diff --git a/packages/utils/src/plugin/index.js b/packages/utils/src/plugin/index.js deleted file mode 100644 index abc456d..0000000 --- a/packages/utils/src/plugin/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export { default as pluginTarget } from './pluginTarget'; -import * as mixins from './mixins'; -export { mixins }; diff --git a/packages/utils/src/plugin/mixins.js b/packages/utils/src/plugin/mixins.js deleted file mode 100644 index 114de2a..0000000 --- a/packages/utils/src/plugin/mixins.js +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Mixes all enumerable properties and methods from a source object to a target object. - * - * @memberof PIXI.utils.mixins - * @function mixin - * @param {object} target The prototype or instance that properties and methods should be added to. - * @param {object} source The source of properties and methods to mix in. - */ -export function mixin(target, source) -{ - if (!target || !source) return; - // in ES8/ES2017, this would be really easy: - // Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); - - // get all the enumerable property keys - const keys = Object.keys(source); - - // loop through properties - for (let i = 0; i < keys.length; ++i) - { - const propertyName = keys[i]; - - // Set the property using the property descriptor - this works for accessors and normal value properties - Object.defineProperty(target, propertyName, Object.getOwnPropertyDescriptor(source, propertyName)); - } -} - -const mixins = []; - -/** - * Queues a mixin to be handled towards the end of the initialization of PIXI, so that deprecation - * can take effect. - * - * @memberof PIXI.utils.mixins - * @function delayMixin - * @private - * @param {object} target The prototype or instance that properties and methods should be added to. - * @param {object} source The source of properties and methods to mix in. - */ -export function delayMixin(target, source) -{ - mixins.push(target, source); -} - -/** - * Handles all mixins queued via delayMixin(). - * - * @memberof PIXI.utils.mixins - * @function performMixins - * @private - */ -export function performMixins() -{ - for (let i = 0; i < mixins.length; i += 2) - { - mixin(mixins[i], mixins[i + 1]); - } - mixins.length = 0; -} diff --git a/packages/core/src/renderers/SystemRenderer.js b/packages/core/src/renderers/SystemRenderer.js index 5ccb9b5..7f5a7db 100644 --- a/packages/core/src/renderers/SystemRenderer.js +++ b/packages/core/src/renderers/SystemRenderer.js @@ -187,6 +187,27 @@ * @private */ this._lastObjectRendered = this._tempDisplayObjectParent; + + /** + * Collection of plugins. + * @readonly + * @member {object} + */ + this.plugins = {}; + } + + /** + * Initialize the plugins. + * + * @protected + * @param {object} staticMap - The dictionary of staticly saved plugins. + */ + initPlugins(staticMap) + { + for (const o in staticMap) + { + this.plugins[o] = new (staticMap[o])(this); + } } /** @@ -265,11 +286,19 @@ */ destroy(removeView) { + for (const o in this.plugins) + { + this.plugins[o].destroy(); + this.plugins[o] = null; + } + if (removeView && this.view.parentNode) { this.view.parentNode.removeChild(this.view); } + this.plugins = null; + this.type = RENDERER_TYPE.UNKNOWN; this.view = null; diff --git a/packages/core/src/renderers/canvas/CanvasRenderer.js b/packages/core/src/renderers/canvas/CanvasRenderer.js index 37ffdce..38a7a44 100644 --- a/packages/core/src/renderers/canvas/CanvasRenderer.js +++ b/packages/core/src/renderers/canvas/CanvasRenderer.js @@ -2,7 +2,6 @@ import CanvasMaskManager from './utils/CanvasMaskManager'; import CanvasRenderTarget from './utils/CanvasRenderTarget'; import mapCanvasBlendModesToPixi from './utils/mapCanvasBlendModesToPixi'; -import { pluginTarget } from '@pixi/utils'; import { RENDERER_TYPE, SCALE_MODES, BLEND_MODES } from '@pixi/constants'; import { settings } from '@pixi/settings'; @@ -98,7 +97,7 @@ } } - this.initPlugins(); + this.initPlugins(CanvasRenderer.__plugins); this.blendModes = mapCanvasBlendModesToPixi(); this._activeBlendMode = null; @@ -288,8 +287,6 @@ */ destroy(removeView) { - this.destroyPlugins(); - // call the base destroy super.destroy(removeView); @@ -330,27 +327,30 @@ { this._activeBlendMode = this.blendModes.indexOf(this.context.globalCompositeOperation); } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.CanvasRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + CanvasRenderer.__plugins = CanvasRenderer.__plugins || {}; + CanvasRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.CanvasRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.CanvasExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.CanvasPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.CanvasRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(CanvasRenderer); diff --git a/packages/core/src/renderers/webgl/WebGLRenderer.js b/packages/core/src/renderers/webgl/WebGLRenderer.js index c36e138..f57f6ba 100644 --- a/packages/core/src/renderers/webgl/WebGLRenderer.js +++ b/packages/core/src/renderers/webgl/WebGLRenderer.js @@ -1,5 +1,5 @@ import SystemRenderer from '../SystemRenderer'; -import { sayHello, pluginTarget } from '@pixi/utils'; +import { sayHello } from '@pixi/utils'; import MaskSystem from './systems/MaskSystem'; import StencilSystem from './systems/StencilSystem'; import FilterSystem from './systems/FilterSystem'; @@ -107,7 +107,8 @@ .addSystem(RenderTextureSystem, 'renderTexture') .addSystem(BatchSystem, 'batch'); - this.initPlugins(); + this.initPlugins(WebGLRenderer.__plugins); + /** * The options passed in to create a new webgl context. * @@ -292,27 +293,30 @@ // TODO nullify all the managers.. this.gl = null; } + + /** + * Collection of installed plugins. These are included by default in PIXI, but can be excluded + * by creating a custom build. Consult the README for more information about creating custom + * builds and excluding plugins. + * @name PIXI.WebGLRenderer#plugins + * @type {object} + * @readonly + * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. + * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. + * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. + * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. + */ + + /** + * Adds a plugin to the renderer. + * + * @method + * @param {string} pluginName - The name of the plugin. + * @param {Function} ctor - The constructor function or class for the plugin. + */ + static registerPlugin(pluginName, ctor) + { + WebGLRenderer.__plugins = WebGLRenderer.__plugins || {}; + WebGLRenderer.__plugins[pluginName] = ctor; + } } - -/** - * Collection of installed plugins. These are included by default in PIXI, but can be excluded - * by creating a custom build. Consult the README for more information about creating custom - * builds and excluding plugins. - * @name PIXI.WebGLRenderer#plugins - * @type {object} - * @readonly - * @property {PIXI.accessibility.AccessibilityManager} accessibility Support tabbing interactive elements. - * @property {PIXI.extract.WebGLExtract} extract Extract image data from renderer. - * @property {PIXI.interaction.InteractionManager} interaction Handles mouse, touch and pointer events. - * @property {PIXI.prepare.WebGLPrepare} prepare Pre-render display objects. - */ - -/** - * Adds a plugin to the renderer. - * - * @method PIXI.WebGLRenderer#registerPlugin - * @param {string} pluginName - The name of the plugin. - * @param {Function} ctor - The constructor function or class for the plugin. - */ - -pluginTarget.mixin(WebGLRenderer); diff --git a/packages/utils/src/index.js b/packages/utils/src/index.js index 9e794c5..9152ec1 100644 --- a/packages/utils/src/index.js +++ b/packages/utils/src/index.js @@ -47,12 +47,17 @@ import EventEmitter from 'eventemitter3'; export { EventEmitter }; +/** + * @namespace PIXI.utils.mixins + */ +import * as mixins from './mixins'; +export { mixins }; + export * from './browser'; export * from './color'; export * from './data'; export * from './media'; export * from './network'; -export * from './plugin'; export * from './const'; export * from './deprecation'; diff --git a/packages/utils/src/mixins.js b/packages/utils/src/mixins.js new file mode 100644 index 0000000..335c54f --- /dev/null +++ b/packages/utils/src/mixins.js @@ -0,0 +1,57 @@ +/** + * Mixes all enumerable properties and methods from a source object to a target object. + * + * @memberof PIXI.utils.mixins + * @function mixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function mixin(target, source) +{ + if (!target || !source) return; + // in ES8/ES2017, this would be really easy: + // Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); + + // get all the enumerable property keys + const keys = Object.keys(source); + + // loop through properties + for (let i = 0; i < keys.length; ++i) + { + const propertyName = keys[i]; + + // Set the property using the property descriptor - this works for accessors and normal value properties + Object.defineProperty(target, propertyName, Object.getOwnPropertyDescriptor(source, propertyName)); + } +} + +const mixins = []; + +/** + * Queues a mixin to be handled towards the end of the initialization of PIXI, so that deprecation + * can take effect. + * + * @memberof PIXI.utils.mixins + * @function delayMixin + * @param {object} target The prototype or instance that properties and methods should be added to. + * @param {object} source The source of properties and methods to mix in. + */ +export function delayMixin(target, source) +{ + mixins.push(target, source); +} + +/** + * Handles all mixins queued via delayMixin(). + * + * @memberof PIXI.utils.mixins + * @function performMixins + */ +export function performMixins() +{ + for (let i = 0; i < mixins.length; i += 2) + { + mixin(mixins[i], mixins[i + 1]); + } + mixins.length = 0; +} diff --git a/packages/utils/src/plugin/index.js b/packages/utils/src/plugin/index.js deleted file mode 100644 index abc456d..0000000 --- a/packages/utils/src/plugin/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export { default as pluginTarget } from './pluginTarget'; -import * as mixins from './mixins'; -export { mixins }; diff --git a/packages/utils/src/plugin/mixins.js b/packages/utils/src/plugin/mixins.js deleted file mode 100644 index 114de2a..0000000 --- a/packages/utils/src/plugin/mixins.js +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Mixes all enumerable properties and methods from a source object to a target object. - * - * @memberof PIXI.utils.mixins - * @function mixin - * @param {object} target The prototype or instance that properties and methods should be added to. - * @param {object} source The source of properties and methods to mix in. - */ -export function mixin(target, source) -{ - if (!target || !source) return; - // in ES8/ES2017, this would be really easy: - // Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); - - // get all the enumerable property keys - const keys = Object.keys(source); - - // loop through properties - for (let i = 0; i < keys.length; ++i) - { - const propertyName = keys[i]; - - // Set the property using the property descriptor - this works for accessors and normal value properties - Object.defineProperty(target, propertyName, Object.getOwnPropertyDescriptor(source, propertyName)); - } -} - -const mixins = []; - -/** - * Queues a mixin to be handled towards the end of the initialization of PIXI, so that deprecation - * can take effect. - * - * @memberof PIXI.utils.mixins - * @function delayMixin - * @private - * @param {object} target The prototype or instance that properties and methods should be added to. - * @param {object} source The source of properties and methods to mix in. - */ -export function delayMixin(target, source) -{ - mixins.push(target, source); -} - -/** - * Handles all mixins queued via delayMixin(). - * - * @memberof PIXI.utils.mixins - * @function performMixins - * @private - */ -export function performMixins() -{ - for (let i = 0; i < mixins.length; i += 2) - { - mixin(mixins[i], mixins[i + 1]); - } - mixins.length = 0; -} diff --git a/packages/utils/src/plugin/pluginTarget.js b/packages/utils/src/plugin/pluginTarget.js deleted file mode 100644 index c4f7a67..0000000 --- a/packages/utils/src/plugin/pluginTarget.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Mixins functionality to make an object have "plugins". - * - * @example - * function MyObject() {} - * - * pluginTarget.mixin(MyObject); - * - * @function pluginTarget - * @memberof PIXI.utils - * @type {mixin} - * @param {object} obj - The object to mix into. - */ -function pluginTarget(obj) -{ - obj.__plugins = {}; - - /** - * Adds a plugin to an object - * - * @param {string} pluginName - The events that should be listed. - * @param {Function} ctor - The constructor function for the plugin. - */ - obj.registerPlugin = function registerPlugin(pluginName, ctor) - { - obj.__plugins[pluginName] = ctor; - }; - - /** - * Instantiates all the plugins of this object - * - */ - obj.prototype.initPlugins = function initPlugins() - { - this.plugins = this.plugins || {}; - - for (const o in obj.__plugins) - { - this.plugins[o] = new (obj.__plugins[o])(this); - } - }; - - /** - * Removes all the plugins of this object - * - */ - obj.prototype.destroyPlugins = function destroyPlugins() - { - for (const o in this.plugins) - { - this.plugins[o].destroy(); - this.plugins[o] = null; - } - - this.plugins = null; - }; -} - -export default { - /** - * Mixes in the properties of the pluginTarget into another object - * - * @param {object} obj - The obj to mix into - */ - mixin: function mixin(obj) - { - pluginTarget(obj); - }, -};