Change broad brush helper and segment brush helper into their own classes, instead of adding functions to tool

This commit is contained in:
DD Liu 2017-08-16 17:44:52 -04:00
parent 33a01c1396
commit 4ea7d154ee
7 changed files with 380 additions and 363 deletions

View file

@ -4,9 +4,7 @@ import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
import Modes from '../modes/modes';
import Blobbiness from '../modes/blob';
import BrushModeReducer from '../reducers/brush-mode';
import {changeBrushSize} from '../reducers/brush-mode';
import paper from 'paper';
class BrushMode extends React.Component {
static get MODE () {

View file

@ -5,7 +5,6 @@ import bindAll from 'lodash.bindall';
import Modes from '../modes/modes';
import Blobbiness from '../modes/blob';
import EraserModeReducer from '../reducers/eraser-mode';
import paper from 'paper';
class EraserMode extends React.Component {
static get MODE () {

View file

@ -1,7 +1,8 @@
import paper from 'paper';
import log from '../log/log';
import broadBrushHelper from './broad-brush-helper';
import segmentBrushHelper from './segment-brush-helper';
import BroadBrushHelper from './broad-brush-helper';
import SegmentBrushHelper from './segment-brush-helper';
import {styleCursorPreview} from './style-path';
/**
* Shared code for the brush and eraser mode. Adds functions on the paper tool object
@ -9,7 +10,6 @@ import segmentBrushHelper from './segment-brush-helper';
* based on the brushSize in the state.
*/
class Blobbiness {
static get BROAD () {
return 'broadbrush';
}
@ -24,18 +24,97 @@ class Blobbiness {
return 9;
}
setOptions (options) {
if (this.tool) {
this.tool.options = options;
this.tool.resizeCursorIfNeeded();
constructor () {
this.broadBrushHelper = new BroadBrushHelper();
this.segmentBrushHelper = new SegmentBrushHelper();
}
setOptions (options) {
this.options = options;
this.resizeCursorIfNeeded();
}
activateTool (isEraser, options) {
this.tool = new paper.Tool();
this.isEraser = isEraser;
this.cursorPreviewLastPoint = new paper.Point(-10000, -10000);
this.setOptions(options);
styleCursorPreview(this.cursorPreview);
this.tool.fixedDistance = 1;
const blob = this;
this.tool.onMouseMove = function (event) {
blob.resizeCursorIfNeeded(event.point);
styleCursorPreview(blob.cursorPreview);
blob.cursorPreview.bringToFront();
blob.cursorPreview.position = event.point;
};
this.tool.onMouseDown = function (event) {
blob.resizeCursorIfNeeded(event.point);
if (event.event.button > 0) return; // only first mouse button
if (blob.options.brushSize < Blobbiness.THRESHOLD) {
blob.brush = Blobbiness.BROAD;
blob.broadBrushHelper.onBroadMouseDown(event, blob.tool, blob.options);
} else {
blob.brush = Blobbiness.SEGMENT;
blob.segmentBrushHelper.onSegmentMouseDown(event, blob.tool, blob.options);
}
blob.cursorPreview.bringToFront();
blob.cursorPreview.position = event.point;
paper.view.draw();
};
this.tool.onMouseDrag = function (event) {
blob.resizeCursorIfNeeded(event.point);
if (event.event.button > 0) return; // only first mouse button
if (blob.brush === Blobbiness.BROAD) {
blob.broadBrushHelper.onBroadMouseDrag(event, blob.options);
} else if (blob.brush === Blobbiness.SEGMENT) {
blob.segmentBrushHelper.onSegmentMouseDrag(event, blob.options);
} else {
log.warn(`Brush type does not exist: ${blob.brush}`);
}
blob.cursorPreview.bringToFront();
blob.cursorPreview.position = event.point;
paper.view.draw();
};
this.tool.onMouseUp = function (event) {
blob.resizeCursorIfNeeded(event.point);
if (event.event.button > 0) return; // only first mouse button
let lastPath;
if (blob.brush === Blobbiness.BROAD) {
lastPath = blob.broadBrushHelper.onBroadMouseUp(event, blob.tool, blob.options);
} else if (blob.brush === Blobbiness.SEGMENT) {
lastPath = blob.segmentBrushHelper.onSegmentMouseUp(event);
} else {
log.warn(`Brush type does not exist: ${blob.brush}`);
}
if (isEraser) {
blob.mergeEraser(lastPath);
} else {
blob.mergeBrush(lastPath);
}
blob.cursorPreview.bringToFront();
blob.cursorPreview.position = event.point;
// Reset
blob.brush = null;
this.fixedDistance = 1;
};
}
resizeCursorIfNeeded (point) {
if (!this.options) {
return;
}
tool.cursorPreviewLastPoint = new paper.Point(-10000, -10000);
tool.resizeCursorIfNeeded = function (point) {
if (typeof point === 'undefined') {
point = this.cursorPreviewLastPoint;
} else {
@ -56,129 +135,37 @@ class Blobbiness {
this.cursorPreview = newPreview;
}
this.brushSize = this.options.brushSize;
};
this.setOptions(options);
tool.stylePath = function (path) {
if (isEraser) {
path.fillColor = 'white';
if (path === this.cursorPreview) {
path.strokeColor = 'cornflowerblue';
path.strokeWidth = 1;
}
} else {
// TODO: Add back brush styling. Keep a separate active toolbar style for brush vs pen.
// path = pg.stylebar.applyActiveToolbarStyle(path);
path.fillColor = 'black';
if (path === this.cursorPreview) {
path.strokeColor = 'cornflowerblue';
path.strokeWidth = 1;
}
}
};
tool.stylePath(this.tool.cursorPreview);
tool.fixedDistance = 1;
broadBrushHelper(tool);
segmentBrushHelper(tool);
tool.onMouseMove = function (event) {
tool.resizeCursorIfNeeded(event.point);
tool.stylePath(this.cursorPreview);
this.cursorPreview.bringToFront();
this.cursorPreview.position = event.point;
};
tool.onMouseDown = function (event) {
tool.resizeCursorIfNeeded(event.point);
if (event.event.button > 0) return; // only first mouse button
if (this.options.brushSize < Blobbiness.THRESHOLD) {
this.brush = Blobbiness.BROAD;
this.onBroadMouseDown(event);
} else {
this.brush = Blobbiness.SEGMENT;
this.onSegmentMouseDown(event);
}
this.cursorPreview.bringToFront();
this.cursorPreview.position = event.point;
paper.view.draw();
};
tool.onMouseDrag = function (event) {
tool.resizeCursorIfNeeded(event.point);
if (event.event.button > 0) return; // only first mouse button
if (this.brush === Blobbiness.BROAD) {
this.onBroadMouseDrag(event);
} else if (this.brush === Blobbiness.SEGMENT) {
this.onSegmentMouseDrag(event);
} else {
log.warn(`Brush type does not exist: ${this.brush}`);
}
this.cursorPreview.bringToFront();
this.cursorPreview.position = event.point;
paper.view.draw();
};
mergeBrush (lastPath) {
const blob = this;
tool.onMouseUp = function (event) {
tool.resizeCursorIfNeeded(event.point);
if (event.event.button > 0) return; // only first mouse button
let lastPath;
if (this.brush === Blobbiness.BROAD) {
lastPath = this.onBroadMouseUp(event);
} else if (this.brush === Blobbiness.SEGMENT) {
lastPath = this.onSegmentMouseUp(event);
} else {
log.warn(`Brush type does not exist: ${this.brush}`);
}
if (isEraser) {
tool.mergeEraser(lastPath);
} else {
tool.mergeBrush(lastPath);
}
this.cursorPreview.bringToFront();
this.cursorPreview.position = event.point;
// Reset
this.brush = null;
tool.fixedDistance = 1;
};
tool.mergeBrush = function (lastPath) {
// Get all path items to merge with
const paths = paper.project.getItems({
match: function (item) {
return tool.isMergeable(lastPath, item);
return blob.isMergeable(lastPath, item);
}
});
let mergedPath = lastPath;
let i;
// Move down z order to first overlapping item
for (i = paths.length - 1; i >= 0 && !tool.touches(paths[i], lastPath); i--) {
for (i = paths.length - 1; i >= 0 && !this.touches(paths[i], lastPath); i--) {
continue;
}
let mergedPathIndex = i;
for (; i >= 0; i--) {
if (!tool.touches(paths[i], lastPath)) {
if (!this.touches(paths[i], lastPath)) {
continue;
}
if (!paths[i].getFillColor()) {
// Ignore for merge. Paths without fill need to be in paths though,
// since they can visibly change if z order changes
} else if (tool.colorMatch(paths[i], lastPath)) {
} else if (this.colorMatch(paths[i], lastPath)) {
// Make sure the new shape isn't overlapped by anything that would
// visibly change if we change its z order
for (let j = mergedPathIndex; j > i; j--) {
if (tool.touches(paths[j], paths[i])) {
if (this.touches(paths[j], paths[i])) {
continue;
}
}
@ -200,14 +187,16 @@ class Blobbiness {
}
// TODO: Add back undo
// pg.undo.snapshot('broadbrush');
};
}
mergeEraser (lastPath) {
const blob = this;
tool.mergeEraser = function (lastPath) {
// Get all path items to merge with
// If there are selected items, try to erase from amongst those.
let items = paper.project.getItems({
match: function (item) {
return item.selected && tool.isMergeable(lastPath, item) && tool.touches(lastPath, item);
return item.selected && blob.isMergeable(lastPath, item) && blob.touches(lastPath, item);
}
});
// Eraser didn't hit anything selected, so assume they meant to erase from all instead of from subset
@ -217,7 +206,7 @@ class Blobbiness {
// pg.selection.clearSelection();
items = paper.project.getItems({
match: function (item) {
return tool.isMergeable(lastPath, item) && tool.touches(lastPath, item);
return blob.isMergeable(lastPath, item) && blob.touches(lastPath, item);
}
});
}
@ -278,7 +267,7 @@ class Blobbiness {
let newCw = cw;
for (let k = ccwChildren.length - 1; k >= 0; k--) {
const ccw = ccwChildren[k];
if (tool.firstEnclosesSecond(ccw, cw) || tool.firstEnclosesSecond(cw, ccw)) {
if (this.firstEnclosesSecond(ccw, cw) || this.firstEnclosesSecond(cw, ccw)) {
const temp = newCw.subtract(ccw);
temp.insertAbove(newCw);
newCw.remove();
@ -295,27 +284,27 @@ class Blobbiness {
lastPath.remove();
// TODO: Add back undo handling
// pg.undo.snapshot('eraser');
};
}
tool.colorMatch = function (existingPath, addedPath) {
colorMatch (existingPath, addedPath) {
// Note: transparent fill colors do notdetect as touching
return existingPath.getFillColor().equals(addedPath.getFillColor()) &&
(addedPath.getStrokeColor() === existingPath.getStrokeColor() || // both null
(addedPath.getStrokeColor() &&
addedPath.getStrokeColor().equals(existingPath.getStrokeColor()))) &&
addedPath.getStrokeWidth() === existingPath.getStrokeWidth() &&
tool.touches(existingPath, addedPath);
};
this.touches(existingPath, addedPath);
}
tool.touches = function (path1, path2) {
touches (path1, path2) {
// Two shapes are touching if their paths intersect
if (path1 && path2 && path1.intersects(path2)) {
return true;
}
return tool.firstEnclosesSecond(path1, path2) || tool.firstEnclosesSecond(path2, path1);
};
return this.firstEnclosesSecond(path1, path2) || this.firstEnclosesSecond(path2, path1);
}
tool.firstEnclosesSecond = function (path1, path2) {
firstEnclosesSecond (path1, path2) {
// Two shapes are also touching if one is completely inside the other
if (path1 && path2 && path2.firstSegment && path2.firstSegment.point &&
path1.hitTest(path2.firstSegment.point)) {
@ -323,21 +312,18 @@ class Blobbiness {
}
// TODO: clean up these no point paths
return false;
};
}
tool.isMergeable = function (newPath, existingPath) {
isMergeable (newPath, existingPath) {
return existingPath instanceof paper.PathItem && // path or compound path
existingPath !== this.cursorPreview && // don't merge with the mouse preview
existingPath !== newPath && // don't merge with self
existingPath.parent instanceof paper.Layer; // don't merge with nested in group
};
}
deactivateTool () {
if (this.tool) {
this.tool.cursorPreview.remove();
this.tool.remove();
}
this.cursorPreview.remove();
this.remove();
}
}

View file

@ -1,8 +1,9 @@
// Broadbrush based on http://paperjs.org/tutorials/interaction/working-with-mouse-vectors/
import paper from 'paper';
import {stylePath} from './style-path';
/**
* Applies broad brush functions to the tool. Call them when the corresponding mouse event happens
* Broad brush functions to add as listeners on the mouse. Call them when the corresponding mouse event happens
* to get the broad brush behavior.
*
* Broad brush draws strokes by drawing points equidistant from the mouse event, perpendicular to the
@ -11,101 +12,103 @@ import paper from 'paper';
*
* @param {!Tool} tool paper.js mouse object
*/
const broadBrushHelper = function (tool) {
let lastPoint;
let secondLastPoint;
let finalPath;
class BroadBrushHelper {
constructor () {
this.lastPoint = null;
this.secondLastPoint = null;
this.finalPath = null;
}
tool.onBroadMouseDown = function (event) {
onBroadMouseDown (event, tool, options) {
tool.minDistance = 1;
tool.maxDistance = this.options.brushSize;
tool.maxDistance = options.brushSize;
if (event.event.button > 0) return; // only first mouse button
finalPath = new paper.Path();
tool.stylePath(finalPath);
finalPath.add(event.point);
lastPoint = secondLastPoint = event.point;
};
this.finalPath = new paper.Path();
stylePath(this.finalPath);
this.finalPath.add(event.point);
this.lastPoint = this.secondLastPoint = event.point;
}
tool.onBroadMouseDrag = function (event) {
const step = (event.delta).normalize(this.options.brushSize / 2);
onBroadMouseDrag (event, options) {
const step = (event.delta).normalize(options.brushSize / 2);
// Move the first point out away from the drag so that the end of the path is rounded
if (finalPath.segments && finalPath.segments.length === 1) {
const removedPoint = finalPath.removeSegment(0).point;
if (this.finalPath.segments && this.finalPath.segments.length === 1) {
const removedPoint = this.finalPath.removeSegment(0).point;
// Add handles to round the end caps
const handleVec = step.clone();
handleVec.length = this.options.brushSize / 2;
handleVec.length = options.brushSize / 2;
handleVec.angle += 90;
finalPath.add(new paper.Segment(removedPoint.subtract(step), -handleVec, handleVec));
this.finalPath.add(new paper.Segment(removedPoint.subtract(step), -handleVec, handleVec));
}
step.angle += 90;
const top = event.middlePoint.add(step);
const bottom = event.middlePoint.subtract(step);
if (finalPath.segments.length > 3) {
finalPath.removeSegment(finalPath.segments.length - 1);
finalPath.removeSegment(0);
if (this.finalPath.segments.length > 3) {
this.finalPath.removeSegment(this.finalPath.segments.length - 1);
this.finalPath.removeSegment(0);
}
finalPath.add(top);
finalPath.add(event.point.add(step));
finalPath.insert(0, bottom);
finalPath.insert(0, event.point.subtract(step));
if (finalPath.segments.length === 5) {
this.finalPath.add(top);
this.finalPath.add(event.point.add(step));
this.finalPath.insert(0, bottom);
this.finalPath.insert(0, event.point.subtract(step));
if (this.finalPath.segments.length === 5) {
// Flatten is necessary to prevent smooth from getting rid of the effect
// of the handles on the first point.
finalPath.flatten(Math.min(5, this.options.brushSize / 5));
this.finalPath.flatten(Math.min(5, options.brushSize / 5));
}
this.finalPath.smooth();
this.lastPoint = event.point;
this.secondLastPoint = event.lastPoint;
}
finalPath.smooth();
lastPoint = event.point;
secondLastPoint = event.lastPoint;
};
tool.onBroadMouseUp = function (event) {
onBroadMouseUp (event, tool, options) {
// If the mouse up is at the same point as the mouse drag event then we need
// the second to last point to get the right direction vector for the end cap
if (event.point.equals(lastPoint)) {
lastPoint = secondLastPoint;
if (event.point.equals(this.lastPoint)) {
this.lastPoint = this.secondLastPoint;
}
// If the points are still equal, then there was no drag, so just draw a circle.
if (event.point.equals(lastPoint)) {
finalPath.remove();
finalPath = new paper.Path.Circle({
if (event.point.equals(this.lastPoint)) {
this.finalPath.remove();
this.finalPath = new paper.Path.Circle({
center: event.point,
radius: this.options.brushSize / 2
radius: options.brushSize / 2
});
tool.stylePath(finalPath);
stylePath(this.finalPath);
} else {
const step = (event.point.subtract(lastPoint)).normalize(this.options.brushSize / 2);
const step = (event.point.subtract(this.lastPoint)).normalize(options.brushSize / 2);
step.angle += 90;
const handleVec = step.clone();
handleVec.length = this.options.brushSize / 2;
handleVec.length = options.brushSize / 2;
const top = event.point.add(step);
const bottom = event.point.subtract(step);
finalPath.add(top);
finalPath.insert(0, bottom);
this.finalPath.add(top);
this.finalPath.insert(0, bottom);
// Simplify before adding end cap so cap doesn't get warped
finalPath.simplify(1);
this.finalPath.simplify(1);
// Add end cap
step.angle -= 90;
finalPath.add(new paper.Segment(event.point.add(step), handleVec, -handleVec));
finalPath.closed = true;
this.finalPath.add(new paper.Segment(event.point.add(step), handleVec, -handleVec));
this.finalPath.closed = true;
}
// Resolve self-crossings
const newPath =
finalPath
this.finalPath
.resolveCrossings()
.reorient(true /* nonZero */, true /* clockwise */)
.reduce({simplify: true});
newPath.copyAttributes(finalPath);
newPath.fillColor = finalPath.fillColor;
finalPath = newPath;
return finalPath;
};
};
newPath.copyAttributes(this.finalPath);
newPath.fillColor = this.finalPath.fillColor;
this.finalPath = newPath;
return this.finalPath;
}
}
export default broadBrushHelper;
export default BroadBrushHelper;

View file

@ -1,7 +1,8 @@
import paper from 'paper';
import {stylePath} from './style-path';
/**
* Applies segment brush functions to the tool. Call them when the corresponding mouse event happens
* Segment brush functions to add as listeners on the mouse. Call them when the corresponding mouse event happens
* to get the broad brush behavior.
*
* Segment brush draws by creating a rounded rectangle for each mouse move event and merging all of
@ -13,30 +14,32 @@ import paper from 'paper';
*
* @param {!Tool} tool paper.js mouse object
*/
const segmentBrushHelper = function (tool) {
let lastPoint;
let finalPath;
class SegmentBrushHelper {
constructor () {
this.lastPoint = null;
this.finalPath = null;
}
tool.onSegmentMouseDown = function (event) {
onSegmentMouseDown (event, tool, options) {
if (event.event.button > 0) return; // only first mouse button
tool.minDistance = 1;
tool.maxDistance = this.options.brushSize;
tool.maxDistance = options.brushSize;
finalPath = new paper.Path.Circle({
this.finalPath = new paper.Path.Circle({
center: event.point,
radius: this.options.brushSize / 2
radius: options.brushSize / 2
});
tool.stylePath(finalPath);
lastPoint = event.point;
};
stylePath(this.finalPath);
this.lastPoint = event.point;
}
tool.onSegmentMouseDrag = function (event) {
onSegmentMouseDrag (event, options) {
if (event.event.button > 0) return; // only first mouse button
const step = (event.delta).normalize(this.options.brushSize / 2);
const step = (event.delta).normalize(options.brushSize / 2);
const handleVec = step.clone();
handleVec.length = this.options.brushSize / 2;
handleVec.length = options.brushSize / 2;
handleVec.angle += 90;
const path = new paper.Path();
@ -46,7 +49,7 @@ const segmentBrushHelper = function (tool) {
path.fillColor = 'black';
// Add handles to round the end caps
path.add(new paper.Segment(lastPoint.subtract(step), handleVec.multiply(-1), handleVec));
path.add(new paper.Segment(this.lastPoint.subtract(step), handleVec.multiply(-1), handleVec));
step.angle += 90;
path.add(event.lastPoint.add(step));
@ -60,25 +63,25 @@ const segmentBrushHelper = function (tool) {
path.closed = true;
// The unite function on curved paths does not always work (sometimes deletes half the path)
// so we have to flatten.
path.flatten(Math.min(5, this.options.brushSize / 5));
path.flatten(Math.min(5, options.brushSize / 5));
lastPoint = event.point;
const newPath = finalPath.unite(path);
this.lastPoint = event.point;
const newPath = this.finalPath.unite(path);
path.remove();
finalPath.remove();
finalPath = newPath;
};
this.finalPath.remove();
this.finalPath = newPath;
}
tool.onSegmentMouseUp = function (event) {
onSegmentMouseUp (event) {
if (event.event.button > 0) return; // only first mouse button
// TODO: This smoothing tends to cut off large portions of the path! Would like to eventually
// add back smoothing, maybe a custom implementation that only applies to a subset of the line?
// Smooth the path.
finalPath.simplify(2);
return finalPath;
};
};
this.finalPath.simplify(2);
return this.finalPath;
}
}
export default segmentBrushHelper;
export default SegmentBrushHelper;

28
src/modes/style-path.js Normal file
View file

@ -0,0 +1,28 @@
const stylePath = function (path, isEraser) {
if (isEraser) {
path.fillColor = 'white';
} else {
// TODO: Add back brush styling. Keep a separate active toolbar style for brush vs pen.
// path = pg.stylebar.applyActiveToolbarStyle(path);
path.fillColor = 'black';
}
};
const styleCursorPreview = function (path, isEraser) {
if (isEraser) {
path.fillColor = 'white';
path.strokeColor = 'cornflowerblue';
path.strokeWidth = 1;
} else {
// TODO: Add back brush styling. Keep a separate active toolbar style for brush vs pen.
// path = pg.stylebar.applyActiveToolbarStyle(path);
path.fillColor = 'black';
path.strokeColor = 'cornflowerblue';
path.strokeWidth = 1;
}
};
export {
stylePath,
styleCursorPreview
};