Implemented issue #403

This commit is contained in:
Matt D 2016-05-10 21:37:00 +10:00
parent 7e88aeb9a8
commit 0e4f1046d3
5 changed files with 65 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 521 B

View File

@ -5,7 +5,8 @@
this.tools = [ this.tools = [
new pskl.tools.transform.Flip(), new pskl.tools.transform.Flip(),
new pskl.tools.transform.Rotate(), new pskl.tools.transform.Rotate(),
new pskl.tools.transform.Clone() new pskl.tools.transform.Clone(),
new pskl.tools.transform.Align()
]; ];
this.toolIconBuilder = new pskl.tools.ToolIconBuilder(); this.toolIconBuilder = new pskl.tools.ToolIconBuilder();

View File

@ -0,0 +1,19 @@
(function () {
var ns = $.namespace('pskl.tools.transform');
ns.Align = function () {
this.toolId = 'tool-align';
this.helpText = 'Align selection to the center';
this.tooltipDescriptors = [
{key : 'ctrl', description : 'Apply to all layers'},
{key : 'shift', description : 'Apply to all frames'}
];
};
pskl.utils.inherit(ns.Align, ns.AbstractTransformTool);
ns.Align.prototype.applyToolOnFrame_ = function (frame, altKey) {
ns.TransformUtils.align(frame);
};
})();

View File

@ -62,6 +62,49 @@
}); });
frame.version++; frame.version++;
return frame; return frame;
},
align : function(frame) {
// Figure out the boundary
var minx = frame.width, miny = frame.height;
var maxx = 0, maxy = 0;
for(var col = 0; col < frame.width; col++) {
for(var row = 0; row < frame.height; row++) {
if(frame.pixels[col][row] !== Constants.TRANSPARENT_COLOR) {
if(row < miny) miny = row;
if(row > maxy) maxy = row;
if(col < minx) minx = col;
if(col > maxx) maxx = col;
}
}
}
// Calculate how much to move the pixels
var bw = (maxx - minx + 1) / 2;
var bh = (maxy - miny + 1) / 2;
var fw = frame.width / 2;
var fh = frame.height / 2;
var dx = Math.floor(fw - bw - minx);
var dy = Math.floor(fh - bh - miny);
// Actually move the pixels
var clone = frame.clone();
frame.forEachPixel(function(color, x, y) {
var _x = x, _y = y;
x -= dx;
y -= dy;
if(clone.containsPixel(x, y)) {
frame.pixels[_x][_y] = clone.getPixel(x, y);
}
else {
frame.pixels[_x][_y] = Constants.TRANSPARENT_COLOR;
}
});
frame.version++;
return frame;
} }
}; };
})(); })();

View File

@ -201,6 +201,7 @@
"js/tools/drawing/ColorSwap.js", "js/tools/drawing/ColorSwap.js",
"js/tools/drawing/DitheringTool.js", "js/tools/drawing/DitheringTool.js",
"js/tools/transform/AbstractTransformTool.js", "js/tools/transform/AbstractTransformTool.js",
"js/tools/transform/Align.js",
"js/tools/transform/Clone.js", "js/tools/transform/Clone.js",
"js/tools/transform/Flip.js", "js/tools/transform/Flip.js",
"js/tools/transform/Rotate.js", "js/tools/transform/Rotate.js",