| import paper from '@turbowarp/paper'; |
| import {rectSelect} from '../guides'; |
| import {clearSelection, processRectangularSelection} from '../selection'; |
| import {getRaster} from '../layer'; |
| import {ART_BOARD_WIDTH, ART_BOARD_HEIGHT} from '../view'; |
| import {getHitBounds} from '../../helper/bitmap'; |
|
|
| |
| class SelectionBoxTool { |
| |
| |
| |
| |
| |
| constructor (mode, setSelectedItems, clearSelectedItems) { |
| this.selectionRect = null; |
| this.mode = mode; |
| this.setSelectedItems = setSelectedItems; |
| this.clearSelectedItems = clearSelectedItems; |
| } |
| |
| |
| |
| onMouseDown (multiselect) { |
| if (!multiselect) { |
| clearSelection(this.clearSelectedItems); |
| this.clearSelectedItems(); |
| } |
| } |
| onMouseDrag (event) { |
| if (event.event.button > 0) return; |
| if (this.selectionRect) { |
| this.selectionRect.remove(); |
| } |
| this.selectionRect = rectSelect(event); |
| } |
| onMouseUpVector (event) { |
| if (event.event.button > 0) return; |
| if (this.selectionRect) { |
| processRectangularSelection(event, this.selectionRect, this.mode); |
| this.selectionRect.remove(); |
| this.selectionRect = null; |
| this.setSelectedItems(); |
| } |
| } |
| onMouseUpBitmap (event) { |
| if (event.event.button > 0) return; |
| if (this.selectionRect) { |
| let rect = new paper.Rectangle({ |
| from: new paper.Point( |
| Math.max(0, Math.round(this.selectionRect.bounds.topLeft.x)), |
| Math.max(0, Math.round(this.selectionRect.bounds.topLeft.y))), |
| to: new paper.Point( |
| Math.min(ART_BOARD_WIDTH, Math.round(this.selectionRect.bounds.bottomRight.x)), |
| Math.min(ART_BOARD_HEIGHT, Math.round(this.selectionRect.bounds.bottomRight.y))) |
| }); |
|
|
| |
| rect = getHitBounds(getRaster(), rect); |
|
|
| if (rect.area) { |
| |
| const raster = getRaster().getSubRaster(rect); |
| raster.parent = paper.project.activeLayer; |
| raster.canvas.getContext('2d').imageSmoothingEnabled = false; |
| raster.selected = true; |
| |
| const expanded = getRaster().getSubRaster(rect.expand(4)); |
| expanded.remove(); |
| raster.data = {expanded: expanded}; |
|
|
| |
| const context = getRaster().getContext(true ); |
| context.clearRect(rect.x, rect.y, rect.width, rect.height); |
| this.setSelectedItems(); |
| } |
|
|
| |
| this.selectionRect.remove(); |
| this.selectionRect = null; |
| } |
| } |
| } |
|
|
| export default SelectionBoxTool; |
|
|