| import {clearSelection, getSelectedLeafItems} from '../selection'; |
|
|
| |
| class HandleTool { |
| |
| |
| |
| |
| |
| constructor (setSelectedItems, clearSelectedItems, onUpdateImage) { |
| this.hitType = null; |
| this.setSelectedItems = setSelectedItems; |
| this.clearSelectedItems = clearSelectedItems; |
| this.onUpdateImage = onUpdateImage; |
| this.selectedItems = []; |
| } |
| |
| |
| |
| |
| |
| onMouseDown (hitProperties) { |
| if (!hitProperties.multiselect) { |
| clearSelection(this.clearSelectedItems); |
| } |
| |
| hitProperties.hitResult.segment.handleIn.selected = true; |
| hitProperties.hitResult.segment.handleOut.selected = true; |
| this.hitType = hitProperties.hitResult.type; |
| } |
| onMouseDrag (event) { |
| this.selectedItems = getSelectedLeafItems(); |
|
|
| for (const item of this.selectedItems) { |
| for (const seg of item.segments) { |
| |
| |
| if (!seg.origPoint) { |
| seg.origPoint = seg.point.clone(); |
| } |
|
|
| if (seg.handleOut.selected && this.hitType === 'handle-out'){ |
| |
| |
| if (event.modifiers.option || |
| !seg.handleOut.isColinear(seg.handleIn)) { |
| seg.handleOut = seg.handleOut.add(event.delta); |
| } else { |
| seg.handleOut = seg.handleOut.add(event.delta); |
| seg.handleIn = seg.handleOut.multiply(-seg.handleIn.length / seg.handleOut.length); |
| } |
| } else if (seg.handleIn.selected && this.hitType === 'handle-in') { |
| |
| |
| if (event.modifiers.option || |
| !seg.handleOut.isColinear(seg.handleIn)) { |
| seg.handleIn = seg.handleIn.add(event.delta); |
|
|
| } else { |
| seg.handleIn = seg.handleIn.add(event.delta); |
| seg.handleOut = seg.handleIn.multiply(-seg.handleOut.length / seg.handleIn.length); |
| } |
| } |
| } |
| } |
| } |
| onMouseUp () { |
| |
| let moved = false; |
| for (const item of this.selectedItems) { |
| if (!item.segments) { |
| return; |
| } |
| for (const seg of item.segments) { |
| if (seg.origPoint && !seg.equals(seg.origPoint)) { |
| moved = true; |
| } |
| seg.origPoint = null; |
| } |
| } |
| if (moved) { |
| this.setSelectedItems(); |
| this.onUpdateImage(); |
| } |
| this.selectedItems = []; |
| } |
| } |
|
|
| export default HandleTool; |
|
|