X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=engine%2Fcreative_inventory.js;h=e94490dfae7d9efce0c5ec237a452b96e99e1537;hb=647040aa14a0ada7430ad4df56ef3e8323c14033;hp=7c134f7c929a740adcfffb73fc95f6caec85532f;hpb=570a55bf0d724cc03347af10d0124233ecac8729;p=dragonblocks.git diff --git a/engine/creative_inventory.js b/engine/creative_inventory.js old mode 100755 new mode 100644 index 7c134f7..e94490d --- a/engine/creative_inventory.js +++ b/engine/creative_inventory.js @@ -1,102 +1,124 @@ /* * creative_inventory.js - * + * * Copyright 2020 Elias Fleckenstein - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. - * - * + * + * */ -dragonblocks.CreativeInventory = class extends dragonblocks.Inventory{ - constructor(slots, list, columns){ + +dragonblocks.CreativeInventory = class extends dragonblocks.Inventory +{ + constructor(slots, list, columns) + { super(slots, columns); + this.fullList = list || this.list; + this.page = 0; this.pages = Math.ceil(this.fullList.length / this.list.length); - let inventory = this; - for(let i = 0; i < this.slots; i++){ - i = parseInt(i); - this.list[i].addUpdateListener(_ => { - if(inventory.list[i].refilling) + + let self = this; + + for (let i = 0; i < this.slots; i++) { + let stack = this.list[i]; + stack.addEventListener("update", event => { + if (event.stack.refilling) return; - inventory.list[i].refilling = true; - inventory.list[i].parse(inventory.fullList[inventory.slots * inventory.page + i] || ""); - inventory.list[i].refilling = false; + + stack.refilling = true; + stack.deserialize(self.fullList[self.slots * self.page + i] || ""); + stack.refilling = false; }); } } - calculateHeight(){ + + calculateHeight() + { return super.calculateHeight() + dragonblocks.settings.inventory.scale; } - draw(parent, x, y){ - if (!super.draw(parent, x, y)) + + draw(parent, x, y) + { + if (! super.draw(parent, x, y)) return false; - let inventory = this; - this.getDisplay().style.height = this.calculateHeight(); - let creativeDisplay = document.createElement("div"); + + let display = this.getDisplay(); + display.style.height = this.calculateHeight(); + + let creativeDisplay = display.appendChild(document.createElement("div")); creativeDisplay.id = "dragonblocks.inventory[" + this.id + "].creative"; creativeDisplay.style.height = dragonblocks.settings.inventory.scale + "px"; - creativeDisplay.style.width = this.calculateWidth(); + creativeDisplay.style.width = this.calculateWidth() + "px"; creativeDisplay.style.left = "0px"; creativeDisplay.style.top = super.calculateHeight() + "px"; creativeDisplay.style.position = "absolute"; - this.getDisplay().appendChild(creativeDisplay); - creativeDisplay = document.getElementById(creativeDisplay.id); - let pageDisplay = document.createElement("span"); + + let pageDisplay = creativeDisplay.appendChild(document.createElement("span")); pageDisplay.id = "dragonblocks.inventory[" + this.id + "].creative.page"; - pageDisplay.style.color = "343434"; + pageDisplay.style.color = "#343434"; pageDisplay.style.position = "absolute"; pageDisplay.style.left = dragonblocks.settings.inventory.scale * 1.1 + "px"; pageDisplay.style.width = "100%"; - pageDisplay.style.fontSize = dragonblocks.settings.inventory.scale / (5/3) + "px"; - pageDisplay.style.height = dragonblocks.settings.inventory.scale / (5/3) + "px"; - creativeDisplay.appendChild(pageDisplay); - dblib.centerVertical(document.getElementById(pageDisplay.id)); - for(let dir of ["left", "right"]){ - let arrow = document.createElement("div"); + pageDisplay.style.fontSize = dragonblocks.settings.inventory.scale / (5 / 3) + "px"; + pageDisplay.style.height = dragonblocks.settings.inventory.scale / (5 / 3) + "px"; + + dblib.centerVertical(pageDisplay); + + let self = this; + + for (let dir of ["left", "right"]) { + let arrow = creativeDisplay.appendChild(document.createElement("div")); arrow.id = "dragonblocks.inventory[" + this.id + "].creative.arrow." + dir; arrow.style.position = "absolute"; arrow.style.width = dragonblocks.settings.inventory.scale + "px"; arrow.style.height = dragonblocks.settings.inventory.scale + "px"; - arrow.style.position = "absolute"; arrow.style[dir] = "0px"; arrow.style.background = dragonblocks.getTexture("arrow.png"); - arrow.style.backgroundSize = "cover"; arrow.style.cursor = "pointer"; - if(dir == "right") + + if (dir == "right") arrow.style.transform = "rotate(180deg)"; + arrow.addEventListener("click", _ => { if(dir == "right") - inventory.page++; + self.page++; else - inventory.page--; - inventory.update(); + self.page--; + self.update(); }); - creativeDisplay.appendChild(arrow); - dblib.centerVertical(document.getElementById(arrow.id)); + + dblib.centerVertical(arrow); } + this.update(); - } - update(){ - if(this.page == -1) - this.page = 0; - if(this.page == this.pages) + } + + update() + { + if (this.page == -1) + this.page++; + + if (this.page == this.pages) this.page--; - document.getElementById("dragonblocks.inventory[" + this.id + "].creative.page").textContent = "Page " + (this.page + 1 ) + " of " + this.pages; - for(let slot of this.list) + + document.getElementById("dragonblocks.inventory[" + this.id + "].creative.page").textContent = "Page " + (this.page + 1) + " of " + this.pages; + + for (let slot of this.list) slot.update(); } -} +};