-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwire.js
More file actions
154 lines (132 loc) · 5.69 KB
/
wire.js
File metadata and controls
154 lines (132 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//wire object
function Wire(node1, node2, scope) {
this.objectType = "Wire";
//if data changes
this.updateData = function() {
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();
if (this.x1 == this.x2) this.type = "vertical";
}
this.updateScope=function(scope){
this.scope=scope;
this.checkConnections();
}
this.updateData();
this.scope.wires.push(this);
//to check if nodes are disconnected
this.checkConnections = function() {
var check = this.node1.deleted||this.node2.deleted||!this.node1.connections.contains(this.node2) || !this.node2.connections.contains(this.node1);
if (check) this.delete();
return check;
}
this.update = function() {
if(embed)return;
if(this.node1.absX()==this.node2.absX()){
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";
}
var updated = false;
if (wireToBeChecked && this.checkConnections()) {
this.delete();
return;
} // SLOW , REMOVE
if (simulationArea.shiftDown==false&&simulationArea.mouseDown == true && simulationArea.selected == false && this.checkWithin(simulationArea.mouseDownX, simulationArea.mouseDownY)) {
simulationArea.selected = true;
simulationArea.lastSelected = this;
updated = true;
}
else if(simulationArea.lastSelected==this&& !this.checkWithin(simulationArea.mouseX, simulationArea.mouseY)){
var n = new Node(simulationArea.mouseDownX, simulationArea.mouseDownY, 2, this.scope.root);
n.clicked = true;
n.wasClicked = true;
simulationArea.lastSelected=n;
this.converge(n);
//console.log("wire:",n);
}
if (simulationArea.lastSelected == this) {
// //console.log("HITT");
}
//console.log("wire:",simulationArea.lastSelected);
if (this.node1.deleted || this.node2.deleted) this.delete(); //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;}
var 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;}
var 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;}
var 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;}
var n = new Node(this.x2, this.node2.absY(), 2, this.scope.root);
this.converge(n);
updated = true;
}
}
}
return updated;
}
this.draw = function() {
// for calculating min-max Width,min-max Height
ctx = simulationArea.context;
var color;
if (this.node1.value == undefined||this.node2.value == undefined)
color = "red";
else if (this.node1.bitWidth == 1)
color = ["red", "DarkGreen", "Lime"][this.node1.value + 1];
else
color = "black";
drawLine(ctx, this.node1.absX(), this.node1.absY(), this.node2.absX(), this.node2.absY(), color, 3);
}
// checks if node lies on wire
this.checkConvergence = function(n) {
return this.checkWithin(n.absX(), n.absY());
}
// fn checks if coordinate lies on wire
this.checkWithin = function(x, y) {
if ((this.type == "horizontal") && (this.node1.absX() < this.node2.absX()) && (x > this.node1.absX()) && (x < this.node2.absX()) && (y === this.node2.absY()))
return true;
else if ((this.type == "horizontal") && (this.node1.absX() > this.node2.absX()) && (x < this.node1.absX()) && (x > this.node2.absX()) && (y === this.node2.absY()))
return true;
else if ((this.type == 'vertical') && (this.node1.absY() < this.node2.absY()) && (y > this.node1.absY()) && (y < this.node2.absY()) && (x === this.node2.absX()))
return true;
else 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;
}
//add intermediate node between these 2 nodes
this.converge = function(n) {
this.node1.connect(n);
this.node2.connect(n);
this.delete();
}
this.delete = function() {
updateSimulation = true;
this.node1.connections.clean(this.node2);
this.node2.connections.clean(this.node1);
this.scope.wires.clean(this);
this.node1.checkDeleted();
this.node2.checkDeleted();
}
}