| import paper from '@turbowarp/paper'; |
| import Modes, {BitmapModes} from '../../lib/modes'; |
| import {isGroup} from '../group'; |
| import {isCompoundPathItem, getRootItem} from '../item'; |
| import {checkPointsClose, snapDeltaToAngle} from '../math'; |
| import {getActionBounds, CENTER} from '../view'; |
| import {clearSelection, cloneSelection, getSelectedLeafItems, getSelectedRootItems, setItemSelection} |
| from '../selection'; |
| import {getDragCrosshairLayer, CROSSHAIR_FULL_OPACITY} from '../layer'; |
|
|
| |
| const SNAPPING_THRESHOLD = 4; |
| const FADE_DISTANCE = 10; |
|
|
| |
| |
| |
| class MoveTool { |
| |
| |
| |
| |
| |
| |
| |
| constructor (mode, setSelectedItems, clearSelectedItems, onUpdateImage, switchToTextTool) { |
| this.mode = mode; |
| this.setSelectedItems = setSelectedItems; |
| this.clearSelectedItems = clearSelectedItems; |
| this.selectedItems = null; |
| this.selectionCenter = null; |
| this.onUpdateImage = onUpdateImage; |
| this.switchToTextTool = switchToTextTool; |
| this.boundsPath = null; |
| this.firstDrag = false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| onMouseDown (hitProperties) { |
| let item = hitProperties.hitResult.item; |
| if (!hitProperties.subselect) { |
| const root = getRootItem(hitProperties.hitResult.item); |
| item = isCompoundPathItem(root) || isGroup(root) ? root : hitProperties.hitResult.item; |
| } |
| if (item.selected) { |
| |
| |
| if (hitProperties.doubleClicked) { |
| if (!hitProperties.multiselect) { |
| if (this.switchToTextTool && item instanceof paper.PointText) { |
| this.switchToTextTool(); |
| return; |
| } |
| clearSelection(this.clearSelectedItems); |
| } |
| this._select(item, true , hitProperties.subselect, true ); |
| } else if (hitProperties.multiselect) { |
| this._select(item, false , hitProperties.subselect); |
| } |
| } else { |
| |
| if (!hitProperties.multiselect) { |
| clearSelection(this.clearSelectedItems); |
| } |
| this._select(item, true, hitProperties.subselect); |
| } |
| if (hitProperties.clone) cloneSelection(hitProperties.subselect, this.onUpdateImage); |
|
|
| this.selectedItems = this.mode === Modes.RESHAPE ? getSelectedLeafItems() : getSelectedRootItems(); |
| if (this.selectedItems.length === 0) { |
| return; |
| } |
|
|
| let selectionBounds; |
| for (const selectedItem of this.selectedItems) { |
| if (selectionBounds) { |
| selectionBounds = selectionBounds.unite(selectedItem.bounds); |
| } else { |
| selectionBounds = selectedItem.bounds; |
| } |
| } |
| this.selectionCenter = selectionBounds.center; |
|
|
| if (this.boundsPath) { |
| this.selectedItems.push(this.boundsPath); |
| } |
|
|
| this.firstDrag = true; |
| } |
| setBoundsPath (boundsPath) { |
| this.boundsPath = boundsPath; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| _select (item, state, subselect, fullySelect) { |
| if (subselect) { |
| item.selected = false; |
| if (fullySelect) { |
| item.fullySelected = state; |
| } else { |
| item.selected = state; |
| } |
| } else { |
| setItemSelection(item, state); |
| } |
| this.setSelectedItems(); |
| } |
| onMouseDrag (event) { |
| const point = event.point; |
| const actionBounds = getActionBounds(this.mode in BitmapModes); |
|
|
| point.x = Math.max(actionBounds.left, Math.min(point.x, actionBounds.right)); |
| point.y = Math.max(actionBounds.top, Math.min(point.y, actionBounds.bottom)); |
| |
| const dragVector = point.subtract(event.downPoint); |
| let snapVector; |
|
|
| |
| if (!event.modifiers.shift && this.mode !== Modes.RESHAPE) { |
| if (checkPointsClose( |
| this.selectionCenter.add(dragVector), |
| CENTER, |
| SNAPPING_THRESHOLD / paper.view.zoom )) { |
|
|
| snapVector = CENTER.subtract(this.selectionCenter); |
| } |
| } |
| if (this.selectedItems.length === 0) { |
| return; |
| } |
|
|
| let bounds; |
| for (const item of this.selectedItems) { |
| |
| |
| if (!item.data.origPos) { |
| item.data.origPos = item.position; |
| } |
|
|
| if (snapVector) { |
| item.position = item.data.origPos.add(snapVector); |
| } else if (event.modifiers.shift) { |
| item.position = item.data.origPos.add(snapDeltaToAngle(dragVector, Math.PI / 4)); |
| } else { |
| item.position = item.data.origPos.add(dragVector); |
| } |
|
|
| if (bounds) { |
| bounds = bounds.unite(item.bounds); |
| } else { |
| bounds = item.bounds; |
| } |
| } |
| |
| if (this.firstDrag) { |
| |
| getDragCrosshairLayer().visible = true; |
| this.firstDrag = false; |
| } |
|
|
| |
| |
| let opacityMultiplier = 1; |
| const newCenter = this.selectionCenter.add(dragVector); |
| if ((CENTER.y < bounds.top && CENTER.x < bounds.left) || |
| (CENTER.y > bounds.bottom && CENTER.x < bounds.left) || |
| (CENTER.y < bounds.top && CENTER.x > bounds.right) || |
| (CENTER.y > bounds.bottom && CENTER.x > bounds.right)) { |
|
|
| |
| const distX = Math.max(CENTER.x - bounds.right, bounds.left - CENTER.x); |
| const distY = Math.max(CENTER.y - bounds.bottom, bounds.top - CENTER.y); |
| const dist = Math.sqrt((distX * distX) + (distY * distY)); |
| opacityMultiplier = |
| Math.max(0, (1 - (dist / (FADE_DISTANCE / paper.view.zoom)))); |
| } else if (CENTER.y < bounds.top || CENTER.y > bounds.bottom) { |
| |
| opacityMultiplier = Math.max(0, |
| (1 - ((Math.abs(CENTER.y - newCenter.y) - (bounds.height / 2)) / (FADE_DISTANCE / paper.view.zoom)))); |
| } else if (CENTER.x < bounds.left || CENTER.x > bounds.right) { |
| |
| opacityMultiplier = Math.max(0, |
| (1 - ((Math.abs(CENTER.x - newCenter.x) - (bounds.width / 2)) / (FADE_DISTANCE / paper.view.zoom)))); |
| } |
| getDragCrosshairLayer().opacity = CROSSHAIR_FULL_OPACITY * opacityMultiplier; |
| } |
| onMouseUp () { |
| this.firstDrag = false; |
| let moved = false; |
| |
| for (const item of this.selectedItems) { |
| if (item.data.origPos) { |
| if (!item.position.equals(item.data.origPos)) moved = true; |
| delete item.data.origPos; |
| } |
| } |
| this.selectedItems = null; |
| this.selectionCenter = null; |
|
|
| if (moved) { |
| this.onUpdateImage(); |
| } |
|
|
| |
| getDragCrosshairLayer().visible = false; |
| } |
| } |
|
|
| export default MoveTool; |
|
|