piskel/src/js/app.js

243 lines
8.3 KiB
JavaScript
Raw Normal View History

/**
* @require Constants
* @require Events
*/
(function () {
var ns = $.namespace("pskl");
/**
* Main application controller
*/
ns.app = {
init : function () {
2013-12-19 02:22:25 +04:00
/**
* True when piskel is running in static mode (no back end needed).
* When started from APP Engine, appEngineToken_ (Boolean) should be set on window.pskl
*/
this.isAppEngineVersion = !!pskl.appEngineToken_;
this.shortcutService = new pskl.service.keyboard.ShortcutService();
this.shortcutService.init();
var size = this.readSizeFromURL_();
2013-12-11 00:25:36 +04:00
var descriptor = new pskl.model.piskel.Descriptor('New Piskel', '');
var piskel = new pskl.model.Piskel(size.width, size.height, descriptor);
var layer = new pskl.model.Layer("Layer 1");
var frame = new pskl.model.Frame(size.width, size.height);
layer.addFrame(frame);
piskel.addLayer(layer);
this.corePiskelController = new pskl.controller.piskel.PiskelController(piskel);
this.corePiskelController.init();
this.piskelController = new pskl.controller.piskel.PublicPiskelController(this.corePiskelController);
this.piskelController.init();
this.paletteController = new pskl.controller.PaletteController();
this.paletteController.init();
this.palettesListController = new pskl.controller.PalettesListController();
this.palettesListController.init();
this.cursorCoordinatesController = new pskl.controller.CursorCoordinatesController(this.piskelController);
this.cursorCoordinatesController.init();
this.drawingController = new pskl.controller.DrawingController(this.piskelController, this.paletteController, $('#drawing-canvas-container'));
this.drawingController.init();
this.animationController = new pskl.controller.AnimatedPreviewController(this.piskelController, $('#preview-canvas-container'));
2012-09-05 02:09:42 +04:00
this.animationController.init();
this.minimapController = new pskl.controller.MinimapController(this.piskelController, this.animationController, this.drawingController, $('#preview-canvas-container'));
this.minimapController.init();
this.previewFilmController = new pskl.controller.PreviewFilmController(this.piskelController, $('#preview-list'));
this.previewFilmController.init();
this.layersListController = new pskl.controller.LayersListController(this.piskelController);
this.layersListController.init();
this.settingsController = new pskl.controller.settings.SettingsController(this.piskelController);
2013-06-16 12:17:50 +04:00
this.settingsController.init();
this.dialogsController = new pskl.controller.dialogs.DialogsController(this.piskelController);
this.dialogsController.init();
this.toolController = new pskl.controller.ToolController();
this.toolController.init();
this.selectionManager = new pskl.selection.SelectionManager(this.piskelController);
this.selectionManager.init();
this.historyService = new pskl.service.HistoryService(this.corePiskelController);
2012-09-16 15:10:05 +04:00
this.historyService.init();
this.notificationController = new pskl.controller.NotificationController();
this.notificationController.init();
this.localStorageService = new pskl.service.LocalStorageService(this.piskelController);
this.localStorageService.init();
2013-09-07 19:50:43 +04:00
this.imageUploadService = new pskl.service.ImageUploadService();
this.imageUploadService.init();
this.cheatsheetService = new pskl.service.keyboard.CheatsheetService();
this.cheatsheetService.init();
this.savedStatusService = new pskl.service.SavedStatusService(this.piskelController);
this.savedStatusService.init();
2013-12-19 02:22:25 +04:00
if (this.isAppEngineVersion) {
this.storageService = new pskl.service.AppEngineStorageService(this.piskelController);
} else {
this.storageService = new pskl.service.GithubStorageService(this.piskelController);
}
this.storageService.init();
var drawingLoop = new pskl.rendering.DrawingLoop();
drawingLoop.addCallback(this.render, this);
drawingLoop.start();
2013-12-11 00:25:36 +04:00
this.initTooltips_();
2013-12-19 02:22:25 +04:00
if (this.isAppEngineVersion) {
2013-10-10 02:06:11 +04:00
this.finishInitAppEngine_();
2013-12-19 02:22:25 +04:00
} else {
this.finishInitGithub_();
2013-10-10 02:06:11 +04:00
}
},
2013-12-19 02:22:25 +04:00
finishInitGithub_ : function () {
2013-10-10 02:06:11 +04:00
var framesheetId = this.readFramesheetIdFromURL_();
if (framesheetId) {
$.publish(Events.SHOW_NOTIFICATION, [{
"content" : "Loading animation with id : [" + framesheetId + "]"
}]);
this.loadFramesheetFromService(framesheetId);
}
},
finishInitAppEngine_ : function () {
2013-12-19 02:22:25 +04:00
if (pskl.appEnginePiskelData_ && pskl.appEnginePiskelData_.piskel) {
pskl.utils.serialization.Deserializer.deserialize(pskl.appEnginePiskelData_.piskel, function (piskel) {
piskel.setDescriptor(pskl.appEnginePiskelData_.descriptor);
pskl.app.piskelController.setPiskel(piskel);
2013-12-19 02:22:25 +04:00
pskl.app.animationController.setFPS(pskl.appEnginePiskelData_.fps);
});
2012-08-30 03:16:13 +04:00
}
},
2014-02-10 04:00:16 +04:00
isLoggedIn : function () {
return pskl.appEnginePiskelData_ && pskl.appEnginePiskelData_.isLoggedIn;
},
2013-12-11 00:25:36 +04:00
initTooltips_ : function () {
$('body').tooltip({
selector: '[rel=tooltip]'
});
},
render : function (delta) {
this.drawingController.render(delta);
this.animationController.render(delta);
this.previewFilmController.render(delta);
},
2012-09-16 20:48:32 +04:00
readSizeFromURL_ : function () {
2013-12-11 00:25:36 +04:00
var sizeParam = this.readUrlParameter_("size");
var size;
2012-09-16 20:48:32 +04:00
// parameter expected as size=64x48 => size=widthxheight
var parts = sizeParam.split("x");
if (parts && parts.length == 2 && !isNaN(parts[0]) && !isNaN(parts[1])) {
var width = parseInt(parts[0], 10),
height = parseInt(parts[1], 10);
2012-09-16 20:48:32 +04:00
size = {
height : Math.min(height, Constants.MAX_HEIGHT),
2012-09-17 01:47:37 +04:00
width : Math.min(width, Constants.MAX_WIDTH)
2012-09-16 20:48:32 +04:00
};
} else {
size = {
height : Constants.DEFAULT.HEIGHT,
width : Constants.DEFAULT.WIDTH
};
2012-09-16 20:48:32 +04:00
}
return size;
},
readFramesheetIdFromURL_ : function () {
2012-09-16 20:48:32 +04:00
return this.readUrlParameter_("frameId");
},
readUrlParameter_ : function (paramName) {
2013-12-11 00:25:36 +04:00
var searchString = window.location.search.substring(1);
var params = searchString.split("&");
for (var i = 0; i < params.length; i++) {
var param = params[i].split("=");
if (param[0] == paramName) {
return window.unescape(param[1]);
2012-09-16 20:48:32 +04:00
}
2012-08-30 03:16:13 +04:00
}
2012-09-16 20:48:32 +04:00
return "";
2012-08-30 03:16:13 +04:00
},
loadFramesheetFromService : function (frameId) {
var xhr = new XMLHttpRequest();
2013-10-10 02:06:11 +04:00
xhr.open('GET', Constants.STATIC.URL.GET + '?l=' + frameId, true);
2012-08-30 03:16:13 +04:00
xhr.responseType = 'text';
xhr.onload = function (e) {
2012-09-21 01:57:26 +04:00
var res = JSON.parse(this.responseText);
pskl.utils.serialization.Deserializer.deserialize(res.framesheet, function (piskel) {
pskl.app.piskelController.setPiskel(piskel);
pskl.app.animationController.setFPS(res.fps);
2013-12-06 01:12:48 +04:00
$.publish(Events.HIDE_NOTIFICATION);
});
2012-08-30 03:16:13 +04:00
};
xhr.onerror = function () {
$.publish(Events.HIDE_NOTIFICATION);
2012-08-30 03:16:13 +04:00
};
xhr.send();
2012-08-30 01:32:51 +04:00
},
2012-08-30 01:01:04 +04:00
2013-12-19 02:22:25 +04:00
store : function (callbacks) {
this.storageService.store(callbacks);
2013-10-10 02:06:11 +04:00
},
2012-08-30 03:16:13 +04:00
2013-10-10 02:06:11 +04:00
getFirstFrameAsPng : function () {
var firstFrame = this.piskelController.getFrameAt(0);
var canvasRenderer = new pskl.rendering.CanvasRenderer(firstFrame, 1);
canvasRenderer.drawTransparentAs('rgba(0,0,0,0)');
2013-12-06 01:12:48 +04:00
var firstFrameCanvas = canvasRenderer.render();
2013-10-10 02:06:11 +04:00
return firstFrameCanvas.toDataURL("image/png");
},
getFramesheetAsPng : function () {
var renderer = new pskl.rendering.PiskelRenderer(this.piskelController);
var framesheetCanvas = renderer.renderAsCanvas();
2013-10-10 02:06:11 +04:00
return framesheetCanvas.toDataURL("image/png");
},
uploadAsSpritesheetPNG : function () {
2013-10-10 02:06:11 +04:00
var imageData = this.getFramesheetAsPng();
this.imageUploadService.upload(imageData, this.openWindow.bind(this));
},
openWindow : function (url) {
var options = [
"dialog=yes", "scrollbars=no", "status=no",
"width=" + this.piskelController.getWidth() * this.piskelController.getFrameCount(),
"height=" + this.piskelController.getHeight()
].join(",");
window.open(url, "piskel-export", options);
}
};
})();