piskel/src/js/service/FileDropperService.js

112 lines
3.8 KiB
JavaScript
Raw Normal View History

2014-07-12 02:41:39 +04:00
(function () {
var ns = $.namespace('pskl.service');
ns.FileDropperService = function (piskelController, drawingAreaContainer) {
2014-07-12 02:41:39 +04:00
this.piskelController = piskelController;
this.drawingAreaContainer = drawingAreaContainer;
2014-07-13 02:43:35 +04:00
this.dropPosition_ = null;
2014-07-12 02:41:39 +04:00
};
ns.FileDropperService.prototype.init = function () {
2014-07-12 02:41:39 +04:00
document.body.addEventListener('drop', this.onFileDrop.bind(this), false);
document.body.addEventListener('dragover', this.onFileDragOver.bind(this), false);
};
ns.FileDropperService.prototype.onFileDragOver = function (event) {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
};
ns.FileDropperService.prototype.onFileDrop = function (event) {
2014-07-12 02:41:39 +04:00
event.preventDefault();
event.stopPropagation();
2014-07-13 02:43:35 +04:00
this.dropPosition_ = {
x : event.clientX,
y : event.clientY
};
2014-07-12 02:41:39 +04:00
2014-07-13 02:21:36 +04:00
var files = event.dataTransfer.files;
2014-07-12 02:41:39 +04:00
for (var i = 0; i < files.length ; i++) {
var file = files[i];
var isImage = file.type.indexOf('image') === 0;
2014-09-18 09:18:07 +04:00
var isPiskel = /\.piskel$/i.test(file.name);
2014-09-30 02:58:15 +04:00
var isPalette = /\.(gpl|txt|pal)$/i.test(file.name);
2014-07-12 02:41:39 +04:00
if (isImage) {
this.readImageFile_(file);
2014-09-18 09:18:07 +04:00
} else if (isPiskel) {
pskl.utils.PiskelFileUtils.loadFromFile(file, this.onPiskelFileLoaded_);
2014-09-18 09:18:07 +04:00
} else if (isPalette) {
pskl.app.paletteImportService.read(file, this.onPaletteLoaded_.bind(this));
2014-07-12 02:41:39 +04:00
}
}
};
2014-09-18 09:18:07 +04:00
ns.FileDropperService.prototype.readImageFile_ = function (imageFile) {
pskl.utils.FileUtils.readFile(imageFile, this.processImageSource_.bind(this));
};
ns.FileDropperService.prototype.onPaletteLoaded_ = function (palette) {
pskl.app.paletteService.savePalette(palette);
pskl.UserSettings.set(pskl.UserSettings.SELECTED_PALETTE, palette.id);
};
ns.FileDropperService.prototype.onPiskelFileLoaded_ = function (piskel, descriptor, fps) {
if (window.confirm('This will replace your current animation')) {
piskel.setDescriptor(descriptor);
pskl.app.piskelController.setPiskel(piskel);
pskl.app.previewController.setFPS(fps);
}
2014-07-12 02:41:39 +04:00
};
ns.FileDropperService.prototype.processImageSource_ = function (imageSource) {
2014-07-12 02:41:39 +04:00
this.importedImage_ = new Image();
this.importedImage_.onload = this.onImageLoaded_.bind(this);
this.importedImage_.src = imageSource;
};
ns.FileDropperService.prototype.onImageLoaded_ = function () {
2014-07-13 02:43:35 +04:00
var droppedFrame = pskl.utils.FrameUtils.createFromImage(this.importedImage_);
2014-07-12 02:41:39 +04:00
var currentFrame = this.piskelController.getCurrentFrame();
2014-07-13 02:43:35 +04:00
var dropCoordinates = this.adjustDropPosition_(this.dropPosition_, droppedFrame);
2014-07-12 02:41:39 +04:00
currentFrame.forEachPixel(function (color, x, y) {
2014-07-13 02:43:35 +04:00
var fColor = droppedFrame.getPixel(x-dropCoordinates.x, y-dropCoordinates.y);
2014-07-12 02:41:39 +04:00
if (fColor && fColor != Constants.TRANSPARENT_COLOR) {
currentFrame.setPixel(x, y, fColor);
}
});
$.publish(Events.PISKEL_RESET);
$.publish(Events.PISKEL_SAVE_STATE, {
type : pskl.service.HistoryService.SNAPSHOT
});
};
2014-07-13 02:43:35 +04:00
ns.FileDropperService.prototype.adjustDropPosition_ = function (position, droppedFrame) {
var framePosition = pskl.app.drawingController.getSpriteCoordinates(position.x, position.y);
var xCoord = framePosition.x - Math.floor(droppedFrame.width/2);
var yCoord = framePosition.y - Math.floor(droppedFrame.height/2);
xCoord = Math.max(0, xCoord);
yCoord = Math.max(0, yCoord);
var currentFrame = this.piskelController.getCurrentFrame();
if (droppedFrame.width <= currentFrame.width) {
xCoord = Math.min(xCoord, currentFrame.width - droppedFrame.width);
}
if (droppedFrame.height <= currentFrame.height) {
yCoord = Math.min(yCoord, currentFrame.height - droppedFrame.height);
}
return {
x : xCoord,
y : yCoord
};
};
2014-07-12 02:41:39 +04:00
})();