2017-10-16 14:33:11 -04:00
|
|
|
import paper from '@scratch/paper';
|
2017-10-19 16:52:24 -04:00
|
|
|
import Modes from '../../modes/modes';
|
|
|
|
import {styleShape} from '../style-path';
|
|
|
|
import BoundingBoxTool from '../selection-tools/bounding-box-tool';
|
2017-10-16 14:33:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tool for drawing rectangles.
|
|
|
|
*/
|
|
|
|
class RectTool extends paper.Tool {
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
* @param {!function} onUpdateSvg A callback to call when the image visibly changes
|
|
|
|
*/
|
2017-10-19 12:00:21 -04:00
|
|
|
constructor (setSelectedItems, clearSelectedItems, onUpdateSvg) {
|
2017-10-16 14:33:11 -04:00
|
|
|
super();
|
|
|
|
this.onUpdateSvg = onUpdateSvg;
|
|
|
|
this.prevHoveredItemId = null;
|
2017-10-19 16:52:24 -04:00
|
|
|
this.boundingBoxTool = new BoundingBoxTool(Modes.SELECT, setSelectedItems, clearSelectedItems, onUpdateSvg);
|
2017-10-16 14:33:11 -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.onMouseDrag = this.handleMouseDrag;
|
|
|
|
this.onMouseUp = this.handleMouseUp;
|
2017-10-19 16:52:24 -04:00
|
|
|
|
|
|
|
this.downPoint = null;
|
|
|
|
this.rect = null;
|
|
|
|
this.colorState = null;
|
2017-10-16 14:33:11 -04:00
|
|
|
}
|
2017-10-19 16:52:24 -04:00
|
|
|
setColorState (colorState) {
|
|
|
|
this.colorState = colorState;
|
2017-10-16 14:33:11 -04:00
|
|
|
}
|
2017-10-19 16:52:24 -04:00
|
|
|
handleMouseDrag (event) {
|
|
|
|
if (event.event.button > 0) return; // only first mouse button
|
|
|
|
|
|
|
|
if (this.rect) {
|
|
|
|
this.rect.remove();
|
|
|
|
}
|
|
|
|
this.rect = new paper.Path.Rectangle(event.downPoint, event.point);
|
|
|
|
|
|
|
|
if (event.modifiers.shift) {
|
|
|
|
this.rect.height = this.rect.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.modifiers.alt) {
|
|
|
|
this.rect.position = event.downPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
styleShape(this.rect, this.colorState);
|
2017-10-16 14:33:11 -04:00
|
|
|
}
|
2017-10-19 16:52:24 -04:00
|
|
|
handleMouseUp (event) {
|
|
|
|
if (event.event.button > 0) return; // only first mouse button
|
|
|
|
|
|
|
|
if (this.rect) {
|
|
|
|
this.onUpdateSvg();
|
|
|
|
this.rect = null;
|
|
|
|
}
|
2017-10-16 14:33:11 -04:00
|
|
|
}
|
|
|
|
deactivateTool () {
|
2017-10-19 16:52:24 -04:00
|
|
|
this.downPoint = null;
|
|
|
|
this.rect = null;
|
|
|
|
this.colorState = null;
|
2017-10-16 14:33:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RectTool;
|