Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor common layer code #1130

Merged
merged 1 commit into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/graphics/canvas-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
},

Expand Down
22 changes: 15 additions & 7 deletions src/graphics/dom-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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;
Expand Down
55 changes: 11 additions & 44 deletions src/graphics/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/graphics/webgl-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
},

Expand Down