2013-05-27 21:42:53 +00:00
|
|
|
(function () {
|
2013-08-10 10:11:16 +00:00
|
|
|
var ns = $.namespace("pskl.controller");
|
2013-09-22 19:02:43 +00:00
|
|
|
ns.AnimatedPreviewController = function (piskelController, container, dpi) {
|
|
|
|
this.piskelController = piskelController;
|
2013-08-10 10:11:16 +00:00
|
|
|
this.container = container;
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
this.elapsedTime = 0;
|
|
|
|
this.currentIndex = 0;
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-09-30 20:00:31 +00:00
|
|
|
this.setFPS(Constants.DEFAULT.FPS);
|
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
var renderingOptions = {
|
|
|
|
"dpi": this.calculateDPI_()
|
2013-05-27 21:42:53 +00:00
|
|
|
};
|
2013-08-10 10:11:16 +00:00
|
|
|
this.renderer = new pskl.rendering.FrameRenderer(this.container, renderingOptions);
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
$.subscribe(Events.FRAME_SIZE_CHANGED, this.updateDPI_.bind(this));
|
|
|
|
};
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
ns.AnimatedPreviewController.prototype.init = function () {
|
|
|
|
// 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
|
|
|
|
$("#preview-fps")[0].addEventListener('change', this.onFPSSliderChange.bind(this));
|
|
|
|
};
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
ns.AnimatedPreviewController.prototype.onFPSSliderChange = function (evt) {
|
|
|
|
this.setFPS(parseInt($("#preview-fps")[0].value, 10));
|
|
|
|
};
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
ns.AnimatedPreviewController.prototype.setFPS = function (fps) {
|
|
|
|
this.fps = fps;
|
|
|
|
$("#preview-fps").val(this.fps);
|
|
|
|
$("#display-fps").html(this.fps + " FPS");
|
|
|
|
};
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-09-30 20:00:31 +00:00
|
|
|
ns.AnimatedPreviewController.prototype.getFPS = function () {
|
|
|
|
return this.fps;
|
|
|
|
};
|
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
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;
|
2013-09-22 19:02:43 +00:00
|
|
|
if (!this.piskelController.hasFrameAt(this.currentIndex)) {
|
2013-08-10 10:11:16 +00:00
|
|
|
this.currentIndex = 0;
|
|
|
|
this.elapsedTime = 0;
|
|
|
|
}
|
2013-09-22 19:02:43 +00:00
|
|
|
this.renderer.render(this.piskelController.getFrameAt(this.currentIndex));
|
2013-08-10 10:11:16 +00:00
|
|
|
}
|
|
|
|
};
|
2013-05-27 21:42:53 +00:00
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
/**
|
|
|
|
* Calculate the preview DPI depending on the framesheet size
|
|
|
|
*/
|
|
|
|
ns.AnimatedPreviewController.prototype.calculateDPI_ = function () {
|
|
|
|
var previewSize = 200,
|
2013-09-22 19:02:43 +00:00
|
|
|
framePixelHeight = this.piskelController.getCurrentFrame().getHeight(),
|
|
|
|
framePixelWidth = this.piskelController.getCurrentFrame().getWidth();
|
2013-08-10 10:11:16 +00:00
|
|
|
// TODO (julz) : should have a utility to get a Size from framesheet easily (what about empty framesheets though ?)
|
2013-09-30 20:00:31 +00:00
|
|
|
|
2013-08-10 10:11:16 +00:00
|
|
|
//return pskl.PixelUtils.calculateDPIForContainer($(".preview-container"), framePixelHeight, framePixelWidth);
|
|
|
|
return pskl.PixelUtils.calculateDPI(previewSize, previewSize, framePixelHeight, framePixelWidth);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.AnimatedPreviewController.prototype.updateDPI_ = function () {
|
|
|
|
this.dpi = this.calculateDPI_();
|
2013-09-30 20:44:02 +00:00
|
|
|
this.renderer.setDPI(this.dpi);
|
2013-08-10 10:11:16 +00:00
|
|
|
};
|
2012-09-04 22:09:42 +00:00
|
|
|
})();
|