2014-07-12 02:41:39 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.service');
|
|
|
|
|
2014-07-12 17:34:50 +04:00
|
|
|
ns.FileDropperService = function (piskelController, drawingAreaContainer) {
|
2014-07-12 02:41:39 +04:00
|
|
|
this.piskelController = piskelController;
|
|
|
|
this.drawingAreaContainer = drawingAreaContainer;
|
|
|
|
};
|
|
|
|
|
2014-07-12 17:34:50 +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);
|
|
|
|
};
|
|
|
|
|
2014-07-12 17:34:50 +04:00
|
|
|
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:21:36 +04:00
|
|
|
this.coords_ = pskl.app.drawingController.getSpriteCoordinates(event.clientX, 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;
|
|
|
|
if (isImage) {
|
|
|
|
this.readImageFile_(file);
|
2014-07-12 17:34:50 +04:00
|
|
|
} else if (/\.piskel$/i.test(file.name)) {
|
|
|
|
pskl.utils.PiskelFileUtils.loadFromFile(file, this.onPiskelFileLoaded_);
|
2014-07-12 02:41:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-12 17:34:50 +04:00
|
|
|
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.animationController.setFPS(fps);
|
|
|
|
}
|
2014-07-12 02:41:39 +04:00
|
|
|
};
|
|
|
|
|
2014-07-12 17:34:50 +04:00
|
|
|
ns.FileDropperService.prototype.readImageFile_ = function (imageFile) {
|
2014-07-12 02:41:39 +04:00
|
|
|
pskl.utils.FileUtils.readFile(imageFile, this.processImageSource_.bind(this));
|
|
|
|
};
|
|
|
|
|
2014-07-12 17:34:50 +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;
|
|
|
|
};
|
|
|
|
|
2014-07-12 17:34:50 +04:00
|
|
|
ns.FileDropperService.prototype.onImageLoaded_ = function () {
|
2014-07-12 02:41:39 +04:00
|
|
|
var frame = pskl.utils.FrameUtils.createFromImage(this.importedImage_);
|
|
|
|
var currentFrame = this.piskelController.getCurrentFrame();
|
|
|
|
|
|
|
|
var xCoord = this.coords_.x - Math.floor(frame.width/2);
|
|
|
|
var yCoord = this.coords_.y - Math.floor(frame.height/2);
|
|
|
|
xCoord = Math.max(0, xCoord);
|
|
|
|
yCoord = Math.max(0, yCoord);
|
|
|
|
|
|
|
|
if (frame.width <= currentFrame.width) {
|
|
|
|
xCoord = Math.min(xCoord, currentFrame.width - frame.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.height <= currentFrame.height) {
|
|
|
|
yCoord = Math.min(yCoord, currentFrame.height - frame.height);
|
|
|
|
}
|
|
|
|
currentFrame.forEachPixel(function (color, x, y) {
|
|
|
|
var fColor = frame.getPixel(x-xCoord, y-yCoord);
|
|
|
|
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
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|