2012-09-04 12:10:16 +00:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.rendering");
|
|
|
|
|
2012-09-08 20:26:17 +00:00
|
|
|
|
2012-09-08 13:34:13 +00:00
|
|
|
ns.FrameRenderer = function (container, renderingOptions, className) {
|
2012-09-08 20:26:17 +00:00
|
|
|
|
|
|
|
this.defaultRenderingOptions = {
|
|
|
|
"gridStrokeWidth" : 0,
|
|
|
|
"gridStrokeColor" : "lightgray"
|
|
|
|
};
|
|
|
|
renderingOptions = $.extend(true, {}, this.defaultRenderingOptions, renderingOptions);
|
|
|
|
|
2012-09-08 13:08:00 +00:00
|
|
|
if(container == undefined) {
|
|
|
|
throw "Bad FrameRenderer initialization. <container> undefined.";
|
|
|
|
}
|
2012-09-08 20:26:17 +00:00
|
|
|
|
|
|
|
if(isNaN(renderingOptions.dpi)) {
|
2012-09-08 10:24:55 +00:00
|
|
|
throw "Bad FrameRenderer initialization. <dpi> not well defined.";
|
|
|
|
}
|
|
|
|
|
2012-09-08 20:26:17 +00:00
|
|
|
this.container = container;
|
2012-09-08 13:34:13 +00:00
|
|
|
this.dpi = renderingOptions.dpi;
|
2012-09-08 13:08:00 +00:00
|
|
|
this.className = className;
|
2012-09-08 20:26:17 +00:00
|
|
|
this.canvas = null;
|
|
|
|
console.log(renderingOptions)
|
|
|
|
this.gridStrokeWidth = renderingOptions.gridStrokeWidth;
|
|
|
|
this.gridStrokeColor = renderingOptions.gridStrokeColor;
|
2012-09-08 14:32:28 +00:00
|
|
|
|
|
|
|
// Flag to know if the config was altered
|
|
|
|
this.canvasConfigDirty = true;
|
2012-09-08 13:08:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.init = function (frame) {
|
|
|
|
this.render(frame);
|
2012-09-08 10:24:55 +00:00
|
|
|
};
|
|
|
|
|
2012-09-08 14:32:28 +00:00
|
|
|
ns.FrameRenderer.prototype.updateDPI = function (newDPI) {
|
|
|
|
this.dpi = newDPI;
|
|
|
|
this.canvasConfigDirty = true;
|
|
|
|
};
|
|
|
|
|
2012-09-08 13:08:00 +00:00
|
|
|
ns.FrameRenderer.prototype.render = function (frame) {
|
2012-09-04 12:10:16 +00:00
|
|
|
for(var col = 0, width = frame.getWidth(); col < width; col++) {
|
|
|
|
for(var row = 0, height = frame.getHeight(); row < height; row++) {
|
2012-09-08 20:26:17 +00:00
|
|
|
this.drawPixel(col, row, frame, this.getCanvas_(frame, col, row), this.dpi);
|
2012-09-04 12:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-08 13:08:00 +00:00
|
|
|
ns.FrameRenderer.prototype.drawPixel = function (col, row, frame) {
|
2012-09-08 20:26:17 +00:00
|
|
|
var context = this.getCanvas_(frame, col, row).getContext('2d');
|
2012-09-04 12:10:16 +00:00
|
|
|
var color = frame.getPixel(col, row);
|
|
|
|
if(color == Constants.TRANSPARENT_COLOR) {
|
2012-09-08 20:26:17 +00:00
|
|
|
context.clearRect(this.getFrameY_(col), this.getFrameY_(row), this.dpi, this.dpi);
|
2012-09-04 12:10:16 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(color != Constants.SELECTION_TRANSPARENT_COLOR) {
|
|
|
|
// TODO(vincz): Found a better design to update the palette, it's called too frequently.
|
|
|
|
$.publish(Events.COLOR_USED, [color]);
|
|
|
|
}
|
|
|
|
context.fillStyle = color;
|
2012-09-08 20:26:17 +00:00
|
|
|
context.fillRect(this.getFrameY_(col), this.getFrameY_(row), this.dpi, this.dpi);
|
2012-09-04 12:10:16 +00:00
|
|
|
}
|
|
|
|
};
|
2012-09-08 13:08:00 +00:00
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.clear = function (col, row, frame) {
|
2012-09-08 20:26:17 +00:00
|
|
|
var canvas = this.getCanvas_(frame, col, row)
|
2012-09-08 14:32:28 +00:00
|
|
|
canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
|
2012-09-08 13:08:00 +00:00
|
|
|
};
|
|
|
|
|
2012-09-08 20:26:17 +00: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
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.getFrameX_ = function(col) {
|
|
|
|
return col * this.dpi + ((col - 1) * this.gridStrokeWidth);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.getFrameY_ = function(row) {
|
|
|
|
return row * this.dpi + ((row - 1) * this.gridStrokeWidth);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.drawGrid_ = function(canvas, width, height, col, row) {
|
|
|
|
var ctx = canvas.getContext("2d");
|
|
|
|
ctx.lineWidth = this.gridStrokeWidth;
|
|
|
|
ctx.strokeStyle = this.gridStrokeColor;
|
|
|
|
for(var c=1; c < col; c++) {
|
|
|
|
ctx.moveTo(this.getFrameX_(c), 0);
|
|
|
|
ctx.lineTo(this.getFrameX_(c), height);
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var r=1; r < row; r++) {
|
|
|
|
ctx.moveTo(0, this.getFrameY_(r));
|
|
|
|
ctx.lineTo(width, this.getFrameY_(r));
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-08 13:08:00 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-08 14:32:28 +00:00
|
|
|
ns.FrameRenderer.prototype.getCanvas_ = function (frame) {
|
|
|
|
if(this.canvasConfigDirty) {
|
|
|
|
$(this.canvas).remove();
|
2012-09-08 20:26:17 +00:00
|
|
|
var col = frame.getWidth(),
|
|
|
|
row = frame.getHeight();
|
2012-09-08 14:32:28 +00:00
|
|
|
|
2012-09-08 13:08:00 +00:00
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
|
2012-09-08 20:26:17 +00:00
|
|
|
var pixelWidth = col * this.dpi + this.gridStrokeWidth * (col - 1);
|
|
|
|
var pixelHeight = row * this.dpi + this.gridStrokeWidth * (row - 1);
|
|
|
|
canvas.setAttribute("width", pixelWidth);
|
|
|
|
canvas.setAttribute("height", pixelHeight);
|
2012-09-08 13:08:00 +00:00
|
|
|
var canvasClassname = "canvas";
|
|
|
|
if(this.className) {
|
|
|
|
canvasClassname += " " + this.className;
|
|
|
|
}
|
|
|
|
canvas.setAttribute("class", canvasClassname);
|
2012-09-08 20:26:17 +00:00
|
|
|
this.container.append(canvas);
|
|
|
|
|
|
|
|
if(this.gridStrokeWidth > 0) {
|
|
|
|
this.drawGrid_(canvas, pixelWidth, pixelHeight, col, row);
|
|
|
|
}
|
2012-09-08 13:08:00 +00:00
|
|
|
|
2012-09-08 20:26:17 +00:00
|
|
|
|
2012-09-08 13:08:00 +00:00
|
|
|
this.canvas = canvas;
|
2012-09-08 14:32:28 +00:00
|
|
|
this.canvasConfigDirty = false;
|
2012-09-08 13:08:00 +00:00
|
|
|
}
|
2012-09-08 14:32:28 +00:00
|
|
|
return this.canvas;
|
2012-09-08 13:08:00 +00:00
|
|
|
};
|
2012-09-04 06:35:58 +00:00
|
|
|
})();
|