BrowserLogic/js/mainevents.js

213 lines
6.8 KiB
JavaScript

function RegisterEvents() {
/*
DISPLAY AND RESIZING
*/
window.addEventListener('resize', function (evt) {
logicEngine.Resize(evt);
}, false);
/*
SYSTEM EVENTS
*/
document.addEventListener('copy', function (evt) {
evt.preventDefault();
let copyObject = GetCopyObject();
if (!copyObject) return;
evt.clipboardData.setData('logicparts/json', JSON.stringify(copyObject));
evt.clipboardData.setData('text', JSON.stringify(copyObject));
});
document.addEventListener('cut', function (evt) {
evt.preventDefault();
let copyObject = GetCopyObject();
if (!copyObject) return;
evt.clipboardData.setData('logicparts/json', JSON.stringify(copyObject));
evt.clipboardData.setData('text', JSON.stringify(copyObject));
logicEngine.Key_Press({ctrlKey: false, key: "Delete"});
disableSelectedMenus(true);
});
document.addEventListener('paste', function (evt) {
evt.preventDefault();
if (evt.clipboardData.getData('logicparts/json')) {
//console.log(evt.clipboardData.getData('logicparts/json'));
loadActiveContainer(JSON.parse(evt.clipboardData.getData('logicparts/json')).Elements);
} else {
// Lets see if they pasted a proper LogicParts format atleast
if (evt.clipboardData.getData('text')) {
let jsonObj = JSON.parse(evt.clipboardData.getData('text'));
if (jsonObj?.LogicParts && jsonObj?.Paste) {
console.log("We got a paste of a general text object that is valid");
loadActiveContainer(jsonObj.Elements);
}
}
}
});
/*
KEYBOARD EVENTS
*/
window.addEventListener('keydown', function (evt) {
logicEngine.Key_Press(evt);
}, false);
window.addEventListener('keyup', function (evt) {
logicEngine.Key_Up(evt);
}, false);
/*
MOUSE EVENTS
*/
window.addEventListener('mouseup', function (evt) {
let rcm = document.getElementById("RightClickMenu");
rcm.style.display = "none";
let tfms = document.getElementsByClassName("top-menu-div");
for (let a = 0; a < tfms.length; a++) {
tfms[a].style.display = "none";
}
}, 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);
/*
WINDOW CONTROLS
*/
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_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_SaveDesign = document.getElementById("btn_SaveDesign");
btn_SaveDesign.addEventListener('click', function (evt) {
let DesignName = document.getElementById("saveName");
let savestate = createSaveState(logicEngine.ActiveContainer);
savestate.Name = DesignName.value;
let saveWindow = document.getElementById("SaveWindow");
saveWindow.style.display = "none";
download(savestate.Name + ".LogicParts", savestate);
});
let btn_Save_Cancel = document.getElementById("btn_CancelSave");
btn_Save_Cancel.addEventListener('click', function (evt) {
let saveWindow = document.getElementById("SaveWindow");
saveWindow.style.display = "none";
});
let file_Load = document.getElementById("file_Load");
let tfm_Open = document.getElementById("tfm_Open");
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);
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! " + ex);
console.log(ex);
}
}
})(evt.target.files[0]);
fread.readAsText(evt.target.files[0]);
}, false);
let in_GridSize = document.getElementById("in_GridSize");
in_GridSize.addEventListener("change", function (evt) {
logicEngine.Settings.GridSize = parseInt(in_GridSize.value);
logicEngine.Resize("");
SaveSettings();
//setTimeout(function(){hideMenus()},10);
});
let helpWindow = document.getElementById("HelpWindow");
let helpWindow_Close = helpWindow.getElementsByClassName("CloseWindow")[0];
helpWindow_Close.addEventListener("click", function (evt) {
HideHelp();
});
let leftmenu = document.getElementById("left-menu");
let propwin = document.getElementById("PropertiesBox");
lCanvasElement.addEventListener("contextmenu", function (evt) {
showRCM(evt)
});
leftmenu.addEventListener("contextmenu", function (evt) {
showRCM(evt)
});
propwin.addEventListener("contextmenu", function (evt) {
showRCM(evt)
});
let topbar = document.getElementById("top-bar");
topbar.addEventListener("contextmenu", function (evt) {
evt.preventDefault();
});
}