2013-05-28 01:42:53 +04:00
|
|
|
(function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.controller");
|
2013-11-06 01:11:47 +04:00
|
|
|
ns.AnimatedPreviewController = function (piskelController, container) {
|
2013-09-22 23:02:43 +04:00
|
|
|
this.piskelController = piskelController;
|
2013-08-10 14:11:16 +04:00
|
|
|
this.container = container;
|
2013-05-28 01:42:53 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
this.elapsedTime = 0;
|
|
|
|
this.currentIndex = 0;
|
2013-05-28 01:42:53 +04:00
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
this.setFPS(Constants.DEFAULT.FPS);
|
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
var zoom = this.calculateZoom_();
|
2013-11-02 03:00:38 +04:00
|
|
|
var frame = this.piskelController.getCurrentFrame();
|
2013-08-10 14:11:16 +04:00
|
|
|
var renderingOptions = {
|
2013-11-01 18:39:42 +04:00
|
|
|
"zoom": zoom,
|
2013-11-02 03:00:38 +04:00
|
|
|
"height" : frame.getHeight() * zoom,
|
|
|
|
"width" : frame.getWidth() * zoom
|
2013-05-28 01:42:53 +04:00
|
|
|
};
|
2013-11-01 18:39:42 +04:00
|
|
|
this.renderer = new pskl.rendering.frame.FrameRenderer(this.container, renderingOptions);
|
2013-05-28 01:42:53 +04:00
|
|
|
|
2013-12-06 01:12:48 +04:00
|
|
|
$.subscribe(Events.FRAME_SIZE_CHANGED, this.onFrameSizeChange_.bind(this));
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2013-05-28 01:42:53 +04:00
|
|
|
|
2013-08-10 14:11:16 +04: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-28 01:42:53 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.AnimatedPreviewController.prototype.onFPSSliderChange = function (evt) {
|
|
|
|
this.setFPS(parseInt($("#preview-fps")[0].value, 10));
|
|
|
|
};
|
2013-05-28 01:42:53 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.AnimatedPreviewController.prototype.setFPS = function (fps) {
|
|
|
|
this.fps = fps;
|
|
|
|
$("#preview-fps").val(this.fps);
|
|
|
|
$("#display-fps").html(this.fps + " FPS");
|
|
|
|
};
|
2013-05-28 01:42:53 +04:00
|
|
|
|
2013-10-01 00:00:31 +04:00
|
|
|
ns.AnimatedPreviewController.prototype.getFPS = function () {
|
|
|
|
return this.fps;
|
|
|
|
};
|
|
|
|
|
2013-08-10 14:11:16 +04: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 23:02:43 +04:00
|
|
|
if (!this.piskelController.hasFrameAt(this.currentIndex)) {
|
2013-08-10 14:11:16 +04:00
|
|
|
this.currentIndex = 0;
|
|
|
|
this.elapsedTime = 0;
|
|
|
|
}
|
2013-09-22 23:02:43 +04:00
|
|
|
this.renderer.render(this.piskelController.getFrameAt(this.currentIndex));
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
};
|
2013-05-28 01:42:53 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
/**
|
2013-11-06 01:11:47 +04:00
|
|
|
* Calculate the preview zoom depending on the framesheet size
|
2013-08-10 14:11:16 +04:00
|
|
|
*/
|
2013-10-30 01:16:39 +04:00
|
|
|
ns.AnimatedPreviewController.prototype.calculateZoom_ = function () {
|
2013-11-02 03:00:38 +04:00
|
|
|
var frame = this.piskelController.getCurrentFrame();
|
2013-08-10 14:11:16 +04:00
|
|
|
var previewSize = 200,
|
2013-11-02 03:00:38 +04:00
|
|
|
hZoom = previewSize / frame.getHeight(),
|
|
|
|
wZoom = previewSize / frame.getWidth();
|
2013-10-01 00:00:31 +04:00
|
|
|
|
2013-11-01 18:39:42 +04:00
|
|
|
return Math.min(hZoom, wZoom);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
|
|
|
|
2013-12-06 01:12:48 +04:00
|
|
|
ns.AnimatedPreviewController.prototype.onFrameSizeChange_ = function () {
|
2013-11-02 03:00:38 +04:00
|
|
|
var frame = this.piskelController.getCurrentFrame();
|
2013-11-01 18:39:42 +04:00
|
|
|
var zoom = this.calculateZoom_();
|
2013-11-02 03:00:38 +04:00
|
|
|
this.renderer.setDisplaySize(frame.getWidth() * zoom, frame.getHeight() * zoom);
|
2013-12-06 01:12:48 +04:00
|
|
|
this.renderer.setZoom(zoom);
|
|
|
|
this.renderer.setOffset(0, 0);
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2012-09-05 02:09:42 +04:00
|
|
|
})();
|