| import paper from '@turbowarp/paper'; |
| import Modes from '../../lib/modes'; |
| import { styleShape } from '../style-path'; |
| import { clearSelection } from '../selection'; |
| import { getSquareDimensions } from '../math'; |
| import BoundingBoxTool from '../selection-tools/bounding-box-tool'; |
| import NudgeTool from '../selection-tools/nudge-tool'; |
|
|
| const constructArrowPath = (left, lineWidth, lineLength, headWidth, headLength) => { |
| if (typeof lineWidth !== "number") { |
| lineWidth = 24; |
| } |
| if (typeof lineLength !== "number") { |
| lineLength = 60; |
| } |
| if (typeof headWidth !== "number") { |
| headWidth = 60; |
| } |
| if (typeof headLength !== "number") { |
| headLength = 60; |
| } |
|
|
| if (left) { |
| return `M 0 ${lineWidth} H ${0 - lineLength} V ${headWidth} L ${0 - (lineLength + headLength)} 0 L ${0 - lineLength} ${0 - headWidth} V ${0 - lineWidth} H 0 Z`; |
| } |
| return `M 0 ${0 - lineWidth} H ${lineLength} V ${0 - headWidth} L ${lineLength + headLength} 0 L ${lineLength} ${headWidth} V ${lineWidth} H 0 Z`; |
| } |
|
|
| |
| |
| |
| class ArrowTool extends paper.Tool { |
| static get TOLERANCE() { |
| return 2; |
| } |
| |
| |
| |
| |
| |
| |
| constructor(setSelectedItems, clearSelectedItems, setCursor, onUpdateImage) { |
| super(); |
| this.setSelectedItems = setSelectedItems; |
| this.clearSelectedItems = clearSelectedItems; |
| this.onUpdateImage = onUpdateImage; |
| this.boundingBoxTool = new BoundingBoxTool( |
| Modes.ARROW, |
| setSelectedItems, |
| clearSelectedItems, |
| setCursor, |
| onUpdateImage |
| ); |
| const nudgeTool = new NudgeTool(Modes.ARROW, this.boundingBoxTool, onUpdateImage); |
|
|
| |
| |
| this.onMouseDown = this.handleMouseDown; |
| this.onMouseMove = this.handleMouseMove; |
| this.onMouseDrag = this.handleMouseDrag; |
| this.onMouseUp = this.handleMouseUp; |
| this.onKeyUp = nudgeTool.onKeyUp; |
| this.onKeyDown = nudgeTool.onKeyDown; |
|
|
| this.tri = null; |
| this.colorState = null; |
| this.isBoundingBoxMode = null; |
| this.active = false; |
|
|
| this.canModifyState = true; |
| this.arrowPathState = { |
| length: 60, |
| angle: 0, |
| width: 24, |
| head: { |
| length: 60, |
| width: 60 |
| } |
| }; |
| this.arrowPathLocked = false; |
| this.arrowPathLockedState = {}; |
| this.arrowPathLockedPositionSet = false; |
| this.arrowPathLockedPosition = { x: 0, y: 0 }; |
| } |
| getHitOptions() { |
| return { |
| segments: true, |
| stroke: true, |
| curves: true, |
| fill: true, |
| guide: false, |
| match: hitResult => |
| (hitResult.item.data && (hitResult.item.data.isScaleHandle || hitResult.item.data.isRotHandle)) || |
| hitResult.item.selected, |
| tolerance: ArrowTool.TOLERANCE / paper.view.zoom |
| }; |
| } |
| |
| |
| |
| |
| onSelectionChanged(selectedItems) { |
| this.boundingBoxTool.onSelectionChanged(selectedItems); |
| } |
| setColorState(colorState) { |
| this.colorState = colorState; |
| } |
| handleMouseDown(event) { |
| if (event.event.button > 0) return; |
| this.active = true; |
|
|
| const pathOptions = { |
| length: 60, |
| angle: 0, |
| width: 24, |
| head: { |
| length: 60, |
| width: 60 |
| } |
| }; |
| this.arrowPathState = pathOptions; |
| this.canModifyState = true; |
| this.arrowPathLocked = false; |
| this.arrowPathLockedState = {}; |
| this.arrowPathLockedPositionSet = false; |
| this.arrowPathLockedPosition = { x: 0, y: 0 }; |
|
|
| if (this.boundingBoxTool.onMouseDown( |
| event, false , false , false , this.getHitOptions())) { |
| this.isBoundingBoxMode = true; |
| } else { |
| this.isBoundingBoxMode = false; |
| clearSelection(this.clearSelectedItems); |
| } |
| } |
|
|
| radToDeg(rad) { |
| return rad * 180 / Math.PI; |
| } |
|
|
| calculateDirection(x1, y1, x2, y2) { |
| const dx = x2 - x1; |
| const dy = y2 - y1; |
| const direction = 90 - this.radToDeg(Math.atan2(dy, dx)); |
| return direction; |
| } |
| calculateDistance(x1, y1, x2, y2) { |
| const dx = x2 - x1; |
| const dy = y2 - y1; |
| return Math.sqrt((dx * dx) + (dy * dy)); |
| } |
|
|
| handleMouseDrag(event) { |
| if (event.event.button > 0 || !this.active) return; |
|
|
| if (this.isBoundingBoxMode) { |
| this.boundingBoxTool.onMouseDrag(event); |
| return; |
| } |
|
|
| if (this.tri) { |
| this.tri.remove(); |
| } |
|
|
| |
| |
| const pathOptions = this.arrowPathState; |
| if (event.modifiers.alt) { |
| this.canModifyState = false; |
| this.arrowPathLocked = true; |
| if (!this.arrowPathLockedPositionSet) { |
| this.arrowPathLockedPosition = { |
| x: event.point.x, |
| y: event.point.y |
| }; |
| } |
| this.arrowPathLockedPositionSet = true; |
| } else { |
| this.canModifyState = true; |
| this.arrowPathLocked = false; |
| this.arrowPathLockedPositionSet = false; |
| this.arrowPathLockedPosition = { x: 0, y: 0 }; |
| } |
|
|
| if (this.canModifyState) { |
| const x1 = event.downPoint.x; |
| const y1 = event.downPoint.y; |
|
|
| const x2 = event.point.x; |
| const y2 = event.point.y; |
|
|
| pathOptions.length = this.calculateDistance(x1, y1, x2, y2); |
| } |
|
|
| if (this.canModifyState) { |
| const x1 = event.downPoint.x; |
| const y1 = event.downPoint.y; |
|
|
| const x2 = event.point.x; |
| const y2 = event.point.y; |
|
|
| pathOptions.angle = 90 - this.calculateDirection(x1, y1, x2, y2); |
| if (event.modifiers.shift) { |
| pathOptions.angle = Math.round((pathOptions.angle / 360) * 8) * 45; |
| } |
| } |
|
|
| |
| if (event.modifiers.alt) { |
| if (event.modifiers.shift) { |
| |
| const x1 = this.arrowPathLockedPosition.x; |
| const y1 = this.arrowPathLockedPosition.y; |
|
|
| const x2 = event.point.x; |
| const y2 = event.point.y; |
|
|
| pathOptions.head.length = this.calculateDistance(x1, y1, x2, y1); |
| pathOptions.head.width = this.calculateDistance(x1, y1, x1, y2); |
| } else { |
| |
| const x1 = this.arrowPathLockedPosition.x; |
| const y1 = this.arrowPathLockedPosition.y; |
|
|
| const x2 = event.point.x; |
| const y2 = event.point.y; |
|
|
| pathOptions.width = this.calculateDistance(x1, y1, x2, y2); |
| } |
| } |
|
|
| this.tri = new paper.Path(constructArrowPath( |
| event.modifiers.control, |
| pathOptions.width, pathOptions.length, |
| pathOptions.head.width, |
| pathOptions.head.length |
| )); |
|
|
| |
| if ((!this.arrowPathLocked) && this.arrowPathLockedState) { |
| this.tri.position = event.downPoint; |
|
|
| const dimensions = new paper.Point(event.point.getDistance(event.downPoint),0); |
| this.tri.position = event.downPoint.add(dimensions.multiply(0.5)); |
| } else if (this.arrowPathLocked) { |
| this.tri.position = this.arrowPathLockedState; |
| } |
|
|
| if (this.canModifyState) { |
| this.tri.rotate(pathOptions.angle, event.downPoint); |
| } |
|
|
| this.arrowPathLockedState = { |
| x: this.tri.position.x, |
| y: this.tri.position.y, |
| }; |
| |
|
|
| styleShape(this.tri, this.colorState); |
| } |
| handleMouseUp(event) { |
| if (event.event.button > 0 || !this.active) return; |
|
|
| if (this.isBoundingBoxMode) { |
| this.boundingBoxTool.onMouseUp(event); |
| this.isBoundingBoxMode = null; |
| return; |
| } |
|
|
| this.arrowPathLockedPositionSet = false; |
| this.arrowPathLockedPosition = { x: 0, y: 0 }; |
|
|
| if (this.tri) { |
| if (this.tri.area < ArrowTool.TOLERANCE / paper.view.zoom) { |
| |
| this.tri.remove(); |
| this.tri = null; |
| } else { |
| this.tri.selected = true; |
| this.setSelectedItems(); |
| this.onUpdateImage(); |
| this.tri = null; |
| } |
| } |
| this.active = false; |
| } |
| handleMouseMove(event) { |
| this.boundingBoxTool.onMouseMove(event, this.getHitOptions()); |
| } |
| deactivateTool() { |
| this.boundingBoxTool.deactivateTool(); |
| } |
| } |
|
|
| export default ArrowTool; |
|
|