2012-09-04 16:10:16 +04:00
|
|
|
(function () {
|
2013-11-01 18:39:42 +04:00
|
|
|
var ns = $.namespace("pskl.rendering.frame");
|
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 = {
|
2013-10-30 01:16:39 +04:00
|
|
|
'supportGridRendering' : false,
|
2013-11-01 14:17:50 +04:00
|
|
|
'zoom' : 1
|
2013-06-18 01:22:30 +04:00
|
|
|
};
|
2013-10-30 01:16:39 +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-10-30 01:16:39 +04:00
|
|
|
if(isNaN(renderingOptions.zoom)) {
|
|
|
|
throw 'Bad FrameRenderer initialization. <zoom> not well defined.';
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.container = container;
|
2013-09-30 01:14:10 +04:00
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
this.zoom = renderingOptions.zoom;
|
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
this.offset = {
|
|
|
|
x : 0,
|
|
|
|
y : 0
|
|
|
|
};
|
2013-11-01 14:17:50 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
this.margin = {
|
|
|
|
x : 0,
|
|
|
|
y : 0
|
|
|
|
};
|
2013-10-30 01:16:39 +04:00
|
|
|
|
2013-11-05 03:05:49 +04:00
|
|
|
this.isGridEnabled_ = false;
|
2013-08-10 14:11:16 +04:00
|
|
|
this.supportGridRendering = renderingOptions.supportGridRendering;
|
|
|
|
|
2013-09-30 01:14:10 +04:00
|
|
|
this.classes = classes || [];
|
|
|
|
this.classes.push('canvas');
|
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
/**
|
|
|
|
* Off dom canvas, will be used to draw the frame at 1:1 ratio
|
|
|
|
* @type {HTMLElement}
|
|
|
|
*/
|
2013-09-30 01:14:10 +04:00
|
|
|
this.canvas = null;
|
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
/**
|
|
|
|
* Displayed canvas, scaled-up from the offdom canvas
|
|
|
|
* @type {HTMLElement}
|
|
|
|
*/
|
2013-11-01 14:17:50 +04:00
|
|
|
this.displayCanvas = null;
|
2013-10-30 01:16:39 +04:00
|
|
|
this.setDisplaySize(renderingOptions.width, renderingOptions.height);
|
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
this.setGridEnabled(pskl.UserSettings.get(pskl.UserSettings.SHOW_GRID));
|
2013-08-10 14:11:16 +04:00
|
|
|
|
|
|
|
this.updateBackgroundClass_(pskl.UserSettings.get(pskl.UserSettings.CANVAS_BACKGROUND));
|
|
|
|
$.subscribe(Events.USER_SETTINGS_CHANGED, $.proxy(this.onUserSettingsChange_, this));
|
|
|
|
};
|
|
|
|
|
2013-11-01 20:12:59 +04:00
|
|
|
pskl.utils.inherit(pskl.rendering.frame.FrameRenderer, pskl.rendering.AbstractRenderer);
|
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
ns.FrameRenderer.prototype.render = function (frame) {
|
|
|
|
if (frame) {
|
|
|
|
this.clear();
|
|
|
|
this.renderFrame_(frame);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.clear = function () {
|
|
|
|
pskl.CanvasUtils.clear(this.canvas);
|
|
|
|
pskl.CanvasUtils.clear(this.displayCanvas);
|
|
|
|
};
|
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.FrameRenderer.prototype.setZoom = function (zoom) {
|
2013-11-02 02:11:11 +04:00
|
|
|
// back up center coordinates
|
|
|
|
var centerX = this.offset.x + (this.displayWidth/(2*this.zoom));
|
|
|
|
var centerY = this.offset.y + (this.displayHeight/(2*this.zoom));
|
|
|
|
|
|
|
|
this.zoom = Math.max(1, zoom);
|
|
|
|
|
|
|
|
// recenter
|
|
|
|
this.setOffset(
|
|
|
|
centerX - (this.displayWidth/(2*this.zoom)),
|
|
|
|
centerY - (this.displayHeight/(2*this.zoom))
|
|
|
|
);
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
ns.FrameRenderer.prototype.getZoom = function () {
|
|
|
|
return this.zoom;
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.setDisplaySize = function (width, height) {
|
|
|
|
this.displayWidth = width;
|
2013-11-01 18:39:42 +04:00
|
|
|
this.displayHeight = height;
|
2013-11-01 14:17:50 +04:00
|
|
|
if (this.displayCanvas) {
|
|
|
|
$(this.displayCanvas).remove();
|
|
|
|
this.displayCanvas = null;
|
2013-10-30 01:16:39 +04:00
|
|
|
}
|
2013-11-01 18:39:42 +04:00
|
|
|
this.createDisplayCanvas_();
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.getDisplaySize = function () {
|
|
|
|
return {
|
|
|
|
height : this.displayHeight,
|
|
|
|
width : this.displayWidth
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.getOffset = function () {
|
|
|
|
return {
|
|
|
|
x : this.offset.x,
|
|
|
|
y : this.offset.y
|
|
|
|
};
|
2013-11-05 03:05:49 +04:00
|
|
|
};
|
2013-11-01 18:39:42 +04:00
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.moveOffset = function (x, y) {
|
2013-11-02 02:11:11 +04:00
|
|
|
this.setOffset(this.offset.x + x, this.offset.y + y);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.setOffset = function (x, y) {
|
|
|
|
// TODO : provide frame size information to the FrameRenderer constructor
|
|
|
|
// here I first need to verify I have a 'canvas' which I can use to infer the frame information
|
|
|
|
// and then perform my boundaries checking. This sucks
|
|
|
|
if (this.canvas) {
|
|
|
|
var maxX = this.canvas.width - (this.displayWidth/this.zoom);
|
|
|
|
x = pskl.utils.Math.minmax(x, 0, maxX);
|
|
|
|
var maxY = this.canvas.height - (this.displayHeight/this.zoom);
|
|
|
|
y = pskl.utils.Math.minmax(y, 0, maxY);
|
|
|
|
}
|
|
|
|
this.offset.x = x;
|
|
|
|
this.offset.y = y;
|
2013-11-01 18:39:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.setGridEnabled = function (flag) {
|
2013-11-05 03:05:49 +04:00
|
|
|
this.isGridEnabled_ = flag && this.supportGridRendering;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.FrameRenderer.prototype.isGridEnabled = function () {
|
|
|
|
return this.isGridEnabled_;
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
|
|
|
|
2013-11-01 14:17:50 +04:00
|
|
|
ns.FrameRenderer.prototype.updateMargins_ = function () {
|
2013-11-01 18:39:42 +04:00
|
|
|
var deltaX = this.displayWidth - (this.zoom * this.canvas.width);
|
|
|
|
this.margin.x = Math.max(0, deltaX) / 2;
|
2013-10-30 01:16:39 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
var deltaY = this.displayHeight - (this.zoom * this.canvas.height);
|
|
|
|
this.margin.y = Math.max(0, deltaY) / 2;
|
2013-10-30 01:16:39 +04:00
|
|
|
};
|
|
|
|
|
2013-11-01 14:17:50 +04:00
|
|
|
ns.FrameRenderer.prototype.createDisplayCanvas_ = function () {
|
2013-10-30 01:16:39 +04:00
|
|
|
var height = this.displayHeight;
|
|
|
|
var width = this.displayWidth;
|
|
|
|
|
2013-11-01 14:17:50 +04:00
|
|
|
this.displayCanvas = pskl.CanvasUtils.createCanvas(width, height, this.classes);
|
2013-10-30 01:16:39 +04:00
|
|
|
if (true || this.zoom > 2) {
|
2013-11-01 14:17:50 +04:00
|
|
|
pskl.CanvasUtils.disableImageSmoothing(this.displayCanvas);
|
2013-10-30 01:16:39 +04:00
|
|
|
}
|
2013-11-01 14:17:50 +04:00
|
|
|
this.container.append(this.displayCanvas);
|
|
|
|
};
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.FrameRenderer.prototype.onUserSettingsChange_ = function (evt, settingName, settingValue) {
|
|
|
|
if(settingName == pskl.UserSettings.SHOW_GRID) {
|
2013-11-01 18:39:42 +04:00
|
|
|
this.setGridEnabled(settingValue);
|
|
|
|
} else if (settingName == pskl.UserSettings.CANVAS_BACKGROUND) {
|
2013-08-10 14:11:16 +04:00
|
|
|
this.updateBackgroundClass_(settingValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
ns.FrameRenderer.prototype.renderPixel_ = function (color, x, y, context) {
|
2013-08-10 14:11:16 +04:00
|
|
|
if(color != Constants.TRANSPARENT_COLOR) {
|
|
|
|
context.fillStyle = color;
|
2013-11-01 18:39:42 +04:00
|
|
|
context.fillRect(x, y, 1, 1);
|
2013-09-26 23:53:37 +04:00
|
|
|
}
|
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
|
|
|
|
*/
|
2013-11-01 18:39:42 +04:00
|
|
|
ns.FrameRenderer.prototype.getCoordinates = function(x, y) {
|
|
|
|
var containerOffset = this.container.offset();
|
|
|
|
x = x - containerOffset.left;
|
|
|
|
y = y - containerOffset.top;
|
|
|
|
|
|
|
|
// apply margins
|
|
|
|
x = x - this.margin.x;
|
|
|
|
y = y - this.margin.y;
|
|
|
|
|
2013-11-05 03:05:49 +04:00
|
|
|
var cellSize = this.zoom;
|
2013-11-01 18:39:42 +04:00
|
|
|
// apply frame offset
|
|
|
|
x = x + this.offset.x * cellSize;
|
|
|
|
y = y + this.offset.y * cellSize;
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
return {
|
2013-11-15 03:39:43 +04:00
|
|
|
x : Math.floor(x / cellSize),
|
|
|
|
y : Math.floor(y / cellSize)
|
2013-06-18 01:22:30 +04:00
|
|
|
};
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2013-11-01 14:17:50 +04:00
|
|
|
ns.FrameRenderer.prototype.renderFrame_ = function (frame) {
|
2013-11-02 03:00:38 +04:00
|
|
|
if (!this.canvas || frame.getWidth() != this.canvas.width || frame.getHeight() != this.canvas.height) {
|
2013-10-30 01:16:39 +04:00
|
|
|
this.canvas = pskl.CanvasUtils.createCanvas(frame.getWidth(), frame.getHeight());
|
|
|
|
}
|
2013-09-29 01:52:51 +04:00
|
|
|
|
2013-10-30 01:16:39 +04:00
|
|
|
var context = this.canvas.getContext('2d');
|
2013-11-01 18:39:42 +04:00
|
|
|
for(var x = 0, width = frame.getWidth(); x < width; x++) {
|
|
|
|
for(var y = 0, height = frame.getHeight(); y < height; y++) {
|
|
|
|
var color = frame.getPixel(x, y);
|
|
|
|
this.renderPixel_(color, x, y, context);
|
2013-10-30 01:16:39 +04:00
|
|
|
}
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
2013-10-30 01:16:39 +04:00
|
|
|
|
2013-11-01 14:17:50 +04:00
|
|
|
this.updateMargins_();
|
|
|
|
|
|
|
|
context = this.displayCanvas.getContext('2d');
|
2013-10-30 01:16:39 +04:00
|
|
|
context.save();
|
2013-11-05 03:05:49 +04:00
|
|
|
|
|
|
|
if (this.canvas.width*this.zoom < this.displayCanvas.width) {
|
|
|
|
context.fillStyle = Constants.ZOOMED_OUT_BACKGROUND_COLOR;
|
|
|
|
context.fillRect(0,0,this.displayCanvas.width, this.displayCanvas.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.translate(
|
|
|
|
this.margin.x-this.offset.x*this.zoom,
|
|
|
|
this.margin.y-this.offset.y*this.zoom
|
|
|
|
);
|
|
|
|
|
|
|
|
context.clearRect(0, 0, this.canvas.width*this.zoom, this.canvas.height*this.zoom);
|
|
|
|
|
|
|
|
var isIE10 = pskl.utils.UserAgent.isIE && pskl.utils.UserAgent.version === 10;
|
|
|
|
if (this.isGridEnabled() || isIE10) {
|
|
|
|
var gridWidth = this.isGridEnabled() ? Constants.GRID_STROKE_WIDTH : 0;
|
|
|
|
var scaled = pskl.utils.ImageResizer.resizeNearestNeighbour(this.canvas, this.zoom, gridWidth);
|
|
|
|
context.drawImage(scaled, 0, 0);
|
|
|
|
} else {
|
|
|
|
context.scale(this.zoom, this.zoom);
|
|
|
|
context.drawImage(this.canvas, 0, 0);
|
|
|
|
}
|
2013-10-30 01:16:39 +04:00
|
|
|
context.restore();
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2012-09-04 10:35:58 +04:00
|
|
|
})();
|