From 10e5320261986dd5ffd1597643d0d05992aba6b1 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien Date: Sat, 3 Aug 2024 20:19:48 -0500 Subject: [PATCH] Add Emulatorjs exit button --- data/src/GameManager.js | 13 ++++++---- data/src/emulator.js | 53 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/data/src/GameManager.js b/data/src/GameManager.js index c03f268..76c78ab 100644 --- a/data/src/GameManager.js +++ b/data/src/GameManager.js @@ -43,10 +43,15 @@ class EJS_GameManager { this.FS.syncfs(true, () => {}); this.initShaders(); - - this.EJS.addEventListener(window, "beforeunload", () => { - this.saveSaveFiles(); - this.FS.syncfs(() => {}); + + this.EJS.on("exit", () => { + this.toggleMainLoop(0); + this.functions.saveSaveFiles(); + setTimeout(() => { + this.FS.syncfs(() => { + try {window.abort()} catch(e){}; + }); + }, 500); }) } loadExternalFiles() { diff --git a/data/src/emulator.js b/data/src/emulator.js index 0776cea..7121a8d 100644 --- a/data/src/emulator.js +++ b/data/src/emulator.js @@ -1686,19 +1686,18 @@ class EmulatorJS { const restartButton = addButton("Restart", '', () => { if (this.isNetplay && this.netplay.owner) { - this.gameManager.saveSaveFiles(); this.gameManager.restart(); this.netplay.reset(); this.netplay.sendMessage({restart:true}); this.play(); } else if (!this.isNetplay) { - this.gameManager.saveSaveFiles(); this.gameManager.restart(); } }); const pauseButton = addButton("Pause", '', () => { if (this.isNetplay && this.netplay.owner) { this.pause(); + this.gameManager.saveSaveFiles(); this.netplay.sendMessage({pause:true}); } else if (!this.isNetplay) { this.pause(); @@ -2007,6 +2006,56 @@ class EmulatorJS { } } } + + const exitEmulation = addButton("Exit EmulatorJS", '', async () => { + const popups = this.createSubPopup(); + this.game.appendChild(popups[0]); + popups[1].classList.add("ejs_cheat_parent"); + popups[1].style.width = "100%"; + const popup = popups[1]; + const header = this.createElement("div"); + header.classList.add("ejs_cheat_header"); + const title = this.createElement("h2"); + title.innerText = this.localization("Are you sure you want to exit?"); + title.classList.add("ejs_cheat_heading"); + const close = this.createElement("button"); + close.classList.add("ejs_cheat_close"); + header.appendChild(title); + header.appendChild(close); + popup.appendChild(header); + this.addEventListener(close, "click", (e) => { + popups[0].remove(); + }) + popup.appendChild(this.createElement("br")); + + const footer = this.createElement("footer"); + const submit = this.createElement("button"); + const closeButton = this.createElement("button"); + submit.innerText = this.localization("Exit"); + closeButton.innerText = this.localization("Cancel"); + submit.classList.add("ejs_button_button"); + closeButton.classList.add("ejs_button_button"); + submit.classList.add("ejs_popup_submit"); + closeButton.classList.add("ejs_popup_submit"); + submit.style["background-color"] = "rgba(var(--ejs-primary-color),1)"; + footer.appendChild(submit); + const span = this.createElement("span"); + span.innerText = " "; + footer.appendChild(span); + footer.appendChild(closeButton); + popup.appendChild(footer); + + this.addEventListener(closeButton, "click", (e) => { + popups[0].remove(); + }) + + this.addEventListener(submit, "click", (e) => { + popups[0].remove(); + const body = this.createPopup("EmulatorJS has exited", {}); + this.callEvent("exit"); + }) + + }); this.addEventListener(document, "webkitfullscreenchange mozfullscreenchange fullscreenchange", (e) => {