]> git.lizzy.rs Git - dragonblocks.git/blob - engine/creative_inventory.js
e94490dfae7d9efce0c5ec237a452b96e99e1537
[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                         stack.addEventListener("update", event => {
40                                 if (event.stack.refilling)
41                                         return;
42
43                                 stack.refilling = true;
44                                 stack.deserialize(self.fullList[self.slots * self.page + i] || "");
45                                 stack.refilling = false;
46                         });
47                 }
48         }
49
50         calculateHeight()
51         {
52                 return super.calculateHeight() + dragonblocks.settings.inventory.scale;
53         }
54
55         draw(parent, x, y)
56         {
57                 if (! super.draw(parent, x, y))
58                         return false;
59
60                 let display = this.getDisplay();
61                 display.style.height = this.calculateHeight();
62
63                 let creativeDisplay = display.appendChild(document.createElement("div"));
64                 creativeDisplay.id = "dragonblocks.inventory[" + this.id + "].creative";
65                 creativeDisplay.style.height = dragonblocks.settings.inventory.scale + "px";
66                 creativeDisplay.style.width = this.calculateWidth() + "px";
67                 creativeDisplay.style.left = "0px";
68                 creativeDisplay.style.top = super.calculateHeight() + "px";
69                 creativeDisplay.style.position = "absolute";
70
71                 let pageDisplay = creativeDisplay.appendChild(document.createElement("span"));
72                 pageDisplay.id = "dragonblocks.inventory[" + this.id + "].creative.page";
73                 pageDisplay.style.color = "#343434";
74                 pageDisplay.style.position = "absolute";
75                 pageDisplay.style.left = dragonblocks.settings.inventory.scale * 1.1 + "px";
76                 pageDisplay.style.width = "100%";
77                 pageDisplay.style.fontSize = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
78                 pageDisplay.style.height = dragonblocks.settings.inventory.scale / (5 / 3) + "px";
79
80                 dblib.centerVertical(pageDisplay);
81
82                 let self = this;
83
84                 for (let dir of ["left", "right"]) {
85                         let arrow = creativeDisplay.appendChild(document.createElement("div"));
86                         arrow.id = "dragonblocks.inventory[" + this.id + "].creative.arrow." + dir;
87                         arrow.style.position = "absolute";
88                         arrow.style.width = dragonblocks.settings.inventory.scale + "px";
89                         arrow.style.height = dragonblocks.settings.inventory.scale + "px";
90                         arrow.style[dir] = "0px";
91                         arrow.style.background = dragonblocks.getTexture("arrow.png");
92                         arrow.style.cursor = "pointer";
93
94                         if (dir == "right")
95                                 arrow.style.transform = "rotate(180deg)";
96
97                         arrow.addEventListener("click", _ => {
98                                 if(dir == "right")
99                                         self.page++;
100                                 else
101                                         self.page--;
102                                 self.update();
103                         });
104
105                         dblib.centerVertical(arrow);
106                 }
107
108                 this.update();
109         }
110
111         update()
112         {
113                 if (this.page == -1)
114                         this.page++;
115
116                 if (this.page == this.pages)
117                         this.page--;
118
119                 document.getElementById("dragonblocks.inventory[" + this.id + "].creative.page").textContent = "Page " + (this.page + 1) + " of " + this.pages;
120
121                 for (let slot of this.list)
122                         slot.update();
123         }
124 };