178 lines
5.6 KiB
JavaScript
178 lines
5.6 KiB
JavaScript
|
class Component {
|
||
|
type;
|
||
|
image;
|
||
|
inputs=0;
|
||
|
pins = [];
|
||
|
subComponents = [this];
|
||
|
zIndex = [];
|
||
|
wires = [];
|
||
|
position = { x: 0, y: 0 };
|
||
|
groups = [];
|
||
|
tooltips=[];
|
||
|
getJSONandName(name){
|
||
|
var niceStructure={type:name,image:this.image,groups:this.groups,inputs:this.inputs,tooltips:this.tooltips,pins:new Array(this.pins.length).fill(0),subComponents:[],positions:[],wires:this.wires};
|
||
|
this.subComponents.forEach(c=>{
|
||
|
if (c!==this){
|
||
|
niceStructure.subComponents.push(c.type);
|
||
|
niceStructure.positions.push(c.position);
|
||
|
}
|
||
|
});
|
||
|
return JSON.stringify(niceStructure);
|
||
|
|
||
|
}
|
||
|
constructor(name,direct=false,djson=""){
|
||
|
this.type = name;
|
||
|
if (!(name in chips) && !direct) {
|
||
|
return;
|
||
|
}
|
||
|
var chip;
|
||
|
if(!direct){
|
||
|
chip = JSON.parse(JSON.stringify(chips[name]));
|
||
|
} else {
|
||
|
chip = JSON.parse(djson);
|
||
|
}
|
||
|
this.image = chip.image;
|
||
|
this.groups = [1,2,3]//chip.groups || [];
|
||
|
this.pins = chip.pins;
|
||
|
this.wires = chip.wires;
|
||
|
this.inputs = chip.inputs;
|
||
|
this.tooltips = chip.tooltips;
|
||
|
chip.subComponents.forEach((sc,i)=>{
|
||
|
this.subComponents.push(new Component(sc));
|
||
|
this.zIndex.push(this.subComponents[i+1]);
|
||
|
if(chip.type=="main"){
|
||
|
this.subComponents[i+1].position = chip.positions[i];
|
||
|
}
|
||
|
|
||
|
});
|
||
|
this.process();
|
||
|
}
|
||
|
process(){
|
||
|
//var startTime = performance.now()
|
||
|
if (this.type=="nand"){
|
||
|
this.pins[2] = (this.pins[0] && this.pins[1])?0:1;
|
||
|
} else {
|
||
|
this.subComponents.forEach(s=>{if(s != this) {s.process()}});
|
||
|
this.wires.forEach(w=>{
|
||
|
this.subComponents[w.destination.component].pins[w.destination.pin] = this.subComponents[w.source.component].pins[w.source.pin]
|
||
|
});
|
||
|
}
|
||
|
//var endTime = performance.now()
|
||
|
//if(this.type=="main")
|
||
|
// console.log(`Updating ${this.type} took ${endTime - startTime} milliseconds`)
|
||
|
return this.pins;
|
||
|
}
|
||
|
addComponent(name){
|
||
|
var nc = new Component(name);
|
||
|
this.subComponents.push(nc);
|
||
|
this.zIndex.unshift(nc);
|
||
|
}
|
||
|
removeComponent(index){
|
||
|
this.zIndex = this.zIndex.filter(zEntry=> this.getProperIndex(zEntry) !== index);
|
||
|
this.wires.forEach(w=>{
|
||
|
if(w.source.component == index)
|
||
|
this.subComponents[w.destination.component].pins[w.destination.pin] = 0;
|
||
|
});
|
||
|
this.wires = this.wires.filter(w=>w.source.component != index && w.destination.component != index).map(w => { if(w.destination.component>index){w.destination.component-=1};if(w.source.component>index){w.source.component-=1}; return w });
|
||
|
|
||
|
this.subComponents.splice(index,1);
|
||
|
console.log("hi");
|
||
|
console.log(this.subComponents);
|
||
|
|
||
|
}
|
||
|
sendToFront(index){
|
||
|
this.zIndex=this.zIndex.filter(zEntry=>this.getProperIndex(zEntry)!==index);
|
||
|
this.zIndex.push(this.subComponents[index]);
|
||
|
}
|
||
|
getProperIndex(zEntry){
|
||
|
return this.subComponents.indexOf(zEntry);
|
||
|
}
|
||
|
connect(source,destination,wid){
|
||
|
var sourceIsInput,destinationIsOutput;
|
||
|
sourceIsInput=destinationIsOutput=0;
|
||
|
if (source.pin < this.subComponents[source.component].inputs)
|
||
|
sourceIsInput = true;
|
||
|
if (destination.pin >= this.subComponents[destination.component].inputs)
|
||
|
destinationIsOutput = true;
|
||
|
source = {component:source.component, pin:source.pin};
|
||
|
destination={component:destination.component,pin:destination.pin};
|
||
|
if(sourceIsInput){
|
||
|
if(source.component == 0){
|
||
|
if((destinationIsOutput&&destination.component!=0)||(!destinationIsOutput && destination.component==0)){
|
||
|
console.log(1);
|
||
|
return false;
|
||
|
}
|
||
|
} else {
|
||
|
if(destinationIsOutput && destination.component==0){
|
||
|
console.log({n:2,c:source.component});
|
||
|
return false;
|
||
|
} else if (destinationIsOutput||destination.component==0){
|
||
|
console.log("swap");
|
||
|
var tmp = source;
|
||
|
source=destination;
|
||
|
destination = tmp;
|
||
|
} else if(!destinationIsOutput&&destination.component==0) {
|
||
|
} else {
|
||
|
console.log(3);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
if(source.component==0){
|
||
|
if((destinationIsOutput&&destination.component!=0)||(!destinationIsOutput&&destination.component==0)){
|
||
|
console.log("swap");
|
||
|
var tmp=source;
|
||
|
source=destination;
|
||
|
destination=tmp;
|
||
|
} else {
|
||
|
console.log(4);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
if(destinationIsOutput&&destination.component!=0){
|
||
|
console.log(5);
|
||
|
return false;
|
||
|
/*
|
||
|
} else if (!destinationIsOutput){
|
||
|
console.log("swap");
|
||
|
var tmp=source;
|
||
|
source=destination;
|
||
|
destination=tmp;
|
||
|
*/
|
||
|
} else if(destination.component==0&&!destinationIsOutput){
|
||
|
console.log(6);
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
this.wires = this.wires.filter(w=>JSON.stringify(w.destination)!=JSON.stringify(destination));
|
||
|
this.wires.push({ source: source, destination: destination, wireId:wid });
|
||
|
return true;
|
||
|
|
||
|
}
|
||
|
disconnect(source,destination){
|
||
|
this.wires = this.wires.filter(w=>JSON.stringify(w)!=JSON.stringify({source:source,destination:destination}));
|
||
|
}
|
||
|
addPin(isInput){
|
||
|
if(isInput){
|
||
|
this.inputs++;
|
||
|
this.pins.unshift(0);
|
||
|
this.wires.forEach(w=>{ if(w.destination.component==0){w.destination.pin+=1} });
|
||
|
} else {
|
||
|
this.pins.push(0);
|
||
|
}
|
||
|
}
|
||
|
removePin(i){;
|
||
|
this.wires = this.wires.filter(w=>!((w.source.component==0 && w.source.pin == pin)||(w.destination.component==0 && w.destination.pin == pin)));
|
||
|
if(i<this.inputs){
|
||
|
this.pins.shift();
|
||
|
this.wires.forEach(w=>{ if(w.source.component==0){w.source.pin-=1}});
|
||
|
} else {
|
||
|
this.pins.pop();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|