2017-09-21 10:36:26 -04:00
|
|
|
import paper from 'paper';
|
|
|
|
import log from '../../log/log';
|
|
|
|
import keyMirror from 'keymirror';
|
|
|
|
|
|
|
|
import Modes from '../../modes/modes';
|
|
|
|
import {getHoveredItem} from '../hover';
|
|
|
|
import {deleteSelection} from '../selection';
|
|
|
|
import {getRootItem, isPGTextItem} from '../item';
|
|
|
|
import MoveTool from './move-tool';
|
|
|
|
import PointTool from './point-tool';
|
|
|
|
import HandleTool from './handle-tool';
|
|
|
|
import SelectionBoxTool from './selection-box-tool';
|
|
|
|
|
|
|
|
/** Modes of the reshape tool, which can do many things depending on how it's used. */
|
|
|
|
const ReshapeModes = keyMirror({
|
|
|
|
FILL: null,
|
|
|
|
POINT: null,
|
|
|
|
HANDLE: null,
|
|
|
|
SELECTION_BOX: null
|
|
|
|
});
|
|
|
|
|
2017-09-22 14:14:48 -04:00
|
|
|
/**
|
|
|
|
* paper.Tool to handle reshape mode, which allows manipulation of control points and
|
|
|
|
* handles of path items. Can be used to select items within groups and points within items.
|
|
|
|
* Reshape is made up of 4 tools:
|
|
|
|
* - Selection box tool, which is activated by clicking an empty area. Draws a box and selects
|
|
|
|
* points and curves inside it
|
|
|
|
* - Move tool, which translates items
|
|
|
|
* - Point tool, which translates, adds and removes points
|
|
|
|
* - Handle tool, which translates handles, changing the shape of curves
|
|
|
|
*/
|
2017-09-21 10:36:26 -04:00
|
|
|
class ReshapeTool extends paper.Tool {
|
2017-09-22 14:14:48 -04:00
|
|
|
/** Distance within which mouse is considered to be hitting an item */
|
2017-09-21 10:36:26 -04:00
|
|
|
static get TOLERANCE () {
|
|
|
|
return 8;
|
|
|
|
}
|
2017-09-22 14:14:48 -04:00
|
|
|
/** Clicks registered within this amount of time are registered as double clicks */
|
2017-09-21 10:36:26 -04:00
|
|
|
static get DOUBLE_CLICK_MILLIS () {
|
|
|
|
return 250;
|
|
|
|
}
|
2017-09-22 14:14:48 -04:00
|
|
|
/**
|
|
|
|
* @param {function} setHoveredItem Callback to set the hovered item
|
|
|
|
* @param {function} clearHoveredItem Callback to clear the hovered item
|
2017-10-02 15:25:04 -04:00
|
|
|
* @param {function} setSelectedItems Callback to set the set of selected items in the Redux state
|
|
|
|
* @param {function} clearSelectedItems Callback to clear the set of selected items in the Redux state
|
2017-09-22 14:14:48 -04:00
|
|
|
* @param {!function} onUpdateSvg A callback to call when the image visibly changes
|
|
|
|
*/
|
2017-10-05 15:41:22 -04:00
|
|
|
constructor (setHoveredItem, clearHoveredItem, setSelectedItems, clearSelectedItems, onUpdateSvg, undoSnapshot) {
|
2017-09-21 10:36:26 -04:00
|
|
|
super();
|
|
|
|
this.setHoveredItem = setHoveredItem;
|
|
|
|
this.clearHoveredItem = clearHoveredItem;
|
2017-09-21 18:20:44 -04:00
|
|
|
this.onUpdateSvg = onUpdateSvg;
|
2017-10-05 15:41:22 -04:00
|
|
|
this.undoSnapshot = undoSnapshot;
|
2017-09-22 13:56:58 -04:00
|
|
|
this.prevHoveredItemId = null;
|
2017-09-21 10:36:26 -04:00
|
|
|
this.lastEvent = null;
|
|
|
|
this.mode = ReshapeModes.SELECTION_BOX;
|
|
|
|
this._modeMap = {};
|
2017-10-05 15:41:22 -04:00
|
|
|
this._modeMap[ReshapeModes.FILL] = new MoveTool(setSelectedItems, clearSelectedItems, onUpdateSvg, undoSnapshot);
|
|
|
|
this._modeMap[ReshapeModes.POINT] = new PointTool(setSelectedItems, clearSelectedItems, onUpdateSvg, undoSnapshot);
|
|
|
|
this._modeMap[ReshapeModes.HANDLE] = new HandleTool(setSelectedItems, clearSelectedItems, onUpdateSvg, undoSnapshot);
|
2017-10-03 13:45:19 -04:00
|
|
|
this._modeMap[ReshapeModes.SELECTION_BOX] =
|
|
|
|
new SelectionBoxTool(Modes.RESHAPE, setSelectedItems, clearSelectedItems);
|
2017-09-21 10:36:26 -04:00
|
|
|
|
|
|
|
// We have to set these functions instead of just declaring them because
|
|
|
|
// paper.js tools hook up the listeners in the setter functions.
|
|
|
|
this.onMouseDown = this.handleMouseDown;
|
|
|
|
this.onMouseMove = this.handleMouseMove;
|
|
|
|
this.onMouseDrag = this.handleMouseDrag;
|
|
|
|
this.onMouseUp = this.handleMouseUp;
|
|
|
|
this.onKeyUp = this.handleKeyUp;
|
2017-09-21 18:20:44 -04:00
|
|
|
|
|
|
|
paper.settings.handleSize = 8;
|
2017-09-21 10:36:26 -04:00
|
|
|
}
|
2017-09-22 14:14:48 -04:00
|
|
|
/**
|
|
|
|
* Returns the hit options to use when conducting hit tests.
|
|
|
|
* @param {boolean} preselectedOnly True if we should only return results that are already
|
|
|
|
* selected.
|
|
|
|
* @return {object} See paper.Item.hitTest for definition of options
|
|
|
|
*/
|
2017-09-21 10:36:26 -04:00
|
|
|
getHitOptions (preselectedOnly) {
|
2017-09-22 12:31:39 -04:00
|
|
|
const hitOptions = {
|
|
|
|
segments: true,
|
|
|
|
stroke: true,
|
|
|
|
curves: true,
|
|
|
|
handles: true,
|
|
|
|
fill: true,
|
2017-09-22 14:08:19 -04:00
|
|
|
guide: false,
|
|
|
|
tolerance: ReshapeTool.TOLERANCE / paper.view.zoom
|
2017-09-22 12:31:39 -04:00
|
|
|
};
|
|
|
|
if (preselectedOnly) {
|
|
|
|
hitOptions.match = item => {
|
|
|
|
if (!item.item || !item.item.selected) return;
|
|
|
|
if (item.type === 'handle-out' || item.type === 'handle-in') {
|
|
|
|
// Only hit test against handles that are visible, that is,
|
|
|
|
// their segment is selected
|
|
|
|
if (!item.segment.selected) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
hitOptions.match = item => {
|
|
|
|
if (item.type === 'handle-out' || item.type === 'handle-in') {
|
|
|
|
// Only hit test against handles that are visible, that is,
|
|
|
|
// their segment is selected
|
|
|
|
if (!item.segment.selected) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return hitOptions;
|
2017-09-21 10:36:26 -04:00
|
|
|
}
|
2017-09-22 14:14:48 -04:00
|
|
|
/**
|
|
|
|
* To be called when the hovered item changes. When the select tool hovers over a
|
|
|
|
* new item, it compares against this to see if a hover item change event needs to
|
|
|
|
* be fired.
|
|
|
|
* @param {paper.Item} prevHoveredItemId ID of the highlight item that indicates the mouse is
|
|
|
|
* over a given item currently
|
|
|
|
*/
|
2017-09-22 13:56:58 -04:00
|
|
|
setPrevHoveredItemId (prevHoveredItemId) {
|
|
|
|
this.prevHoveredItemId = prevHoveredItemId;
|
2017-09-21 10:36:26 -04:00
|
|
|
}
|
|
|
|
handleMouseDown (event) {
|
|
|
|
if (event.event.button > 0) return; // only first mouse button
|
|
|
|
this.clearHoveredItem();
|
|
|
|
|
|
|
|
// Check if double clicked
|
|
|
|
let doubleClicked = false;
|
|
|
|
if (this.lastEvent) {
|
|
|
|
if ((event.event.timeStamp - this.lastEvent.event.timeStamp) < ReshapeTool.DOUBLE_CLICK_MILLIS) {
|
|
|
|
doubleClicked = true;
|
|
|
|
} else {
|
|
|
|
doubleClicked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.lastEvent = event;
|
|
|
|
|
2017-09-22 14:14:48 -04:00
|
|
|
// Choose hit result to use ===========================================================
|
2017-09-21 10:36:26 -04:00
|
|
|
// Prefer hits on already selected items
|
|
|
|
let hitResults =
|
|
|
|
paper.project.hitTestAll(event.point, this.getHitOptions(true /* preselectedOnly */));
|
|
|
|
if (hitResults.length === 0) {
|
|
|
|
hitResults = paper.project.hitTestAll(event.point, this.getHitOptions());
|
|
|
|
}
|
|
|
|
if (hitResults.length === 0) {
|
|
|
|
this._modeMap[ReshapeModes.SELECTION_BOX].onMouseDown(event.modifiers.shift);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefer hits on segments to other types of hits, to make sure handles are movable.
|
|
|
|
let hitResult = hitResults[0];
|
|
|
|
for (let i = 0; i < hitResults.length; i++) {
|
|
|
|
if (hitResults[i].type === 'segment') {
|
|
|
|
hitResult = hitResults[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow detail-selection of PGTextItem
|
|
|
|
if (isPGTextItem(getRootItem(hitResult.item))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hitProperties = {
|
|
|
|
hitResult: hitResult,
|
|
|
|
clone: event.modifiers.alt,
|
|
|
|
multiselect: event.modifiers.shift,
|
|
|
|
doubleClicked: doubleClicked,
|
|
|
|
subselect: true
|
|
|
|
};
|
|
|
|
|
|
|
|
// If item is not yet selected, don't behave differently depending on if they clicked a segment
|
|
|
|
// or stroke (since those were invisible), just select the whole thing as if they clicked the fill.
|
|
|
|
if (!hitResult.item.selected ||
|
|
|
|
hitResult.type === 'fill' ||
|
|
|
|
(hitResult.type !== 'segment' && doubleClicked)) {
|
|
|
|
this.mode = ReshapeModes.FILL;
|
|
|
|
this._modeMap[this.mode].onMouseDown(hitProperties);
|
|
|
|
} else if (hitResult.type === 'segment') {
|
|
|
|
this.mode = ReshapeModes.POINT;
|
|
|
|
this._modeMap[this.mode].onMouseDown(hitProperties);
|
|
|
|
} else if (
|
|
|
|
hitResult.type === 'stroke' ||
|
|
|
|
hitResult.type === 'curve') {
|
|
|
|
this.mode = ReshapeModes.POINT;
|
|
|
|
this._modeMap[this.mode].addPoint(hitProperties);
|
|
|
|
this._modeMap[this.mode].onMouseDown(hitProperties);
|
|
|
|
} else if (
|
|
|
|
hitResult.type === 'handle-in' ||
|
|
|
|
hitResult.type === 'handle-out') {
|
|
|
|
this.mode = ReshapeModes.HANDLE;
|
|
|
|
this._modeMap[this.mode].onMouseDown(hitProperties);
|
|
|
|
} else {
|
|
|
|
log.warn(`Unhandled hit result type: ${hitResult.type}`);
|
|
|
|
this.mode = ReshapeModes.FILL;
|
|
|
|
this._modeMap[this.mode].onMouseDown(hitProperties);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo Trigger selection changed. Update styles based on selection.
|
|
|
|
}
|
|
|
|
handleMouseMove (event) {
|
|
|
|
const hoveredItem = getHoveredItem(event, this.getHitOptions(), true /* subselect */);
|
2017-09-22 13:56:58 -04:00
|
|
|
if ((!hoveredItem && this.prevHoveredItemId) || // There is no longer a hovered item
|
|
|
|
(hoveredItem && !this.prevHoveredItemId) || // There is now a hovered item
|
|
|
|
(hoveredItem && this.prevHoveredItemId &&
|
|
|
|
hoveredItem.id !== this.prevHoveredItemId)) { // hovered item changed
|
|
|
|
this.setHoveredItem(hoveredItem ? hoveredItem.id : null);
|
2017-09-21 10:36:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
handleMouseDrag (event) {
|
|
|
|
if (event.event.button > 0) return; // only first mouse button
|
|
|
|
this._modeMap[this.mode].onMouseDrag(event);
|
|
|
|
}
|
|
|
|
handleMouseUp (event) {
|
|
|
|
if (event.event.button > 0) return; // only first mouse button
|
|
|
|
this._modeMap[this.mode].onMouseUp(event);
|
|
|
|
this.mode = ReshapeModes.SELECTION_BOX;
|
|
|
|
}
|
|
|
|
handleKeyUp (event) {
|
|
|
|
// Backspace, delete
|
|
|
|
if (event.key === 'delete' || event.key === 'backspace') {
|
2017-10-05 15:41:22 -04:00
|
|
|
deleteSelection(Modes.RESHAPE, this.undoSnapshot);
|
2017-09-21 18:20:44 -04:00
|
|
|
this.onUpdateSvg();
|
2017-09-21 10:36:26 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 13:56:58 -04:00
|
|
|
deactivateTool () {
|
2017-09-21 18:20:44 -04:00
|
|
|
paper.settings.handleSize = 0;
|
|
|
|
this.clearHoveredItem();
|
2017-09-22 13:56:58 -04:00
|
|
|
this.setHoveredItem = null;
|
|
|
|
this.clearHoveredItem = null;
|
|
|
|
this.onUpdateSvg = null;
|
|
|
|
this.lastEvent = null;
|
2017-09-21 18:20:44 -04:00
|
|
|
}
|
2017-09-21 10:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ReshapeTool;
|