From e5cb0717e2de3563bb2fa5bc8ab037b1ffb492ce Mon Sep 17 00:00:00 2001 From: jdescottes Date: Sun, 13 Jul 2014 00:43:35 +0200 Subject: [PATCH] cleanup --- .../controller/settings/SettingsController.js | 2 +- src/js/service/FileDropperService.js | 47 +++++++++++++------ src/templates/settings.html | 16 +------ 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/js/controller/settings/SettingsController.js b/src/js/controller/settings/SettingsController.js index bd7d9c39..7b8ba906 100644 --- a/src/js/controller/settings/SettingsController.js +++ b/src/js/controller/settings/SettingsController.js @@ -10,7 +10,7 @@ template : 'templates/settings/resize.html', controller : ns.ResizeController }, - 'png' : { + 'export' : { template : 'templates/settings/export.html', controller : ns.ImageExportController }, diff --git a/src/js/service/FileDropperService.js b/src/js/service/FileDropperService.js index fb3aa97a..bf9104ad 100644 --- a/src/js/service/FileDropperService.js +++ b/src/js/service/FileDropperService.js @@ -4,6 +4,7 @@ ns.FileDropperService = function (piskelController, drawingAreaContainer) { this.piskelController = piskelController; this.drawingAreaContainer = drawingAreaContainer; + this.dropPosition_ = null; }; ns.FileDropperService.prototype.init = function () { @@ -21,8 +22,10 @@ event.preventDefault(); event.stopPropagation(); - - this.coords_ = pskl.app.drawingController.getSpriteCoordinates(event.clientX, event.clientY); + this.dropPosition_ = { + x : event.clientX, + y : event.clientY + }; var files = event.dataTransfer.files; for (var i = 0; i < files.length ; i++) { @@ -55,23 +58,13 @@ }; ns.FileDropperService.prototype.onImageLoaded_ = function () { - var frame = pskl.utils.FrameUtils.createFromImage(this.importedImage_); + var droppedFrame = 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); + var dropCoordinates = this.adjustDropPosition_(this.dropPosition_, droppedFrame); - 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); + var fColor = droppedFrame.getPixel(x-dropCoordinates.x, y-dropCoordinates.y); if (fColor && fColor != Constants.TRANSPARENT_COLOR) { currentFrame.setPixel(x, y, fColor); } @@ -83,4 +76,28 @@ }); }; + 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 + }; + }; + })(); \ No newline at end of file diff --git a/src/templates/settings.html b/src/templates/settings.html index 61367b57..13970923 100644 --- a/src/templates/settings.html +++ b/src/templates/settings.html @@ -23,7 +23,7 @@
@@ -35,19 +35,5 @@ title="Import an existing image,
an animated GIF or a .piskel file" rel="tooltip" data-placement="left">
- - \ No newline at end of file