Update betterMenuScreens

This commit is contained in:
GGodPL 2023-07-31 00:47:40 +02:00 committed by GitHub
parent 783156eeac
commit b1356f82d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 5 deletions

View File

@ -5,6 +5,7 @@
* @property {string} buttonDescription Description that is shown when the button is hovered * @property {string} buttonDescription Description that is shown when the button is hovered
* @property {boolean} show Whether menu screen button should get shown on tool controls * @property {boolean} show Whether menu screen button should get shown on tool controls
* @property {() => void} [close] Closing method. Optional * @property {() => void} [close] Closing method. Optional
* @property {() => void} [preOpen] Method called before opening. Optional
* @property {() => void} [open] Opening method. Optional * @property {() => void} [open] Opening method. Optional
* @property {() => void} [onClose] Method that gets called on close (except when menu is force closed, like when clicking on a different menu button). Optional * @property {() => void} [onClose] Method that gets called on close (except when menu is force closed, like when clicking on a different menu button). Optional
* @property {() => void} [loader] Method that injects the menu screen into HTML. Can be set to ModScreen build method. Optional * @property {() => void} [loader] Method that injects the menu screen into HTML. Can be set to ModScreen build method. Optional
@ -46,7 +47,7 @@ var menuScreens = {
} }
} }
closeMenu = (force) => { closeMenu = (force = false) => {
if (!showingMenu) return; if (!showingMenu) return;
const menu = menuScreens[showingMenu]; const menu = menuScreens[showingMenu];
if (!menu) { if (!menu) {
@ -68,6 +69,8 @@ closeMenu = (force) => {
const inject = () => { const inject = () => {
const toolControls = document.getElementById("toolControls"); const toolControls = document.getElementById("toolControls");
const buttons = []; const buttons = [];
const style = document.createElement("style");
document.head.appendChild(style);
for (const key in menuScreens) { for (const key in menuScreens) {
const element = menuScreens[key]; const element = menuScreens[key];
if (element.show) { if (element.show) {
@ -77,6 +80,7 @@ const inject = () => {
button.onclick = () => { button.onclick = () => {
if (showingMenu != key) { if (showingMenu != key) {
closeMenu(true); closeMenu(true);
if (element.preOpen) element.preOpen();
if (element.open) element.open(); if (element.open) element.open();
else { else {
const menuParent = document.getElementById(element.parentDiv); const menuParent = document.getElementById(element.parentDiv);
@ -100,12 +104,32 @@ const inject = () => {
} }
/**
*
* @param {string} menu Menu do be opened
* @param {boolean} [closeCurrent] Whether it should forcefully close the current screen
*/
const openMenu = (menu, closeCurrent = false) => {
if (closeCurrent) closeMenu(true);
const menuScreen = menuScreens[menu];
if (menuScreen) {
showingMenu = menu;
if (menuScreen.preOpen) menuScreen.preOpen();
if (menuScreen.open) menuScreen.open();
else {
const menuParent = document.getElementById(menuScreen.parentDiv);
menuParent.style.display = "block";
}
}
}
class MenuScreen { class MenuScreen {
constructor () { constructor () {
this.nodes = []; this.nodes = [];
this.innerHtml = ""; this.innerHtml = "";
this.showCloseButton = true; this.showCloseButton = true;
this.closeButtonText = "-"; this.closeButtonText = "-";
this.closeButtonClass = "XButton";
} }
/** /**
@ -123,6 +147,7 @@ class MenuScreen {
*/ */
setShowCloseButton(show) { setShowCloseButton(show) {
this.showCloseButton = show; this.showCloseButton = show;
return this;
} }
/** /**
@ -131,6 +156,16 @@ class MenuScreen {
*/ */
setCloseButtonText(text = "-") { setCloseButtonText(text = "-") {
this.closeButtonText = text; this.closeButtonText = text;
return this;
}
/**
* Sets the close button class
* @param {string} [className] Close button class. "XButton" by default
*/
setCloseButtonClass(className = "XButton") {
this.closeButtonClass = className;
return this;
} }
/** /**
@ -199,7 +234,6 @@ class MenuScreen {
/** /**
* Checks whether the menu screen is ready for build. That method should not be called outside of build method * Checks whether the menu screen is ready for build. That method should not be called outside of build method
* @private
*/ */
_check() { _check() {
if (!this.parentDivId) throw "No parent div id specified"; if (!this.parentDivId) throw "No parent div id specified";
@ -218,12 +252,12 @@ class MenuScreen {
parent.style.display = "none"; parent.style.display = "none";
const inner = document.createElement("div"); const inner = document.createElement("div");
inner.className = this.innerDivClass ?? "menuScreen"; inner.className = this.innerDivClass ?? "menuScreen";
inner.innerHTML = `${this.showCloseButton ? `<button class="XButton" onclick="closeMenu();">${this.closeButtonText}` : ""}</button> inner.innerHTML = `${this.showCloseButton ? `<button class="${this.closeButtonClass ?? "XButton"}" onclick="closeMenu();">${this.closeButtonText}` : ""}</button>
<span class="menuTitle">${this.title ?? "Menu Screen"}</span><br><br><div class="menuText">` + this.innerHtml + "</div>"; <span class="menuTitle">${this.title ?? "Menu Screen"}</span><br><br><div class="menuText">` + this.innerHtml + "</div>";
inner.append(this.nodes); this.nodes.forEach(n => inner.querySelector(".menuText").appendChild(n));
parent.appendChild(inner); parent.appendChild(inner);
document.getElementById(id).appendChild(parent); document.getElementById(id).appendChild(parent);
} }
} }
runAfterLoadList.push(inject); runAfterLoadList.push(inject);