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

} }


this.requestFrame(); 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) { 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") { } else if (key == "Enter") {
for (let node of this.selectedNodes) { for (let node of this.selectedNodes) {
if (node.activate) { if (node.activate) {
} }


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


let fromNode = this.currentLink.from.node; let fromNode = this.currentLink.from.node;
let fromIO = this.currentLink.from.io; let fromIO = this.currentLink.from.io;
if (fromIO.type == "input" && io.type == "output") { 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") { } 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) { if (fromIO.type != io.type) {
} }
} else { } else {
let path = this.currentLink.path; 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(); this.requestFrame();
} }
} else if (io != null) { } else if (io != null) {
this.currentLink = {from: {node, io}, path: [{x, y}]};
this.currentLink = {from: {node, io}, path: []};
} else { } else {
if (node && node.activate) { if (node && node.activate) {
node.activate(); node.activate();
for (let conn of link.connections) { for (let conn of link.connections) {
this.ctx.beginPath(); this.ctx.beginPath();
this.ctx.moveTo(link.from.x + link.from.width, link.from.y + link.index); 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.lineTo(conn.node.x, conn.node.y + conn.index);
this.ctx.stroke(); this.ctx.stroke();
} }
this.ctx.lineTo(el.x, el.y); this.ctx.lineTo(el.x, el.y);
} }


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

this.ctx.stroke(); this.ctx.stroke();
} }



+ 6
- 2
src/Scene.svelte View File

let nodes = []; let nodes = [];
let y = 0; let y = 0;
for (let name of level.inputs) { 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 += 2;
} }


y = 0; y = 0;
for (let name of level.outputs) { 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; y += 2;
} }



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

this.connections = []; this.connections = [];
} }


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


disconnect(node, index) { disconnect(node, index) {
export class Input { export class Input {
constructor(x, y, name) { constructor(x, y, name) {
this.name = name || "Input"; this.name = name || "Input";
this.protected = false;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.inputs = []; this.inputs = [];
export class Output { export class Output {
constructor(x, y, name) { constructor(x, y, name) {
this.name = name || "Output"; this.name = name || "Output";
this.protected = false;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.inputs = [{name: "Output", links: []}]; this.inputs = [{name: "Output", links: []}];
export class Switch { export class Switch {
constructor(x, y) { constructor(x, y) {
this.name = "OFF"; this.name = "OFF";
this.protected = false;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.inputs = []; this.inputs = [];
export class NotGate { export class NotGate {
constructor(x, y) { constructor(x, y) {
this.name = "NOT"; this.name = "NOT";
this.protected = false;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.inputs = [{name: "In", links: []}]; this.inputs = [{name: "In", links: []}];
export class Diode { export class Diode {
constructor(x, y) { constructor(x, y) {
this.name = "DIODE"; this.name = "DIODE";
this.protected = false;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.inputs = [{name: "In", links: []}]; this.inputs = [{name: "In", links: []}];
export class Lamp { export class Lamp {
constructor(x, y) { constructor(x, y) {
this.name = "LAMP"; this.name = "LAMP";
this.protected = false;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.inputs = [{name: "In", links: []}]; this.inputs = [{name: "In", links: []}];

Loading…
Cancel
Save