This commit is contained in:
slweeb 2023-07-31 23:35:00 -04:00
commit 7256aab393
5 changed files with 1245 additions and 52 deletions

View File

@ -5,6 +5,7 @@
* @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 {() => void} [close] Closing method. Optional
* @property {() => void} [preOpen] Method called before opening. 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} [loader] Method that injects the menu screen into HTML. Can be set to ModScreen build method. Optional
@ -22,7 +23,7 @@ var menuScreens = {
infoSearch.value = "";
infoHistory = [];
},
open: showInfo
open: () => {showInfo();}
},
mods: {
name: "Mods",
@ -35,18 +36,18 @@ var menuScreens = {
modParent.style.display = "none";
modManagerUrl.value = "";
},
open: showModManager
open: () => {showModManager();}
},
settings: {
name: "Settings",
parentDiv: "settingsParent",
buttonDescription: "Brings up the settings screen",
show: true,
open: showSettings
open: () => {showSettings();}
}
}
closeMenu = (force) => {
closeMenu = (force = false) => {
if (!showingMenu) return;
const menu = menuScreens[showingMenu];
if (!menu) {
@ -68,6 +69,8 @@ closeMenu = (force) => {
const inject = () => {
const toolControls = document.getElementById("toolControls");
const buttons = [];
const style = document.createElement("style");
document.head.appendChild(style);
for (const key in menuScreens) {
const element = menuScreens[key];
if (element.show) {
@ -77,6 +80,7 @@ const inject = () => {
button.onclick = () => {
if (showingMenu != key) {
closeMenu(true);
if (element.preOpen) element.preOpen();
if (element.open) element.open();
else {
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 {
constructor () {
this.nodes = [];
this.innerHtml = "";
this.showCloseButton = true;
this.closeButtonText = "-";
this.closeButtonClass = "XButton";
}
/**
@ -123,6 +147,7 @@ class MenuScreen {
*/
setShowCloseButton(show) {
this.showCloseButton = show;
return this;
}
/**
@ -131,6 +156,16 @@ class MenuScreen {
*/
setCloseButtonText(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
* @private
*/
_check() {
if (!this.parentDivId) throw "No parent div id specified";
@ -218,12 +252,12 @@ class MenuScreen {
parent.style.display = "none";
const inner = document.createElement("div");
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>";
inner.append(this.nodes);
this.nodes.forEach(n => inner.querySelector(".menuText").appendChild(n));
parent.appendChild(inner);
document.getElementById(id).appendChild(parent);
}
}
runAfterLoadList.push(inject);
runAfterLoadList.push(inject);

View File

@ -114,45 +114,61 @@ function openModList() {
showingMenu = "modList";
}
runAfterLoadList.push(updateModManager);
closeMenu = function() {
if (!showingMenu) { return; }
if (showingMenu == "info") {
var infoParent = document.getElementById("infoParent");
var infoSearch = document.getElementById("infoSearch");
infoParent.style.display = "none";
infoSearch.value = "";
showingMenu = false;
infoHistory = [];
if (enabledMods.includes("mods/betterMenuScreens.js")) {
menuScreens.modList = {
name: "Mod manager",
parentDiv: "modListParent",
show: false,
close: () => {
var modParent = document.getElementById("modListParent");
var modManagerUrl = document.getElementById("modManagerUrl");
modParent.style.display = "none";
modManagerUrl.value = "";
showingMenu = false;
},
onClose: showModManager,
loader: updateModManager
}
else if (showingMenu == "mods") {
var modParent = document.getElementById("modParent");
var modManagerUrl = document.getElementById("modManagerUrl");
modParent.style.display = "none";
modManagerUrl.value = "";
showingMenu = false;
}
else if (showingMenu == "modList") {
var modParent = document.getElementById("modListParent");
var modManagerUrl = document.getElementById("modManagerUrl");
modParent.style.display = "none";
modManagerUrl.value = "";
showingMenu = false;
// open mod manager again so the mod list menu looks like a submenu
showModManager();
}
else if (showingMenu == "settings") {
var settingsParent = document.getElementById("settingsParent");
settingsParent.style.display = "none";
showingMenu = false;
}
else {
// do it to all elements with the class "menuParent"
var menuParents = document.getElementsByClassName("menuParent");
for (var i = 0; i < menuParents.length; i++) {
menuParents[i].style.display = "none";
} else {
closeMenu = function() {
if (!showingMenu) { return; }
if (showingMenu == "info") {
var infoParent = document.getElementById("infoParent");
var infoSearch = document.getElementById("infoSearch");
infoParent.style.display = "none";
infoSearch.value = "";
showingMenu = false;
infoHistory = [];
}
else if (showingMenu == "mods") {
var modParent = document.getElementById("modParent");
var modManagerUrl = document.getElementById("modManagerUrl");
modParent.style.display = "none";
modManagerUrl.value = "";
showingMenu = false;
}
else if (showingMenu == "modList") {
var modParent = document.getElementById("modListParent");
var modManagerUrl = document.getElementById("modManagerUrl");
modParent.style.display = "none";
modManagerUrl.value = "";
showingMenu = false;
// open mod manager again so the mod list menu looks like a submenu
showModManager();
}
else if (showingMenu == "settings") {
var settingsParent = document.getElementById("settingsParent");
settingsParent.style.display = "none";
showingMenu = false;
}
else {
// do it to all elements with the class "menuParent"
var menuParents = document.getElementsByClassName("menuParent");
for (var i = 0; i < menuParents.length; i++) {
menuParents[i].style.display = "none";
}
showingMenu = false;
}
showingMenu = false;
}
runAfterLoadList.push(updateModManager);
}

View File

@ -4,7 +4,7 @@ let realTps = 0;
let lastTps = 0;
window.addEventListener("load", ()=>{
requireMods(["mods/libhooktick.js"], () => {
everyTick(()=>{
beforeEveryTick(()=>{
lastTps++;
});
setInterval(()=>{

1109
mods/elementsManager.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,49 @@
let __registeredTickCallbacks = [];
let __registeredAfterTickCallbacks = [];
let __registeredBeforeTickCallbacks = [];
window.addEventListener("load", ()=>{
let oldTick = tick;
const oldTick = tick;
clearInterval(tickInterval);
tick = function(){
__registeredBeforeTickCallbacks.forEach(func=>{
func();
});
oldTick();
__registeredTickCallbacks.forEach(func=>{
__registeredAfterTickCallbacks.forEach(func=>{
func();
});
}
tickInterval = setInterval(tick, 1000/tps);
});
function everyTick(callback){
__registeredTickCallbacks.push(callback);
function everyTick(callback) {
afterEveryTick(callback);
}
window.everyTick = everyTick;
function beforeEveryTick(callback) {
__registeredBeforeTickCallbacks.push(callback);
}
window.beforeEveryTick = beforeEveryTick;
function afterEveryTick(callback) {
__registeredAfterTickCallbacks.push(callback);
}
window.afterEveryTick = afterEveryTick;
function removeTickListener(callback, mode) {
let removed = false;
if(mode!=="before") {
let index = __registeredAfterTickCallbacks.indexOf(callback);
if (index === -1 && mode === "after") throw new Error(`Could not find callback.`);
if(index !== -1) {
__registeredAfterTickCallbacks.splice(index, 1);
removed = true;
}
}
if(mode!=="after") {
let index = __registeredBeforeTickCallbacks.indexOf(callback);
if (index === -1 && mode === "before") throw new Error(`Could not find callback.`);
if(index !== -1) {
__registeredBeforeTickCallbacks.splice(index, 1);
removed = true;
}
}
return removed;
}
window.removeTickListener = removeTickListener;