-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
321 lines (280 loc) · 10.6 KB
/
engine.js
File metadata and controls
321 lines (280 loc) · 10.6 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
var totalObjects = 0;
function scheduleUpdate(count = 0, time = 100, fn) {
// return;
// //console.log(willBeUpdated,updateSimulation,);
if (count) {
for (var i = 0; i < count; i++)
setTimeout(update, 10 + 50 * i);
}
if (willBeUpdated) return;
// if (simulationArea.mouseDown)
if (fn)
setTimeout(function() {
fn();
update();
}, time);
else setTimeout(update, time);
// else
// setTimeout(update, 100);
willBeUpdated = true;
}
// fn that calls update on everything else. If any change is there, it resolves the circuit and draws it again
// fn to change scale (zoom) - It also shifts origin so that the position
//of the object in focus doent changeB
function update(scope = globalScope) {
willBeUpdated = false;
if (loading == true) return;
// //console.log("UPDATE");
var updated = false;
simulationArea.hover = undefined;
// wireToBeChecked=true;
if (wireToBeChecked) {
if (wireToBeChecked == 2) wireToBeChecked = 0; // this required due to timing issues
else wireToBeChecked++;
// WHY IS THIS REQUIRED ???? we are checking inside wire ALSO
var prevLength = scope.wires.length;
for (var i = 0; i < scope.wires.length; i++) {
scope.wires[i].checkConnections();
if (scope.wires.length != prevLength) {
prevLength--;
i--;
}
}
scheduleUpdate();
}
if (updatePosition) {
//console.log("updatePosition");
for (var i = 0; i < scope.objects.length; i++)
for (var j = 0; j < scope[scope.objects[i]].length; j++) {
updated |= scope[scope.objects[i]][j].update();
}
}
// updateSimulation |= updated;
if (updatePosition) {
}
if (updatePosition) {
updateSelectionsAndPane(scope);
}
if(!embed&&simulationArea.mouseDown&&simulationArea.lastSelected&&simulationArea.lastSelected!=globalScope.root){
$('#miniMap').fadeOut('fast');
}
if (updateSimulation) {
play();
}
if (!embed && prevPropertyObj != simulationArea.lastSelected) {
if (simulationArea.lastSelected&&simulationArea.lastSelected.objectType !== "Wire") {
showProperties(simulationArea.lastSelected);
//console.log("yo");
} else {
// hideProperties();
}
}
//Draw
if (updateCanvas) {
renderCanvas(scope);
}
// if (updateSimulation||update) scheduleUpdate();
updateSimulation = updateCanvas = updatePosition = false;
// if(updated){
// updatePosition=true;
// updateCanvas=true;
// scheduleUpdate();
// }
}
function findDimensions(scope = globalScope) {
totalObjects = 0;
simulationArea.minWidth=undefined;
simulationArea.maxWidth=undefined;
simulationArea.minHeight=undefined;
simulationArea.maxHeight=undefined;
for (var i = 0; i < scope.objects.length; i++) {
if (scope.objects[i] !== 'wires')
for (var j = 0; j < scope[scope.objects[i]].length; j++) {
totalObjects += 1;
var obj = scope[scope.objects[i]][j];
if (totalObjects == 1) {
simulationArea.minWidth = obj.absX();
simulationArea.minHeight = obj.absY();
simulationArea.maxWidth = obj.absX();
simulationArea.maxHeight = obj.absY();
}
if (obj.objectType != 'Node') {
if (obj.y - obj.upDimensionY < simulationArea.minHeight)
simulationArea.minHeight = obj.y - obj.upDimensionY;
if (obj.y + obj.downDimensionY > simulationArea.maxHeight)
simulationArea.maxHeight = obj.y + obj.downDimensionY;
if (obj.x - obj.leftDimensionX < simulationArea.minWidth)
simulationArea.minWidth = obj.x - obj.leftDimensionX;
if (obj.x + obj.rightDimensionX > simulationArea.maxWidth)
simulationArea.maxWidth = obj.x + obj.rightDimensionX;
} else {
if (obj.absY() < simulationArea.minHeight)
simulationArea.minHeight = obj.absY();
if (obj.absY() > simulationArea.maxHeight)
simulationArea.maxHeight = obj.absY();
if (obj.absX() < simulationArea.minWidth)
simulationArea.minWidth = obj.absX();
if (obj.absX() > simulationArea.maxWidth)
simulationArea.maxWidth = obj.absX();
}
}
}
simulationArea.objectList = scope.objects;
}
function updateSelectionsAndPane(scope = globalScope) {
//console.log("updateSelectionsAndPane");
if (!simulationArea.selected && simulationArea.mouseDown) {
//mouse click NOT on object
simulationArea.selected = true;
simulationArea.lastSelected = scope.root;
simulationArea.hover = scope.root;
if (simulationArea.shiftDown) {
objectSelection = true;
} else {
if(!embed){
findDimensions(scope);
miniMapArea.setup();
$('#miniMap').show();
}
}
} else if (simulationArea.lastSelected == scope.root && simulationArea.mouseDown) {
//pane canvas
if (!objectSelection) {
globalScope.ox = (simulationArea.mouseRawX - simulationArea.mouseDownRawX) + simulationArea.oldx;
globalScope.oy = (simulationArea.mouseRawY - simulationArea.mouseDownRawY) + simulationArea.oldy;
globalScope.ox = Math.round(globalScope.ox);
globalScope.oy = Math.round(globalScope.oy);
gridUpdate = true;
if(!embed)miniMapArea.setup();
} else {
}
} else if (simulationArea.lastSelected == scope.root) {
simulationArea.lastSelected = undefined;
simulationArea.selected = false;
simulationArea.hover = undefined;
if (objectSelection) {
objectSelection = false;
var x1 = simulationArea.mouseDownX;
var x2 = simulationArea.mouseX;
var y1 = simulationArea.mouseDownY;
var y2 = simulationArea.mouseY;
// simulationArea.multipleObjectSelections=[];
// //console.log(x1,x2,y1,y2);
// [x1,x2]=[x1,x2].sort();
// [y1,y2]=[y1,y2].sort();
if (x1 > x2) {
var temp = x1;
x1 = x2;
x2 = temp;
}
if (y1 > y2) {
var temp = y1;
y1 = y2;
y2 = temp;
}
// //console.log(x1,x2,y1,y2);
for (var i = 0; i < scope.objects.length; i++) {
for (var j = 0; j < scope[scope.objects[i]].length; j++) {
var obj = scope[scope.objects[i]][j];
// //console.log(obj);
if (simulationArea.multipleObjectSelections.contains(obj)) continue;
var x, y;
if (obj.objectType == "Node") {
x = obj.absX();
y = obj.absY();
} else if (obj.objectType != "Wire") {
x = obj.x;
y = obj.y;
} else {
// //console.log(obj);
continue;
}
if (x > x1 && x < x2 && y > y1 && y < y2) {
simulationArea.multipleObjectSelections.push(obj);
}
}
}
}
}
}
function renderCanvas(scope) {
var ctx = simulationArea.context;
simulationArea.clear();
if (gridUpdate) {
gridUpdate = false;
dots();
}
// dots(); // draw dots
canvasMessageData=undefined;
for (var i = 0; i < scope.objects.length; i++)
for (var j = 0; j < scope[scope.objects[i]].length; j++)
scope[scope.objects[i]][j].draw();
if(canvasMessageData){
canvasMessage(ctx,canvasMessageData.string,canvasMessageData.x,canvasMessageData.y)
}
if (objectSelection) {
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "black"
ctx.fillStyle = "rgba(0,0,0,0.1)"
rect2(ctx, simulationArea.mouseDownX, simulationArea.mouseDownY, simulationArea.mouseX - simulationArea.mouseDownX, simulationArea.mouseY - simulationArea.mouseDownY, 0, 0, "RIGHT");
ctx.stroke();
ctx.fill();
}
}
//Main fn that resolves circuit
function play(scope = globalScope, resetNodes = true) {
// throw("ERROR");
//console.log("play");
if (errorDetected) return;
// //console.log("simulation");
if (loading == true) return;
if (!embed) plotArea.stopWatch.Stop();
if (resetNodes) {
for (var i = 0; i < scope.allNodes.length; i++)
scope.allNodes[i].reset();
for (var i = 0; i < scope.SubCircuit.length; i++) {
scope.SubCircuit[i].reset();
}
}
for (var i = 0; i < scope.SubCircuit.length; i++) {
if (scope.SubCircuit[i].isResolvable()) scope.stack.push(scope.SubCircuit[i]);
}
// for (var i = 0; i < scope.FlipFlop.length; i++) {
// scope.stack.push(scope.FlipFlop[i]);
// }
for (var i = 0; i < inputList.length; i++) {
for (var j = 0; j < scope[inputList[i]].length; j++) {
scope.stack.push(scope[inputList[i]][j]);
}
}
var stepCount = 0;
while (scope.stack.length) {
if (errorDetected) return;
var elem = scope.stack.pop();
elem.resolve();
stepCount++;
if (stepCount > 1000) {
showError("Simulation Stack limit exceeded: maybe due to cyclic paths or contention");
return;
}
}
for (var i = 0; i < scope.Flag.length; i++)
scope.Flag[i].setPlotValue();
// for (var i = 0; i < scope.SubCircuit.length; i++) {
// if(!scope.SubCircuit[i].isResolvable())
// {
// scope.stack.push(scope.SubCircuit[i]);
// while (scope.stack.length) {
// if(errorDetected)return;
// var elem = scope.stack.pop();
// elem.resolve();
// stepCount++;
// if (stepCount > 1000) {
// showError("Simulation Stack limit exceeded: maybe due to cyclic paths or contention");
// return;
// }
// }
// }
// }
}