| import paper from '@turbowarp/paper'; |
| import {getHoveredItem} from '../hover'; |
| import {expandBy} from '../math'; |
| import {createGradientObject} from '../style-path'; |
| import GradientTypes from '../../lib/gradient-types'; |
|
|
| class FillTool extends paper.Tool { |
| static get TOLERANCE () { |
| return 2; |
| } |
| |
| |
| |
| |
| |
| constructor (setHoveredItem, clearHoveredItem, onUpdateImage) { |
| super(); |
| this.setHoveredItem = setHoveredItem; |
| this.clearHoveredItem = clearHoveredItem; |
| this.onUpdateImage = onUpdateImage; |
|
|
| |
| |
| this.onMouseDown = this.handleMouseDown; |
| this.onMouseMove = this.handleMouseMove; |
| this.onMouseUp = this.handleMouseUp; |
|
|
| |
| this.fillColor = null; |
| this.fillColor2 = null; |
| this.gradientType = null; |
|
|
| |
| this.fillItem = null; |
| |
| this.fillProperty = null; |
| |
| |
| this.addedFillItem = null; |
| this.fillItemOrigColor = null; |
| this.prevHoveredItemId = null; |
| } |
| getHitOptions () { |
| const isAlmostClosedPath = function (item) { |
| return item instanceof paper.Path && item.segments.length > 2 && |
| item.lastSegment.point.getDistance(item.firstSegment.point) < 8; |
| }; |
| return { |
| segments: false, |
| stroke: true, |
| curves: false, |
| fill: true, |
| guide: false, |
| match: function (hitResult) { |
| |
| const hitFill = hitResult.item.hasFill() || hitResult.item.closed || isAlmostClosedPath(hitResult.item); |
| if (hitResult.item instanceof paper.Path && |
| |
| (hitFill || hitResult.type !== 'fill')) { |
| return true; |
| } |
| if (hitResult.item instanceof paper.PointText) { |
| return true; |
| } |
| }, |
| hitUnfilledPaths: true, |
| |
| |
| |
| |
| |
| hitUnstrokedPaths: this.gradientType === GradientTypes.SOLID && this.fillColor === null, |
| tolerance: FillTool.TOLERANCE / paper.view.zoom |
| }; |
| } |
| setFillColor (fillColor) { |
| this.fillColor = fillColor; |
| } |
| setFillColor2 (fillColor2) { |
| this.fillColor2 = fillColor2; |
| } |
| setGradientType (gradientType) { |
| this.gradientType = gradientType; |
| } |
| |
| |
| |
| |
| |
| |
| |
| setPrevHoveredItemId (prevHoveredItemId) { |
| this.prevHoveredItemId = prevHoveredItemId; |
| } |
| updateFillPreview (event) { |
| const hoveredItem = getHoveredItem(event, this.getHitOptions(), true ); |
| if ((!hoveredItem && this.prevHoveredItemId) || |
| (hoveredItem && !this.prevHoveredItemId) || |
| (hoveredItem && this.prevHoveredItemId && |
| hoveredItem.id !== this.prevHoveredItemId)) { |
| this.setHoveredItem(hoveredItem ? hoveredItem.id : null); |
| } |
| const hitItem = hoveredItem ? hoveredItem.data.origItem : null; |
| const hitType = hoveredItem ? hoveredItem.data.hitResult.type : null; |
|
|
| |
| const hitTargetChanged = hitItem !== this.fillItem || hitType !== this.fillProperty; |
|
|
| |
| if (!hitTargetChanged) { |
| |
| if (this.gradientType === GradientTypes.RADIAL) { |
| this._setFillItemColor(this.fillColor, this.fillColor2, this.gradientType, event.point); |
| } |
| return; |
| } |
| if (this.fillItem) { |
| if (this.addedFillItem) { |
| this.addedFillItem.remove(); |
| this.addedFillItem = null; |
| } else { |
| this._setFillItemColor(this.fillItemOrigColor); |
| } |
| this.fillItemOrigColor = null; |
| this.fillItem = null; |
| this.fillProperty = null; |
| } |
| if (hitItem) { |
| this.fillItem = hitItem; |
| this.fillProperty = hitType; |
| const colorProp = hitType === 'fill' ? 'fillColor' : 'strokeColor'; |
| this.fillItemOrigColor = hitItem[colorProp]; |
| if (hitItem.parent instanceof paper.CompoundPath && hitItem.area < 0 && hitType === 'fill') { |
| if (!this.fillColor) { |
| |
| this.fillItem = null; |
| this.fillProperty = null; |
| this.fillItemOrigColor = null; |
| return; |
| } |
| |
| this.addedFillItem = hitItem.clone(); |
| this.addedFillItem.setClockwise(true); |
| this.addedFillItem.data.noHover = true; |
| this.addedFillItem.data.origItem = hitItem; |
| |
| |
| expandBy(this.addedFillItem, .1); |
| this.addedFillItem.insertAbove(hitItem.parent); |
| } else if (this.fillItem.parent instanceof paper.CompoundPath) { |
| this.fillItemOrigColor = hitItem.parent[colorProp]; |
| } |
| this._setFillItemColor(this.fillColor, this.fillColor2, this.gradientType, event.point); |
| } |
| } |
| handleMouseDown (event) { |
| |
| |
| |
| this.updateFillPreview(event); |
| } |
| handleMouseMove (event) { |
| this.updateFillPreview(event); |
| } |
| handleMouseUp (event) { |
| if (event.event.button > 0) return; |
| if (this.fillItem) { |
| |
| if (this.addedFillItem && |
| this._noStroke(this.fillItem.parent) && |
| this.addedFillItem.fillColor.type !== 'gradient' && |
| this.fillItem.parent.fillColor.toCSS() === this.addedFillItem.fillColor.toCSS()) { |
| this.addedFillItem.remove(); |
| this.addedFillItem = null; |
| let parent = this.fillItem.parent; |
| this.fillItem.remove(); |
| parent = parent.reduce(); |
| parent.fillColor = this.fillColor; |
| } else if (this.addedFillItem) { |
| |
| this.addedFillItem.data.noHover = false; |
| } else if (!this.fillColor && |
| this.fillItem.data && |
| this.fillItem.data.origItem) { |
| |
| |
| const group = this.fillItem.parent; |
| this.fillItem.remove(); |
| if (!(group instanceof paper.Layer) && group.children.length === 1) { |
| group.reduce(); |
| } |
| } |
|
|
| this.clearHoveredItem(); |
| this.fillItem = null; |
| this.fillProperty = null; |
| this.addedFillItem = null; |
| this.fillItemOrigColor = null; |
| this.onUpdateImage(); |
| } |
| } |
| _noStroke (item) { |
| return !item.strokeColor || |
| item.strokeColor.alpha === 0 || |
| item.strokeWidth === 0; |
| } |
| |
| |
| _setFillItemColor (color1, color2, gradientType, pointerLocation) { |
| const item = this._getFillItem(); |
| if (!item) return; |
| const colorProp = this.fillProperty === 'fill' ? 'fillColor' : 'strokeColor'; |
| |
| |
| if (gradientType && gradientType !== GradientTypes.SOLID) { |
| item[colorProp] = createGradientObject( |
| color1, |
| color2, |
| gradientType, |
| item.bounds, |
| pointerLocation, |
| item.strokeWidth |
| ); |
| } else { |
| item[colorProp] = color1; |
| } |
| } |
| _getFillItem () { |
| if (this.addedFillItem) { |
| return this.addedFillItem; |
| } else if (this.fillItem && this.fillItem.parent instanceof paper.CompoundPath) { |
| return this.fillItem.parent; |
| } |
| return this.fillItem; |
| } |
| deactivateTool () { |
| if (this.fillItem) { |
| this._setFillItemColor(this.fillItemOrigColor); |
| this.fillItemOrigColor = null; |
| this.fillItem = null; |
| this.fillProperty = null; |
| } |
| this.clearHoveredItem(); |
| this.setHoveredItem = null; |
| this.clearHoveredItem = null; |
| } |
| } |
|
|
| export default FillTool; |
|
|