2012-09-04 12:10:16 +00:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.rendering");
|
|
|
|
|
2012-09-08 13:34:13 +00:00
|
|
|
ns.FrameRenderer = function (container, renderingOptions, className) {
|
2012-09-08 13:08:00 +00:00
|
|
|
if(container == undefined) {
|
|
|
|
throw "Bad FrameRenderer initialization. <container> undefined.";
|
|
|
|
}
|
2012-09-08 13:34:13 +00:00
|
|
|
this.container = container;
|
2012-09-08 10:24:55 +00:00
|
|
|
|
2012-09-08 21:10:05 +00:00
|
|
|
if(renderingOptions == undefined || renderingOptions.dpi == undefined) {
|
2012-09-08 10:24:55 +00:00
|
|
|
throw "Bad FrameRenderer initialization. <dpi> not well defined.";
|
|
|
|
}
|
|
|
|
|
2012-09-08 13:34:13 +00:00
|
|
|
this.displayGrid = !!renderingOptions.displayGrid;
|
|
|
|
this.dpi = renderingOptions.dpi;
|
2012-09-08 13:08:00 +00:00
|
|
|
this.className = className;
|
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 14:32:28 +00:00
|
|
|
this.drawPixel(col, row, frame, this.getCanvas_(frame), 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 14:32:28 +00:00
|
|
|
var context = this.getCanvas_(frame).getContext('2d');
|
2012-09-04 12:10:16 +00:00
|
|
|
var color = frame.getPixel(col, row);
|
|
|
|
if(color == Constants.TRANSPARENT_COLOR) {
|
2012-09-08 10:24:55 +00:00
|
|
|
context.clearRect(col * this.dpi, row * this.dpi, 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 10:24:55 +00:00
|
|
|
context.fillRect(col * this.dpi, row * this.dpi, 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 14:32:28 +00:00
|
|
|
var canvas = this.getCanvas_(frame)
|
|
|
|
canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
|
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 21:10:05 +00:00
|
|
|
this.canvas = this.createCanvasForFrame_(frame);
|
2012-09-08 14:32:28 +00:00
|
|
|
this.container.appendChild(this.canvas);
|
|
|
|
|
|
|
|
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-08 21:10:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ns.FrameRenderer.prototype.createCanvasForFrame_ = function (frame) {
|
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
canvas.setAttribute("width", frame.getWidth() * this.dpi);
|
|
|
|
canvas.setAttribute("height", frame.getHeight() * this.dpi);
|
|
|
|
|
|
|
|
canvas.classList.add("canvas");
|
|
|
|
if(this.className) canvas.classList.add(this.className);
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
};
|
2012-09-04 06:35:58 +00:00
|
|
|
})();
|