Browse Source

suport fancy paths, protectable nodes, quality of life improvements

main
Martin Dørum 1 year ago
parent
commit
c426f0d2fb
3 changed files with 41 additions and 16 deletions
  1. 27
    12
      src/CircuitSim.svelte
  2. 6
    2
      src/Scene.svelte
  3. 8
    2
      src/circuit-components.js

+ 27
- 12
src/CircuitSim.svelte View File

@@ -157,11 +157,23 @@
}

this.requestFrame();
} else if (key == "Delete") {
} else if (this.currentLink != null && key == "Backspace") {
if (this.currentLink.path.length > 0) {
this.currentLink.path.pop();
} else {
this.currentLink = null;
}
this.requestFrame();
} else if (key == "Delete" || key == "Backspace") {
let newSelectedNodes = [];
for (let node of this.selectedNodes) {
this.deleteNode(node);
if (node.protected) {
newSelectedNodes.push(node);
} else {
this.deleteNode(node);
}
}
this.selectedNodes = [];
this.selectedNodes = newSelectedNodes;
} else if (key == "Enter") {
for (let node of this.selectedNodes) {
if (node.activate) {
@@ -305,9 +317,6 @@
}

if (this.currentLink != null) {
let p = this.currentLink.path[this.currentLink.path.length - 1];
p.x = x;
p.y = y;
this.requestFrame();
}

@@ -349,9 +358,10 @@
let fromNode = this.currentLink.from.node;
let fromIO = this.currentLink.from.io;
if (fromIO.type == "input" && io.type == "output") {
io.io.link.connect(fromNode, fromIO.index);
this.currentLink.path.reverse();
io.io.link.connect(fromNode, fromIO.index, this.currentLink.path);
} else if (fromIO.type == "output" && io.type == "input") {
fromIO.io.link.connect(node, io.index);
fromIO.io.link.connect(node, io.index, this.currentLink.path);
}

if (fromIO.type != io.type) {
@@ -360,13 +370,11 @@
}
} else {
let path = this.currentLink.path;
path[path.length - 1].x = Math.round(path[path.length - 1].x);
path[path.length - 1].y = Math.round(path[path.length - 1].y);
path.push({x, y});
path.push({x: Math.round(x), y: Math.round(y)});
this.requestFrame();
}
} else if (io != null) {
this.currentLink = {from: {node, io}, path: [{x, y}]};
this.currentLink = {from: {node, io}, path: []};
} else {
if (node && node.activate) {
node.activate();
@@ -509,6 +517,11 @@
for (let conn of link.connections) {
this.ctx.beginPath();
this.ctx.moveTo(link.from.x + link.from.width, link.from.y + link.index);

for (let point of conn.path) {
this.ctx.lineTo(point.x, point.y);
}

this.ctx.lineTo(conn.node.x, conn.node.y + conn.index);
this.ctx.stroke();
}
@@ -578,6 +591,8 @@
this.ctx.lineTo(el.x, el.y);
}

this.ctx.lineTo(this.cursorX, this.cursorY);

this.ctx.stroke();
}


+ 6
- 2
src/Scene.svelte View File

@@ -59,13 +59,17 @@
let nodes = [];
let y = 0;
for (let name of level.inputs) {
nodes.push(new availableComponents.Input(-4, y, name));
let n = new availableComponents.Input(-4, y, name);
n.protected = true;
nodes.push(n);
y += 2;
}

y = 0;
for (let name of level.outputs) {
nodes.push(new availableComponents.Output(4, y, name));
let n = new availableComponents.Output(4, y, name);
n.protected = true;
nodes.push(n);
y += 2;
}


+ 8
- 2
src/circuit-components.js View File

@@ -8,9 +8,9 @@ export class Link {
this.connections = [];
}

connect(node, index) {
connect(node, index, path) {
node.inputs[index].links.push(this);
this.connections.push({node, index});
this.connections.push({node, index, path});
}

disconnect(node, index) {
@@ -40,6 +40,7 @@ export class Link {
export class Input {
constructor(x, y, name) {
this.name = name || "Input";
this.protected = false;
this.x = x;
this.y = y;
this.inputs = [];
@@ -71,6 +72,7 @@ export class Input {
export class Output {
constructor(x, y, name) {
this.name = name || "Output";
this.protected = false;
this.x = x;
this.y = y;
this.inputs = [{name: "Output", links: []}];
@@ -101,6 +103,7 @@ export class Output {
export class Switch {
constructor(x, y) {
this.name = "OFF";
this.protected = false;
this.x = x;
this.y = y;
this.inputs = [];
@@ -133,6 +136,7 @@ export class Switch {
export class NotGate {
constructor(x, y) {
this.name = "NOT";
this.protected = false;
this.x = x;
this.y = y;
this.inputs = [{name: "In", links: []}];
@@ -163,6 +167,7 @@ export class NotGate {
export class Diode {
constructor(x, y) {
this.name = "DIODE";
this.protected = false;
this.x = x;
this.y = y;
this.inputs = [{name: "In", links: []}];
@@ -193,6 +198,7 @@ export class Diode {
export class Lamp {
constructor(x, y) {
this.name = "LAMP";
this.protected = false;
this.x = x;
this.y = y;
this.inputs = [{name: "In", links: []}];

Loading…
Cancel
Save