-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
494 lines (453 loc) · 13.2 KB
/
main.js
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//LOGIC SIM V3.0
const canvas = document.querySelector(".canvas"),
ctx = canvas.getContext("2d");
const root = document.querySelector(":root");
const preloader = document.querySelector(".preloader");
const gateButtons = document.querySelectorAll(".gateBtn");
const addButton = document.querySelectorAll(".add");
const removeButton = document.querySelectorAll(".remove");
const bg = document.querySelector(".bg");
const loadToMemory = true;
let paused = false;
let tempSelection = null;
let currentConLineIndex = null;
let currentSelectedGate = null;
let snapable = false;
let pressedKeys = [];
let keyCodes = {
control: 17,
command: 91,
delete: 46,
enter: 13,
plus: 107,
minus: 109,
};
let gateInputCount = 2;
let inputBoxCount = 2;
let outputBoxCount = 2;
const maxBoxCount = 14;
const minBoxCount = 1;
let GATES = [];
let INPUTBOXES = [];
let OUTPUTBOXES = [];
let GATEBUTTONSLIST = {
default: ["AND", "OR", "NOT", "NAND", "NOR", "XOR"],
custom: [],
};
let canvasLastHeight = window.innerHeight - 100;
function main() {
setTheme();
setEventListeners();
updateCanvas();
createBoolBox();
createCustomGateButtons();
}
function updateCanvas() {
if (canvas.height != canvasLastHeight) {
canvasLastHeight = canvas.height;
createBoolBox();
}
canvas.width = window.innerWidth - 120;
canvas.height = window.innerHeight - 100;
// ctx.fillStyle = "red";
// ctx.fill();
if (GATES.length == 0) {
drawHelp(ctx);
}
//boolbox
[...INPUTBOXES, ...OUTPUTBOXES].forEach((box) => box.update(ctx));
//Gates
GATES.forEach((gate) => {
if (gate.markedForDelete) {
deleteGate(gate);
return;
}
gate.update(ctx, gate === currentSelectedGate);
});
// labels
[...connectionPoints.input, ...connectionPoints.output].forEach((con) => {
con.drawLabel(ctx);
});
requestAnimationFrame(updateCanvas);
}
function setTheme() {
canvas.style.backgroundColor = Theme.canvas;
document.body.style.backgroundColor = Theme.body;
root.style.setProperty("--addRemove-color", Theme.addRemove);
}
function createCustomGateButtons() {
if (loadToMemory) loadGates();
if (localStorage.gates !== undefined) {
let gateData = JSON.parse(localStorage.gates);
Object.keys(gateData).forEach((gate) => createGateBtn(gate));
}
}
function createBoolBox() {
let radius = boolBox.radius;
let gap = 10;
//INPUTS
if (INPUTBOXES[inputBoxCount] !== undefined) {
INPUTBOXES[inputBoxCount].connection.disconnect();
remove(INPUTBOXES[inputBoxCount].connection, connectionPoints.output);
INPUTBOXES.pop();
}
for (let i = 0; i < inputBoxCount; i++) {
let fac =
canvas.height / 2 -
(radius + gap / inputBoxCount) -
(inputBoxCount * radius + gap);
let x = 40; //distance from edge
let y = lerp(fac, canvas.height - fac, (i + 1) / (inputBoxCount + 1));
if (INPUTBOXES[i] === undefined) INPUTBOXES.push(new inputBox(x, y, i));
else INPUTBOXES[i].position.y = y;
}
//OUTPUTS
if (OUTPUTBOXES[outputBoxCount] !== undefined) {
OUTPUTBOXES[outputBoxCount].connection.disconnect();
remove(OUTPUTBOXES[outputBoxCount].connection, connectionPoints.input);
OUTPUTBOXES.pop();
}
for (let i = 0; i < outputBoxCount; i++) {
let fac =
canvas.height / 2 -
(radius + gap / outputBoxCount) -
(outputBoxCount * radius + gap);
let x = canvas.width - 40; //distance from edge
let y = lerp(fac, canvas.height - fac, (i + 1) / (outputBoxCount + 1));
if (OUTPUTBOXES[i] === undefined) OUTPUTBOXES.push(new outputBox(x, y, i));
else OUTPUTBOXES[i].position.y = y;
}
}
function drawHelp(context) {
let drawColor = "#666666";
//Text
context.fillStyle = drawColor;
context.font = "400 1.3em Montserrat";
context.textBaseline = "middle";
context.textAlign = "center";
context.fillText("DRAG AND DROP", canvas.width / 2, canvas.height / 2);
//arrow and line
let x = canvas.width / 2;
let y = canvas.height / 2 + canvas.height / 7;
let arrowLength = canvas.height - 60 - y;
drawArrow(context, x, y, x, y + arrowLength, drawColor);
//Line
context.lineWidth = 1;
context.beginPath();
context.moveTo(80, canvas.height - 20);
context.lineTo(canvas.width - 80, canvas.height - 20);
context.stroke();
}
function markForDelete() {
GATES.forEach((gate) => {
if (
gate.position.x >= canvas.width ||
gate.position.x + gate.width <= 0 ||
gate.position.y >= canvas.height ||
gate.position.y + gate.height <= 0
)
gate.markedForDelete = true;
});
}
function getCurrentSelection(event) {
//check for output points
for (let i = connectionPoints.output.length - 1; i >= 0; i--) {
let point = connectionPoints.output[i];
let isIn = isInCircle(
event,
point.position.x,
point.position.y,
connectionPoint.radius
);
if (isIn) return point;
}
//check for input points
for (let i = connectionPoints.input.length - 1; i >= 0; i--) {
let point = connectionPoints.input[i];
let isIn = isInCircle(
event,
point.position.x,
point.position.y,
connectionPoint.radius
);
if (isIn) return point;
}
//check for gates
for (let i = GATES.length - 1; i >= 0; i--) {
let gate = GATES[i];
if (
event.offsetX > gate.position.x &&
event.offsetX < gate.position.x + gate.width &&
event.offsetY > gate.position.y &&
event.offsetY < gate.position.y + gate.height
)
return gate;
}
return null;
}
function isSnapable(event) {
for (let i = connectionPoints.input.length - 1; i >= 0; i--) {
let point = connectionPoints.input[i];
let isIn = isInCircle(
event,
point.position.x,
point.position.y,
inputPoint.snapDistance
);
if (isIn) return point;
}
return false;
}
function addBox(event) {
let button = event.target;
let type = button.getAttribute("data-type");
if (type == 0 && inputBoxCount < maxBoxCount) inputBoxCount++;
else if (type == 1 && outputBoxCount < maxBoxCount) outputBoxCount++;
createBoolBox();
}
function removeBox(event) {
let button = event.target;
let type = button.getAttribute("data-type");
if (type == 0 && inputBoxCount > minBoxCount) inputBoxCount--;
else if (type == 1 && outputBoxCount > minBoxCount) outputBoxCount--;
createBoolBox();
}
function createGateBtn(label) {
let parent = document.querySelector(".gates");
let div = document.querySelector(".custom");
if (div == null) {
div = document.createElement("div");
div.classList.add("group", "custom");
}
let button = document.createElement("div");
button.classList.add("btn", "gateBtn");
button.setAttribute("data-name", label);
button.innerText = label;
button.addEventListener("mousedown", spawn);
div.appendChild(button);
parent.appendChild(div);
GATEBUTTONSLIST.custom.push(label);
}
function spawn(event) {
pressedKeys.push(event.button);
let button = event.target;
let fun = button.getAttribute("data-name");
let index = GATES.push(createGate(fun, event.pageX, event.pageY)) - 1;
currentSelectedGate = tempSelection = GATES[index];
// root.style.setProperty("--cursor", "grabbing"); ////////////
}
function getGateData(name) {
let rawData = JSON.parse(localStorage.gates)[name];
let conLabel = rawData.label;
let circuit = getCircuit(rawData);
let inputCount = circuit.points.input.length;
let outputCount = circuit.points.output.length;
return {
inCount: inputCount,
outCount: outputCount,
conLabel: conLabel,
circuit: circuit.circuit,
name: name,
points: circuit.points,
};
}
function getCircuit(rawData) {
let ob = rawData.connections;
let gate = rawData.gates;
let circuit = [];
let points = { input: [[]], output: [] };
//creating all gates
gate.forEach((g, index) => {
let inputCount = ob[index].input.length;
circuit.push(createGate(g, 0, 0, inputCount));
});
circuit.forEach((gt, index) => {
//setting output points
ob[index].output.forEach((con, i) =>
con.forEach((line) => {
if (line.length == 2) {
let ind =
gt.output[i].connections.push(new connectionLine(gt.output[i])) - 1;
circuit[line[0]].input[line[1]].connect(
gt.output[i].connections[ind]
);
} else points.output[line[0]] = gt.output[i];
})
);
//setting input points
ob[index].input.forEach((inp, i) => {
if (inp != null) {
if (!points.input[inp]) points.input[inp] = [];
points.input[inp].push(gt.input[i]);
}
});
});
return { circuit: circuit, points: points };
}
function createGate(name, x = 0, y = 0, inputCount = gateInputCount) {
inputCount = Math.max(2, inputCount);
switch (name) {
case "AND":
return new AND(x, y, inputCount);
case "OR":
return new OR(x, y, inputCount);
case "NOT":
return new NOT(x, y);
case "NAND":
return new NAND(x, y, inputCount);
case "NOR":
return new NOR(x, y, inputCount);
case "XOR":
return new XOR(x, y, inputCount);
default:
gateData = getGateData(name);
return new customGate(
x,
y,
gateData.inCount,
gateData.outCount,
gateData.circuit,
gateData.name,
gateData.points,
gateData.conLabel
);
}
}
function addCustomGate() {
let isAlert = false;
for (let i = 0; i < INPUTBOXES.length; i++) {
if (!INPUTBOXES[i].connection.connections[0]) isAlert = true;
}
if (!isAlert)
for (let i = 0; i < OUTPUTBOXES.length; i++) {
if (!OUTPUTBOXES[i].connection.connection) isAlert = true;
}
if (isAlert) showAlert("Some Input/Output connections are empty.");
else showPrompt("TYPE NAME", saveGateGroup);
}
function showPrompt(prompt, func) {
const promptBox = document.querySelector(".promptbox");
const titleText = document.querySelector(".promptbox .titlebar span");
const cross = document.querySelector(".promptbox .cross");
const textBox = document.querySelector(".text");
const okBtn = document.querySelector(".okbtn");
const warningBox = document.querySelector(".warning");
togglePause();
titleText.innerText = prompt;
promptBox.style.display = "block";
textBox.focus();
cross.addEventListener(
"click",
() => {
textBox.value = "";
promptBox.style.display = "none";
warningBox.innerText = "";
togglePause();
},
{ once: true }
);
okBtn.addEventListener("click", () => {
let name = textBox.value.trim();
if (name == "") warningBox.innerText = "Text field is empty.";
else if (
[...GATEBUTTONSLIST.custom, GATEBUTTONSLIST.default].includes(name)
)
warningBox.innerText = "Name already exists.";
else {
cross.click();
func(name);
}
});
textBox.addEventListener("keydown", (event) => {
if (event.key == "Enter") okBtn.click();
});
}
function togglePause() {
if (paused) bg.style.display = "none";
else bg.style.display = "block";
paused = !paused;
}
function saveGateGroup(name) {
let gates = GATES.map((g) => g.name);
let rawData = {
gates: gates,
connections: [],
label: { input: [], output: [] },
};
GATES.forEach((gt) => {
let input = [];
let output = [];
gt.input.forEach((inp) => {
if (!inp.connection) {
input.push(null);
return;
}
let parent = inp.connection.start.parent;
if (parent.isGate) input.push(null);
else input.push(parent.index);
});
gt.output.forEach((out) => {
let conList = [];
out.connections.forEach((con) => {
let parent = con.end.parent;
if (parent.isGate) {
let gateIndex = GATES.indexOf(parent.isGate);
conList.push([gateIndex, parent.index]);
} else conList.push([parent.index]);
});
output.push(conList);
});
rawData.connections.push({ input: input, output: output });
});
//setting input labels
INPUTBOXES.forEach((box) =>
rawData.label.input.push(box.connection.label.text)
);
//setting output labels
OUTPUTBOXES.forEach((box) =>
rawData.label.output.push(box.connection.label.text)
);
//saving gate data in localstorage
addToStorage(name, rawData);
createGateBtn(name);
//clearing
GATES = [];
connectionPoints = { input: [], output: [] };
INPUTBOXES = [];
OUTPUTBOXES = [];
createBoolBox();
}
function deleteGate(gate) {
if (gate === null) return;
//disconnect input and output connections
[...gate.input, ...gate.output].forEach((con) => con.disconnect());
//remove input connection points from input array
for (let i = 0; i < gate.inputCount; i++)
remove(gate.input[i], connectionPoints.input);
//remove output connection points from output array
for (let i = 0; i < gate.outputCount; i++)
remove(gate.output[i], connectionPoints.output);
//remove gate from array
remove(gate, GATES);
currentSelectedGate = null;
}
function handleGateInputs(mode, gate) {
//mode = 0 => increment
//mode = 1 => decrement
if (gate != null) {
if (mode == 0) gate.incrementInputCount();
else if (mode == 1) gate.decrementInputCount();
}
}
document.body.scroll = "no";
window.addEventListener("load", () => {
preloader.style.opacity = "0";
setInterval(() => {
preloader.style.display = "none";
}, 300);
document.body.scroll = "yes";
// preloader.style.display = "none";
});
main();
// 3/8/2022
// Aswin-Koroth