From cafdad54ef5165b4cc997415d5e3885d5a42af06 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 2 Jul 2019 02:52:06 +0000 Subject: [PATCH] Allow moving full groups --- architype.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/architype.js b/architype.js index cf36cc5..4f96db9 100644 --- a/architype.js +++ b/architype.js @@ -456,10 +456,13 @@ class Architype { } iterate(graph) { - this.sortByMostTension(graph.nodes); + let nodes = Array.from(graph.nodes); + this.sortByMostTension(nodes); + nodes.push(...graph.groups); + let newOffset = null; - let newTension = this.getTotalTension(graph.nodes); - for (let node of graph.nodes) { + let newTension = this.getTotalTension(nodes); + for (let node of nodes) { let origPos = node.pos; let offsets = new Map(); let addOffset = (x, y) => { @@ -477,7 +480,7 @@ class Architype { } node.savePos(); node.moveBy(graph, offset); - let testTension = this.getTotalTension(graph.nodes); + let testTension = this.getTotalTension(nodes); node.restorePos(graph); if (testTension < newTension) { newOffset = offset; @@ -1019,7 +1022,7 @@ class Node extends EditorEntryBase { offsetCollides(graph, offset) { let newPos = this.offsetToPos(offset); - return graph.nodesByPos.has(newPos.toString()); + return graph.nodesByPos.get(newPos.toString()); } moveBy(graph, offset) { @@ -1184,6 +1187,55 @@ class Group extends EditorEntryBase { return this.elem_; } + setTension() { + this.vec = [0, 0]; + this.tension = 0; + for (let node of this.nodes) { + node.setTension(); + for (let i of [0, 1]) { + this.vec[i] += node.vec[i]; + }; + this.tension += node.tension; + } + } + + offsetCollides(graph, offset) { + // TODO: make this.nodes always a set + let nodeSet = new Set(this.nodes); + for (let node of this.nodes) { + let other = node.offsetCollides(graph, offset); + if (other && !nodeSet.has(other)) { + return other; + } + } + return null; + } + + savePos() { + for (let node of this.nodes) { + node.savePos(); + } + } + + restorePos(graph) { + for (let node of this.nodes) { + node.restorePos(graph); + } + } + + moveBy(graph, offset) { + let nodes = new Set(this.nodes); + while (nodes.size) { + for (let node of nodes) { + if (node.offsetCollides(graph, offset)) { + continue; + } + node.moveBy(graph, offset); + nodes.delete(node); + } + } + } + onInputKeyDown(e) { switch (e.key) { case 'Enter':