Skip to content
Snippets Groups Projects
Commit 8f652d91 authored by Maximilian Giller's avatar Maximilian Giller
Browse files

Added connect tool

parent 03a24de9
No related branches found
No related tags found
No related merge requests found
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
<script src="%WWW%editor/js/tools/collecttool.js"></script> <script src="%WWW%editor/js/tools/collecttool.js"></script>
<script src="%WWW%editor/js/tools/deletetool.js"></script> <script src="%WWW%editor/js/tools/deletetool.js"></script>
<script src="%WWW%editor/js/tools/addnodetool.js"></script> <script src="%WWW%editor/js/tools/addnodetool.js"></script>
<script src="%WWW%editor/js/tools/connecttool.js"></script>
<script src="%WWW%editor/js/display.js"></script> <script src="%WWW%editor/js/display.js"></script>
<script src="%WWW%editor/js/state.js"></script> <script src="%WWW%editor/js/state.js"></script>
<script src="%WWW%editor/js/editor.js"></script> <script src="%WWW%editor/js/editor.js"></script>
......
editor/images/tools/connect.png

3.17 KiB

...@@ -196,6 +196,40 @@ const graph = { ...@@ -196,6 +196,40 @@ const graph = {
return result; return result;
}, },
addLink(sourceId, targetId, linkDetails = {}) {
// Copy params
var newLink = linkDetails;
// Make sure the IDs exist
if (
sourceId === undefined ||
targetId === undefined ||
graph.existsNodeId(sourceId) === false ||
graph.existsNodeId(targetId) === false
) {
return;
}
// Make sure the link is unique
if (graph.existsLink(sourceId, targetId)) {
return;
}
newLink[LINK_SOURCE] = sourceId;
newLink[LINK_TARGET] = targetId;
// Basic node properties
newLink.link = true;
newLink.node = false;
// Add node
graph.data[GRAPH_LINKS].push(newLink);
graph.update();
return newLink;
},
addNode(nodeDetails) { addNode(nodeDetails) {
// Copy params // Copy params
var newNode = nodeDetails; var newNode = nodeDetails;
...@@ -210,7 +244,6 @@ const graph = { ...@@ -210,7 +244,6 @@ const graph = {
// Basic node properties // Basic node properties
newNode.node = true; newNode.node = true;
newNode.link = false; newNode.link = false;
newNode.index = graph.data[GRAPH_NODES].length;
// Add node // Add node
graph.data[GRAPH_NODES].push(newNode); graph.data[GRAPH_NODES].push(newNode);
......
...@@ -3,6 +3,7 @@ const TOOLS = { ...@@ -3,6 +3,7 @@ const TOOLS = {
collect: new CollectTool("collect"), collect: new CollectTool("collect"),
delete: new DeleteTool("delete"), delete: new DeleteTool("delete"),
addnode: new AddNodeTool("addnode"), addnode: new AddNodeTool("addnode"),
connect: new ConnectTool("connect"),
}; };
const CONTEXT = { const CONTEXT = {
......
const KEEP_SOURCE_KEY_ID = 17;
class ConnectTool extends Tool {
constructor(key) {
super("Connect two nodes", "connect", key);
this.keepSource = false;
}
onNodeClick(node) {
// Is a first node selected?
if (state.selectedItem === undefined || state.selectedItem.node === false) {
state.setSelectedItem(node);
return;
}
// Add new link
var link = graph.addLink(state.selectedItem[NODE_ID], node[NODE_ID]);
if (link === undefined) {
console.error("Could not create new link");
return;
}
if (this.keepSource === false) {
// Deselect the current first node
// TODO: Returned object not yet converted to normal one
state.setSelectedItem(link);
}
}
onBackgroundClick(event, positions) {
state.setSelectedItem(undefined);
}
onKeyDown(key) {
if (key.keyCode === KEEP_SOURCE_KEY_ID) {
this.keepSource = true;
}
}
onKeyUp(key) {
if (key.keyCode === KEEP_SOURCE_KEY_ID) {
this.keepSource = false;
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment