]> git.lizzy.rs Git - dragonblocks.git/blob - engine/creative_inventory.js
Implement per-map sky
[dragonblocks.git] / engine / creative_inventory.js
1 /*
2  * creative_inventory.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.CreativeInventory = class extends dragonblocks.Inventory
25 {
26         constructor(slots, list, columns)
27         {
28                 super(slots, columns);
29
30                 this.fullList = list || this.list;
31
32                 this.page = 0;
33                 this.pages = Math.ceil(this.fullList.length / this.list.length);
34
35                 let self = this;
36
37                 for (let i = 0; i < this.slots; i++) {
38                         let stack = this.list[i];
39
40                         stack.addEventListener("update", event => {
41                                 if (event.stack.refilling)
42                                         return;
43
44                                 stack.refilling = true;
45                                 stack.deserialize(self.fullList[self.slots * self.page + i] || "");
46                                 stack.refilling = false;
47                         });
48                 }
49         }
50
51         calculateHeight()
52         {
53                 return super.calculateHeight() + dragonblocks.settings.inventory.scale;
54         }
55
56         update()
57         {
58                 if (this.page == -1)
59                         this.page++;
60
61                 if (this.page == this.pages)
62                         this.page--;
63
64                 this.pageDisplay.textContent = "Page " + (this.page + 1) + " of " + this.pages;
65
66                 for (let slot of this.list)
67                         slot.update();
68         }
69
70         initGraphics()
71         {
72                 super.initGraphics();
73
74                 let pageContainer = this.display.appendChild(document.createElement("div"));
75                 pageContainer.style.height = dragonblocks.settings.inventory.scale + "px";
76                 pageContainer.style.width = this.calculateWidth() + "px";
77                 pageContainer.style.left = "0px";
78                 pageContainer.style.top = super.calculateHeight() + "px";
79                 pageContainer.style.position = "absolute";
80
81                 this.pageDisplay = pageContainer.appendChild(document.createElement("span"));
82                 this.pageDisplay.style.color = "#343434";
83                 this.pageDisplay.style.position = "absolute";
84                 this.pageDisplay.style.left = dragonblocks.settings.inventory.scale * 1.1 + "px";
85                 this.pageDisplay.style.width = "100%";
86                 this.pageDisplay.style.fontSize = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
87                 this.pageDisplay.style.height = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
88
89                 dblib.centerVertical(this.pageDisplay);
90
91                 let self = this;
92
93                 for (let dir of ["left", "right"]) {
94                         let arrow = pageContainer.appendChild(document.createElement("div"));
95                         arrow.style.position = "absolute";
96                         arrow.style.width = dragonblocks.settings.inventory.scale + "px";
97                         arrow.style.height = dragonblocks.settings.inventory.scale + "px";
98                         arrow.style[dir] = "0px";
99                         arrow.style.background = dragonblocks.getTexture("arrow.png");
100                         arrow.style.cursor = "pointer";
101
102                         if (dir == "right")
103                                 arrow.style.transform = "rotate(180deg)";
104
105                         arrow.addEventListener("click", _ => {
106                                 if (dir == "right")
107                                         self.page++;
108                                 else
109                                         self.page--;
110
111                                 self.update();
112                         });
113
114                         dblib.centerVertical(arrow);
115                 }
116         }
117 };