| import paper from '@turbowarp/paper'; |
| import keyMirror from 'keymirror'; |
|
|
| import {getSelectedRootItems} from '../selection'; |
| import {getGuideColor, removeBoundsPath, removeBoundsHandles} from '../guides'; |
| import {getGuideLayer, setGuideItem} from '../layer'; |
|
|
| import Cursors from '../../lib/cursors'; |
| import ScaleTool from './scale-tool'; |
| import RotateTool from './rotate-tool'; |
| import MoveTool from './move-tool'; |
|
|
| const SELECTION_ANCHOR_SIZE = 12; |
| |
| const ARROW_PATH = 'M19.28,1.09C19.28.28,19,0,18.2,0c-1.67,0-3.34,0-5,0-.34,0-.88.24-1,.47a1.4,1.4,' + |
| '0,0,0,.36,1.08,15.27,15.27,0,0,0,1.46,1.36A6.4,6.4,0,0,1,6.52,4,5.85,5.85,0,0,1,5.24,3,15.27,15.27,' + |
| '0,0,0,6.7,1.61,1.4,1.4,0,0,0,7.06.54C7,.3,6.44.07,6.1.06c-1.67,0-3.34,0-5,0C.28,0,0,.31,0,1.12c0,1.67,' + |
| '0,3.34,0,5a1.23,1.23,0,0,0,.49,1,1.22,1.22,0,0,0,1-.31A14.38,14.38,0,0,0,2.84,5.26l.73.62a9.45,9.45,' + |
| '0,0,0,7.34,2,9.45,9.45,0,0,0,4.82-2.05l.73-.62a14.38,14.38,0,0,0,1.29,1.51,1.22,1.22,' + |
| '0,0,0,1,.31,1.23,1.23,0,0,0,.49-1C19.31,4.43,19.29,2.76,19.28,1.09Z'; |
| |
| const BoundingBoxModes = keyMirror({ |
| SCALE: null, |
| ROTATE: null, |
| MOVE: null |
| }); |
|
|
| |
| |
| |
| |
| |
| |
| |
| class BoundingBoxTool { |
| |
| |
| |
| |
| |
| |
| |
| |
| constructor (mode, setSelectedItems, clearSelectedItems, setCursor, onUpdateImage, switchToTextTool) { |
| this.dispatchSetCursor = setCursor; |
| this.onUpdateImage = onUpdateImage; |
| this.mode = null; |
| this.boundsPath = null; |
| this.boundsScaleHandles = []; |
| this.boundsRotHandles = []; |
| this._modeMap = {}; |
| this._modeMap[BoundingBoxModes.SCALE] = new ScaleTool(mode, onUpdateImage); |
| this._modeMap[BoundingBoxModes.ROTATE] = new RotateTool(onUpdateImage); |
| this._modeMap[BoundingBoxModes.MOVE] = |
| new MoveTool(mode, setSelectedItems, clearSelectedItems, onUpdateImage, switchToTextTool); |
| this._currentCursor = null; |
| } |
|
|
| |
| |
| |
| |
| onSelectionChanged (selectedItems) { |
| if (selectedItems && selectedItems.length) { |
| this.setSelectionBounds(); |
| } else { |
| this.removeBoundsPath(); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| onMouseDown (event, clone, multiselect, doubleClicked, hitOptions) { |
| if (event.event.button > 0) return; |
| const {hitResult, mode} = this._determineMode(event, multiselect, hitOptions); |
| if (!hitResult) { |
| if (!multiselect) { |
| this.removeBoundsPath(); |
| } |
| return false; |
| } |
| this.mode = mode; |
|
|
| const hitProperties = { |
| hitResult: hitResult, |
| clone: clone, |
| multiselect: multiselect, |
| doubleClicked: doubleClicked |
| }; |
| if (this.mode === BoundingBoxModes.MOVE) { |
| this._modeMap[this.mode].onMouseDown(hitProperties); |
| this.removeBoundsHandles(); |
| } else if (this.mode === BoundingBoxModes.SCALE) { |
| this._modeMap[this.mode].onMouseDown(hitResult, this.boundsPath, getSelectedRootItems()); |
| this.removeBoundsHandles(); |
| } else if (this.mode === BoundingBoxModes.ROTATE) { |
| this.setCursor(Cursors.GRABBING); |
| this._modeMap[this.mode].onMouseDown(hitResult, this.boundsPath, getSelectedRootItems()); |
| |
| this.removeBoundsPath(); |
| } |
|
|
| return true; |
| } |
| onMouseMove (event, hitOptions) { |
| this._updateCursor(event, hitOptions); |
| } |
| _updateCursor (event, hitOptions) { |
| const {mode, hitResult} = this._determineMode(event, false, hitOptions); |
| if (hitResult) { |
| if (mode === BoundingBoxModes.MOVE) { |
| this.setCursor(Cursors.DEFAULT); |
| } else if (mode === BoundingBoxModes.ROTATE) { |
| this.setCursor(Cursors.GRAB); |
| } else if (mode === BoundingBoxModes.SCALE) { |
| this.setSelectionBounds(); |
| if (this._impreciseEqual(hitResult.item.position.x, this.boundsPath.position.x)) { |
| this.setCursor(Cursors.RESIZE_NS); |
| } else if (this._impreciseEqual(hitResult.item.position.y, this.boundsPath.position.y)) { |
| this.setCursor(Cursors.RESIZE_EW); |
| } else if ( |
| hitResult.item.position.equals(this.boundsPath.bounds.bottomLeft) || |
| hitResult.item.position.equals(this.boundsPath.bounds.topRight) |
| ) { |
| this.setCursor(Cursors.RESIZE_NESW); |
| } else { |
| this.setCursor(Cursors.RESIZE_NWSE); |
| } |
| } |
| } else { |
| this.setCursor(Cursors.DEFAULT); |
| } |
| } |
| _impreciseEqual (a, b) { |
| |
| return Math.abs(a - b) < 1e-8; |
| } |
| _determineMode (event, multiselect, hitOptions) { |
| const hitResults = paper.project.hitTestAll(event.point, hitOptions); |
|
|
| let mode; |
|
|
| |
| let hitResult = hitResults[0]; |
| for (let i = 0; i < hitResults.length; i++) { |
| if (hitResults[i].item.data && hitResults[i].item.data.isScaleHandle) { |
| hitResult = hitResults[i]; |
| mode = BoundingBoxModes.SCALE; |
| break; |
| } else if (hitResults[i].item.data && hitResults[i].item.data.isRotHandle) { |
| hitResult = hitResults[i]; |
| mode = BoundingBoxModes.ROTATE; |
| } |
| } |
| if (!mode) { |
| mode = BoundingBoxModes.MOVE; |
| } |
|
|
| return {mode, hitResult}; |
| } |
| onMouseDrag (event) { |
| if (event.event.button > 0 || !this.mode) return; |
| this._modeMap[this.mode].onMouseDrag(event); |
|
|
| |
| |
| if (this.mode === BoundingBoxModes.MOVE) { |
| this.setCursor(Cursors.GRABBING); |
| } |
| } |
| onMouseUp (event, hitOptions) { |
| if (event.event.button > 0 || !this.mode) return; |
| this._modeMap[this.mode].onMouseUp(event); |
|
|
| |
| this.setSelectionBounds(); |
| this.mode = null; |
| this._updateCursor(event, hitOptions); |
| } |
| setSelectionBounds () { |
| this.removeBoundsPath(); |
|
|
| const items = getSelectedRootItems(); |
| if (items.length <= 0) return; |
|
|
| let rect = null; |
| for (const item of items) { |
| if (item instanceof paper.Raster && item.loaded === false) { |
| item.onLoad = this.setSelectionBounds.bind(this); |
| return; |
| } |
|
|
| if (rect) { |
| rect = rect.unite(item.bounds); |
| } else { |
| rect = item.bounds; |
| } |
| } |
|
|
| if (!this.boundsPath) { |
| this.boundsPath = new paper.Group(); |
| this.boundsRect = paper.Path.Rectangle(rect); |
| this.boundsRect.curves[0].divideAtTime(0.5); |
| this.boundsRect.curves[2].divideAtTime(0.5); |
| this.boundsRect.curves[4].divideAtTime(0.5); |
| this.boundsRect.curves[6].divideAtTime(0.5); |
| this.boundsPath.addChild(this.boundsRect); |
|
|
| const vRect = new paper.Path.Rectangle({ |
| point: [-1, -6], |
| size: [2, 12], |
| radius: 1, |
| insert: false |
| }); |
| const hRect = new paper.Path.Rectangle({ |
| point: [-6, -1], |
| size: [12, 2], |
| radius: 1, |
| insert: false |
| }); |
| const anchorIcon = vRect.unite(hRect); |
|
|
| this.boundsPath.addChild(anchorIcon); |
| this.boundsPath.selectionAnchor = anchorIcon; |
| this._modeMap[BoundingBoxModes.MOVE].setBoundsPath(this.boundsPath); |
| } |
| setGuideItem(this.boundsPath); |
| this.boundsPath.data.isSelectionBound = true; |
| this.boundsPath.data.isHelperItem = true; |
| this.boundsPath.fillColor = null; |
| this.boundsPath.parent = getGuideLayer(); |
| this.boundsPath.strokeWidth = 1 / paper.view.zoom; |
| this.boundsPath.strokeColor = getGuideColor(); |
| this.boundsPath.selectionAnchor.scale( |
| SELECTION_ANCHOR_SIZE / paper.view.zoom / this.boundsPath.selectionAnchor.bounds.width); |
| this.boundsPath.selectionAnchor.position = rect.center; |
|
|
| |
| const boundsScaleCircleShadow = |
| new paper.Path.Circle({ |
| center: new paper.Point(0, 0), |
| radius: 5.5 / paper.view.zoom, |
| fillColor: 'black', |
| opacity: .12, |
| data: { |
| isHelperItem: true, |
| noSelect: true, |
| noHover: true |
| } |
| }); |
| const boundsScaleCircle = |
| new paper.Path.Circle({ |
| center: new paper.Point(0, 0), |
| radius: 4 / paper.view.zoom, |
| fillColor: getGuideColor(), |
| data: { |
| isScaleHandle: true, |
| isHelperItem: true, |
| noSelect: true, |
| noHover: true |
| } |
| }); |
| const boundsScaleHandle = new paper.Group([boundsScaleCircleShadow, boundsScaleCircle]); |
| boundsScaleHandle.parent = getGuideLayer(); |
|
|
| for (let index = 0; index < this.boundsRect.segments.length; index++) { |
| const segment = this.boundsRect.segments[index]; |
|
|
| if (index === 7) { |
| const offset = new paper.Point(0, 20); |
|
|
| const arrows = new paper.Path(ARROW_PATH); |
| arrows.translate(segment.point.add(offset).add(-10.5, -5)); |
|
|
| const line = new paper.Path.Rectangle( |
| segment.point.add(offset).subtract(1, 0), |
| segment.point); |
|
|
| const rotHandle = arrows.unite(line); |
| line.remove(); |
| arrows.remove(); |
| rotHandle.scale(1 / paper.view.zoom, segment.point); |
| rotHandle.data = { |
| offset: offset, |
| isRotHandle: true, |
| isHelperItem: true, |
| noSelect: true, |
| noHover: true |
| }; |
| rotHandle.fillColor = getGuideColor(); |
| rotHandle.parent = getGuideLayer(); |
| this.boundsRotHandles[index] = rotHandle; |
| } |
|
|
| this.boundsScaleHandles[index] = boundsScaleHandle.clone(); |
| this.boundsScaleHandles[index].position = segment.point; |
| for (const child of this.boundsScaleHandles[index].children) { |
| child.data.index = index; |
| } |
| this.boundsScaleHandles[index].data = { |
| index: index, |
| isScaleHandle: true, |
| isHelperItem: true, |
| noSelect: true, |
| noHover: true |
| }; |
| } |
| |
| boundsScaleHandle.remove(); |
| } |
| removeBoundsPath () { |
| removeBoundsPath(); |
| this.boundsPath = null; |
| this.boundsRect = null; |
| this.boundsScaleHandles.length = 0; |
| this.boundsRotHandles.length = 0; |
| } |
| removeBoundsHandles () { |
| removeBoundsHandles(); |
| this.boundsScaleHandles.length = 0; |
| this.boundsRotHandles.length = 0; |
| } |
| deactivateTool () { |
| this.removeBoundsPath(); |
| this.setCursor(Cursors.DEFAULT); |
| } |
|
|
| setCursor (cursorString) { |
| if (this._currentCursor !== cursorString) { |
| this.dispatchSetCursor(cursorString); |
| this._currentCursor = cursorString; |
| } |
| } |
| } |
|
|
| export default BoundingBoxTool; |
|
|