2012-09-04 16:10:16 +04:00
|
|
|
(function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.rendering");
|
2013-06-18 01:22:30 +04:00
|
|
|
|
2013-09-30 01:14:10 +04:00
|
|
|
/**
|
|
|
|
* FrameRenderer will display a given frame inside a canvas element.
|
|
|
|
* @param {HtmlElement} container HtmlElement to use as parentNode of the Frame
|
|
|
|
* @param {Object} renderingOptions
|
|
|
|
* @param {Array} classes array of strings to use for css classes
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer = function (container, renderingOptions, classes) {
|
2013-08-10 14:11:16 +04:00
|
|
|
this.defaultRenderingOptions = {
|
|
|
|
'supportGridRendering' : false
|
2013-06-18 01:22:30 +04:00
|
|
|
};
|
2013-08-10 14:11:16 +04:00
|
|
|
renderingOptions = $.extend(true, {}, this.defaultRenderingOptions, renderingOptions);
|
|
|
|
|
|
|
|
if(container === undefined) {
|
|
|
|
throw 'Bad FrameRenderer initialization. <container> undefined.';
|
|
|
|
}
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if(isNaN(renderingOptions.dpi)) {
|
|
|
|
throw 'Bad FrameRenderer initialization. <dpi> not well defined.';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.container = container;
|
2013-09-30 01:14:10 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.dpi = renderingOptions.dpi;
|
|
|
|
this.supportGridRendering = renderingOptions.supportGridRendering;
|
|
|
|
|
2013-09-30 01:14:10 +04:00
|
|
|
this.classes = classes || [];
|
|
|
|
this.classes.push('canvas');
|
|
|
|
|
|
|
|
this.canvas = null;
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.enableGrid(pskl.UserSettings.get(pskl.UserSettings.SHOW_GRID));
|
|
|
|
|
|
|
|
// Flag to know if the config was altered
|
|
|
|
this.canvasConfigDirty = true;
|
|
|
|
this.updateBackgroundClass_(pskl.UserSettings.get(pskl.UserSettings.CANVAS_BACKGROUND));
|
|
|
|
$.subscribe(Events.USER_SETTINGS_CHANGED, $.proxy(this.onUserSettingsChange_, this));
|
|
|
|
};
|
|
|
|
|
2013-10-01 00:44:02 +04:00
|
|
|
ns.FrameRenderer.prototype.setDPI = function (dpi) {
|
|
|
|
this.dpi = dpi;
|
2013-08-10 14:11:16 +04:00
|
|
|
this.canvasConfigDirty = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.onUserSettingsChange_ = function (evt, settingName, settingValue) {
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if(settingName == pskl.UserSettings.SHOW_GRID) {
|
|
|
|
this.enableGrid(settingValue);
|
|
|
|
}
|
|
|
|
else if (settingName == pskl.UserSettings.CANVAS_BACKGROUND) {
|
|
|
|
this.updateBackgroundClass_(settingValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.updateBackgroundClass_ = function (newClass) {
|
|
|
|
var currentClass = this.container.data('current-background-class');
|
|
|
|
if (currentClass) {
|
|
|
|
this.container.removeClass(currentClass);
|
2013-09-29 01:52:51 +04:00
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
this.container.addClass(newClass);
|
|
|
|
this.container.data('current-background-class', newClass);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.enableGrid = function (flag) {
|
|
|
|
this.gridStrokeWidth = (flag && this.supportGridRendering) ? Constants.GRID_STROKE_WIDTH : 0;
|
|
|
|
this.canvasConfigDirty = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.render = function (frame) {
|
2013-09-26 09:47:11 +04:00
|
|
|
if (frame) {
|
2013-09-27 00:43:45 +04:00
|
|
|
this.clear();
|
2013-09-26 09:47:11 +04:00
|
|
|
var context = this.getCanvas_(frame).getContext('2d');
|
|
|
|
for(var col = 0, width = frame.getWidth(); col < width; col++) {
|
|
|
|
for(var row = 0, height = frame.getHeight(); row < height; row++) {
|
|
|
|
var color = frame.getPixel(col, row);
|
|
|
|
this.renderPixel_(color, col, row, context);
|
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
2013-09-26 09:47:11 +04:00
|
|
|
this.lastRenderedFrame = frame;
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.renderPixel_ = function (color, col, row, context) {
|
|
|
|
if(color != Constants.TRANSPARENT_COLOR) {
|
|
|
|
context.fillStyle = color;
|
|
|
|
context.fillRect(this.getFramePos_(col), this.getFramePos_(row), this.dpi, this.dpi);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-26 23:53:37 +04:00
|
|
|
ns.FrameRenderer.prototype.clear = function () {
|
|
|
|
if (this.canvas) {
|
|
|
|
this.canvas.getContext("2d").clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform a screen pixel-based coordinate (relative to the top-left corner of the rendered
|
|
|
|
* frame) into a sprite coordinate in column and row.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.convertPixelCoordinatesIntoSpriteCoordinate = function(coords) {
|
|
|
|
var cellSize = this.dpi + this.gridStrokeWidth;
|
|
|
|
return {
|
|
|
|
"col" : (coords.x - coords.x % cellSize) / cellSize,
|
|
|
|
"row" : (coords.y - coords.y % cellSize) / cellSize
|
2013-06-18 01:22:30 +04:00
|
|
|
};
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.getFramePos_ = function(index) {
|
|
|
|
return index * this.dpi + ((index - 1) * this.gridStrokeWidth);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.getCanvas_ = function (frame) {
|
|
|
|
if(this.canvasConfigDirty) {
|
|
|
|
$(this.canvas).remove();
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var col = frame.getWidth(),
|
|
|
|
row = frame.getHeight();
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var pixelWidth = col * this.dpi + this.gridStrokeWidth * (col - 1);
|
|
|
|
var pixelHeight = row * this.dpi + this.gridStrokeWidth * (row - 1);
|
|
|
|
|
2013-09-30 01:14:10 +04:00
|
|
|
var canvas = pskl.CanvasUtils.createCanvas(pixelWidth, pixelHeight, this.classes);
|
2013-08-10 14:11:16 +04:00
|
|
|
this.container.append(canvas);
|
|
|
|
|
|
|
|
this.canvas = canvas;
|
|
|
|
this.canvasConfigDirty = false;
|
|
|
|
}
|
|
|
|
return this.canvas;
|
|
|
|
};
|
2012-09-04 10:35:58 +04:00
|
|
|
})();
|