diff --git a/app/js/canvas.js b/app/js/canvas.js index 9337b223..ed935484 100755 --- a/app/js/canvas.js +++ b/app/js/canvas.js @@ -343,13 +343,6 @@ c.onmousedown = function(e) { } else { component.update(); } - } else if(found = findWireByPos()) { - connecting = new Wire(); - connecting.input.push(found); - connecting.pos.push({ - x: mouse.grid.x, - y: mouse.grid.y - }); } else if(found = findPortByPos()) { const port = found; if(port.type == "output") { @@ -360,6 +353,13 @@ c.onmousedown = function(e) { y: mouse.grid.y }); } + } else if(found = findWireByPos()) { + connecting = new Wire(); + connecting.input.push(found); + connecting.pos.push({ + x: mouse.grid.x, + y: mouse.grid.y + }); } else { const component = new Selected(); add( @@ -457,18 +457,26 @@ c.onmousedown = function(e) { component.pos.y += dy / 2.5; for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire) { - wire.pos.slice(-1)[0].x += dx / 2.5; - wire.pos.slice(-1)[0].y += dy / 2.5; + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos.slice(-1)[0].x += dx / 2.5; + wire.pos.slice(-1)[0].y += dy / 2.5; + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire) { - wire.pos[0].x += dx / 2.5; - wire.pos[0].y += dy / 2.5; + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos[0].x += dx / 2.5; + wire.pos[0].y += dy / 2.5; + } + } } } @@ -483,18 +491,26 @@ c.onmousedown = function(e) { component.pos.y = Math.round(component.pos.y); for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire) { - wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); - wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y) + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); + wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y) + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire) { - wire.pos[0].x = Math.round(wire.pos[0].x); - wire.pos[0].y = Math.round(wire.pos[0].y) + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos[0].x = Math.round(wire.pos[0].x); + wire.pos[0].y = Math.round(wire.pos[0].y) + } + } } } @@ -555,18 +571,26 @@ c.onmousemove = function(e) { component.pos.y += dy; for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection - if(wire && !wires.includes(wire)) { - wire.pos.slice(-1)[0].x += dx; - wire.pos.slice(-1)[0].y += dy; + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos.slice(-1)[0].x += dx; + wire.pos.slice(-1)[0].y += dy; + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection - if(wire && !wires.includes(wire)) { - wire.pos[0].x += dx; - wire.pos[0].y += dy; + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos[0].x += dx; + wire.pos[0].y += dy; + } + } } } } @@ -600,18 +624,26 @@ c.onmousemove = function(e) { // Then, all the wires to and from the component need to be fixed... for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire) { - wire.pos.slice(-1)[0].x += dx; - wire.pos.slice(-1)[0].y += dy; + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos.slice(-1)[0].x += dx; + wire.pos.slice(-1)[0].y += dy; + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire) { - wire.pos[0].x += dx; - wire.pos[0].y += dy; + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos[0].x += dx; + wire.pos[0].y += dy; + } + } } } } else if(dragging.port) { @@ -652,12 +684,14 @@ c.onmousemove = function(e) { if(pos.side % 2 == 0) gridPos.x += pos.pos; else gridPos.y -= pos.pos; - if(port.type == "input") { - port.connection.pos.slice(-1)[0].x = gridPos.x; - port.connection.pos.slice(-1)[0].y = gridPos.y; - } else { - port.connection.pos[0].x = gridPos.x; - port.connection.pos[0].y = gridPos.y; + for (let wire of port.connection) { + if(port.type == "input") { + wire.pos.slice(-1)[0].x = gridPos.x; + wire.pos.slice(-1)[0].y = gridPos.y; + } else { + wire.pos[0].x = gridPos.x; + wire.pos[0].y = gridPos.y; + } } } } @@ -847,18 +881,26 @@ c.onmouseup = function(e) { component.pos.y += dy / 2.5; for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection - if(wire && !wires.includes(wire)) { - wire.pos.slice(-1)[0].x += dx / 2.5; - wire.pos.slice(-1)[0].y += dy / 2.5; + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos.slice(-1)[0].x += dx / 2.5; + wire.pos.slice(-1)[0].y += dy / 2.5; + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection - if(wire && !wires.includes(wire)) { - wire.pos[0].x += dx / 2.5; - wire.pos[0].y += dy / 2.5; + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos[0].x += dx / 2.5; + wire.pos[0].y += dy / 2.5; + } + } } } } @@ -896,18 +938,26 @@ c.onmouseup = function(e) { component.pos.y = Math.round(component.pos.y); for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection - if(wire && !wires.includes(wire)) { - wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); - wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y); + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); + wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y); + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection - if(wire && !wires.includes(wire)) { - wire.pos[0].x = Math.round(wire.pos[0].x); - wire.pos[0].y = Math.round(wire.pos[0].y); + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos[0].x = Math.round(wire.pos[0].x); + wire.pos[0].y = Math.round(wire.pos[0].y); + } + } } } } @@ -963,18 +1013,26 @@ c.onmouseup = function(e) { component.pos.y += dy / 2.5; for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire) { - wire.pos.slice(-1)[0].x += dx / 2.5; - wire.pos.slice(-1)[0].y += dy / 2.5; + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos.slice(-1)[0].x += dx / 2.5; + wire.pos.slice(-1)[0].y += dy / 2.5; + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire) { - wire.pos[0].x += dx / 2.5; - wire.pos[0].y += dy / 2.5; + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos[0].x += dx / 2.5; + wire.pos[0].y += dy / 2.5; + } + } } } @@ -989,18 +1047,26 @@ c.onmouseup = function(e) { component.pos.y = Math.round(component.pos.y); for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire) { - wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); - wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y); + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); + wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y); + } + } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire) { - wire.pos[0].x = Math.round(wire.pos[0].x); - wire.pos[0].y = Math.round(wire.pos[0].y); + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos[0].x = Math.round(wire.pos[0].x); + wire.pos[0].y = Math.round(wire.pos[0].y); + } + } } } @@ -1033,14 +1099,18 @@ c.onmouseup = function(e) { } } - const wire = port.connection; - if(wire) { - if(port.type == "input") { - wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); - wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y); - } else { - wire.pos[0].x = Math.round(wire.pos[0].x); - wire.pos[0].y = Math.round(wire.pos[0].y); + const cons = port.connection; + if (cons) { + for (let wire of cons) { + if(wire) { + if(port.type == "input") { + wire.pos.slice(-1)[0].x = Math.round(wire.pos.slice(-1)[0].x); + wire.pos.slice(-1)[0].y = Math.round(wire.pos.slice(-1)[0].y); + } else { + wire.pos[0].x = Math.round(wire.pos[0].x); + wire.pos[0].y = Math.round(wire.pos[0].y); + } + } } } @@ -1092,11 +1162,13 @@ c.onmouseup = function(e) { if(wires.length > 1) { const wire1PosIndex = wires[0].pos.findIndex(pos => pos.x == mouse.grid.x && pos.y == mouse.grid.y); if(wire1PosIndex) { - const wire1Dx = wires[0].pos[wire1PosIndex].x - wires[0].pos[wire1PosIndex + 1].x; - if (wire1Dx != 0) { - const tmp = wires[0]; - wires[0] = wires[1]; - wires[1] = tmp; + if (wires[0].pos[wire1PosIndex]) { + const wire1Dx = wires[0].pos[wire1PosIndex].x - wires[0].pos[wire1PosIndex + 1].x; + if (wire1Dx != 0) { + const tmp = wires[0]; + wires[0] = wires[1]; + wires[1] = tmp; + } } } @@ -1109,7 +1181,6 @@ c.onmouseup = function(e) { // TODO: meer dan 2 if(wires.length > 2) dialog.warning(wires.length + " wires found. Please don't. Get out (menuutje voor Teun)"); if(wires[0].input.includes(wires[1]) && wires[0].output.includes(wires[1])) { - console.log(1); const inputIndex = wires[0].input.indexOf(wires[1]); if(inputIndex > -1) wires[0].input.splice(inputIndex,1); const outputIndex = wires[1].output.indexOf(wires[0]); @@ -1120,7 +1191,6 @@ c.onmouseup = function(e) { intersection.type = 1; } } else if(wires[1].input.includes(wires[0])) { - console.log(2); const inputIndex = wires[1].input.indexOf(wires[0]); if(inputIndex > -1) wires[1].input.splice(inputIndex,1); const outputIndex = wires[0].output.indexOf(wires[1]); @@ -1133,7 +1203,6 @@ c.onmouseup = function(e) { intersection.type = 2; } } else if(wires[1].output.includes(wires[0])) { - console.log(3); const inputIndex = wires[0].input.indexOf(wires[1]); if(inputIndex > -1) wires[0].input.splice(inputIndex,1); const outputIndex = wires[1].output.indexOf(wires[0]); @@ -1144,7 +1213,6 @@ c.onmouseup = function(e) { wires[1].intersections.splice(intersection,1); } } else { - console.log(4); connectWires(wires[0],wires[1]); connectWires(wires[1],wires[0]); diff --git a/app/js/componentUpdates.js b/app/js/componentUpdates.js index 306c2e4e..5e60c89b 100644 --- a/app/js/componentUpdates.js +++ b/app/js/componentUpdates.js @@ -5,17 +5,23 @@ let ticksPerSecond = 0; let pauseSimulation = false; let updates; +let maxUpdatesInitial = 10000; +let maxUpdates = maxUpdatesInitial; function tick() { const start = new Date; updates = 0; - while(updateQueue.length > 0 && updates < 10000 && !pauseSimulation) { + while(updateQueue.length > 0 && updates < maxUpdates && !pauseSimulation) { updateQueue.splice(0,1)[0](); ++updates; } + // randomize max updates, so that flickering wires will flicker on screen as their value will be different each tick + // due to different max updates + maxUpdates = maxUpdatesInitial + Math.floor(Math.random() * Math.floor(100)) - 50; + ticksPerSecond = 1000 / (new Date - lastTick); lastTick = new Date; } diff --git a/app/js/components.js b/app/js/components.js index 87fdd8c9..1cb9ccc8 100644 --- a/app/js/components.js +++ b/app/js/components.js @@ -71,10 +71,20 @@ function addSelection( //connect(wire.from,wire.to,wire); if(wire.to) { - wire.to.connection = wire; + if (!wire.to.connection) { + wire.to.connection = []; + } + if (wire.to.connection.indexOf(wire) == -1) { + wire.to.connection.push(wire); + } } if(wire.from) { - wire.from.connection = wire; + if (!wire.from.connection) { + wire.from.connection = []; + } + if (wire.from.connection.indexOf(wire) == -1) { + wire.from.connection.push(wire); + } const component = wire.from.component; updateQueue.unshift(component.update.bind(component)); @@ -156,19 +166,29 @@ function removeComponent(component, undoable = false, sendToSocket = true) { for(let i = 0; i < component.input.length; ++i) { // Remove connections - const wire = component.input[i].connection; - if(wire) { - const removed = removeWire(wire,false,false); - removedWires.push(...removed); + const cons = component.input[i].connection; + if (cons) { + while (cons.length) { + let wire = cons[0]; + if(wire) { + const removed = removeWire(wire,false,false); + removedWires.push(...removed); + } + } } } for(let i = 0; i < component.output.length; ++i) { // Remove connections - const wire = component.output[i].connection; - if(wire) { - const removed = removeWire(wire,false,false); - removedWires.push(...removed); + const cons = component.output[i].connection; + if (cons) { + while (cons.length) { + let wire = cons[0]; + if(wire) { + const removed = removeWire(wire,false,false); + removedWires.push(...removed); + } + } } } @@ -194,6 +214,9 @@ function removeComponent(component, undoable = false, sendToSocket = true) { for(let i = 0; i < removedWires.length; ++i) { const wire = removedWires[i]; connect(wire.from,wire.to,wire); + if (wire.to && wire.to.component && wire.to.component.update) { + wire.to.component.update() + } for(let i = 0; i < wire.input.length; ++i) { connectWires(wire.input[i],wire); } @@ -224,18 +247,20 @@ function removeWire(wire, undoable = false, sendToSocket = true) { const to = wire.to; if(from) { - delete from.connection; + const index = from.connection.indexOf(wire); + if (index != -1) { + from.connection.splice(index, 1); + } } if(to) { - delete to.connection; - to.value = 0; + const index = to.connection.indexOf(wire); + if (index != -1) { + to.connection.splice(index, 1); + } to.component.update(); } - //delete wire.from; - //delete wire.to; - for(let i = 0; i < wire.input.length; ++i) { const index = wire.input[i].output.indexOf(wire); if(index > -1) { @@ -248,12 +273,16 @@ function removeWire(wire, undoable = false, sendToSocket = true) { } for(let i = 0; i < wire.output.length; ++i) { - const index = wire.output[i].input.indexOf(wire); - if(index > -1) { - wire.output[i].input.splice(index,1); - if(!wire.output[i].from) { - const removed = removeWire(wire.output[i]); - removedWires.push(...removed); + if (wire.output[i]) { + const index = wire.output[i].input.indexOf(wire); + if(index > -1) { + wire.output[i].input.splice(index,1); + if(!wire.output[i].from) { + const removed = removeWire(wire.output[i]); + removedWires.push(...removed); + } else { + wire.output[i].update(0, this); + } } } } @@ -268,6 +297,9 @@ function removeWire(wire, undoable = false, sendToSocket = true) { for(let i = 0; i < removedWires.length; ++i) { const wire = removedWires[i]; connect(wire.from,wire.to,wire); + if (wire.to && wire.to.component && wire.to.component.update) { + wire.to.component.update(); + } for(let i = 0; i < wire.input.length; ++i) { connectWires(wire.input[i],wire); } @@ -320,6 +352,9 @@ function removeSelection(components, wires, undoable = false) { for(let i = 0; i < removedWires.length; ++i) { const wire = removedWires[i]; connect(wire.from,wire.to,wire); + if (wire.to && wire.to.component && wire.to.component.update) { + wire.to.component.update() + } for(let i = 0; i < wire.input.length; ++i) { connectWires(wire.input[i],wire); } @@ -327,6 +362,7 @@ function removeSelection(components, wires, undoable = false) { for(let i = 0; i < wire.output.length; ++i) { connectWires(wire,wire.output[i]); } + wires.push(wire); } redoStack.push(removeSelection.bind(redoCaller,...arguments)); @@ -348,11 +384,21 @@ Connects two components with a wire */ function connect(from, to, wire, undoable = false, sendToSocket = true) { if(to) { - to.connection = wire; + if (!to.connection) { + to.connection = []; + } + if (to.connection.indexOf(wire) == -1) { + to.connection.push(wire); + } wire.to = to; } if(from) { - from.connection = wire; + if (!from.connection) { + from.connection = []; + } + if (from.connection.indexOf(wire) == -1) { + from.connection.push(wire); + } wire.from = from; updateQueue.push(from.component.update.bind(from.component)); @@ -521,116 +567,125 @@ function moveComponent( let oldInputWirePos = []; for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire) { - if(undoable) { - oldInputWirePos.push([...wire.pos]); - } + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + if(undoable) { + oldInputWirePos.push([...wire.pos]); + } - const pos = wire.pos.slice(-1)[0]; - pos.x = component.pos.x; - pos.y = component.pos.y; - const portPos = component.input[i].pos; + const pos = wire.pos.slice(-1)[0]; + pos.x = component.pos.x; + pos.y = component.pos.y; + const portPos = component.input[i].pos; - const angle = Math.PI / 2 * portPos.side; - pos.x += Math.sin(angle); - pos.y += Math.cos(angle); - if(portPos.side == 1) pos.x += (component.width - 1); - else if(portPos.side == 2) pos.y += (component.height - 1); + const angle = Math.PI / 2 * portPos.side; + pos.x += Math.sin(angle); + pos.y += Math.cos(angle); + if(portPos.side == 1) pos.x += (component.width - 1); + else if(portPos.side == 2) pos.y += (component.height - 1); - if(portPos.side % 2 == 0) pos.x += portPos.pos; - else pos.y -= portPos.pos; + if(portPos.side % 2 == 0) pos.x += portPos.pos; + else pos.y -= portPos.pos; - let dx = wire.pos.slice(-1)[0].x - wire.pos.slice(-2)[0].x; - let dy = wire.pos.slice(-1)[0].y - wire.pos.slice(-2)[0].y; + let dx = wire.pos.slice(-1)[0].x - wire.pos.slice(-2)[0].x; + let dy = wire.pos.slice(-1)[0].y - wire.pos.slice(-2)[0].y; - const index = wire.pos.findIndex( - (pos,i) => i < wire.pos.length - 1 && pos.x == wire.pos.slice(-1)[0].x && pos.y == wire.pos.slice(-1)[0].y - ); + const index = wire.pos.findIndex( + (pos,i) => i < wire.pos.length - 1 && pos.x == wire.pos.slice(-1)[0].x && pos.y == wire.pos.slice(-1)[0].y + ); - if(index > -1) { - wire.pos.splice(index + 1,wire.pos.length); - continue; - } + if(index > -1) { + wire.pos.splice(index + 1,wire.pos.length); + continue; + } - while(Math.abs(dx) + Math.abs(dy) > 0) { - let sdx = 0; - let sdy = 0; - if(Math.abs(dx) > Math.abs(dy)) { - sdx = Math.sign(dx); - } else { - sdy = Math.sign(dy); - } + while(Math.abs(dx) + Math.abs(dy) > 0) { + let sdx = 0; + let sdy = 0; + if(Math.abs(dx) > Math.abs(dy)) { + sdx = Math.sign(dx); + } else { + sdy = Math.sign(dy); + } - wire.pos.splice( - wire.pos.length - 1, 0, - { - x: wire.pos.slice(-2)[0].x + sdx, - y: wire.pos.slice(-2)[0].y + sdy + wire.pos.splice( + wire.pos.length - 1, 0, + { + x: wire.pos.slice(-2)[0].x + sdx, + y: wire.pos.slice(-2)[0].y + sdy + } + ); + dx = dx - sdx; + dy = dy - sdy; } - ); - dx = dx - sdx; - dy = dy - sdy; + } } } } let oldOutputWirePos = []; for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire) { - if(undoable) { - oldOutputWirePos.push([...wire.pos]); - } + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + if(undoable) { + oldOutputWirePos.push([...wire.pos]); + } - const pos = wire.pos[0]; - pos.x = component.pos.x; - pos.y = component.pos.y; - const portPos = component.output[i].pos; + const pos = wire.pos[0]; + pos.x = component.pos.x; + pos.y = component.pos.y; + const portPos = component.output[i].pos; - const angle = Math.PI / 2 * portPos.side; - pos.x += Math.sin(angle); - pos.y += Math.cos(angle); - if(portPos.side == 1) pos.x += (component.width - 1); - else if(portPos.side == 2) pos.y += (component.height - 1); + const angle = Math.PI / 2 * portPos.side; + pos.x += Math.sin(angle); + pos.y += Math.cos(angle); + if(portPos.side == 1) pos.x += (component.width - 1); + else if(portPos.side == 2) pos.y += (component.height - 1); - if(portPos.side % 2 == 0) pos.x += portPos.pos; - else pos.y -= portPos.pos; + if(portPos.side % 2 == 0) pos.x += portPos.pos; + else pos.y -= portPos.pos; - let dx = wire.pos[0].x - wire.pos[1].x; - let dy = wire.pos[0].y - wire.pos[1].y; + let dx = wire.pos[0].x - wire.pos[1].x; + let dy = wire.pos[0].y - wire.pos[1].y; - const index = wire.pos.findIndex( - (pos,i) => i > 0 && pos.x == wire.pos[0].x && pos.y == wire.pos[0].y - ); + const index = wire.pos.findIndex( + (pos,i) => i > 0 && pos.x == wire.pos[0].x && pos.y == wire.pos[0].y + ); - if(index > -1) { - wire.pos.splice(1,index); - continue; - } + if(index > -1) { + wire.pos.splice(1,index); + continue; + } - while(Math.abs(dx) + Math.abs(dy) > 0) { - let sdx = 0; - let sdy = 0; - if(Math.abs(dx) > Math.abs(dy)) { - sdx = Math.sign(dx); - } else { - sdy = Math.sign(dy); - } + while(Math.abs(dx) + Math.abs(dy) > 0) { + let sdx = 0; + let sdy = 0; + if(Math.abs(dx) > Math.abs(dy)) { + sdx = Math.sign(dx); + } else { + sdy = Math.sign(dy); + } - wire.pos.splice( - 1, 0, - { - x: wire.pos[1].x + sdx, - y: wire.pos[1].y + sdy + wire.pos.splice( + 1, 0, + { + x: wire.pos[1].x + sdx, + y: wire.pos[1].y + sdy + } + ); + dx = dx - sdx; + dy = dy - sdy; } - ); - dx = dx - sdx; - dy = dy - sdy; + } } } } + // TODO: fix undo stack oldInputWirePos/oldOutputWirePos and in other places if(undoable) { if(this != redoCaller) redoStack = []; @@ -638,21 +693,33 @@ function moveComponent( component.pos.x = oldPos.x; component.pos.y = oldPos.y; + let j = 0; for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire) { - wire.pos = oldInputWirePos[i]; - wire.pos.slice(-1)[0].x -= dx; - wire.pos.slice(-1)[0].y -= dy; + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos = oldInputWirePos[j]; + wire.pos.slice(-1)[0].x -= dx; + wire.pos.slice(-1)[0].y -= dy; + j++; + } + } } } + j = 0; for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire) { - wire.pos = oldOutputWirePos[i]; - wire.pos[0].x -= dx; - wire.pos[0].y -= dy; + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos = oldOutputWirePos[j]; + wire.pos[0].x -= dx; + wire.pos[0].y -= dy; + j++; + } + } } } @@ -677,120 +744,124 @@ function movePort(port, side = port.pos.side, pos = port.pos.pos, undoable = fal port.pos.side = side; port.pos.pos = pos; - const wire = port.connection; - let oldWirePos; - if(wire) { - oldWirePos = [...wire.pos]; - - if(port.type == "input") { + const cons = port.connection; + let oldWirePos = []; + if (cons) { + for (let wire of cons) { + if(wire) { + oldWirePos.push([...wire.pos]); - const pos = wire.pos.slice(-1)[0]; - pos.x = port.component.pos.x; - pos.y = port.component.pos.y; - const portPos = port.pos; - - const angle = Math.PI / 2 * portPos.side; - pos.x += Math.sin(angle); - pos.y += Math.cos(angle); - if(portPos.side == 1) pos.x += (port.component.width - 1); - else if(portPos.side == 2) pos.y -= (port.component.height - 1); - - if(portPos.side % 2 == 0) pos.x += portPos.pos; - else pos.y -= portPos.pos; - - const dx = wire.pos.slice(-1)[0].x - wire.pos.slice(-2)[0].x; - const dy = wire.pos.slice(-1)[0].y - wire.pos.slice(-2)[0].y; - - const vertical = () => { - const x = wire.pos.slice(-2)[0].x; - const y = wire.pos.slice(-2)[0].y + Math.sign(dy); + if(port.type == "input") { - for(let i = 0; i < Math.abs(dy); ++i) { - wire.pos.splice( - wire.pos.length - 1, 0, - { x,y } - ); - } - } + const pos = wire.pos.slice(-1)[0]; + pos.x = port.component.pos.x; + pos.y = port.component.pos.y; + const portPos = port.pos; - const horizontal = () => { - const x = wire.pos.slice(-2)[0].x + Math.sign(dx); - const y = wire.pos.slice(-2)[0].y; + const angle = Math.PI / 2 * portPos.side; + pos.x += Math.sin(angle); + pos.y += Math.cos(angle); + if(portPos.side == 1) pos.x += (port.component.width - 1); + else if(portPos.side == 2) pos.y -= (port.component.height - 1); - for(let i = 0; i < Math.abs(dx); ++i) { - wire.pos.splice( - wire.pos.length - 1, 0, - { x,y } - ); - } - } + if(portPos.side % 2 == 0) pos.x += portPos.pos; + else pos.y -= portPos.pos; - if(port.pos.side % 2 == 0) { - vertical(); - horizontal(); - } else { - horizontal(); - vertical(); - } - } else { - const pos = wire.pos[0]; - pos.x = port.component.pos.x; - pos.y = port.component.pos.y; - const portPos = port.pos; + const dx = wire.pos.slice(-1)[0].x - wire.pos.slice(-2)[0].x; + const dy = wire.pos.slice(-1)[0].y - wire.pos.slice(-2)[0].y; - const angle = Math.PI / 2 * portPos.side; - pos.x += Math.sin(angle); - pos.y += Math.cos(angle); - if(portPos.side == 1) pos.x += (port.component.width - 1); - else if(portPos.side == 2) pos.y -= (port.component.height - 1); + const vertical = () => { + const x = wire.pos.slice(-2)[0].x; + const y = wire.pos.slice(-2)[0].y + Math.sign(dy); - if(portPos.side % 2 == 0) pos.x += portPos.pos; - else pos.y -= portPos.pos; + for(let i = 0; i < Math.abs(dy); ++i) { + wire.pos.splice( + wire.pos.length - 1, 0, + { x,y } + ); + } + } - const dx = wire.pos[0].x - wire.pos[1].x; - const dy = wire.pos[0].y - wire.pos[1].y; + const horizontal = () => { + const x = wire.pos.slice(-2)[0].x + Math.sign(dx); + const y = wire.pos.slice(-2)[0].y; - const vertical = () => { - for(let i = 0; i < Math.abs(dy); ++i) { - const x = wire.pos[1].x; - const y = wire.pos[1].y + Math.sign(dy); + for(let i = 0; i < Math.abs(dx); ++i) { + wire.pos.splice( + wire.pos.length - 1, 0, + { x,y } + ); + } + } - const index = wire.pos.findIndex(pos => pos.x == x && pos.y == y); - if(index > -1) { - wire.pos.splice(0,index); + if(port.pos.side % 2 == 0) { + vertical(); + horizontal(); } else { - wire.pos.splice( - 1, 0, - {x, y} - ); + horizontal(); + vertical(); + } + } else { + const pos = wire.pos[0]; + pos.x = port.component.pos.x; + pos.y = port.component.pos.y; + const portPos = port.pos; + + const angle = Math.PI / 2 * portPos.side; + pos.x += Math.sin(angle); + pos.y += Math.cos(angle); + if(portPos.side == 1) pos.x += (port.component.width - 1); + else if(portPos.side == 2) pos.y -= (port.component.height - 1); + + if(portPos.side % 2 == 0) pos.x += portPos.pos; + else pos.y -= portPos.pos; + + const dx = wire.pos[0].x - wire.pos[1].x; + const dy = wire.pos[0].y - wire.pos[1].y; + + const vertical = () => { + for(let i = 0; i < Math.abs(dy); ++i) { + const x = wire.pos[1].x; + const y = wire.pos[1].y + Math.sign(dy); + + const index = wire.pos.findIndex(pos => pos.x == x && pos.y == y); + if(index > -1) { + wire.pos.splice(0,index); + } else { + wire.pos.splice( + 1, 0, + {x, y} + ); + } + } } - } - } - const horizontal = () => { - for(let i = 0; i < Math.abs(dx); ++i) { - const x = wire.pos[1].x + Math.sign(dx); - const y = wire.pos[1].y; + const horizontal = () => { + for(let i = 0; i < Math.abs(dx); ++i) { + const x = wire.pos[1].x + Math.sign(dx); + const y = wire.pos[1].y; + + const index = wire.pos.findIndex(pos => pos.x == x && pos.y == y); + if(index > -1) { + wire.pos.splice(0,index); + } else { + wire.pos.splice( + 1, 0, + {x, y} + ); + } + } + } - const index = wire.pos.findIndex(pos => pos.x == x && pos.y == y); - if(index > -1) { - wire.pos.splice(0,index); + if(port.pos.side % 2 == 0) { + vertical(); + horizontal(); } else { - wire.pos.splice( - 1, 0, - {x, y} - ); + horizontal(); + vertical(); } } } - - if(port.pos.side % 2 == 0) { - vertical(); - horizontal(); - } else { - horizontal(); - vertical(); - } } } @@ -801,28 +872,35 @@ function movePort(port, side = port.pos.side, pos = port.pos.pos, undoable = fal port.pos.side = oldPos.side; port.pos.pos = oldPos.pos; - if(port.connection) { - port.connection.pos = oldWirePos; - - const component = port.component; - const pos = port.pos; - const gridPos = Object.assign({}, component.pos); - - const angle = Math.PI / 2 * pos.side; - gridPos.x += Math.sin(angle); - gridPos.y += Math.cos(angle); - if(pos.side == 1) gridPos.x += (component.width - 1); - else if(pos.side == 2) gridPos.y += (component.height - 1); - - if(pos.side % 2 == 0) gridPos.x += pos.pos; - else gridPos.y -= pos.pos; - - if(port.type == "input") { - port.connection.pos.slice(-1)[0].x = gridPos.x; - port.connection.pos.slice(-1)[0].y = gridPos.y; - } else { - port.connection.pos[0].x = gridPos.x; - port.connection.pos[0].y = gridPos.y; + let j = 0; + let cons = port.connection; + if (cons) { + for (let wire of cons) { + if(wire) { + wire.pos = oldWirePos[j]; + j++; + + const component = port.component; + const pos = port.pos; + const gridPos = Object.assign({}, component.pos); + + const angle = Math.PI / 2 * pos.side; + gridPos.x += Math.sin(angle); + gridPos.y += Math.cos(angle); + if(pos.side == 1) gridPos.x += (component.width - 1); + else if(pos.side == 2) gridPos.y += (component.height - 1); + + if(pos.side % 2 == 0) gridPos.x += pos.pos; + else gridPos.y -= pos.pos; + + if(port.type == "input") { + wire.pos.slice(-1)[0].x = gridPos.x; + wire.pos.slice(-1)[0].y = gridPos.y; + } else { + wire.pos[0].x = gridPos.x; + port.connection.pos[0].y = gridPos.y; + } + } } } @@ -843,79 +921,87 @@ function moveSelection( component.pos.y += dy; for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - if(wire && !wires.includes(wire)) { - wire.pos.slice(-1)[0].x += dx; - wire.pos.slice(-1)[0].y += dy; - let wdx = wire.pos.slice(-1)[0].x - wire.pos.slice(-2)[0].x; - let wdy = wire.pos.slice(-1)[0].y - wire.pos.slice(-2)[0].y; - - const index = wire.pos.findIndex( - (pos,i) => i < wire.pos.length - 1 && pos.x == wire.pos.slice(-1)[0].x && pos.y == wire.pos.slice(-1)[0].y - ); - - if(index > -1) { - wire.pos.splice(index + 1,wire.pos.length); - continue; - } + const cons = component.input[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos.slice(-1)[0].x += dx; + wire.pos.slice(-1)[0].y += dy; + let wdx = wire.pos.slice(-1)[0].x - wire.pos.slice(-2)[0].x; + let wdy = wire.pos.slice(-1)[0].y - wire.pos.slice(-2)[0].y; + + const index = wire.pos.findIndex( + (pos,i) => i < wire.pos.length - 1 && pos.x == wire.pos.slice(-1)[0].x && pos.y == wire.pos.slice(-1)[0].y + ); - while(Math.abs(wdx) + Math.abs(wdy) > 0) { - let sdx = 0; - let sdy = 0; - if(Math.abs(wdx) > Math.abs(wdy)) { - sdx = Math.sign(wdx); - } else { - sdy = Math.sign(wdy); - } + if(index > -1) { + wire.pos.splice(index + 1,wire.pos.length); + continue; + } - wire.pos.splice( - wire.pos.length - 1, 0, - { - x: wire.pos.slice(-2)[0].x + sdx, - y: wire.pos.slice(-2)[0].y + sdy + while(Math.abs(wdx) + Math.abs(wdy) > 0) { + let sdx = 0; + let sdy = 0; + if(Math.abs(wdx) > Math.abs(wdy)) { + sdx = Math.sign(wdx); + } else { + sdy = Math.sign(wdy); + } + + wire.pos.splice( + wire.pos.length - 1, 0, + { + x: wire.pos.slice(-2)[0].x + sdx, + y: wire.pos.slice(-2)[0].y + sdy + } + ); + wdx = wdx - sdx; + wdy = wdy - sdy; } - ); - wdx = wdx - sdx; - wdy = wdy - sdy; + } } } } for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - if(wire && !wires.includes(wire)) { - wire.pos[0].x += dx; - wire.pos[0].y += dy; - let wdx = wire.pos[0].x - wire.pos[1].x; - let wdy = wire.pos[0].y - wire.pos[1].y; - - const index = wire.pos.findIndex( - (pos,i) => i > 0 && pos.x == wire.pos[0].x && pos.y == wire.pos[0].y - ); - - if(index > -1) { - wire.pos.splice(1,index); - continue; - } + const cons = component.output[i].connection; + if (cons) { + for (let wire of cons) { + if(wire && !wires.includes(wire)) { + wire.pos[0].x += dx; + wire.pos[0].y += dy; + let wdx = wire.pos[0].x - wire.pos[1].x; + let wdy = wire.pos[0].y - wire.pos[1].y; + + const index = wire.pos.findIndex( + (pos,i) => i > 0 && pos.x == wire.pos[0].x && pos.y == wire.pos[0].y + ); - while(Math.abs(wdx) + Math.abs(wdy) > 0) { - let sdx = 0; - let sdy = 0; - if(Math.abs(wdx) > Math.abs(wdy)) { - sdx = Math.sign(wdx); - } else { - sdy = Math.sign(wdy); - } + if(index > -1) { + wire.pos.splice(1,index); + continue; + } - wire.pos.splice( - 1, 0, - { - x: wire.pos[1].x + sdx, - y: wire.pos[1].y + sdy + while(Math.abs(wdx) + Math.abs(wdy) > 0) { + let sdx = 0; + let sdy = 0; + if(Math.abs(wdx) > Math.abs(wdy)) { + sdx = Math.sign(wdx); + } else { + sdy = Math.sign(wdy); + } + + wire.pos.splice( + 1, 0, + { + x: wire.pos[1].x + sdx, + y: wire.pos[1].y + sdy + } + ); + wdx = wdx - sdx; + wdy = wdy - sdy; } - ); - wdx = wdx - sdx; - wdy = wdy - sdy; + } } } } @@ -1281,30 +1367,38 @@ function cloneComponent(component, dx = 0, dy = 0) { clone.width = component.width; for(let i = 0; i < component.input.length; ++i) { - clone.input[i].name = component.input[i].name; - clone.input[i].value = component.input[i].value; - clone.input[i].pos = Object.assign({},component.input[i].pos); + if (component.input[i]) { + clone.input[i].name = component.input[i].name; + clone.input[i].value = component.input[i].value; + clone.input[i].pos = Object.assign({},component.input[i].pos); + } } for(let i = 0; i < component.output.length; ++i) { - clone.output[i].name = component.output[i].name; - clone.output[i].value = component.output[i].value; - clone.output[i].pos = Object.assign({},component.output[i].pos); + if (component.output[i]) { + clone.output[i].name = component.output[i].name; + clone.output[i].value = component.output[i].value; + clone.output[i].pos = Object.assign({},component.output[i].pos); + } } } else { clone.input = []; for(let i = 0; i < component.input.length; ++i) { - const port = clone.addInputPort(); - port.name = component.input[i].name; - port.value = component.input[i].value; - port.pos = Object.assign({},component.input[i].pos); + if (component.input[i]) { + const port = clone.addInputPort(); + port.name = component.input[i].name; + port.value = component.input[i].value; + port.pos = Object.assign({},component.input[i].pos); + } } clone.output = []; for(let i = 0; i < component.output.length; ++i) { - const port = clone.addOutputPort(); - port.name = component.output[i].name; - port.value = component.output[i].value; - port.pos = Object.assign({},component.output[i].pos); + if (component.output[i]) { + const port = clone.addOutputPort(); + port.name = component.output[i].name; + port.value = component.output[i].value; + port.pos = Object.assign({},component.output[i].pos); + } } } return clone; @@ -1377,11 +1471,21 @@ function cloneSelection(components = [], wires = [], dx = 0, dy = 0) { // ); if(toPort) { - toPort.connection = clonedWire; + if (!toPort.connection) { + toPort.connection = []; + } + if (toPort.connection.indexOf(clonedWire) == -1) { + toPort.connection.push(clonedWire); + } clonedWire.to = toPort; } if(fromPort) { - fromPort.connection = clonedWire; + if (!fromPort.connection) { + fromPort.connection = []; + } + if (fromPort.connection.indexOf(clonedWire) == -1) { + fromPort.connection.push(clonedWire); + } clonedWire.from = fromPort; const component = fromPort.component; @@ -1502,6 +1606,9 @@ function componentize( for(let i = 0; i < removed.wires.length; ++i) { const wire = removed.wires[i]; connect(wire.from,wire.to,wire); + if (wire.to && wire.to.component && wire.to.component.update) { + wire.to.component.update() + } for(let i = 0; i < wire.input.length; ++i) { connectWires(wire.input[i],wire); } @@ -1566,6 +1673,8 @@ class Component { // Highlight if(settings.showComponentUpdates) this.highlight(250); + this.updateInputPorts(); + // Update output ports this.function(); @@ -1580,12 +1689,17 @@ class Component { // port.connection.update(port.value); // } - const index = wires.indexOf(port.connection); - if(index == -1) { - wires.push(port.connection); - values.push(port.value); - } else if(values[index] < port.value) { - values[index] = port.value; + let cons = port.connection; + if (cons) { + for (let wire of cons) { + const index = wires.indexOf(wire); + if(index == -1) { + wires.push(wire); + values.push(port.value); + } else if(values[index] < port.value) { + values[index] = port.value; + } + } } } @@ -1595,6 +1709,25 @@ class Component { } } + updateInputPorts() { + for(let i = 0; i < this.input.length; ++i) { + let cons = this.input[i].connection; + + let value = 0; + + if (cons) { + for (let wire of cons) { + if (wire.value == 1) { + value = 1; + break; + } + } + } + + this.input[i].value = value; + } + } + highlight(duration = 500) { this.outline = 1; setTimeout(() => this.outline = 0, duration) @@ -1830,13 +1963,13 @@ class Component { rotate() { // TODO: solution for input/output for(let i = 0; i < this.input.length; ++i) { - if(this.input[i].connection) { + if(this.input[i].connection && this.input[i].connection.length) { return; } } for(let i = 0; i < this.output.length; ++i) { - if(this.output[i].connection) { + if(this.output[i].connection && this.output[i].connection.length) { return; } } @@ -1930,10 +2063,16 @@ class TimerStart extends Component { console.time(); timerStart = new Date; + this.updateInputPorts(); this.function(); this.output[0].value = this.value; - this.output[0].connection && this.output[0].connection.update(this.value); + let cons = this.output[0].connection; + if (cons) { + for (let wire of cons) { + wire.update(this.value); + } + } } function() { @@ -1952,6 +2091,7 @@ class TimerEnd extends Component { console.timeEnd(); boolrConsole.log(this.name + ": " + (new Date - timerStart) + " ms"); + this.updateInputPorts(); this.function(); this.input[0].value == 1 && (this.value = 1); @@ -2075,12 +2215,18 @@ class Delay extends Component { if(settings.showComponentUpdates) this.highlight(250); this.lastUpdate = new Date; + this.updateInputPorts(); const value = this.input[0].value; setTimeout(() => updateQueue.push( () => { this.output[0].value = value; - this.output[0].connection && this.output[0].connection.update(value); + let cons = this.output[0].connection; + if (cons) { + for (let wire of cons) { + wire.update(value); + } + } } ), this.properties.delay); } @@ -2569,7 +2715,7 @@ class Display extends Component { } update() { - + this.updateInputPorts(); } draw() { @@ -2861,6 +3007,7 @@ class Display extends Component { // if(settings.showComponentUpdates) this.highlight(250); // // // Update output ports +// this.updateInputPorts(); // this.function(); // // const port = this.output[0]; @@ -2993,6 +3140,7 @@ class Custom extends Component { update() { // Highlight if(settings.showComponentUpdates) this.highlight(250); + this.updateInputPorts(); this.function(); } @@ -3005,8 +3153,14 @@ class Custom extends Component { // If an input/output port is removed, remove the corresponding port for(let i = 0; i < this.input.length; ++i) { if(!input.includes(this.input[i].inputPort)) { - if(this.input[i].connection) { - removeWire(this.input[i].connection); + let cons = this.input[i].connection; + if(cons) { + while (cons.length) { + let wire = cons[0]; + if (wire) { + removeWire(wire); + } + } } this.input.splice(i,1); --i; @@ -3014,8 +3168,14 @@ class Custom extends Component { } for(let i = 0; i < this.output.length; ++i) { if(!output.includes(this.output[i].outputPort)) { - if(this.output[i].connection) { - removeWire(this.output[i].connection); + let cons = this.output[i].connection; + if(cons) { + while (cons.length) { + let wire = cons[0]; + if (wire) { + removeWire(wire); + } + } } this.output.splice(i,1); --i; @@ -3065,7 +3225,14 @@ class Custom extends Component { const value = this.input[0].value; this.port.value = value; - this.port.connection && this.port.connection.update(value); + let cons = this.port.connection; + if(cons) { + for (let wire of cons) { + if (wire) { + wire.update(value) + } + } + } } } else { port.name = output[i].name; @@ -3381,51 +3548,89 @@ class Wire { this.color = color; } - updateValue(value = 0,from) { - if(value == 1) { - value = 1; - } else if(this.from && this.from.value == 1) { + getNewValue() { + // check value of inputs and outpus if value of this wire would be zero + // in this way we can ensure no self-powering or loop powering + let value = 0; + let old_value = this.value; + this.value = 0; + + if(this.from && this.from.value == 1) { value = 1; - } else if(this.input.find(wire => wire != from && wire.value == 1)) { - const input = this.input.map(wire => wire.value); + } - for(let i = 0; i < this.input.length; ++i) { - if(this.input[i].input.includes(this)) { - input[i] = this.input[i].updateValue(value,this); + for (let i = 0; i < this.input.length; ++i) { + const inp = this.input[i]; + if (inp && inp.value == 1) { + if (inp.getNewValue) { + value = Math.max(value, inp.getNewValue()); + } else { + value = inp.value; } } + } - if(input.indexOf(1) > -1) { - value = 1; - } else { - value = 0; + for (let i = 0; i < this.output.length; ++i) { + const inp = this.output[i]; + if (inp && inp.value == 1) { + if (inp.getNewValue) { + value = Math.max(value, inp.getNewValue()); + } else { + value = inp.value; + } } - } else { - value = 0; } + this.value = old_value; + return value; } update(value,from) { - if(this.input.length > 0) { - value = this.updateValue(value, from); + let initial_value = value; + + if (value == 0 && this.value == 1) { + value = this.getNewValue(); } - if(this.value == value) return; + if(this.value == value && initial_value == value) return; + this.value = value; - for(let i = 0; i < this.output.length; ++i) { - const wire = this.output[i]; - if(wire != from) { - wire.update && updateQueue.push(wire.update.bind(wire,this.value,this)); - } + if (initial_value != value) { + from = null; } if(this.to && this.to.value != this.value) { this.to.value = this.value; this.to.component && updateQueue.push(this.to.component.update.bind(this.to.component)); } + + for(let i = 0; i < this.output.length; ++i) { + const wire = this.output[i]; + if(wire && wire != from) { + if (wire.constructor.name == this.constructor.name) { + if (wire.value != this.value) { + wire.update(this.value, this); + } + } else { + wire.update && updateQueue.push(wire.update.bind(wire,this.value,this)); + } + } + } + + for(let i = 0; i < this.input.length; ++i) { + const wire = this.input[i]; + if(wire && wire != from && wire.value != this.value) { + if (wire.constructor.name == this.constructor.name) { + if (wire.value != this.value) { + wire.update(this.value, this); + } + } else { + wire.update && updateQueue.push(wire.update.bind(wire,this.value,this)); + } + } + } } draw() { diff --git a/app/js/contextmenu2.js b/app/js/contextmenu2.js index 2157f46c..4b6041f8 100644 --- a/app/js/contextmenu2.js +++ b/app/js/contextmenu2.js @@ -300,7 +300,7 @@ createContextMenuOption( "Delete", function() { const component = findComponentByPos(...contextMenu.getPos()); - removeComponent(component); + removeComponent(component, true); }, function() { const component = findComponentByPos(...contextMenu.getPos()); @@ -313,7 +313,7 @@ createContextMenuOption( "delete", "Delete", function() { - removeSelection(selecting.components,selecting.wires); + removeSelection(selecting.components,selecting.wires, true); }, function() { return selecting; diff --git a/app/js/dialogs.js b/app/js/dialogs.js index 1fe88d95..3a26ada5 100644 --- a/app/js/dialogs.js +++ b/app/js/dialogs.js @@ -203,32 +203,42 @@ dialog.connections = function(component) { const input = document.createElement("ul"); for(let i = 0; i < component.input.length; ++i) { - const wire = component.input[i].connection; - wire && (function getInputs(wire) { - if(wire.from) { - const li = document.createElement("li"); - li.innerHTML = wire.from.component.name; - input.appendChild(li); - } - for(let i = 0; i < wire.input.length; ++i) { - getInputs(wire.input[i]); + const cons = component.input[i].connection; + + if (cons) { + for (let wire of cons) { + wire && (function getInputs(wire) { + if(wire.from) { + const li = document.createElement("li"); + li.innerHTML = wire.from.component.name; + input.appendChild(li); + } + for(let i = 0; i < wire.input.length; ++i) { + getInputs(wire.input[i]); + } + })(wire); } - })(wire); + } } const output = document.createElement("ul"); for(let i = 0; i < component.output.length; ++i) { - const wire = component.output[i].connection; - wire && (function getOutputs(wire) { - if(wire.to) { - const li = document.createElement("li"); - li.innerHTML = wire.to.component.name; - output.appendChild(li); - } - for(let i = 0; i < wire.output.length; ++i) { - getOutputs(wire.output[i]); + const cons = component.output[i].connection; + + if (cons) { + for (let wire of cons) { + wire && (function getOutputs(wire) { + if(wire.to) { + const li = document.createElement("li"); + li.innerHTML = wire.to.component.name; + output.appendChild(li); + } + for(let i = 0; i < wire.output.length; ++i) { + getOutputs(wire.output[i]); + } + })(wire); } - })(wire); + } } const connections = input.children.length + output.children.length; @@ -490,10 +500,20 @@ dialog.editPort = function(port) { dialog.container.appendChild(position); const deleteConnection = document.createElement("button"); - deleteConnection.innerHTML = "Delete connection"; + if (port.connection && port.connection.length > 1) { + deleteConnection.innerHTML = "Delete connections"; + } else { + console.log(port.connection) + deleteConnection.innerHTML = "Delete connection"; + } deleteConnection.style.background = "#600"; deleteConnection.onclick = () => { - removeWire(port.connection); + if (port.connection) { + while (port.connection.length) { + let wire = port.connection[0]; + removeWire(wire); + } + } } dialog.container.appendChild(deleteConnection); diff --git a/app/js/localStorage2.js b/app/js/localStorage2.js index d3f4550c..658d98ca 100644 --- a/app/js/localStorage2.js +++ b/app/js/localStorage2.js @@ -372,11 +372,21 @@ function parse(data) { } if(wire.to) { - wire.to.connection = wire; + if (!wire.to.connection) { + wire.to.connection = []; + } + if (wire.to.connection.indexOf(wire) == -1) { + wire.to.connection.push(wire); + } } if(wire.from) { - wire.from.connection = wire; + if (!wire.from.connection) { + wire.from.connection = []; + } + if (wire.from.connection.indexOf(wire) == -1) { + wire.from.connection.push(wire); + } } } diff --git a/app/js/stringifier.js b/app/js/stringifier.js index 96d288c5..ce1805cd 100755 --- a/app/js/stringifier.js +++ b/app/js/stringifier.js @@ -87,11 +87,13 @@ function parse_old(string,dx,dy,select) { const from = result[connection[0]]; const to = result[connection[1]]; - const wire = result[connection[2]]; + const cons = result[connection[2]]; - wire.from = from; - wire.to = to; - connect(from,to,wire); + for (let wire of cons) { + wire.from = from; + wire.to = to; + connect(from,to,wire); + } } if(select) { @@ -101,4 +103,4 @@ function parse_old(string,dx,dy,select) { selecting.components = result; contextMenu.show({ x: (selecting.x + selecting.w + offset.x) * zoom, y: (-(selecting.y + selecting.h) + offset.y) * zoom }); } -} \ No newline at end of file +} diff --git a/app/js/tips.js b/app/js/tips.js index 0d3b9249..b51009da 100755 --- a/app/js/tips.js +++ b/app/js/tips.js @@ -13,18 +13,18 @@ for(let i in tips) { this.style.opacity = .9; (function animate() { - tips[i].style.left = + tips[i].style.left = 20;/* Math.max( Math.min( mouse.screen.x - tips[i].clientWidth / 2, c.width - tips[i].clientWidth), 0 - ); - tips[i].style.top = + );*/ + tips[i].style.top = 20; /* Math.max( mouse.screen.y - tips[i].clientHeight - 20, 0 - ); + );*/ if(tips[i].style.display == "block") requestAnimationFrame(animate); })(); @@ -68,4 +68,4 @@ setInterval(function() { if(!tips.waypoints.disabled && Math.random() < .01) { tips.waypoints.show(); } -}, 500); \ No newline at end of file +}, 500);