]> git.lizzy.rs Git - dragonblocks.git/blob - engine/menu.js
a6c31202f3562d2da2e14cb2d039a175a555af6a
[dragonblocks.git] / engine / menu.js
1 /*
2  * menu.js
3  * 
4  * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
5  * 
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.
10  * 
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.
15  * 
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,
19  * MA 02110-1301, USA.
20  * 
21  * 
22  */
23 dragonblocks.Menu = class{
24         constructor(){
25                 let display = document.createElement("div");
26                 display.id = "dragonblocks.menu";
27                 display.style.position = "fixed";
28                 display.style.backgroundColor = "#7E7E7E";
29                 display.style.width = "500px";
30                 display.style.height = "10px";
31                 display.style.visibility = "hidden";
32                 document.body.appendChild(display);
33                 display = document.getElementById(display.id);
34                 dblib.center(display);
35                 dblib.centerVertical(display);
36                 dragonblocks.keyHandler.down("Escape", _ => {
37                         dragonblocks.menu.toggle();
38                 });
39                 let headlineContainer = document.createElement("div");
40                 let headline = headlineContainer.appendChild(document.createElement("h2"));
41                 headline.innerHTML = "Options";
42                 headline.align = "center";
43                 this.addElement(headlineContainer);
44         }
45         toggle(){
46                 this.opened ? this.close() : this.open();
47         }
48         close(){
49                 this.opened = false;
50                 dragonblocks.gui.closeLayer();
51                 dragonblocks.keyHandler.unlockAll();
52                 document.getElementById("dragonblocks.menu").style.visibility = "hidden";
53         }
54         open(){
55                 this.opened = true;
56                 dragonblocks.gui.showLayer();
57                 dragonblocks.keyHandler.lockAll();
58                 dragonblocks.keyHandler.unlock("Escape");
59                 document.getElementById("dragonblocks.menu").style.visibility = "visible";
60         }
61         addElement(elem){
62                 let menu = document.getElementById("dragonblocks.menu");
63                 elem = menu.appendChild(elem);
64                 elem.style.position = "absolute";
65                 elem.style.top = menu.offsetHeight + "px";
66                 elem.style.width = "80%";
67                 dblib.center(elem);
68                 menu.style.height = menu.offsetHeight + 10 + elem.offsetHeight + "px"; 
69                 dblib.centerVertical(menu);
70                 return elem;
71         }
72         addButton(html, func){
73                 let elem = document.createElement("button");
74                 elem.innerHTML = html;
75                 elem.style.fontSize = "20px";
76                 elem.style.borderRadius = "0%";
77                 elem.addEventListener("click", event => {
78                         dragonblocks.menu.close();
79                         func && func(event);
80                 });
81                 this.addElement(elem);
82         }
83 }
84 dragonblocks.menu = new dragonblocks.Menu();
85 dragonblocks.menu.addButton("Continue Playing");
86 dragonblocks.registerOnStarted( _ => {
87         dragonblocks.menu.addButton(dragonblocks.loggedin ? "Save and Quit to Title" : "Quit to Title", dragonblocks.quit);
88 });
89 dragonblocks.keyHandler.down("F5", _ => {});