| import paper from '@turbowarp/paper'; |
| import log from '../../log/log'; |
| import keyMirror from 'keymirror'; |
|
|
| import Modes from '../../lib/modes'; |
| import { isBoundsItem } from '../item'; |
| import { hoverBounds, hoverItem } from '../guides'; |
| import { sortItemsByZIndex } from '../math'; |
| import { getSelectedLeafItems, getSelectedSegments } from '../selection'; |
| import MoveTool from './move-tool'; |
| import PointTool from './point-tool'; |
| import HandleTool from './handle-tool'; |
| import SelectionBoxTool from './selection-box-tool'; |
|
|
| |
| const ReshapeModes = keyMirror({ |
| FILL: null, |
| POINT: null, |
| HANDLE: null, |
| SELECTION_BOX: null |
| }); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class ReshapeTool extends paper.Tool { |
| |
| static get TOLERANCE() { |
| return ReshapeTool.HANDLE_RADIUS + ReshapeTool.HANDLE_PADDING; |
| } |
| |
| |
| |
| static get HANDLE_PADDING() { |
| return 1; |
| } |
| |
| |
| |
| static get HANDLE_RADIUS() { |
| return 5.25; |
| } |
| |
| static get DOUBLE_CLICK_MILLIS() { |
| return 250; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| constructor(setHoveredItem, clearHoveredItem, setSelectedItems, clearSelectedItems, onUpdateImage, |
| switchToTextTool) { |
| super(); |
| this.setHoveredItem = setHoveredItem; |
| this.clearHoveredItem = clearHoveredItem; |
| this.onUpdateImage = onUpdateImage; |
| this.prevHoveredItemId = null; |
| this.lastEvent = null; |
| this.active = false; |
| this.mode = ReshapeModes.SELECTION_BOX; |
| this._modeMap = {}; |
| this._modeMap[ReshapeModes.FILL] = |
| new MoveTool(Modes.RESHAPE, setSelectedItems, clearSelectedItems, onUpdateImage, switchToTextTool); |
| this._modeMap[ReshapeModes.POINT] = new PointTool(setSelectedItems, clearSelectedItems, onUpdateImage); |
| this._modeMap[ReshapeModes.HANDLE] = new HandleTool(setSelectedItems, clearSelectedItems, onUpdateImage); |
| this._modeMap[ReshapeModes.SELECTION_BOX] = |
| new SelectionBoxTool(Modes.RESHAPE, setSelectedItems, clearSelectedItems); |
|
|
| |
| |
| this.onMouseDown = this.handleMouseDown; |
| this.onMouseMove = this.handleMouseMove; |
| this.onMouseDrag = this.handleMouseDrag; |
| this.onMouseUp = this.handleMouseUp; |
| this.onKeyUp = this.handleKeyUp; |
| this.onKeyDown = this.handleKeyDown; |
|
|
| |
| |
| |
| paper.settings.handleSize = (ReshapeTool.HANDLE_RADIUS * 2) - 2.5; |
| } |
| |
| |
| |
| |
| |
| |
| getSelectedSegmentHitOptions() { |
| const hitOptions = { |
| segments: true, |
| tolerance: ReshapeTool.TOLERANCE / paper.view.zoom, |
| match: hitResult => { |
| if (hitResult.type !== 'segment') return false; |
| if (hitResult.item.data && hitResult.item.data.noHover) return false; |
| if (!hitResult.item.selected) return false; |
| return true; |
| } |
| }; |
| return hitOptions; |
| } |
| |
| |
| |
| |
| |
| |
| getHandleHitOptions() { |
| const hitOptions = { |
| handles: true, |
| tolerance: ReshapeTool.TOLERANCE / paper.view.zoom, |
| match: hitResult => { |
| if (hitResult.item.data && hitResult.item.data.noHover) return false; |
| |
| |
| if (!hitResult.segment || !hitResult.segment.selected) return false; |
| |
| if (hitResult.item.fullySelected) return false; |
| return true; |
| } |
| }; |
| return hitOptions; |
| } |
| |
| |
| |
| |
| |
| getSelectedStrokeHitOptions() { |
| const hitOptions = { |
| segments: false, |
| stroke: false, |
| curves: true, |
| handles: false, |
| fill: false, |
| guide: false, |
| tolerance: ReshapeTool.TOLERANCE / paper.view.zoom, |
| match: hitResult => { |
| if (hitResult.type !== 'curve') return false; |
| if (!hitResult.item.selected) return false; |
| if (hitResult.item.data && hitResult.item.data.noHover) return false; |
| return true; |
| } |
| }; |
| return hitOptions; |
| } |
| |
| |
| |
| |
| |
| |
| getUnselectedAndFillHitOptions() { |
| const hitOptions = { |
| fill: true, |
| stroke: true, |
| curves: true, |
| tolerance: ReshapeTool.TOLERANCE / paper.view.zoom, |
| match: hitResult => { |
| if (hitResult.item.data && hitResult.item.data.noHover) return false; |
| return true; |
| } |
| }; |
| return hitOptions; |
| } |
| |
| |
| |
| |
| |
| |
| |
| setPrevHoveredItemId(prevHoveredItemId) { |
| this.prevHoveredItemId = prevHoveredItemId; |
| } |
| |
| |
| |
| |
| |
| getHitResult(point) { |
| |
| let hitResults = |
| paper.project.hitTestAll(point, this.getSelectedSegmentHitOptions()); |
| if (!hitResults.length) { |
| hitResults = paper.project.hitTestAll(point, this.getHandleHitOptions()); |
| } |
| if (!hitResults.length) { |
| hitResults = paper.project.hitTestAll(point, this.getSelectedStrokeHitOptions()); |
| } |
| if (!hitResults.length) { |
| hitResults = paper.project.hitTestAll(point, this.getUnselectedAndFillHitOptions()); |
| } |
| if (!hitResults.length) { |
| return null; |
| } |
|
|
| |
| let hitResult; |
| for (const result of hitResults) { |
| if (!hitResult || sortItemsByZIndex(hitResult.item, result.item) < 0) { |
| hitResult = result; |
| } |
| } |
| return hitResult; |
| } |
| handleMouseDown(event) { |
| if (event.event.button > 0) return; |
| this.active = true; |
| this.clearHoveredItem(); |
|
|
| |
| let doubleClicked = false; |
| if (this.lastEvent) { |
| if ((event.event.timeStamp - this.lastEvent.event.timeStamp) < ReshapeTool.DOUBLE_CLICK_MILLIS) { |
| doubleClicked = true; |
| } else { |
| doubleClicked = false; |
| } |
| } |
| this.lastEvent = event; |
|
|
| const hitResult = this.getHitResult(event.point); |
| if (!hitResult) { |
| this._modeMap[ReshapeModes.SELECTION_BOX].onMouseDown(event.modifiers.shift); |
| return; |
| } |
|
|
| const hitProperties = { |
| hitResult: hitResult, |
| clone: event.modifiers.alt, |
| multiselect: event.modifiers.shift, |
| doubleClicked: doubleClicked, |
| subselect: true |
| }; |
|
|
| |
| |
| if (!hitResult.item.selected || |
| hitResult.type === 'fill' || |
| hitResult.type === 'stroke' || |
| (hitResult.type !== 'segment' && doubleClicked)) { |
| this.mode = ReshapeModes.FILL; |
| this._modeMap[this.mode].onMouseDown(hitProperties); |
| } else if (hitResult.type === 'segment') { |
| this.mode = ReshapeModes.POINT; |
| this._modeMap[this.mode].onMouseDown(hitProperties); |
| } else if ( |
| hitResult.type === 'curve') { |
| this.mode = ReshapeModes.POINT; |
| this._modeMap[this.mode].addPoint(hitProperties); |
| this.onUpdateImage(); |
| this._modeMap[this.mode].onMouseDown(hitProperties); |
| } else if ( |
| hitResult.type === 'handle-in' || |
| hitResult.type === 'handle-out') { |
| this.mode = ReshapeModes.HANDLE; |
| this._modeMap[this.mode].onMouseDown(hitProperties); |
| } else { |
| log.warn(`Unhandled hit result type: ${hitResult.type}`); |
| this.mode = ReshapeModes.FILL; |
| this._modeMap[this.mode].onMouseDown(hitProperties); |
| } |
| } |
| handleMouseMove(event) { |
| const hitResult = this.getHitResult(event.point); |
| let hoveredItem; |
|
|
| if (hitResult) { |
| const item = hitResult.item; |
| if (item.selected) { |
| hoveredItem = null; |
| } else if (isBoundsItem(item)) { |
| hoveredItem = hoverBounds(item); |
| } else { |
| hoveredItem = hoverItem(item); |
| } |
| } |
|
|
| if ((!hoveredItem && this.prevHoveredItemId) || |
| (hoveredItem && !this.prevHoveredItemId) || |
| (hoveredItem && this.prevHoveredItemId && |
| hoveredItem.id !== this.prevHoveredItemId)) { |
| this.setHoveredItem(hoveredItem ? hoveredItem.id : null); |
| } |
| } |
| handleMouseDrag(event) { |
| if (event.event.button > 0 || !this.active) return; |
| this._modeMap[this.mode].onMouseDrag(event); |
| } |
| handleMouseUp(event) { |
| if (event.event.button > 0 || !this.active) return; |
| if (this.mode === ReshapeModes.SELECTION_BOX) { |
| this._modeMap[this.mode].onMouseUpVector(event); |
| } else { |
| this._modeMap[this.mode].onMouseUp(event); |
| } |
| this.mode = ReshapeModes.SELECTION_BOX; |
| this.active = false; |
| } |
| handleKeyDown(event) { |
| if (event.event.target instanceof HTMLInputElement || event.event.target instanceof HTMLTextAreaElement) { |
| |
| return; |
| } |
|
|
| const nudgeAmount = 1 / paper.view.zoom; |
| const selected = getSelectedLeafItems(); |
| if (selected.length === 0) return; |
|
|
| let translation; |
| if (event.key === 'up') { |
| translation = new paper.Point(0, -nudgeAmount); |
| } else if (event.key === 'down') { |
| translation = new paper.Point(0, nudgeAmount); |
| } else if (event.key === 'left') { |
| translation = new paper.Point(-nudgeAmount, 0); |
| } else if (event.key === 'right') { |
| translation = new paper.Point(nudgeAmount, 0); |
| } else if (event.key == 'i' || event.key == 'o') { |
|
|
| const segments = getSelectedSegments(); |
| for (const seg of segments) { |
| let parent = seg.path; |
| let p = seg.point; |
| let o = parent.getOffsetOf(p); |
| let o1 = o + 0.01; |
| let o2 = o + (parent.length - 0.01); |
| let n = (parent.getNormalAt(o1 % parent.length).add(parent.getNormalAt(o2 % parent.length))).divide(2); |
| if (event.key == 'o') { |
| seg.point = p.add(n); |
| } else { |
| seg.point = p.subtract(n); |
| } |
| } |
| } |
|
|
| if (translation) { |
| const segments = getSelectedSegments(); |
| |
| if (segments.length === 0) { |
| for (const item of selected) { |
| item.translate(translation); |
| } |
| } else { |
| for (const seg of segments) { |
| seg.point = seg.point.add(translation); |
| } |
| } |
| } |
| } |
| handleKeyUp(event) { |
| const selected = getSelectedLeafItems(); |
| if (selected.length === 0) return; |
|
|
| if (event.key === 'up' || event.key === 'down' || event.key === 'left' || event.key === 'right') { |
| this.onUpdateImage(); |
| } |
| } |
| deactivateTool() { |
| paper.settings.handleSize = 0; |
| this.clearHoveredItem(); |
| this.setHoveredItem = null; |
| this.clearHoveredItem = null; |
| this.onUpdateImage = null; |
| this.lastEvent = null; |
| } |
| } |
|
|
| export default ReshapeTool; |
|
|