164 lines
5.3 KiB
JavaScript
164 lines
5.3 KiB
JavaScript
function addElement(RestoreData = null,refClass,props) {
|
|
let newElement = new refClass(RestoreData,logicEngine,...props);
|
|
|
|
if (!RestoreData) {
|
|
let x = Math.round(logicEngine.Canvas.width / 2);
|
|
let y = Math.round(logicEngine.Canvas.height / 2);
|
|
let width = newElement.Width;
|
|
let height = newElement.Height;
|
|
|
|
let noclearspot = true;
|
|
while (noclearspot) {
|
|
if (!logicEngine.ActiveContainer.checkOverlayBounds(x, y, width, height)) {
|
|
noclearspot = false;
|
|
} else {
|
|
x += Math.floor(Math.random() * (width - (width - (width * 2)))) + (width - (width * 2));
|
|
y += Math.floor(Math.random() * (height - (height - (height * 2)))) + (height - (height * 2));
|
|
if (x < 0) x = 0;
|
|
if (y < 0) y = 0;
|
|
if (x + width > logicEngine.Canvas.width) x = logicEngine.Canvas.width - width;
|
|
if (y + height > logicEngine.Canvas.height) y = logicEngine.Canvas.height - height;
|
|
}
|
|
}
|
|
newElement.X = x;
|
|
newElement.Y = y;
|
|
}
|
|
logicEngine.ActiveContainer.AddElement(newElement);
|
|
logicEngine.ActiveContainer.Select(newElement);
|
|
return newElement;
|
|
}
|
|
|
|
function setCookie(cname, cvalue, exdays) {
|
|
let d = new Date();
|
|
d.setTime(d.getTime() + (exdays*24*60*60*1000));
|
|
let expires = "expires="+ d.toUTCString();
|
|
document.cookie = cname + '="' + cvalue + '";' + expires;
|
|
localStorage.setItem(cname,cvalue);
|
|
}
|
|
|
|
function getCookie(cname) {
|
|
let name = cname + "=";
|
|
let decodedCookie = decodeURIComponent(document.cookie);
|
|
let ca = decodedCookie.split(';');
|
|
for(var i = 0; i <ca.length; i++) {
|
|
var c = ca[i];
|
|
while (c.charAt(0) == ' ') {
|
|
c = c.substring(1);
|
|
}
|
|
if (c.indexOf(name) == 0) {
|
|
return c.substring(name.length, c.length);
|
|
}
|
|
}
|
|
return localStorage.getItem(cname);
|
|
}
|
|
|
|
function CheckForWelcomeCookie() {
|
|
if (getCookie("hidewelcomescreen")) {
|
|
let WelcomeScreen = document.getElementById("WelcomeWindow");
|
|
let DarkOverlay = document.getElementById("darkout-overlay");
|
|
WelcomeScreen.style.display = "none";
|
|
DarkOverlay.style.display = "none";
|
|
}
|
|
}
|
|
|
|
function getMousePos(canvas, evt) {
|
|
let rect = canvas.getBoundingClientRect();
|
|
return {
|
|
x: evt.clientX - rect.left,
|
|
y: evt.clientY - rect.top
|
|
};
|
|
}
|
|
|
|
function averageArray(arr) {
|
|
return arr.reduce((a, b) => (a + b)) / arr.length;
|
|
}
|
|
|
|
function length2D(x1,y1,x2,y2) {
|
|
let xDist = x1 - x2;
|
|
let yDist = y1 - y2;
|
|
return Math.sqrt(xDist*xDist + yDist * yDist);
|
|
}
|
|
|
|
function getElementInfo(element) {
|
|
for (let a = 0; a < ElementReferenceTable.length; a++) {
|
|
if (ElementReferenceTable[a].Name == element) return ElementReferenceTable[a];
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isVersionNewer(version1,version2,orEqual = true) {
|
|
let v1 = version1.split(".");
|
|
let v2 = version1.split(".");
|
|
if (v1.length != 3) return false;
|
|
if (v2.length != 3) return false;
|
|
for (let a = 0; a < 3; a++) {
|
|
v1[a] = parseInt(v1[a]);
|
|
v2[a] = parseInt(v2[a]);
|
|
}
|
|
if (v1[0] > v2[0]) return true;
|
|
if (v1[0] == v2[0] && v1[1] > v2[1]) return true;
|
|
if (v1[0] == v2[0] && v1[1] == v2[1] && v1[2] > v2[2]) return true;
|
|
if ((v1[0] == v2[0] && v1[1] == v2[1]) && (orEqual && v1[2] == v2[2])) return true;
|
|
return false;
|
|
}
|
|
|
|
function createSaveState(container = false) {
|
|
let saveState = {
|
|
Name: "LogicDesign",
|
|
Version: Version,
|
|
Timestamp: Date.now(),
|
|
Elements: new Array()
|
|
};
|
|
if (container.Elements.length > 0) saveState.Elements = container.Elements;
|
|
return saveState;
|
|
}
|
|
|
|
function download(filename, savestate) {
|
|
let text = JSON.stringify(savestate);
|
|
let element = document.createElement('a');
|
|
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
|
|
element.setAttribute('download', filename);
|
|
|
|
element.style.display = 'none';
|
|
document.body.appendChild(element);
|
|
|
|
element.click();
|
|
|
|
document.body.removeChild(element);
|
|
}
|
|
|
|
function loadsave(savedata) {
|
|
if (!savedata) return false;
|
|
if (!savedata.Version) return false; // TODO: Let the person know invalid save file
|
|
if (!isVersionNewer(savedata.Version,"0.3.0")) return false; // TODO: Let the person know the version is too old
|
|
let newContainer = new elementContainer();
|
|
let elementConnections = new Array();
|
|
for (let a = 0; a < savedata.Elements.length; a++) {
|
|
let classRef = getElementInfo(savedata.Elements[a].Name).Class;
|
|
let newElement = new classRef(savedata.Elements[a],logicEngine,savedata.Elements[a].Args);
|
|
newContainer.AddElement(newElement);
|
|
newElement.Designator = savedata.Elements[a].Designator;
|
|
if (savedata.Elements[a].Outputs) {
|
|
if (savedata.Elements[a].Outputs.length > 0) {
|
|
for (let b=0; b < savedata.Elements[a].Outputs.length; b++) {
|
|
elementConnections.push({
|
|
FromElement: newElement,
|
|
Input: savedata.Elements[a].Outputs[b].Input,
|
|
ToElement: savedata.Elements[a].Outputs[b].Element
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Now we need to make all of the connections
|
|
for (let a = 0; a < elementConnections.length; a++) {
|
|
let toElement = newContainer.HasElement(elementConnections[a].ToElement);
|
|
if (toElement) {
|
|
let newConnection = new ElementConnection(newContainer,toElement,elementConnections[a].Input);
|
|
elementConnections[a].FromElement.OutputConnections.push(newConnection);
|
|
}
|
|
}
|
|
logicEngine.ActiveContainer = newContainer;
|
|
return true;
|
|
}
|