Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipboard Improvements #225

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/core/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ export default class Device {
return component;
}
}
return null; // if no component found return null
}

/**
Expand All @@ -1007,6 +1008,7 @@ export default class Device {
return connection;
}
}
return null; // if no connection found return null
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/app/core/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import CustomComponent from "./customComponent";
import Params from "./params";
import Device from "./device";

import paper from "paper";

import * as Parameters from "./parameters";
const StringValue = Parameters.StringValue;
import * as FeatureSets from "../featureSets";
Expand Down Expand Up @@ -31,6 +33,12 @@ export default class Feature {
this.__fabtype = fabtype;
this.__dxfObjects = [];
this.__referenceID = null;
this.__bounds = null;

if (this.__params != null & this.__type !== "Connection") {
let [x,y] = this.__params.getValue("position");
this.__bounds = new paper.Point(x, y);
}
}

/**
Expand All @@ -43,6 +51,25 @@ export default class Feature {
return this.__referenceObject;
}

get bounds() {
return this.__bounds;
}

/**
* Sets the bounds i.e. the x,y position and the width and length of the feature
* @param {Object} bounds PaperJS Rectangle object associated with a Path.bounds property
* @memberof Feature
* @returns {void}
*/
setBounds(bounds) {
this.__bounds = bounds;
let topleftpt = bounds.topLeft;
this.__params.position = [topleftpt.x, topleftpt.y];
this.__params.xspan = bounds.width;
this.__params.yspan = bounds.height;
}


