4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 document.title = dragonblocks.version.string;
27 let mainmenu = document.body.insertBefore(document.createElement("div"), document.body.firstChild);
28 mainmenu.style.visibility = "hidden";
30 let center = mainmenu.appendChild(document.createElement("center"));
32 let logo = center.appendChild(document.createElement("img"));
33 logo.src = "textures/logo-mainmenu.png";
35 let status = center.appendChild(document.createElement("h1"));
36 status.style.fontSize = "50px";
37 status.style.display = "none";
39 let content = center.appendChild(document.createElement("div"));
40 content.id = "dragonblocks.mainmenu.content";
41 content.style.position = "relative";
42 content.style.top = "50px";
47 let clearChildren = parent => {
48 while (elem = parent.firstChild)
55 let loadWorldGUI, worldlistDisplay, noWorldsNotice;
57 if (dragonblocks.loggedin) {
60 clearChildren(worldlistDisplay);
62 loadWorldGUI = new dragonblocks.gui.Box();
64 let headline = loadWorldGUI.create("h1");
65 headline.innerHTML = "Select World";
66 headline.align = "center";
68 noWorldsNotice = loadWorldGUI.create("center").appendChild(document.createElement("b"));
70 worldlistDisplay = loadWorldGUI.create("ul");
73 noWorldsNotice.innerHTML = dragonblocks.worlds.length == 0 ? "No Worlds" : "";
75 for (let worldname in dragonblocks.worlds) {
76 let world = dragonblocks.worlds[worldname];
79 let worldDisplay = worldlistDisplay.appendChild(document.createElement("li"));
80 worldDisplay.style.fontSize = "20px";
81 worldDisplay.textContent = world.name;
82 worldDisplay.style.postion = "relative";
84 let button = worldDisplay.appendChild(document.createElement("button"));
85 button.textContent = "Play";
86 button.style.position = "absolute";
87 button.style.right = "5px";
88 button.style.fontSize = "12px";
89 button.addEventListener("click", event => {
90 event.srcElement.blur();
93 dragonblocks.worldIsLoaded = true;
94 dragonblocks.worldname = world.name;
95 dragonblocks.world = $.getJSON("worlds/" + worldname + "/world.json").responseJSON;
97 dragonblocks.mods = dragonblocks.world.mods;
107 text: "Load Saved World",
111 disabled: ! dragonblocks.loggedin,
115 // Create World Button
118 let createWorldGUI = new dragonblocks.gui.Box();
121 let worldProperties = {};
123 let headline = createWorldGUI.create("h1");
124 headline.innerHTML = "New World";
125 headline.align = "center";
128 createWorldGUI.create("h2").innerHTML = " World Name";
130 let worldnameInput = createWorldGUI.create("input");
131 worldnameInput.type = "text";
132 worldnameInput.style.position = "relative";
133 worldnameInput.style.left = "40px";
135 let worldnameAlert = createWorldGUI.create("b");
136 worldnameAlert.style.position = "relative";
137 worldnameAlert.style.left = "50px";
139 worldnameInput.addEventListener("input", _ => {
140 let worldname = worldnameInput.value;
142 if(! dragonblocks.loggedin) {
143 worldnameAlert.textContent = "Warning: You are not logged in and cannot save worlds.";
144 worldnameAlert.style.color = "#FF7D00";
145 createButton.disabled = false;
146 } else if (worldname == "") {
147 worldnameAlert.textContent = "";
148 createButton.disabled = true;
149 } else if (! dragonblocks.checkWorldnameSpelling(worldname)) {
150 worldnameAlert.textContent = "The world name contains forbidden characters";
151 worldnameAlert.style.color = "#FF001F";
152 createButton.disabled = true;
153 } else if (dragonblocks.worlds[worldname]) {
154 if (dragonblocks.worlds[worldname].owned) {
155 worldnameAlert.textContent = "Warning: This will overwrite an existing world";
156 worldnameAlert.style.color = "#FF7D00";
157 createButton.disabled = false;
159 worldnameAlert.textContent = "This Worldname is taken";
160 worldnameAlert.style.color = "#FF001F";
161 createButton.disabled = true;
164 worldnameAlert.textContent = "";
165 createButton.disabled = false;
168 worldProperties.worldname = worldname;
172 worldProperties.mods = {};
174 createWorldGUI.create("h2").innerHTML = " Mods";
178 let updateModlist = _ => {
180 clearChildren(modlistDisplay);
182 modlistDisplay = createWorldGUI.create("ul");
184 let oldSelectedMods = worldProperties.mods;
185 worldProperties.mods = {};
187 for (let modname in dragonblocks.mods) {
188 let modinfo = dragonblocks.mods[modname];
190 let modDisplay = modlistDisplay.appendChild(document.createElement("li"));
191 modDisplay.style.fontSize = "20px";
192 modDisplay.innerHTML = mod;
193 modDisplay.style.postion = "relative";
194 modDisplay.title = modinfo.description;
196 let checkbox = modDisplay.appendChild(document.createElement("input"));
197 checkbox.type = "checkbox";
198 checkbox.style.position = "absolute";
199 checkbox.style.right = "5px";
201 checkbox.addEventListener("input", _ => {
202 worldProperties.mods[mod] = checkbox.checked;
205 worldProperties.mods[mod] = checkbox.checked = oldSelectedMods[mod];
210 worldProperties.gamemode = "survival";
212 createWorldGUI.create("h2").innerHTML = " Gamemode";
214 for (let gamemode of ["survival", "creative"]){
215 let radiobox = createWorldGUI.create("input");
216 radiobox.name = "dragonblocks.mainmenu.createWorldGUI.gamemode";
217 radiobox.type = "radio";
218 radiobox.checked = gamemode == worldProperties.gamemode;
219 radiobox.style.position = "relative";
220 radiobox.style.left = "40px";
222 radiobox.addEventListener("input", _ => {
223 if (radiobox.checked)
224 worldProperties.gamemode = gamemode;
227 let label = createWorldGUI.create("label");
228 label.innerHTML = dblib.humanFormat(gamemode);
229 label.style.position = "relative";
230 label.style.left = "40px";
234 createWorldGUI.create("h2").innerHTML = " Mapgen";
236 let selectMapgen = createWorldGUI.create("select");
237 selectMapgen.style.position = "relative";
238 selectMapgen.style.left = "40px";
240 selectMapgen.addEventListener("input", _ => {
241 worldProperties.mapgen = selectMapgen.value;
244 for (let mapgen in dragonblocks.mapgen.list)
245 selectMapgen.appendChild(document.createElement("option")).innerHTML = mapgen;
247 worldProperties.mapgen = selectMapgen.value;
249 createWorldGUI.create("br");
250 createWorldGUI.create("br");
253 createButton = createWorldGUI.create("button");
254 createButton.style.position = "absolute";
255 createButton.style.left = "1%";
256 createButton.style.bottom = "5px";
257 createButton.style.width = "98%";
258 createButton.style.fontSize = "20px";
259 createButton.innerHTML = "Create World";
261 createButton.addEventListener("click", event => {
262 event.srcElement.blur();
263 createWorldGUI.close();
265 dragonblocks.worldIsLoaded = false;
266 dragonblocks.worldname = worldProperties.worldname;
267 dragonblocks.world = dragonblocks.getEmptyWorld();
269 dragonblocks.entities["dragonblocks:player"].meta.creative = (worldProperties.gamemode == "creative");
271 dragonblocks.mapgen.selected = worldProperties.mapgen;
273 dragonblocks.start(worldProperties.mods);
276 createWorldGUI.create("br");
277 createWorldGUI.create("br");
280 worldnameInput.value = "";
281 worldnameAlert.textContent = "";
282 createButton.disabled = dragonblocks.loggedin;
288 text: "Create New World",
290 createWorldGUI.open();
298 let creditsGUI = new dragonblocks.gui.Box();
300 let pages = $.getJSON("credits.json").responseJSON;
303 for (let dir of ["left", "right"]) {
304 let arrow = creditsGUI.create("div");
305 arrow.style.position = "absolute";
306 arrow.style.width = "80px";
307 arrow.style.height = "80px";
308 arrow.style.position = "absolute";
309 arrow.style[dir] = "3px";
310 arrow.style.background = dragonblocks.getTexture("arrow.png");
311 arrow.style.backgroundSize = "cover";
312 arrow.style.cursor = "pointer";
315 arrow.style.transform = "rotate(180deg)";
317 arrow.addEventListener("click", _ => {
326 dblib.centerVertical(arrow);
329 let creditsContent = creditsGUI.create("center");
331 creditsGUI.addEventListener("open", _ => {
333 page = pages.length - 1;
334 else if (page >= pages.length)
337 creditsContent.innerHTML = pages[page];
338 dragonblocks.resolveTextures(creditsContent);
340 // fix to center the dots of the li elements in chromium as well
342 let lis = creditsContent.getElementsByTagName("li");
345 li.style.width = "max-content";
362 if (dragonblocks.isChromeApp)
370 for (let {text, action, disabled} of buttons) {
371 let button = content.appendChild(document.createElement("button"));
372 button.style.fontSize = "40px";
373 button.style.width = "100%";
374 button.innerHTML = text;
375 button.disabled = disabled;
376 button.addEventListener("click", action);
378 content.appendChild(document.createElement("br"));
379 content.appendChild(document.createElement("br"));
382 for (let [side, text] of [["left", dragonblocks.version.string], ["right", dragonblocks.version.copyright]]) {
383 let notice = content.appendChild(document.createElement("span"));
384 notice.style.position = "fixed";
385 notice.style.bottom = "5px";
386 notice.style[side] = "5px";
387 notice.innerHTML = text;
390 dragonblocks.enterMainMenu = _ => {
391 dragonblocks.loadModList();
392 dragonblocks.loadWorldList();
394 content.style.display = "inherit";
395 status.style.display = "none";
397 for (let func of onReload)
401 dragonblocks.registerOnStart(_ => {
402 content.style.display = "none";
403 status.style.display = "inherit";
405 status.innerHTML = "Loading...";
408 dragonblocks.registerOnStarted(_ => {
409 mainmenu.style.visibility = "hidden";
412 dragonblocks.registerOnQuit(_ => {
413 mainmenu.style.visibility = "visible";
414 status.innerHTML = "Saving...";
417 let initMainMenu = _ => {
418 document.body.style.backgroundColor = "skyblue";
419 document.getElementById("elidragon").remove();
420 content.style.width = logo.offsetWidth + "px";
421 mainmenu.style.visibility = "visible";
423 dragonblocks.enterMainMenu();
429 logo.addEventListener("load", initMainMenu);