]> git.lizzy.rs Git - dragonblocks.git/blob - engine/menu.js
ffa81841e45246cddf9ffa330b8ec06bc7f222f7
[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
24 dragonblocks.Menu = class
25 {
26         constructor()
27         {
28                 let display = document.body.appendChild(document.createElement("div"));
29                 display.id = "dragonblocks.menu";
30                 display.style.position = "fixed";
31                 display.style.backgroundColor = "#7E7E7E";
32                 display.style.width = "500px";
33                 display.style.height = "10px";
34                 display.style.visibility = "hidden";
35
36                 dblib.center(display);
37                 dblib.centerVertical(display);
38
39                 let head = document.createElement("div");
40
41                 let headline = head.appendChild(document.createElement("h2"));
42                 headline.innerHTML = "Options";
43                 headline.align = "center";
44
45                 this.addElement(head);
46         }
47
48         toggle()
49         {
50                 this.opened ? this.close() : this.open();
51         }
52
53         close()
54         {
55                 this.opened = false;
56
57                 dragonblocks.gui.closeLayer();
58                 dragonblocks.keyHandler.unlockAll();
59
60                 document.getElementById("dragonblocks.menu").style.visibility = "hidden";
61         }
62
63         open()
64         {
65                 this.opened = true;
66
67                 dragonblocks.gui.showLayer();
68                 dragonblocks.keyHandler.lockAll();
69                 dragonblocks.keyHandler.unlock("Escape");
70
71                 document.getElementById("dragonblocks.menu").style.visibility = "visible";
72         }
73
74         addElement(elem)
75         {
76                 let menu = document.getElementById("dragonblocks.menu");
77                 elem = menu.appendChild(elem);
78
79                 elem.style.position = "absolute";
80                 elem.style.top = menu.offsetHeight + "px";
81                 elem.style.width = "80%";
82                 dblib.center(elem);
83
84                 menu.style.height = menu.offsetHeight + 10 + elem.offsetHeight + "px";
85                 dblib.centerVertical(menu);
86
87                 return elem;
88         }
89
90         addButton(html, func)
91         {
92                 let elem = document.createElement("button");
93                 elem.innerHTML = html;
94                 elem.style.fontSize = "20px";
95                 elem.style.borderRadius = "0%";
96                 elem.addEventListener("click", event => {
97                         dragonblocks.menu.close();
98                         func && func(event);
99                 });
100
101                 this.addElement(elem);
102         }
103 };
104
105 dragonblocks.menu = new dragonblocks.Menu();
106 dragonblocks.menu.addButton("Continue Playing");
107
108 dragonblocks.registerOnStarted(_ => {
109         dragonblocks.menu.addButton(dragonblocks.loggedin ? "Save and Quit to Title" : "Quit to Title", dragonblocks.quit);
110 });
111
112 dragonblocks.keyHandler.down("F5", _ => {});
113
114 dragonblocks.keyHandler.down("Escape", _ => {
115         dragonblocks.menu.toggle();
116 });