4 * Copyright 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
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.
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.
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,
24 dragonblocks.Inventory = class extends EventTarget
26 constructor(slots, columns)
29 this.id = dragonblocks.getToken();
32 this.columns = columns;
37 for (let i = 0; i < this.slots; i++){
38 let stack = this.list[i] = new dragonblocks.ItemStack();
39 stack.addEventListener("update", event => {
40 self.dispatchEvent(new dragonblocks.Inventory.Event("updateStack", event.stack));
51 for (let stack of this.list)
52 str += stack.serialize() + ",";
59 let strList = str.split(",");
61 for (let i in this.list)
62 this.list[i].deserialize(strList[i]);
67 let itemstack = new dragonblocks.ItemStack(itemstring);
69 for (let stack of this.list)
70 stack.item == itemstack.item && stack.add(itemstack);
72 for (let stack of this.list)
80 for (let stack of this.list)
89 for(let stack of this.list)
93 calculateWidth(columns)
95 return dragonblocks.settings.inventory.scale * 1.1 * this.columns + (dragonblocks.settings.inventory.scale * 0.1);
100 return dragonblocks.settings.inventory.scale * 1.1 * Math.ceil(this.slots / this.columns) + dragonblocks.settings.inventory.scale * 0.1
106 let display = this.getDisplay();
107 if (display.parentElement == parent) {
108 display.style.left = x + "px";
109 display.style.top = y + "px";
118 let display = parent.appendChild(document.createElement("div"));
119 display.id = "dragonblocks.inventory[" + this.id + "]";
120 display.style.position = "absolute";
121 display.style.left = x + "px";
122 display.style.top = y + "px";
123 display.style.width = this.calculateWidth() + "px";
124 display.style.height = this.calculateHeight() + "px";
126 let scale = dragonblocks.settings.inventory.scale * 1.1;
127 let offset = dragonblocks.settings.inventory.scale * 0.1;
129 for (let i in this.list) {
130 let x = i % this.columns;
131 let y = (i - x) / this.columns;
132 this.list[i].draw(display, offset + x * scale, offset + y * scale);
140 this.getDisplay().remove();
141 this.display = false;
146 this.getDisplay().style.visibility = "inherit";
152 this.getDisplay().style.visibility = "hidden";
157 for (let stack of this.list)
168 return document.getElementById("dragonblocks.inventory[" + this.id + "]");
172 dragonblocks.Inventory.Event = class extends Event
174 constructor(type, stack)