Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: typescript integration : src/simulator/src/wire.ts #442

Merged
merged 8 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 116 additions & 93 deletions src/simulator/src/wire.js → src/simulator/src/wire.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* eslint-disable no-multi-assign */
// wire object
import { drawLine } from './canvasApi'
import { simulationArea } from './simulationArea'
import Node from './node'
import { updateSimulationSet, forceResetNodesSet } from './engine'
import { colors } from './themer/themer'
import { drawLine } from './canvasApi';
import { simulationArea } from './simulationArea';
import Node from './node';
import { updateSimulationSet, forceResetNodesSet } from './engine';
import { colors } from './themer/themer';

interface Scope {
wires: Wire[];
root: any; // Replace 'any' with the appropriate type if known
timeStamp: number;
}

/**
* Wire - To connect two nodes.
Expand All @@ -16,63 +22,76 @@ import { colors } from './themer/themer'
* @category wire
*/
export default class Wire {
constructor(node1, node2, scope) {
this.objectType = 'Wire'
this.node1 = node1
this.scope = scope
this.node2 = node2
this.type = 'horizontal'

this.updateData()
this.scope.wires.push(this)
forceResetNodesSet(true)
objectType: string;
node1: Node;
node2: Node;
scope: Scope;
type: string;
x1: number;
y1: number;
x2: number;
y2: number;

constructor(node1: Node, node2: Node, scope: Scope) {
this.objectType = 'Wire';
this.node1 = node1;
this.scope = scope;
this.node2 = node2;
this.type = 'horizontal';

this.x1 = this.node1.absX();
this.y1 = this.node1.absY();
this.x2 = this.node2.absX();
this.y2 = this.node2.absY();
this.scope.wires.push(this);
forceResetNodesSet(true);
}

// if data changes
updateData() {
this.x1 = this.node1.absX()
this.y1 = this.node1.absY()
this.x2 = this.node2.absX()
this.y2 = this.node2.absY()
if (this.x1 === this.x2) this.type = 'vertical'
updateData(): void {
this.x1 = this.node1.absX();
this.y1 = this.node1.absY();
this.x2 = this.node2.absX();
this.y2 = this.node2.absY();
if (this.x1 === this.x2) this.type = 'vertical';
}

updateScope(scope) {
this.scope = scope
this.checkConnections()
updateScope(scope: Scope): void {
this.scope = scope;
this.checkConnections();
}

// to check if nodes are disconnected
checkConnections() {
var check =
checkConnections(): boolean {
const check =
this.node1.deleted ||
this.node2.deleted ||
!this.node1.connections.includes(this.node2) ||
!this.node2.connections.includes(this.node1)
if (check) this.delete()
return check
!(this.node1.connections?.includes(this.node2)) ||
!(this.node2.connections?.includes(this.node1) ?? false);
if (check) this.delete();
return check;
}

dblclick() {
dblclick(): void {
if (
this.node1.parent == globalScope.root &&
this.node2.parent == globalScope.root
) {
simulationArea.multipleObjectSelections = [this.node1, this.node2]
simulationArea.lastSelected = undefined
simulationArea.multipleObjectSelections = [this.node1, this.node2];
simulationArea.lastSelected = undefined;
}
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved
}

update() {
var updated = false
if (embed) return updated
update(): boolean {
let updated = false;
if (embed) return updated;
ThatDeparted2061 marked this conversation as resolved.
Show resolved Hide resolved

if (this.node1.absX() === this.node2.absX()) {
this.x1 = this.x2 = this.node1.absX()
this.type = 'vertical'
this.x1 = this.x2 = this.node1.absX();
this.type = 'vertical';
} else if (this.node1.absY() === this.node2.absY()) {
this.y1 = this.y2 = this.node1.absY()
this.type = 'horizontal'
this.y1 = this.y2 = this.node1.absY();
this.type = 'horizontal';
}
if (
simulationArea.shiftDown === false &&
Expand All @@ -83,84 +102,84 @@ export default class Wire {
simulationArea.mouseDownY
)
) {
simulationArea.selected = true
simulationArea.lastSelected = this
updated = true
simulationArea.selected = true;
simulationArea.lastSelected = this;
updated = true;
} else if (
simulationArea.mouseDown &&
simulationArea.lastSelected === this &&
!this.checkWithin(simulationArea.mouseX, simulationArea.mouseY)
) {
var n = new Node(
const n = new Node(
simulationArea.mouseDownX,
simulationArea.mouseDownY,
2,
this.scope.root
)
n.clicked = true
n.wasClicked = true
simulationArea.lastSelected = n
this.converge(n)
);
n.clicked = true;
n.wasClicked = true;
simulationArea.lastSelected = n;
this.converge(n);
}
// eslint-disable-next-line no-empty
if (simulationArea.lastSelected === this) {
}

if (this.node1.deleted || this.node2.deleted) {
this.delete()
return updated
this.delete();
return updated;
} // if either of the nodes are deleted

if (simulationArea.mouseDown === false) {
if (this.type === 'horizontal') {
if (this.node1.absY() !== this.y1) {
// if(this.checkConnections()){this.delete();return;}
n = new Node(this.node1.absX(), this.y1, 2, this.scope.root)
this.converge(n)
updated = true
const n = new Node(this.node1.absX(), this.y1, 2, this.scope.root);
this.converge(n);
updated = true;
} else if (this.node2.absY() !== this.y2) {
// if(this.checkConnections()){this.delete();return;}
n = new Node(this.node2.absX(), this.y2, 2, this.scope.root)
this.converge(n)
updated = true
const n = new Node(this.node2.absX(), this.y2, 2, this.scope.root);
this.converge(n);
updated = true;
}
} else if (this.type === 'vertical') {
if (this.node1.absX() !== this.x1) {
// if(this.checkConnections()){this.delete();return;}
n = new Node(this.x1, this.node1.absY(), 2, this.scope.root)
this.converge(n)
updated = true
const n = new Node(this.x1, this.node1.absY(), 2, this.scope.root);
this.converge(n);
updated = true;
} else if (this.node2.absX() !== this.x2) {
// if(this.checkConnections()){this.delete();return;}
n = new Node(this.x2, this.node2.absY(), 2, this.scope.root)
this.converge(n)
updated = true
const n = new Node(this.x2, this.node2.absY(), 2, this.scope.root);
this.converge(n);
updated = true;
}
}
}
return updated
return updated;
}

draw() {
draw(): void {
// for calculating min-max Width,min-max Height
const ctx = simulationArea.context
const ctx = simulationArea.context;

var color
let color: string;
if (simulationArea.lastSelected == this) {
color = colors['color_wire_sel']
color = colors['color_wire_sel'];
} else if (
this.node1.value == undefined ||
this.node2.value == undefined
) {
color = colors['color_wire_lose']
color = colors['color_wire_lose'];
} else if (this.node1.bitWidth == 1) {
color = [
colors['color_wire_lose'],
colors['color_wire_con'],
colors['color_wire_pow'],
][this.node1.value + 1]
][this.node1.value + 1];
} else {
color = colors['color_wire']
color = colors['color_wire'];
}
drawLine(
ctx,
Expand All @@ -170,67 +189,71 @@ export default class Wire {
this.node2.absY(),
color,
3
)
);
}

// checks if node lies on wire
checkConvergence(n) {
return this.checkWithin(n.absX(), n.absY())
checkConvergence(n: Node): boolean {
return this.checkWithin(n.absX(), n.absY());
}

// fn checks if coordinate lies on wire
checkWithin(x, y) {
checkWithin(x: number, y: number): boolean {
if (
this.type === 'horizontal' &&
this.node1.absX() < this.node2.absX() &&
x > this.node1.absX() &&
x < this.node2.absX() &&
y === this.node2.absY()
)
return true
return true;
if (
this.type === 'horizontal' &&
this.node1.absX() > this.node2.absX() &&
x < this.node1.absX() &&
x > this.node2.absX() &&
y === this.node2.absY()
)
return true
return true;
if (
this.type === 'vertical' &&
this.node1.absY() < this.node2.absY() &&
y > this.node1.absY() &&
y < this.node2.absY() &&
x === this.node2.absX()
)
return true
return true;
if (
this.type === 'vertical' &&
this.node1.absY() > this.node2.absY() &&
y < this.node1.absY() &&
y > this.node2.absY() &&
x === this.node2.absX()
)
return true
return false
return true;
return false;
}

// add intermediate node between these 2 nodes
converge(n) {
this.node1.connect(n)
this.node2.connect(n)
this.delete()
converge(n: Node): void {
this.node1.connect(n);
this.node2.connect(n);
this.delete();
}

delete() {
forceResetNodesSet(true)
updateSimulationSet(true)
this.node1.connections = this.node1.connections.filter(x => x !== this.node2);
this.node2.connections = this.node2.connections.filter(x => x !== this.node1)
this.scope.wires = this.scope.wires.filter(x => x !== this)
this.node1.checkDeleted()
this.node2.checkDeleted()
delete(): void {
forceResetNodesSet(true);
updateSimulationSet(true);
if (this.node1.connections) {
this.node1.connections = this.node1.connections.filter(x => x !== this.node2);
}
if (this.node2.connections) {
this.node2.connections = this.node2.connections.filter(x => x !== this.node1);
}
this.scope.wires = this.scope.wires.filter(x => x !== this);
this.node1.checkDeleted();
this.node2.checkDeleted();

this.scope.timeStamp = new Date().getTime();
}
}
}