BrowserLogic/js/main.js
2021-03-03 18:30:15 -08:00

135 lines
4.3 KiB
JavaScript

/*
MatCat BrowserLogic Simulator
*/
let Version = "0.3.4";
let spanVersion = document.getElementById("version");
spanVersion.innerText = Version;
// get the canvas and get the engine object going
let lCanvasElement = document.getElementById("LogicPlane");
let logicEngine = new LogicEngine(lCanvasElement);
// Get the game Tick going, this will be 4ms for now which is the fastest that is supported
// by the HTML5 spec!
setInterval(logicEngine.Scheduler.Tick.bind(logicEngine.Scheduler), 4);
window.addEventListener('resize', function(evt) {
logicEngine.Resize(evt);
}, false);
window.addEventListener('keydown', function(evt) {
logicEngine.Key_Press(evt);
}, false);
lCanvasElement.addEventListener('mousedown', function(evt) {
logicEngine.Mouse_Down(evt);
}, false);
lCanvasElement.addEventListener('mouseup', function(evt) {
logicEngine.Mouse_Up(evt);
}, false);
window.addEventListener('mousemove', function(evt) {
logicEngine.Mouse_Move(evt);
}, false);
// Get the engine going
logicEngine.StartEngine();
BuildToolbox();
// Setup interface buttons
let btn_CloseWelcome = document.getElementById("btn_CloseWelcome");
btn_CloseWelcome.addEventListener('click', function(evt) {
let WelcomeScreen = document.getElementById("WelcomeWindow");
let DarkOverlay = document.getElementById("darkout-overlay");
WelcomeScreen.style.display = "none";
DarkOverlay.style.display = "none";
let chk_dontDisplayWelcome = document.getElementById("chk_dontDisplayWelcome");
if (chk_dontDisplayWelcome.checked) {
setCookie("hidewelcomescreen","true",3600);
}
}, false);
let btn_CreateIC = document.getElementById("btn_CreateIC");
btn_CreateIC.addEventListener('click', function(evt) {
let CreateICBox = document.getElementById("CreateICBox");
CreateICBox.style.display = "block";
});
let btn_CreateIC_Cancel = document.getElementById("btn_CreateIC_Cancel");
btn_CreateIC_Cancel.addEventListener('click', function(evt) {
let CreateICBox = document.getElementById("CreateICBox");
CreateICBox.style.display = "none";
});
let btn_CreateIC_Create = document.getElementById("btn_CreateIC_Create");
btn_CreateIC_Create.addEventListener('click', function(evt) {
let CreateICBox = document.getElementById("CreateICBox");
let ICName = document.getElementById("ICName");
let ICDescription = document.getElementById("ICDescription");
createIC(logicEngine.ActiveContainer,ICName.value,ICDescription.value);
CreateICBox.style.display = "none";
});
let ICName = document.getElementById("ICName");
let ICDescription = document.getElementById("ICDescription");
ICName.addEventListener('input', function(evt){
btn_CreateIC_Create.disabled = true;
if (ICName.value.length > 0 && ICDescription.value.length > 0) btn_CreateIC_Create.disabled = false;
});
ICDescription.addEventListener('input', function(evt){
btn_CreateIC_Create.disabled = true;
if (ICName.value.length > 0 && ICDescription.value.length > 0) btn_CreateIC_Create.disabled = false;
});
let btn_New = document.getElementById("btn_New");
btn_New.addEventListener('click', function(evt) {
logicEngine.ActiveContainer = new elementContainer();
});
let btn_Save = document.getElementById("btn_Save");
btn_Save.addEventListener('click', function(evt) {
download("mydeign.LogicParts",createSaveState(logicEngine.ActiveContainer));
});
let file_Load = document.getElementById("file_Load");
let btn_Load = document.getElementById("btn_Load");
btn_Load.addEventListener('click', function(evt) {
file_Load.click();
});
file_Load.addEventListener('change', function(evt) {
let fread = new FileReader();
fread.onload = (function (theFile) {
return function (e) {
try {
let restoredata = JSON.parse(e.target.result);
console.log(restoredata);
loadresult = loadsave(restoredata);
if (!loadresult) {
switch(loadresult) {
case -1:
alert("Invalid LogicParts file!");
break;
case -2:
alert("The version of MatCat BrowserLogic cannot open this save version!");
break;
default:
alert("Bad save file!");
break;
}
}
} catch (ex) {
alert("Bad file!");
console.log(ex);
}
}
})(evt.target.files[0]);
fread.readAsText(evt.target.files[0]);
}, false);
CheckForWelcomeCookie();