piskel/src/js/app.js

242 lines
8.7 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
/**
* 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 = pskl.UserSettings.get(pskl.UserSettings.DEFAULT_SIZE);
var fps = Constants.DEFAULT.FPS;
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, fps, descriptor);
var layer = new pskl.model.Layer('Layer 1');
var frame = new pskl.model.Frame(size.width, size.height);
2014-08-21 02:50:59 +04:00
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.paletteImportService = new pskl.service.palette.PaletteImportService();
this.paletteService = new pskl.service.palette.PaletteService();
2014-09-18 09:18:07 +04:00
this.paletteService.addDynamicPalette(new pskl.service.palette.CurrentColorsPalette());
2015-09-14 22:41:10 +03:00
this.selectedColorsService = new pskl.service.SelectedColorsService();
this.selectedColorsService.init();
2015-09-16 21:27:51 +03:00
this.mouseStateService = new pskl.service.MouseStateService();
this.mouseStateService.init();
this.paletteController = new pskl.controller.PaletteController();
this.paletteController.init();
this.currentColorsService = new pskl.service.CurrentColorsService(this.piskelController);
this.currentColorsService.init();
this.palettesListController = new pskl.controller.PalettesListController(this.currentColorsService);
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.previewController = new pskl.controller.preview.PreviewController(
this.piskelController,
$('#animated-preview-canvas-container'));
this.previewController.init();
this.minimapController = new pskl.controller.MinimapController(
this.piskelController,
this.previewController,
this.drawingController,
$('.minimap-container'));
this.minimapController.init();
this.framesListController = new pskl.controller.FramesListController(
this.piskelController,
$('#preview-list'));
this.framesListController.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();
2014-11-20 01:00:25 +03:00
this.transformationsController = new pskl.controller.TransformationsController();
this.transformationsController.init();
this.progressBarController = new pskl.controller.ProgressBarController();
this.progressBarController.init();
this.canvasBackgroundController = new pskl.controller.CanvasBackgroundController();
this.canvasBackgroundController.init();
this.localStorageService = new pskl.service.storage.LocalStorageService(this.piskelController);
this.localStorageService.init();
this.fileDownloadStorageService = new pskl.service.storage.FileDownloadStorageService(this.piskelController);
this.fileDownloadStorageService.init();
this.desktopStorageService = new pskl.service.storage.DesktopStorageService(this.piskelController);
this.desktopStorageService.init();
this.galleryStorageService = new pskl.service.storage.GalleryStorageService(this.piskelController);
this.galleryStorageService.init();
this.storageService = new pskl.service.storage.StorageService(this.piskelController);
this.storageService.init();
this.importService = new pskl.service.ImportService(this.piskelController);
2013-09-07 19:50:43 +04:00
this.imageUploadService = new pskl.service.ImageUploadService();
this.imageUploadService.init();
this.savedStatusService = new pskl.service.SavedStatusService(this.piskelController, this.historyService);
this.savedStatusService.init();
this.backupService = new pskl.service.BackupService(this.piskelController);
this.backupService.init();
this.beforeUnloadService = new pskl.service.BeforeUnloadService(this.piskelController);
this.beforeUnloadService.init();
this.headerController = new pskl.controller.HeaderController(this.piskelController, this.savedStatusService);
this.headerController.init();
this.penSizeService = new pskl.service.pensize.PenSizeService();
this.penSizeService.init();
this.penSizeController = new pskl.controller.PenSizeController();
this.penSizeController.init();
this.fileDropperService = new pskl.service.FileDropperService(this.piskelController);
this.fileDropperService.init();
2014-07-12 02:41:39 +04:00
this.userWarningController = new pskl.controller.UserWarningController(this.piskelController);
this.userWarningController.init();
this.performanceReportService = new pskl.service.performance.PerformanceReportService(
this.piskelController, this.currentColorsService);
this.performanceReportService.init();
this.drawingLoop = new pskl.rendering.DrawingLoop();
this.drawingLoop.addCallback(this.render, this);
this.drawingLoop.start();
2013-12-11 00:25:36 +04:00
this.initTooltips_();
var piskelData = this.getPiskelInitData_();
if (piskelData && piskelData.piskel) {
2016-10-10 02:47:26 +03:00
this.loadPiskel_(piskelData);
2013-10-10 02:06:11 +04:00
}
2014-08-22 02:37:35 +04:00
if (pskl.devtools) {
pskl.devtools.init();
2014-08-22 02:37:35 +04:00
}
if (pskl.utils.Environment.detectNodeWebkit() && pskl.utils.UserAgent.isMac) {
var gui = require('nw.gui');
var mb = new gui.Menu({type : 'menubar'});
mb.createMacBuiltin('Piskel');
gui.Window.get().menu = mb;
}
if (pskl.utils.UserAgent.isUnsupported()) {
$.publish(Events.DIALOG_DISPLAY, {
dialogId : 'unsupported-browser'
});
}
2013-10-10 02:06:11 +04:00
},
2016-10-10 02:47:26 +03:00
loadPiskel_ : function (piskelData) {
var serializedPiskel = piskelData.piskel;
pskl.utils.serialization.Deserializer.deserialize(serializedPiskel, function (piskel) {
pskl.app.piskelController.setPiskel(piskel);
$.publish(Events.PISKEL_SAVED);
2016-10-10 02:47:26 +03:00
if (piskelData.descriptor) {
// Backward compatibility for v2 or older
piskel.setDescriptor(piskelData.descriptor);
}
});
2013-10-10 02:06:11 +04:00
},
getPiskelInitData_ : function () {
return pskl.appEnginePiskelData_;
},
2014-02-10 04:00:16 +04:00
isLoggedIn : function () {
var piskelData = this.getPiskelInitData_();
return piskelData && piskelData.isLoggedIn;
2014-02-10 04:00:16 +04:00
},
2013-12-11 00:25:36 +04:00
initTooltips_ : function () {
$('body').tooltip({
selector: '[rel=tooltip]'
});
},
render : function (delta) {
this.drawingController.render(delta);
this.previewController.render(delta);
this.framesListController.render(delta);
},
2013-10-10 02:06:11 +04:00
getFirstFrameAsPng : function () {
var frame = pskl.utils.LayerUtils.mergeFrameAt(this.piskelController.getLayers(), 0);
var canvas;
if (frame instanceof pskl.model.frame.RenderedFrame) {
canvas = pskl.utils.CanvasUtils.createFromImage(frame.getRenderedFrame());
} else {
canvas = pskl.utils.FrameUtils.toImage(frame);
}
return canvas.toDataURL('image/png');
2013-10-10 02:06:11 +04:00
},
getFramesheetAsPng : function () {
var renderer = new pskl.rendering.PiskelRenderer(this.piskelController);
var framesheetCanvas = renderer.renderAsCanvas();
return framesheetCanvas.toDataURL('image/png');
}
};
})();