| import paper from '@turbowarp/paper'; |
| import {styleShape} from '../style-path'; |
| import {endPointHit, touching} from '../snapping'; |
| import {drawHitPoint, removeHitPoint} from '../guides'; |
|
|
| |
| |
| |
| class PenTool extends paper.Tool { |
| static get SNAP_TOLERANCE () { |
| return 5; |
| } |
| |
| |
| |
| |
| |
| constructor (clearSelectedItems, onUpdateSvg) { |
| super(); |
| this.clearSelectedItems = clearSelectedItems; |
| this.onUpdateSvg = onUpdateSvg; |
|
|
| this.colorState = null; |
| this.path = null; |
| this.hitResult = null; |
|
|
| |
| this.subpath = null; |
| this.subpathIndex = 0; |
|
|
| |
| |
| this.onMouseDown = this.handleMouseDown; |
| this.onMouseMove = this.handleMouseMove; |
| this.onMouseDrag = this.handleMouseDrag; |
| this.onMouseUp = this.handleMouseUp; |
|
|
| this.fixedDistance = 2; |
| |
| this.simplifySize = 2; |
| } |
| setColorState (colorState) { |
| this.colorState = colorState; |
| } |
| setSimplifySize (simplifySize) { |
| this.simplifySize = simplifySize; |
| } |
| drawHitPoint (hitResult) { |
| |
| if (hitResult) { |
| const hitPath = hitResult.path; |
| if (hitResult.isFirst) { |
| drawHitPoint(hitPath.firstSegment.point); |
| } else { |
| drawHitPoint(hitPath.lastSegment.point); |
| } |
| } |
| } |
| handleMouseDown (event) { |
| if (event.event.button > 0) return; |
| this.subpath = new paper.Path({insert: false}); |
| this.subpath.strokeCap = 'round'; |
|
|
| |
| this.hitResult = endPointHit(event.point, PenTool.SNAP_TOLERANCE); |
| if (this.hitResult) { |
| this.path = this.hitResult.path; |
| styleShape(this.path, { |
| fillColor: null, |
| strokeColor: this.colorState.strokeColor, |
| strokeWidth: this.colorState.strokeWidth |
| }); |
| if (this.hitResult.isFirst) { |
| this.path.reverse(); |
| } |
| this.subpathIndex = this.path.segments.length; |
| this.path.lastSegment.handleOut = null; |
| this.path.lastSegment.handleIn = null; |
| } |
|
|
| |
| if (!this.path) { |
| this.path = new paper.Path(); |
| styleShape(this.path, { |
| fillColor: null, |
| strokeColor: this.colorState.strokeColor, |
| strokeWidth: this.colorState.strokeWidth |
| }); |
| this.path.strokeCap = 'round'; |
| this.path.add(event.point); |
| this.subpath.add(event.point); |
| paper.view.draw(); |
| } |
| } |
| handleMouseMove (event) { |
| |
| |
| if (this.hitResult) { |
| removeHitPoint(); |
| } |
| this.hitResult = endPointHit(event.point, PenTool.SNAP_TOLERANCE); |
| this.drawHitPoint(this.hitResult); |
| } |
| handleMouseDrag (event) { |
| if (event.event.button > 0) return; |
| |
| |
| |
| if (this.hitResult) { |
| removeHitPoint(); |
| this.hitResult = null; |
| } |
|
|
| if (this.path && |
| !this.path.closed && |
| this.path.segments.length > 3 && |
| touching(this.path.firstSegment.point, event.point, PenTool.SNAP_TOLERANCE)) { |
| this.hitResult = { |
| path: this.path, |
| segment: this.path.firstSegment, |
| isFirst: true |
| }; |
| } else { |
| this.hitResult = endPointHit(event.point, PenTool.SNAP_TOLERANCE, this.path); |
| } |
| if (this.hitResult) { |
| this.drawHitPoint(this.hitResult); |
| } |
|
|
| this.path.add(event.point); |
| this.subpath.add(event.point); |
| } |
| handleMouseUp (event) { |
| if (event.event.button > 0) return; |
| |
| |
| if (!this.hitResult && |
| (this.path.segments.length < 2 || |
| (this.path.segments.length === 2 && |
| touching(this.path.firstSegment.point, event.point, PenTool.SNAP_TOLERANCE)))) { |
| this.path.remove(); |
| this.path = null; |
| return; |
| } |
|
|
| |
| const hasStartConnection = this.subpathIndex > 0; |
| const hasEndConnection = !!this.hitResult; |
| this.path.removeSegments(this.subpathIndex); |
| |
| if (this.simplifySize > 0) { |
| this.subpath.simplify(this.simplifySize); |
| } |
| if (hasStartConnection && this.subpath.length > 0) { |
| this.subpath.removeSegment(0); |
| } |
| if (hasEndConnection && this.subpath.length > 0) { |
| this.subpath.removeSegment(this.subpath.length - 1); |
| } |
| this.path.insertSegments(this.subpathIndex, this.subpath.segments); |
| this.subpath = null; |
| this.subpathIndex = 0; |
|
|
| |
| if (this.hitResult) { |
| if (touching(this.path.firstSegment.point, this.hitResult.segment.point, PenTool.SNAP_TOLERANCE)) { |
| |
| this.path.closed = true; |
| } else { |
| |
| if (!this.hitResult.isFirst) { |
| this.hitResult.path.reverse(); |
| } |
| this.path.join(this.hitResult.path); |
| } |
| removeHitPoint(); |
| this.hitResult = null; |
| } |
| |
| if (this.path) { |
| this.onUpdateSvg(); |
| this.path = null; |
| } |
| } |
| deactivateTool () { |
| this.fixedDistance = 1; |
| } |
| } |
|
|
| export default PenTool; |
|
|