2013-05-28 01:42:53 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace("pskl.controller");
|
|
|
|
ns.AnimatedPreviewController = function (framesheet, container, dpi) {
|
|
|
|
this.framesheet = framesheet;
|
|
|
|
this.container = container;
|
|
|
|
|
|
|
|
this.elapsedTime = 0;
|
|
|
|
this.currentIndex = 0;
|
|
|
|
|
|
|
|
this.fps = parseInt($("#preview-fps")[0].value, 10);
|
|
|
|
|
|
|
|
var renderingOptions = {
|
|
|
|
"dpi": this.calculateDPI_()
|
|
|
|
};
|
|
|
|
this.renderer = new pskl.rendering.FrameRenderer(this.container, renderingOptions);
|
|
|
|
|
|
|
|
$.subscribe(Events.FRAME_SIZE_CHANGED, this.updateDPI_.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.AnimatedPreviewController.prototype.init = function () {
|
2013-06-15 23:27:08 +04:00
|
|
|
// the oninput event won't work on IE10 unfortunately, but at least will provide a
|
|
|
|
// consistent behavior across all other browsers that support the input type range
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=853670
|
2013-06-19 02:29:07 +04:00
|
|
|
$("#preview-fps")[0].addEventListener('change', this.onFPSSliderChange.bind(this));
|
2013-05-28 01:42:53 +04:00
|
|
|
};
|
|
|
|
|
2013-06-15 23:27:08 +04:00
|
|
|
ns.AnimatedPreviewController.prototype.onFPSSliderChange = function (evt) {
|
2013-05-28 01:42:53 +04:00
|
|
|
this.setFPS(parseInt($("#preview-fps")[0].value, 10));
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.AnimatedPreviewController.prototype.setFPS = function (fps) {
|
|
|
|
this.fps = fps;
|
2013-06-15 23:27:08 +04:00
|
|
|
$("#preview-fps").val(this.fps);
|
2013-05-28 01:42:53 +04:00
|
|
|
$("#display-fps").html(this.fps + " FPS");
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.AnimatedPreviewController.prototype.render = function (delta) {
|
|
|
|
this.elapsedTime += delta;
|
|
|
|
var index = Math.floor(this.elapsedTime / (1000/this.fps));
|
|
|
|
if (index != this.currentIndex) {
|
|
|
|
this.currentIndex = index;
|
|
|
|
if (!this.framesheet.hasFrameAtIndex(this.currentIndex)) {
|
|
|
|
this.currentIndex = 0;
|
|
|
|
this.elapsedTime = 0;
|
|
|
|
}
|
|
|
|
this.renderer.render(this.framesheet.getFrameByIndex(this.currentIndex));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the preview DPI depending on the framesheet size
|
|
|
|
*/
|
|
|
|
ns.AnimatedPreviewController.prototype.calculateDPI_ = function () {
|
2013-06-12 16:53:47 +04:00
|
|
|
var previewSize = 200,
|
|
|
|
framePixelHeight = this.framesheet.getCurrentFrame().getHeight(),
|
2013-05-28 01:42:53 +04:00
|
|
|
framePixelWidth = this.framesheet.getCurrentFrame().getWidth();
|
|
|
|
// TODO (julz) : should have a utility to get a Size from framesheet easily (what about empty framesheets though ?)
|
|
|
|
|
2013-06-12 16:53:47 +04:00
|
|
|
//return pskl.PixelUtils.calculateDPIForContainer($(".preview-container"), framePixelHeight, framePixelWidth);
|
|
|
|
return pskl.PixelUtils.calculateDPI(previewSize, previewSize, framePixelHeight, framePixelWidth);
|
2013-05-28 01:42:53 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ns.AnimatedPreviewController.prototype.updateDPI_ = function () {
|
|
|
|
this.dpi = this.calculateDPI_();
|
|
|
|
this.renderer.updateDPI(this.dpi);
|
|
|
|
};
|
2012-09-05 02:09:42 +04:00
|
|
|
})();
|