diff --git a/src/graphics/canvas-layer.js b/src/graphics/canvas-layer.js index d8a36f71..8737a756 100644 --- a/src/graphics/canvas-layer.js +++ b/src/graphics/canvas-layer.js @@ -93,9 +93,21 @@ Crafty._registerLayerTemplate("Canvas", { */ _canvas: null, + events: { + // Respond to init & remove events + "LayerInit": "layerInit", + "LayerRemove": "layerRemove", + // Bind scene rendering (see drawing.js) + "RenderScene": "_render", + // Listen for pixelart changes + "PixelartSet": "_setPixelart", + // Handle viewport modifications + "ViewportResize": "_resize" + }, + // When the system is first created, create the necessary canvas element and initial state // Bind to the necessary events - init: function () { + layerInit: function () { //check if canvas is supported if (!Crafty.support.canvas) { Crafty.trigger("NoCanvas"); @@ -133,7 +145,7 @@ Crafty._registerLayerTemplate("Canvas", { }, // When the system is destroyed, remove related resources - remove: function() { + layerRemove: function() { this._canvas.parentNode.removeChild(this._canvas); }, diff --git a/src/graphics/dom-layer.js b/src/graphics/dom-layer.js index 5860b32f..6382ef0e 100644 --- a/src/graphics/dom-layer.js +++ b/src/graphics/dom-layer.js @@ -24,7 +24,20 @@ Crafty._registerLayerTemplate("DOM", { */ _div: null, - init: function () { + events: { + // Respond to init & remove events + "LayerInit": "layerInit", + "LayerRemove": "layerRemove", + // Bind scene rendering (see drawing.js) + "RenderScene": "_render", + // Listen for pixelart changes + "PixelartSet": "_setPixelart" + // Layers should generally listen for resize events, + // but the DOM layers automatically inherit the stage's dimensions + //"ViewportResize": "_resize" + }, + + layerInit: function () { // Avoid shared state between systems this._changedObjs = []; @@ -38,15 +51,10 @@ Crafty._registerLayerTemplate("DOM", { }, // Cleanup the DOM when the layer is destroyed - remove: function() { + layerRemove: function() { this._div.parentNode.removeChild(this._div); }, - // Layers should generally listen for resize events, - // but the DOM layers automatically inherit the stage's dimensions - _resize: function() { - }, - // Handle whether images should be smoothed or not _setPixelArt: function(enabled) { var style = this._div.style; diff --git a/src/graphics/layers.js b/src/graphics/layers.js index c28204a5..131a39ce 100644 --- a/src/graphics/layers.js +++ b/src/graphics/layers.js @@ -20,24 +20,6 @@ Crafty.extend({ this._drawLayerTemplates[type] = layerTemplate; var common = this._commonLayerProperties; - // merge inits - if (typeof layerTemplate.init === 'function') { - var layerInit = layerTemplate.init; - var commonInit = common.init; - layerTemplate.init = function() { - layerInit.call(this); - commonInit.call(this); - }; - } - // merge removes - if (typeof layerTemplate.remove === 'function') { - var layerRemove = layerTemplate.remove; - var commonRemove = common.remove; - layerTemplate.remove = function() { - layerRemove.call(this); - commonRemove.call(this); - }; - } // add common properties, don't overwrite existing ones for (var key in common) { if (layerTemplate[key]) continue; @@ -63,41 +45,26 @@ Crafty.extend({ // A cached version of the viewport rect _cachedViewportRect: null, - // This init code will be run after any other specific layer init code + init: function() { this._cachedViewportRect = {}; - // Handle viewport modifications - this.uniqueBind("ViewportResize", this._resize); - this.uniqueBind("InvalidateViewport", function () { - this._dirtyViewport = true; - }); + // Trigger layer-specific init code + this.trigger("LayerInit"); - // Bind scene rendering (see drawing.js) - this.uniqueBind("RenderScene", this._render); - - // Set pixelart to current status, and listen for changes - this._setPixelart(Crafty._pixelartEnabled); - this.uniqueBind("PixelartSet", this._setPixelart); + // Handle viewport invalidation + this.uniqueBind("InvalidateViewport", function () { this._dirtyViewport = true; }); + // Set pixelart to current status + this.trigger("PixelartSet", Crafty._pixelartEnabled); Crafty._addDrawLayerInstance(this); }, - // This remove code will be run after any other specific layer remove code + remove: function() { - Crafty._removeDrawLayerInstance(this); - }, + // Trigger layer-specific remove code + this.trigger("LayerRemove"); - _resize: function() { - // per default, do nothing - // specific layer should overwrite this method - }, - _setPixelart: function(enabled) { - // per default, do nothing - // specific layer should overwrite this method - }, - _render: function() { - // per default, render nothing - // specific layer should overwrite this method + Crafty._removeDrawLayerInstance(this); }, // Sort function for rendering in the correct order diff --git a/src/graphics/webgl-layer.js b/src/graphics/webgl-layer.js index b7614834..8b8a6453 100644 --- a/src/graphics/webgl-layer.js +++ b/src/graphics/webgl-layer.js @@ -256,7 +256,19 @@ Crafty._registerLayerTemplate("WebGL", { return this.texture_manager.makeTexture(url, image, repeating); }, - init: function() { + events: { + // Respond to init & remove events + "LayerInit": "layerInit", + "LayerRemove": "layerRemove", + // Bind scene rendering (see drawing.js) + "RenderScene": "_render", + // Listen for pixelart changes + "PixelartSet": "_setPixelart", + // Handle viewport modifications + "ViewportResize": "_resize" + }, + + layerInit: function() { //check if we support webgl is supported if (!Crafty.support.webgl) { @@ -311,7 +323,7 @@ Crafty._registerLayerTemplate("WebGL", { }, // Cleanup the DOM when the system is destroyed - remove: function() { + layerRemove: function() { this._canvas.parentNode.removeChild(this._canvas); },