mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Issue #258 : Move resize method to utils + add unit test
This commit is contained in:
@@ -4,10 +4,15 @@
|
||||
var MIN_PENSIZE = 1;
|
||||
var MAX_PENSIZE = 4;
|
||||
|
||||
ns.PenSizeService = function () {};
|
||||
/**
|
||||
* Service to retrieve and modify the current pen size.
|
||||
*/
|
||||
ns.PenSizeService = function () {
|
||||
this.size = MIN_PENSIZE;
|
||||
};
|
||||
|
||||
ns.PenSizeService.prototype.init = function () {
|
||||
this.size = pskl.UserSettings.get('PEN_SIZE');
|
||||
this.size = pskl.UserSettings.get(pskl.UserSettings.PEN_SIZE);
|
||||
|
||||
var shortcuts = pskl.service.keyboard.Shortcuts;
|
||||
pskl.app.shortcutService.registerShortcut(shortcuts.MISC.INCREASE_PENSIZE, this.increasePenSize_.bind(this));
|
||||
@@ -22,10 +27,14 @@
|
||||
this.setPenSize(this.size - 1);
|
||||
};
|
||||
|
||||
ns.PenSizeService.prototype.getPenSize = function () {
|
||||
return this.size;
|
||||
};
|
||||
|
||||
ns.PenSizeService.prototype.setPenSize = function (size) {
|
||||
if (this.isPenSizeValid_(size) && size != this.size) {
|
||||
this.size = size;
|
||||
pskl.UserSettings.set('PEN_SIZE', size);
|
||||
pskl.UserSettings.set(pskl.UserSettings.PEN_SIZE, size);
|
||||
$.publish(Events.PEN_SIZE_CHANGED);
|
||||
}
|
||||
};
|
||||
@@ -38,32 +47,4 @@
|
||||
return size >= MIN_PENSIZE && size <= MAX_PENSIZE;
|
||||
};
|
||||
|
||||
ns.PenSizeService.prototype.getPenSize = function () {
|
||||
return this.size;
|
||||
};
|
||||
|
||||
ns.PenSizeService.prototype.getPixelsForPenSize = function (col, row, penSize) {
|
||||
var size = penSize || this.size;
|
||||
if (size == 1) {
|
||||
return [[col, row]];
|
||||
} else if (size == 2) {
|
||||
return [
|
||||
[col, row], [col + 1, row],
|
||||
[col, row + 1], [col + 1, row + 1]
|
||||
];
|
||||
} else if (size == 3) {
|
||||
return [
|
||||
[col - 1, row - 1], [col, row - 1], [col + 1, row - 1],
|
||||
[col - 1, row + 0], [col, row + 0], [col + 1, row + 0],
|
||||
[col - 1, row + 1], [col, row + 1], [col + 1, row + 1],
|
||||
];
|
||||
} else if (size == 4) {
|
||||
return [
|
||||
[col - 1, row - 1], [col, row - 1], [col + 1, row - 1], [col + 2, row - 1],
|
||||
[col - 1, row + 0], [col, row + 0], [col + 1, row + 0], [col + 2, row + 0],
|
||||
[col - 1, row + 1], [col, row + 1], [col + 1, row + 1], [col + 2, row + 1],
|
||||
[col - 1, row + 2], [col, row + 2], [col + 1, row + 2], [col + 2, row + 2],
|
||||
];
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
@@ -49,15 +49,11 @@
|
||||
}
|
||||
|
||||
var frameColor = frame.getPixel(col, row);
|
||||
|
||||
if (this.supportsDynamicPenSize()) {
|
||||
var pixels = pskl.app.penSizeService.getPixelsForPenSize(col, row);
|
||||
pixels.forEach(function (p) {
|
||||
overlay.setPixel(p[0], p[1], this.getHighlightColor_(frameColor));
|
||||
}.bind(this));
|
||||
} else {
|
||||
overlay.setPixel(col, row, this.getHighlightColor_(frameColor));
|
||||
}
|
||||
var highlightColor = this.getHighlightColor_(frameColor);
|
||||
var size = this.supportsDynamicPenSize() ? pskl.app.penSizeService.getPenSize() : 1;
|
||||
pskl.PixelUtils.resizePixel(col, row, size).forEach(function (point) {
|
||||
overlay.setPixel(point[0], point[1], highlightColor);
|
||||
});
|
||||
|
||||
this.highlightedPixelCol = col;
|
||||
this.highlightedPixelRow = row;
|
||||
|
@@ -20,17 +20,10 @@
|
||||
* @override
|
||||
*/
|
||||
ns.Circle.prototype.draw = function (col, row, color, targetFrame, penSize) {
|
||||
var circlePoints = this.getCirclePixels_(this.startCol, this.startRow, col, row);
|
||||
|
||||
var applyDraw = function (p) {
|
||||
targetFrame.setPixel(p[0], p[1], color);
|
||||
}.bind(this);
|
||||
|
||||
for (var i = 0 ; i < circlePoints.length ; i++) {
|
||||
// Change model:
|
||||
var pixels = pskl.app.penSizeService.getPixelsForPenSize(circlePoints[i].col, circlePoints[i].row, penSize);
|
||||
pixels.forEach(applyDraw);
|
||||
}
|
||||
var circlePixels = this.getCirclePixels_(this.startCol, this.startRow, col, row);
|
||||
pskl.PixelUtils.resizePixels(circlePixels, penSize).forEach(function (point) {
|
||||
targetFrame.setPixel(point[0], point[1], color);
|
||||
});
|
||||
};
|
||||
|
||||
ns.Circle.prototype.getCirclePixels_ = function (x0, y0, x1, y1) {
|
||||
|
@@ -26,9 +26,10 @@
|
||||
this.previousCol = col;
|
||||
this.previousRow = row;
|
||||
|
||||
var pixels = pskl.app.penSizeService.getPixelsForPenSize(col, row);
|
||||
pixels.forEach(function (p) {
|
||||
this.applyToolOnPixel(p[0], p[1], frame, overlay, event);
|
||||
var penSize = pskl.app.penSizeService.getPenSize();
|
||||
var points = pskl.PixelUtils.resizePixel(col, row, penSize);
|
||||
points.forEach(function (point) {
|
||||
this.applyToolOnPixel(point[0], point[1], frame, overlay, event);
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
|
@@ -30,10 +30,11 @@
|
||||
this.previousCol = col;
|
||||
this.previousRow = row;
|
||||
|
||||
var pixels = pskl.app.penSizeService.getPixelsForPenSize(col, row);
|
||||
pixels.forEach(function (p) {
|
||||
var modifiedColor = this.getModifiedColor_(p[0], p[1], frame, overlay, event);
|
||||
this.draw(modifiedColor, p[0], p[1], frame, overlay);
|
||||
var penSize = pskl.app.penSizeService.getPenSize();
|
||||
var points = pskl.PixelUtils.resizePixel(col, row, penSize);
|
||||
points.forEach(function (point) {
|
||||
var modifiedColor = this.getModifiedColor_(point[0], point[1], frame, overlay, event);
|
||||
this.draw(modifiedColor, point[0], point[1], frame, overlay);
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
|
@@ -20,16 +20,10 @@
|
||||
* @override
|
||||
*/
|
||||
ns.Rectangle.prototype.draw = function (col, row, color, targetFrame, penSize) {
|
||||
var strokePoints = pskl.PixelUtils.getBoundRectanglePixels(this.startCol, this.startRow, col, row);
|
||||
var rectanglePixels = pskl.PixelUtils.getBoundRectanglePixels(this.startCol, this.startRow, col, row);
|
||||
|
||||
var applyDraw = function (p) {
|
||||
targetFrame.setPixel(p[0], p[1], color);
|
||||
}.bind(this);
|
||||
|
||||
for (var i = 0 ; i < strokePoints.length ; i++) {
|
||||
// Change model:
|
||||
var pixels = pskl.app.penSizeService.getPixelsForPenSize(strokePoints[i].col, strokePoints[i].row, penSize);
|
||||
pixels.forEach(applyDraw);
|
||||
}
|
||||
pskl.PixelUtils.resizePixels(rectanglePixels, penSize).forEach(function (point) {
|
||||
targetFrame.setPixel(point[0], point[1], color);
|
||||
});
|
||||
};
|
||||
})();
|
||||
|
@@ -29,7 +29,8 @@
|
||||
this.startRow = row;
|
||||
|
||||
// Drawing the first point of the rectangle in the fake overlay canvas:
|
||||
overlay.setPixel(col, row, this.getToolColor());
|
||||
var penSize = pskl.app.penSizeService.getPenSize();
|
||||
this.draw(col, row, this.getToolColor(), overlay, penSize);
|
||||
};
|
||||
|
||||
ns.ShapeTool.prototype.moveToolAt = function(col, row, frame, overlay, event) {
|
||||
@@ -43,7 +44,8 @@
|
||||
}
|
||||
|
||||
// draw in overlay
|
||||
this.draw(coords.col, coords.row, color, overlay);
|
||||
var penSize = pskl.app.penSizeService.getPenSize();
|
||||
this.draw(coords.col, coords.row, color, overlay, penSize);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -53,7 +55,8 @@
|
||||
overlay.clear();
|
||||
var coords = this.getCoordinates_(col, row, event);
|
||||
var color = this.getToolColor();
|
||||
this.draw(coords.col, coords.row, color, frame);
|
||||
var penSize = pskl.app.penSizeService.getPenSize();
|
||||
this.draw(coords.col, coords.row, color, frame, penSize);
|
||||
|
||||
$.publish(Events.DRAG_END);
|
||||
this.raiseSaveStateEvent({
|
||||
@@ -62,7 +65,7 @@
|
||||
startCol : this.startCol,
|
||||
startRow : this.startRow,
|
||||
color : color,
|
||||
penSize : pskl.app.penSizeService.getPenSize()
|
||||
penSize : penSize
|
||||
});
|
||||
};
|
||||
|
||||
|
@@ -36,9 +36,10 @@
|
||||
};
|
||||
|
||||
ns.SimplePen.prototype.drawUsingPenSize = function(color, col, row, frame, overlay) {
|
||||
var pixels = pskl.app.penSizeService.getPixelsForPenSize(col, row);
|
||||
pixels.forEach(function (p) {
|
||||
this.draw(color, p[0], p[1], frame, overlay);
|
||||
var penSize = pskl.app.penSizeService.getPenSize();
|
||||
var points = pskl.PixelUtils.resizePixel(col, row, penSize);
|
||||
points.forEach(function (point) {
|
||||
this.draw(color, point[0], point[1], frame, overlay);
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
|
@@ -47,25 +47,9 @@
|
||||
ns.Stroke.prototype.moveToolAt = function(col, row, frame, overlay, event) {
|
||||
overlay.clear();
|
||||
|
||||
if (event.shiftKey) {
|
||||
var coords = this.getStraightLineCoordinates_(col, row);
|
||||
col = coords. |