2018-04-04 17:37:11 -04:00
|
|
|
import paper from '@scratch/paper';
|
|
|
|
import {getRaster} from '../layer';
|
2018-04-26 14:54:44 -04:00
|
|
|
import {forEachLinePoint, getBrushMark} from '../bitmap';
|
2018-04-13 11:33:47 -04:00
|
|
|
import {getGuideLayer} from '../layer';
|
2018-04-04 17:37:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tool for drawing with the bitmap brush.
|
|
|
|
*/
|
|
|
|
class BrushTool extends paper.Tool {
|
|
|
|
/**
|
2018-04-26 18:45:50 -04:00
|
|
|
* @param {!function} onUpdateImage A callback to call when the image visibly changes
|
2018-04-04 17:37:11 -04:00
|
|
|
*/
|
2018-04-26 18:45:50 -04:00
|
|
|
constructor (onUpdateImage) {
|
2018-04-04 17:37:11 -04:00
|
|
|
super();
|
2018-04-26 18:45:50 -04:00
|
|
|
this.onUpdateImage = onUpdateImage;
|
2018-04-04 17:37:11 -04:00
|
|
|
|
|
|
|
// We have to set these functions instead of just declaring them because
|
|
|
|
// paper.js tools hook up the listeners in the setter functions.
|
2018-04-13 11:33:47 -04:00
|
|
|
this.onMouseMove = this.handleMouseMove;
|
2018-04-04 17:37:11 -04:00
|
|
|
this.onMouseDown = this.handleMouseDown;
|
|
|
|
this.onMouseDrag = this.handleMouseDrag;
|
|
|
|
this.onMouseUp = this.handleMouseUp;
|
|
|
|
|
|
|
|
this.colorState = null;
|
|
|
|
this.active = false;
|
2018-04-04 23:54:41 -04:00
|
|
|
this.lastPoint = null;
|
2018-04-13 11:33:47 -04:00
|
|
|
this.cursorPreview = null;
|
2018-04-04 17:37:11 -04:00
|
|
|
}
|
2018-04-04 23:54:41 -04:00
|
|
|
setColor (color) {
|
2018-04-05 17:20:42 -04:00
|
|
|
this.color = color;
|
2018-04-04 23:54:41 -04:00
|
|
|
}
|
2018-04-12 10:56:16 -04:00
|
|
|
setBrushSize (size) {
|
|
|
|
// For performance, make sure this is an integer
|
2018-04-17 14:49:04 -04:00
|
|
|
this.size = Math.max(1, ~~size);
|
2018-04-12 10:56:16 -04:00
|
|
|
}
|
2018-04-05 17:20:42 -04:00
|
|
|
// Draw a brush mark at the given point
|
|
|
|
draw (x, y) {
|
2018-04-20 10:57:10 -04:00
|
|
|
const roundedUpRadius = Math.ceil(this.size / 2);
|
2018-04-17 14:49:04 -04:00
|
|
|
getRaster().drawImage(this.tmpCanvas, new paper.Point(~~x - roundedUpRadius, ~~y - roundedUpRadius));
|
2018-04-13 11:33:47 -04:00
|
|
|
}
|
|
|
|
updateCursorIfNeeded () {
|
2018-04-17 14:49:04 -04:00
|
|
|
if (!this.size) {
|
2018-04-13 11:33:47 -04:00
|
|
|
return;
|
|
|
|
}
|
2018-04-26 14:54:44 -04:00
|
|
|
|
2018-04-13 11:33:47 -04:00
|
|
|
// The cursor preview was unattached from the view by an outside process,
|
|
|
|
// such as changing costumes or undo.
|
|
|
|
if (this.cursorPreview && !this.cursorPreview.parent) {
|
|
|
|
this.cursorPreview = null;
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:49:04 -04:00
|
|
|
if (!this.cursorPreview || !(this.lastSize === this.size && this.lastColor === this.color)) {
|
2018-04-13 11:33:47 -04:00
|
|
|
if (this.cursorPreview) {
|
|
|
|
this.cursorPreview.remove();
|
|
|
|
}
|
|
|
|
|
2018-04-26 14:54:44 -04:00
|
|
|
this.tmpCanvas = getBrushMark(this.size, this.color);
|
2018-04-13 11:33:47 -04:00
|
|
|
this.cursorPreview = new paper.Raster(this.tmpCanvas);
|
|
|
|
this.cursorPreview.guide = true;
|
|
|
|
this.cursorPreview.parent = getGuideLayer();
|
|
|
|
this.cursorPreview.data.isHelperItem = true;
|
|
|
|
}
|
2018-04-26 14:54:44 -04:00
|
|
|
|
2018-04-17 14:49:04 -04:00
|
|
|
this.lastSize = this.size;
|
2018-04-13 11:33:47 -04:00
|
|
|
this.lastColor = this.color;
|
|
|
|
}
|
|
|
|
handleMouseMove (event) {
|
|
|
|
this.updateCursorIfNeeded();
|
|
|
|
this.cursorPreview.position = new paper.Point(~~event.point.x, ~~event.point.y);
|
2018-04-05 17:20:42 -04:00
|
|
|
}
|
2018-04-04 17:37:11 -04:00
|
|
|
handleMouseDown (event) {
|
|
|
|
if (event.event.button > 0) return; // only first mouse button
|
|
|
|
this.active = true;
|
2018-04-13 11:33:47 -04:00
|
|
|
|
|
|
|
this.cursorPreview.remove();
|
2018-04-05 17:20:42 -04:00
|
|
|
|
2018-04-13 11:33:47 -04:00
|
|
|
this.draw(event.point.x, event.point.y);
|
2018-04-04 23:54:41 -04:00
|
|
|
this.lastPoint = event.point;
|
2018-04-04 17:37:11 -04:00
|
|
|
}
|
|
|
|
handleMouseDrag (event) {
|
|
|
|
if (event.event.button > 0 || !this.active) return; // only first mouse button
|
|
|
|
|
2018-04-20 10:57:10 -04:00
|
|
|
forEachLinePoint(this.lastPoint, event.point, this.draw.bind(this));
|
2018-04-04 23:54:41 -04:00
|
|
|
this.lastPoint = event.point;
|
2018-04-04 17:37:11 -04:00
|
|
|
}
|
|
|
|
handleMouseUp (event) {
|
|
|
|
if (event.event.button > 0 || !this.active) return; // only first mouse button
|
2018-04-05 17:20:42 -04:00
|
|
|
|
2018-04-20 10:57:10 -04:00
|
|
|
forEachLinePoint(this.lastPoint, event.point, this.draw.bind(this));
|
2018-04-26 18:45:50 -04:00
|
|
|
this.onUpdateImage();
|
2018-04-05 17:20:42 -04:00
|
|
|
|
2018-04-04 23:54:41 -04:00
|
|
|
this.lastPoint = null;
|
2018-04-04 17:37:11 -04:00
|
|
|
this.active = false;
|
2018-04-13 11:33:47 -04:00
|
|
|
|
|
|
|
this.updateCursorIfNeeded();
|
|
|
|
this.cursorPreview.position = new paper.Point(~~event.point.x, ~~event.point.y);
|
2018-04-04 17:37:11 -04:00
|
|
|
}
|
|
|
|
deactivateTool () {
|
2018-04-13 11:33:47 -04:00
|
|
|
this.active = false;
|
|
|
|
this.tmpCanvas = null;
|
2018-04-17 14:49:04 -04:00
|
|
|
if (this.cursorPreview) {
|
|
|
|
this.cursorPreview.remove();
|
|
|
|
this.cursorPreview = null;
|
|
|
|
}
|
2018-04-04 17:37:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BrushTool;
|