piskel/js/piskel.js

218 lines
6.7 KiB
JavaScript
Raw Normal View History

/**
* @require Constants
* @require Events
*/
$.namespace("pskl");
(function () {
/**
* FrameSheetModel instance.
*/
2012-09-05 02:09:42 +04:00
var frameSheet,
// Configuration:
// Canvas size in pixel size (not dpi related)
2012-09-08 18:32:28 +04:00
framePixelWidth = 32,
framePixelHeight = 32,
// Scaling factors for a given frameSheet rendering:
// Main drawing area dpi is calculated dynamically
2012-09-08 18:32:28 +04:00
// Canvas preview film canvases:
previewTileCanvasDpi = 4,
// Animated canvas preview:
previewAnimationCanvasDpi = 8;
/**
* Main application controller
*/
var piskel = {
init : function () {
2012-09-06 02:23:24 +04:00
frameSheet = new pskl.model.FrameSheet(framePixelWidth, framePixelHeight);
frameSheet.addEmptyFrame();
this.drawingController = new pskl.controller.DrawingController(
frameSheet,
$('#drawing-canvas-container'),
this.calculateDPIsForDrawingCanvas_()
);
2012-09-05 02:09:42 +04:00
this.animationController = new pskl.controller.AnimatedPreviewController(
frameSheet,
$('#preview-canvas-container'),
2012-09-05 02:09:42 +04:00
previewAnimationCanvasDpi
);
this.previewsController = new pskl.controller.PreviewFilmController(
frameSheet,
$('#preview-list'),
2012-09-05 02:09:42 +04:00
previewTileCanvasDpi
);
2012-09-14 00:57:32 +04:00
// To catch the current active frame, the selection manager have to be initialized before
2012-09-15 04:24:06 +04:00
// the 'frameSheet.setCurrentFrameIndex(0);' line below.
// TODO(vincz): Slice each constructor to have:
// - an event(s) listening init
// - an event(s) triggering init
2012-09-15 04:24:06 +04:00
// All listeners will be hook in a first step, then all event triggering inits will be called
// in a second batch.
this.selectionManager =
new pskl.selection.SelectionManager(frameSheet, this.drawingController.overlayFrame);
// DO NOT MOVE THIS LINE (see comment above)
frameSheet.setCurrentFrameIndex(0);
2012-09-05 02:09:42 +04:00
this.animationController.init();
this.previewsController.init();
this.historyManager = new pskl.HistoryManager(frameSheet);
this.historyManager.init();
this.keyManager = new pskl.KeyManager();
pskl.NotificationService.init();
pskl.LocalStorageService.init(frameSheet);
// TODO: Add comments
var framesheetId = this.getFramesheetIdFromUrl();
if (framesheetId) {
$.publish(Events.SHOW_NOTIFICATION, [{"content": "Loading animation with id : [" + framesheetId + "]"}]);
this.loadFramesheetFromService(framesheetId);
2012-08-30 03:16:13 +04:00
} else {
this.finishInit();
pskl.LocalStorageService.displayRestoreNotification();
2012-08-30 03:16:13 +04:00
}
var drawingLoop = new pskl.rendering.DrawingLoop();
drawingLoop.addCallback(this.render, this);
drawingLoop.start();
2012-09-16 06:33:26 +04:00
// Init (event-delegated) bootstrap tooltips:
$('body').tooltip({
selector: '[rel=tooltip]'
});
this.connectResizeToDpiUpdate_();
},
render : function (delta) {
this.drawingController.render(delta);
this.animationController.render(delta);
this.previewsController.render(delta);
2012-08-30 03:16:13 +04:00
},
connectResizeToDpiUpdate_ : function () {
$(window).resize($.proxy(this.startDPIUpdateTimer_, this));
},
startDPIUpdateTimer_ : function () {
if (this.dpiUpdateTimer) window.clearInterval(this.dpiUpdateTimer);
this.dpiUpdateTimer = window.setTimeout($.proxy(this.updateDPIForViewport, this), 200);
},
updateDPIForViewport : function () {
var dpi = piskel.calculateDPIsForDrawingCanvas_();
this.drawingController.updateDPI(dpi);
2012-09-08 18:32:28 +04:00
},
/**
* @private
*/
calculateDPIsForDrawingCanvas_ : function() {
2012-09-16 06:33:26 +04:00
var userMessageGap = 80; // Reserve some height to show the user message at the bottom
var availableViewportHeight = $('.main-panel').height() - userMessageGap,
availableViewportWidth = $('.main-panel').width(),
previewHeight = $(".preview-container").height(),
previewWidth = $(".preview-container").width();
var heightBoundDpi = Math.floor(availableViewportHeight / framePixelHeight),
widthBoundDpi = Math.floor(availableViewportWidth / framePixelWidth);
var dpi = Math.min(heightBoundDpi, widthBoundDpi);
var drawingCanvasHeight = dpi * framePixelHeight;
var drawingCanvasWidth = dpi * framePixelWidth;
// Check if preview and drawing canvas overlap
var heightGap = drawingCanvasHeight + previewHeight - availableViewportHeight,
widthGap = drawingCanvasWidth + previewWidth - availableViewportWidth;
if (heightGap > 0 && widthGap > 0) {
// Calculate the DPI change needed to bridge height and width gap
var heightGapDpi = Math.ceil(heightGap / framePixelHeight),
widthGapDpi = Math.ceil(widthGap / framePixelWidth);
// substract smallest dpi change to initial dpi
dpi -= Math.min(heightGapDpi, widthGapDpi);
}
return dpi;
2012-09-08 18:32:28 +04:00
},
2012-08-30 03:16:13 +04:00
finishInit : function () {
var toolController = new pskl.controller.ToolController();
toolController.init();
pskl.Palette.init(frameSheet);
2012-08-30 03:16:13 +04:00
},
getFramesheetIdFromUrl : function() {
2012-08-30 03:16:13 +04:00
var href = window.location.href;
// TODO: Change frameId to framesheetId on the backend
2012-08-30 03:16:13 +04:00
if (href.indexOf('frameId=') != -1) {
return href.substring(href.indexOf('frameId=')+8);
}
},
loadFramesheetFromService : function (frameId) {
var xhr = new XMLHttpRequest();
// TODO: Change frameId to framesheetId on the backend
xhr.open('GET', Constants.PISKEL_SERVICE_URL + '/get?l=' + frameId, true);
2012-08-30 03:16:13 +04:00
xhr.responseType = 'text';
xhr.onload = function(e) {
frameSheet.deserialize(this.responseText);
$.publish(Events.HIDE_NOTIFICATION);
2012-08-30 03:16:13 +04:00
piskel.finishInit();
};
xhr.onerror = function () {
$.publish(Events.HIDE_NOTIFICATION);
2012-08-30 03:16:13 +04:00
piskel.finishInit();
};
xhr.send();
2012-08-30 01:32:51 +04:00
},
2012-08-30 01:01:04 +04:00
// TODO(julz): Create package ?
2012-08-30 03:16:13 +04:00
storeSheet : function (event) {
// TODO Refactor using jquery ?
2012-08-30 03:16:13 +04:00
var xhr = new XMLHttpRequest();
var formData = new FormData();
formData.append('framesheet_content', frameSheet.serialize());
formData.append('fps_speed', $('#preview-fps').val());
xhr.open('POST', Constants.PISKEL_SERVICE_URL + "/store", true);
2012-08-30 03:16:13 +04:00
xhr.onload = function(e) {
if (this.status == 200) {
2012-08-30 03:26:14 +04:00
var baseUrl = window.location.href.replace(window.location.search, "");
window.location.href = baseUrl + "?frameId=" + this.responseText;
2012-08-30 03:16:13 +04:00
}
};
xhr.send(formData);
2012-09-05 01:48:02 +04:00
if(event) {
event.stopPropagation();
event.preventDefault();
}
2012-08-30 03:16:13 +04:00
return false;
}
};
window.piskel = piskel;
piskel.init();
})();