2017-09-11 14:23:30 -04:00
|
|
|
import paper from 'paper';
|
2017-10-05 15:41:22 -04:00
|
|
|
import {performSnapshot} from '../undo';
|
2017-09-11 14:23:30 -04:00
|
|
|
|
2017-09-22 12:12:07 -04:00
|
|
|
/**
|
|
|
|
* Tool to handle scaling items by pulling on the handles around the edges of the bounding
|
|
|
|
* box when in the bounding box tool.
|
|
|
|
*/
|
2017-09-11 14:23:30 -04:00
|
|
|
class ScaleTool {
|
2017-09-21 18:20:44 -04:00
|
|
|
/**
|
|
|
|
* @param {!function} onUpdateSvg A callback to call when the image visibly changes
|
|
|
|
*/
|
2017-10-05 15:41:22 -04:00
|
|
|
constructor (onUpdateSvg, undoSnapshot) {
|
2017-09-11 14:23:30 -04:00
|
|
|
this.pivot = null;
|
|
|
|
this.origPivot = null;
|
|
|
|
this.corner = null;
|
|
|
|
this.origSize = null;
|
|
|
|
this.origCenter = null;
|
|
|
|
this.itemGroup = null;
|
|
|
|
this.boundsPath = null;
|
|
|
|
// Lowest item above all scale items in z index
|
|
|
|
this.itemToInsertBelow = null;
|
2017-09-13 17:45:06 -04:00
|
|
|
this.scaleItems = [];
|
|
|
|
this.boundsScaleHandles = [];
|
|
|
|
this.boundsRotHandles = [];
|
2017-09-21 18:20:44 -04:00
|
|
|
this.onUpdateSvg = onUpdateSvg;
|
2017-10-05 15:41:22 -04:00
|
|
|
this.undoSnapshot = undoSnapshot;
|
2017-09-11 14:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!paper.HitResult} hitResult Data about the location of the mouse click
|
|
|
|
* @param {!object} boundsPath Where the boundaries of the hit item are
|
2017-09-14 14:34:45 -04:00
|
|
|
* @param {!object} boundsScaleHandles Bounding box scale handles
|
|
|
|
* @param {!object} boundsRotHandles Bounding box rotation handle
|
2017-09-11 14:23:30 -04:00
|
|
|
* @param {!Array.<paper.Item>} selectedItems Set of selected paper.Items
|
|
|
|
* @param {boolean} clone Whether to clone on mouse down (e.g. alt key held)
|
|
|
|
* @param {boolean} multiselect Whether to multiselect on mouse down (e.g. shift key held)
|
|
|
|
*/
|
2017-09-13 17:45:06 -04:00
|
|
|
onMouseDown (hitResult, boundsPath, boundsScaleHandles, boundsRotHandles, selectedItems) {
|
2017-09-11 14:23:30 -04:00
|
|
|
const index = hitResult.item.data.index;
|
2017-09-13 17:45:06 -04:00
|
|
|
this.boundsPath = boundsPath;
|
|
|
|
this.boundsScaleHandles = boundsScaleHandles;
|
|
|
|
this.boundsRotHandles = boundsRotHandles;
|
2017-09-22 12:12:07 -04:00
|
|
|
this.pivot = this.boundsPath.bounds[this._getOpposingRectCornerNameByIndex(index)].clone();
|
|
|
|
this.origPivot = this.boundsPath.bounds[this._getOpposingRectCornerNameByIndex(index)].clone();
|
|
|
|
this.corner = this.boundsPath.bounds[this._getRectCornerNameByIndex(index)].clone();
|
2017-09-11 14:23:30 -04:00
|
|
|
this.origSize = this.corner.subtract(this.pivot);
|
|
|
|
this.origCenter = this.boundsPath.bounds.center;
|
2017-09-13 17:45:06 -04:00
|
|
|
for (const item of selectedItems) {
|
|
|
|
// Scale only root items
|
|
|
|
if (item.parent instanceof paper.Layer) {
|
|
|
|
this.scaleItems.push(item);
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 14:23:30 -04:00
|
|
|
}
|
|
|
|
onMouseDrag (event) {
|
2017-09-13 17:45:06 -04:00
|
|
|
const scaleTool = this;
|
2017-09-11 14:23:30 -04:00
|
|
|
const modOrigSize = this.origSize;
|
|
|
|
|
|
|
|
// get item to insert below so that scaled items stay in same z position
|
|
|
|
const items = paper.project.getItems({
|
|
|
|
match: function (item) {
|
2017-09-13 17:45:06 -04:00
|
|
|
if (item instanceof paper.Layer || item.data.isHelperItem) {
|
2017-09-11 14:23:30 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-13 17:45:06 -04:00
|
|
|
for (const scaleItem of scaleTool.scaleItems) {
|
2017-09-11 14:23:30 -04:00
|
|
|
if (!scaleItem.isBelow(item)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (items.length > 0) {
|
|
|
|
this.itemToInsertBelow = items[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.itemGroup = new paper.Group(this.scaleItems);
|
|
|
|
this.itemGroup.insertBelow(this.itemToInsertBelow);
|
|
|
|
this.itemGroup.addChild(this.boundsPath);
|
|
|
|
this.itemGroup.data.isHelperItem = true;
|
|
|
|
this.itemGroup.strokeScaling = false;
|
|
|
|
this.itemGroup.applyMatrix = false;
|
|
|
|
|
|
|
|
if (event.modifiers.alt) {
|
|
|
|
this.pivot = this.origCenter;
|
|
|
|
this.modOrigSize = this.origSize * 0.5;
|
|
|
|
} else {
|
|
|
|
this.pivot = this.origPivot;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.corner = this.corner.add(event.delta);
|
|
|
|
const size = this.corner.subtract(this.pivot);
|
|
|
|
let sx = 1.0;
|
|
|
|
let sy = 1.0;
|
|
|
|
if (Math.abs(modOrigSize.x) > 0.0000001) {
|
|
|
|
sx = size.x / modOrigSize.x;
|
|
|
|
}
|
|
|
|
if (Math.abs(modOrigSize.y) > 0.0000001) {
|
|
|
|
sy = size.y / modOrigSize.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.modifiers.shift) {
|
|
|
|
const signx = sx > 0 ? 1 : -1;
|
|
|
|
const signy = sy > 0 ? 1 : -1;
|
|
|
|
sx = sy = Math.max(Math.abs(sx), Math.abs(sy));
|
|
|
|
sx *= signx;
|
|
|
|
sy *= signy;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.itemGroup.scale(sx, sy, this.pivot);
|
|
|
|
|
|
|
|
for (let i = 0; i < this.boundsScaleHandles.length; i++) {
|
|
|
|
const handle = this.boundsScaleHandles[i];
|
2017-09-22 12:12:07 -04:00
|
|
|
handle.position = this.itemGroup.bounds[this._getRectCornerNameByIndex(i)];
|
2017-09-11 14:23:30 -04:00
|
|
|
handle.bringToFront();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < this.boundsRotHandles.length; i++) {
|
|
|
|
const handle = this.boundsRotHandles[i];
|
|
|
|
if (handle) {
|
2017-09-22 12:12:07 -04:00
|
|
|
handle.position = this.itemGroup.bounds[this._getRectCornerNameByIndex(i)] + handle.data.offset;
|
2017-09-11 14:23:30 -04:00
|
|
|
handle.bringToFront();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onMouseUp () {
|
|
|
|
this.pivot = null;
|
|
|
|
this.origPivot = null;
|
|
|
|
this.corner = null;
|
|
|
|
this.origSize = null;
|
|
|
|
this.origCenter = null;
|
2017-09-13 17:45:06 -04:00
|
|
|
this.scaleItems.length = 0;
|
2017-09-11 14:23:30 -04:00
|
|
|
this.boundsPath = null;
|
2017-09-13 17:45:06 -04:00
|
|
|
this.boundsScaleHandles = [];
|
|
|
|
this.boundsRotHandles = [];
|
2017-09-11 14:23:30 -04:00
|
|
|
|
|
|
|
if (!this.itemGroup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.itemGroup.applyMatrix = true;
|
|
|
|
|
|
|
|
// mark text items as scaled (for later use on font size calc)
|
|
|
|
for (let i = 0; i < this.itemGroup.children.length; i++) {
|
|
|
|
const child = this.itemGroup.children[i];
|
|
|
|
if (child.data.isPGTextItem) {
|
|
|
|
child.data.wasScaled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.itemToInsertBelow) {
|
|
|
|
// No increment step because itemGroup.children is getting depleted
|
|
|
|
for (const i = 0; i < this.itemGroup.children.length;) {
|
|
|
|
this.itemGroup.children[i].insertBelow(this.itemToInsertBelow);
|
|
|
|
}
|
|
|
|
this.itemToInsertBelow = null;
|
|
|
|
} else if (this.itemGroup.layer) {
|
|
|
|
this.itemGroup.layer.addChildren(this.itemGroup.children);
|
|
|
|
}
|
|
|
|
this.itemGroup.remove();
|
|
|
|
|
2017-10-05 15:41:22 -04:00
|
|
|
performSnapshot(this.undoSnapshot);
|
2017-09-21 18:20:44 -04:00
|
|
|
this.onUpdateSvg();
|
2017-09-11 14:23:30 -04:00
|
|
|
}
|
2017-09-22 12:12:07 -04:00
|
|
|
_getRectCornerNameByIndex (index) {
|
2017-09-11 14:23:30 -04:00
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
return 'bottomLeft';
|
|
|
|
case 1:
|
|
|
|
return 'leftCenter';
|
|
|
|
case 2:
|
|
|
|
return 'topLeft';
|
|
|
|
case 3:
|
|
|
|
return 'topCenter';
|
|
|
|
case 4:
|
|
|
|
return 'topRight';
|
|
|
|
case 5:
|
|
|
|
return 'rightCenter';
|
|
|
|
case 6:
|
|
|
|
return 'bottomRight';
|
|
|
|
case 7:
|
|
|
|
return 'bottomCenter';
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 12:12:07 -04:00
|
|
|
_getOpposingRectCornerNameByIndex (index) {
|
2017-09-11 14:23:30 -04:00
|
|
|
switch (index) {
|
|
|
|
case 0:
|
|
|
|
return 'topRight';
|
|
|
|
case 1:
|
|
|
|
return 'rightCenter';
|
|
|
|
case 2:
|
|
|
|
return 'bottomRight';
|
|
|
|
case 3:
|
|
|
|
return 'bottomCenter';
|
|
|
|
case 4:
|
|
|
|
return 'bottomLeft';
|
|
|
|
case 5:
|
|
|
|
return 'leftCenter';
|
|
|
|
case 6:
|
|
|
|
return 'topLeft';
|
|
|
|
case 7:
|
|
|
|
return 'topCenter';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ScaleTool;
|