/**
* Sets the reference object id
* @param {} value
Expand Down
27 changes: 20 additions & 7 deletions src/app/view/mouseAndKeyboardHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,41 @@ export default class MouseAndKeyboardHandler {
reference.view.deleteSelectedFeatures();
}
// Copy

if ((event.ctrlKey || event.metaKey) && key === 67) {
//console.log("Ctl c detected");
reference.initiateCopy();
}
// Cut
if ((event.ctrlKey || event.metaKey) && key === 88) {
//console.log("Ctl x detected");
let selectedFeatures = reference.view.getSelectedFeatures();
let selection = reference.view.getSelectedFeatures();
if (selectedFeatures.length > 0) {
reference.pasteboard[0] = selectedFeatures[0];
}
Comment on lines +116 to 119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the cut system also got modified during the variable change but its not using the latest selection system

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I can change that to also use the recent selection system

reference.saveDeviceState();
reference.view.deleteSelectedFeatures();
}
// Paste
if ((event.ctrlKey || event.metaKey) && key === 86) {
//console.log("Ctl v detected");
let pasteboardFeatures = reference.pasteboard;
if (pasteboardFeatures.length > 0) {
reference.updateDefaultsFromFeature(pasteboardFeatures[0]);
reference.activateTool(pasteboardFeatures[0].getType());
if ((event.ctrlKey || event.metaKey) && key == 86) {
console.log("Ctl v detected");
let selection = reference.selection;
let pastedFeatures = selection.getFeatureIDs();
if (pastedFeatures.length > 0) {
reference.activateTool("CopyTool");
} else {
console.error("No features to paste");
}

// if (pasteboardFeatures.length == 1) { // 1 feature
// reference.updateDefaultsFromFeature(pasteboardFeatures[0]);
// reference.activateTool(pasteboardFeatures[0].getType());

// } else if (pasteboardFeatures.length > 1) { // multiple features
// console.log("multiple features detected")
// reference.updateDefaultsFromFeatures(pasteboardFeatures);
// reference.activateTools(pasteboardFeatures);
// }
Comment on lines +133 to +142
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete these if you are not planning to use them anymore

}

//Undo
Expand Down
41 changes: 33 additions & 8 deletions src/app/view/paperView.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as DXFSolidObjectRenderer from "./render2D/dxfSolidObjectRenderer2D";
import Layer from "../core/layer";
import Device from "../core/device";
import Feature from "../core/feature";

import Selection from "./selection";
/**
* Paper View class
*/
Expand Down Expand Up @@ -75,16 +75,17 @@ export default class PaperView {

/**
* Returns a list of selected items on the canvas
* @return {Array}
* @return {Selection}
* @memberof PaperView
*/
getSelectedFeatures() {
let output = [];
let items = paper.project.selectedItems;
for (let i = 0; i < items.length; i++) {
output.push(this.__viewManagerDelegate.currentDevice.getFeatureByID(items[i].featureID));
output.push(items[i].featureID);
}
return output;
let selection = new Selection(output);
return selection;
}
/**
* Deselects the items from the canvas
Expand Down Expand Up @@ -607,10 +608,34 @@ export default class PaperView {
*/
addTarget(featureType, set, position) {
this.removeTarget();
this.lastTargetType = featureType;
this.lastTargetPosition = position;
this.lastTargetSet = set;
this.updateTarget();
if (featureType === "CopyTool") { // render the preview of the copied selection
let selection = this.__viewManagerDelegate.selection;
let referencepoint = selection.getReferencePoint(); // calculate position of where to render target
let featureIDs = selection.getFeatureIDs();
let [x,y] = position;
for (let i=0;i<featureIDs.length;i++) {
let render = Registry.currentDevice.getFeatureByID(featureIDs[i]); // which feature
let type = render.getType(); // Get the type of each item

let newx = referencepoint.x - render.bounds.x;
newx = x + newx;
let newy = referencepoint.y - render.bounds.y;
newy = y + newy;


let newPosition = [newx,newy]

this.lastTargetType = type;
this.lastTargetPosition = newPosition;
this.lastTargetSet = set;
this.updateTarget();
}
} else {
this.lastTargetType = featureType;
this.lastTargetPosition = position;
this.lastTargetSet = set;
this.updateTarget();
}
}

/**
Expand Down
154 changes: 120 additions & 34 deletions src/app/view/selection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import paper from "paper";
import * as Registry from "../core/registry";
import Feature from "../core/feature";
import Params from "../core/params";
import Component from "../core/component";

/**
* Selection class
*/
Expand All @@ -11,13 +16,48 @@ export default class Selection {
this.__components = [];
this.__connections = [];
this.__otherFeatures = [];
//Sort out wether each of the items selected belongs to one of the following
for (let i in items) {
console.log(items[i]);
//Sort out where each of the items selected belongs to one of the following
for (let i in items) { // query if its a component, connection or other feature
let feature = Registry.currentDevice.getComponentByID(items[i]);
if (feature == null) {
feature = Registry.currentDevice.getConnectionByID(items[i]);
if (feature == null) {
feature = Registry.currentDevice.getFeatureByID(items[i]);
console.log(feature);
console.log(items[i]);
if (feature.__type === "Connection") {
console.log("Connection Feature Selected");
this.__connections.push(items[i]);
} else if (feature.__type === "EDGE") {
// ignore the edge
} else {
console.log("Other Feature Selected");
this.__otherFeatures.push(items[i]);
}
} else {
console.log("Connection:",feature);
console.log("Connection Feature Selected");
this.__connections.push(items[i]);
}
} else {
console.log("Component Feature Selected");
this.__components.push(items[i]);
}
}
this.__bounds = this.__calculateSelectionBounds();
console.log("bounds: ", this.__bounds);
}

getFeatureIDs() {
let ret = [];
ret = this.__components.concat(this.__connections);
ret = ret.concat(this.__otherFeatures);
return ret;
}

getReferencePoint() {
return this.__bounds;
}
/**
* Generates a replica
* @param {number} x X coordinate for here the selection should be replicated
Expand All @@ -31,21 +71,68 @@ export default class Selection {
2. Go through each of the items
3. Clone components/connections/other features
*/
let referencepoint = this.__bounds.topleft;
let referencepoint = this.__bounds;

console.log("reference point:", referencepoint);


for (let i in this.__components) {
let render = Registry.currentDevice.getFeatureByID(this.__components[i]);
let render = Registry.currentDevice.getFeatureByID(this.__otherFeatures[i]);
let newx = referencepoint.x - render.bounds.x;
newx = x + newx;
console.log("newx: ", newx);
let newy = referencepoint.y - render.bounds.y;
newy = y + newy;
console.log("newy: ", newy);
let newComponent = render.replicate(newx, newy);
Registry.currentDevice.addComponent(newComponent);
}

for (let i in this.__connections) {
let render = Registry.currentDevice.getFeatureByID(this.__connections[i]);
// let render = Registry.currentDevice.getFeatureByID(this.__connections[i]);
// let replica = render.replicate(x,y);
console.log("implement connections");
}

for (let i in this.__otherFeatures) {
let render = Registry.currentDevice.getFeatureByID(this.__otherFeatures[i]);
let newx = render.bounds.x -referencepoint.x;
newx = x + newx;
console.log("newx: ", newx);
let newy = render.bounds.y - referencepoint.y;
newy = y + newy;
console.log("newy: ", newy);
let replica = render.replicate(newx,newy);
Registry.currentLayer.addFeature(replica);

// if component do this
let featureIDs = [];
featureIDs.push(this.__otherFeatures[i]);
let typeString = replica.__type;
let paramdata = replica.getParams();

let definition = Registry.featureSet.getDefinition(typeString);
let cleanparamdata = {};
for (let key in paramdata) {
cleanparamdata[key] = paramdata[key].getValue();
}
let params = new Params(cleanparamdata, definition.unique, definition.heritable);
let componentid = Feature.generateID();
let name = Registry.currentDevice.generateNewName(typeString);
let newComponent = new Component(typeString, params, name, definition.mint, componentid);
let feature;

for (let i in featureIDs) {
newComponent.addFeatureID(featureIDs[i]);

//Update the component reference
feature = Registry.currentDevice.getFeatureByID(featureIDs[i]);
feature.referenceID = componentid;
}

Registry.currentDevice.addComponent(newComponent);
}

}
/**
* Selects all the components, connections and features
Expand Down Expand Up @@ -88,10 +175,10 @@ export default class Selection {
* @memberof Selection
*/
__calculateSelectionBounds() {
let xmin = 0;
let ymin = 0;
let xmax = 0;
let ymax = 0;
let xmin = Number.MAX_SAFE_INTEGER;
let ymin = Number.MAX_SAFE_INTEGER;
let xmax = Number.MIN_SAFE_INTEGER;
let ymax = Number.MIN_SAFE_INTEGER;
let bounds;

for (let i in this.__components) {
Expand All @@ -103,29 +190,29 @@ export default class Selection {
if (bounds.y < ymin) {
ymin = bounds.y;
}
if (bounds.x + bounds.width > xmax) {
xmax = bounds.x + bounds.width;
if (bounds.x > xmax) {
xmax = bounds.x;
}
if (bounds.y + bounds.height > ymax) {
ymax = bounds.y + bounds.height;
if (bounds.y > ymax) {
ymax = bounds.y;
}
}

for (let i in this.__connections) {
let render = Registry.currentDevice.getFeatureByID(this.__connections[i]);
bounds = render.bounds;
if (bounds.x < xmin) {
xmin = bounds.x;
}
if (bounds.y < ymin) {
ymin = bounds.y;
}
if (bounds.x + bounds.width > xmax) {
xmax = bounds.x + bounds.width;
}
if (bounds.y + bounds.height > ymax) {
ymax = bounds.y + bounds.height;
}
// let render = Registry.currentDevice.getFeatureByID(this.__connections[i]);
// bounds = render.bounds;
// if (bounds.x < xmin) {
// xmin = bounds.x;
// }
// if (bounds.y < ymin) {
// ymin = bounds.y;
// }
// if (bounds.x > xmax) {
// xmax = bounds.x;
// }
// if (bounds.y > ymax) {
// ymax = bounds.y;
// }
}

for (let i in this.__otherFeatures) {
Expand All @@ -137,15 +224,14 @@ export default class Selection {
if (bounds.y < ymin) {
ymin = bounds.y;
}
if (bounds.x + bounds.width > xmax) {
xmax = bounds.x + bounds.width;
if (bounds.x > xmax) {
xmax = bounds.x;
}
if (bounds.y + bounds.height > ymax) {
ymax = bounds.y + bounds.height;
if (bounds.y > ymax) {
ymax = bounds.y;
}
}

let ret = new paper.Rectangle(new paper.Point(xmin, ymin), new paper.Point(xmax, ymax));
let ret = new paper.Point((xmin+xmax)/2, (ymin+ymax)/2);
return ret;
}
}
Loading