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