Removing states in class members and using a SimplePen#draw method instead.

This commit is contained in:
grosbouddha 2015-09-17 02:26:59 +02:00
parent 5a469202e9
commit 90c2ed3470
4 changed files with 45 additions and 45 deletions

View File

@ -16,22 +16,14 @@
/**
* @override
*/
ns.DitheringTool.prototype.getToolColor = function() {
var usePrimaryColor = (this.col_ + this.row_) % 2;
ns.DitheringTool.prototype.applyToolAt = function(col, row, frame, overlay, event) {
var usePrimaryColor = (col + row) % 2;
usePrimaryColor =
pskl.app.mouseStateService.isRightButtonPressed() ? !usePrimaryColor : usePrimaryColor;
var ditheringColor = usePrimaryColor ?
pskl.app.selectedColorsService.getPrimaryColor() :
pskl.app.selectedColorsService.getSecondaryColor();
return ditheringColor;
};
/**
* @override
*/
ns.DitheringTool.prototype.applyToolAt = function(col, row, frame, overlay, event) {
this.col_ = col;
this.row_ = row;
this.superclass.applyToolAt.call(this, col, row, frame, overlay, event);
this.draw(ditheringColor, col, row, frame, overlay);
};
})();

View File

@ -41,42 +41,44 @@
/**
* @Override
*/
ns.Lighten.prototype.getToolColor = function() {
var color = this.superclass.getToolColor.call();
ns.Lighten.prototype.applyToolAt = function(col, row, frame, overlay, event, mouseButton) {
var modifiedColor = this.getModifiedColor_(col, row, frame, overlay, event);
this.draw(modifiedColor, col, row, frame, overlay);
};
var usedPixels = this.isDarken_ ? this.usedPixels_.darken : this.usedPixels_.lighten;
var key = this.col_ + '-' + this.row_;
var doNotModify = this.isTransparent_ || (this.isSinglePass_ && usedPixels[key]);
ns.Lighten.prototype.getModifiedColor_ = function(col, row, frame, overlay, event) {
var overlayColor = overlay.getPixel(col, row);
var frameColor = frame.getPixel(col, row);
var pixelColor = overlayColor === Constants.TRANSPARENT_COLOR ? frameColor : overlayColor;
var isDarken = pskl.utils.UserAgent.isMac ? event.metaKey : event.ctrlKey;
var isTransparent = pixelColor === Constants.TRANSPARENT_COLOR;
var isSinglePass = event.shiftKey;
var usedPixels = isDarken ? this.usedPixels_.darken : this.usedPixels_.lighten;
var key = col + '-' + row;
var doNotModify = isTransparent || (isSinglePass && usedPixels[key]);
var color;
if (doNotModify) {
color = window.tinycolor(this.pixelColor_);
color = window.tinycolor(pixelColor);
} else {
var step = this.isSinglePass_ ? DEFAULT_STEP * 2 : DEFAULT_STEP;
if (this.isDarken_) {
color = window.tinycolor.darken(this.pixelColor_, step);
var step = isSinglePass ? DEFAULT_STEP * 2 : DEFAULT_STEP;
if (isDarken) {
color = window.tinycolor.darken(pixelColor, step);
} else {
color = window.tinycolor.lighten(this.pixelColor_, step);
color = window.tinycolor.lighten(pixelColor, step);
}
}
if (color) {
usedPixels[key] = true;
// Convert tinycolor color to string format.
color = color.toRgbString();
} else {
// Not sure why this check exists in the first place.
// Fallback to the always defined SimplePen tool color in this case.
color = this.getToolColor();
}
return color.toRgbString();
};
usedPixels[key] = true;
/**
* @Override
*/
ns.Lighten.prototype.applyToolAt = function(col, row, frame, overlay, event, mouseButton) {
var overlayColor = overlay.getPixel(col, row);
var frameColor = frame.getPixel(col, row);
this.col_ = col;
this.row_ = row;
this.pixelColor_ = overlayColor === Constants.TRANSPARENT_COLOR ? frameColor : overlayColor;
this.isDarken_ = pskl.utils.UserAgent.isMac ? event.metaKey : event.ctrlKey;
this.isTransparent_ = this.pixelColor_ === Constants.TRANSPARENT_COLOR;
this.isSinglePass_ = event.shiftKey;
this.superclass.applyToolAt.call(this, col, row, frame, overlay, event);
return color;
};
})();

View File

@ -22,11 +22,15 @@
* @override
*/
ns.SimplePen.prototype.applyToolAt = function(col, row, frame, overlay, event) {
var color = this.getToolColor();
this.draw(color, col, row, frame, overlay);
};
ns.SimplePen.prototype.draw = function(color, col, row, frame, overlay) {
this.previousCol = col;
this.previousRow = row;
var color = this.getToolColor();
overlay.setPixel(col, row, color);
overlay.setPixel(col, row, color);
if (color === Constants.TRANSPARENT_COLOR) {
frame.setPixel(col, row, color);
}

View File

@ -29,7 +29,9 @@
* @override
*/
ns.VerticalMirrorPen.prototype.applyToolAt = function(col, row, frame, overlay, event) {
this.superclass.applyToolAt.call(this, col, row, frame, overlay);
var color = this.getToolColor();
this.draw(color, col, row, frame, overlay);
this.backupPreviousPositions_();
var mirroredCol = this.getSymmetricCol_(col, frame);
@ -37,15 +39,15 @@
var hasCtrlKey = pskl.utils.UserAgent.isMac ? event.metaKey : event.ctrlKey;
if (!hasCtrlKey) {
this.superclass.applyToolAt.call(this, mirroredCol, row, frame, overlay);
this.draw(color, mirroredCol, row, frame, overlay);
}
if (event.shiftKey || hasCtrlKey) {
this.superclass.applyToolAt.call(this, col, mirroredRow, frame, overlay);
this.draw(color, col, mirroredRow, frame, overlay);
}
if (event.shiftKey) {
this.superclass.applyToolAt.call(this, mirroredCol, mirroredRow, frame, overlay);
this.draw(color, mirroredCol, mirroredRow, frame, overlay);
}
this.restorePreviousPositions_();