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.Hotbar = class
26 constructor(inventory, slots)
28 this.inventory = inventory;
31 this.selectedSlot = 0;
33 this.display = document.body.insertBefore(document.createElement("div"), dragonblocks.mapDisplay.element.nextSibling);
34 this.display.style.position = "fixed";
35 this.display.style.bottom = "5px";
36 this.display.style.height = "60px";
37 this.display.style.width = "445px";
39 dblib.center(this.display);
41 this.slotDisplays = [];
43 for (let i = 0; i < this.slots; i++) {
44 let slotDisplay = this.display.appendChild(document.createElement("div"));
45 slotDisplay.style.position = "absolute";
46 slotDisplay.style.top = "3px";
47 slotDisplay.style.left = i * 53 + "px";
48 slotDisplay.style.width = "50px";
49 slotDisplay.style.height = "50px";
50 slotDisplay.style.boxShadow = "0 0 0 3px #C5C5C5";
52 let slotCountDisplay = slotDisplay.appendChild(document.createElement("span"));
53 slotCountDisplay.style.position = "absolute";
54 slotCountDisplay.style.right = "5px";
55 slotCountDisplay.style.bottom = "5px";
56 slotCountDisplay.style.color = "white";
58 this.slotDisplays[i] = {slotDisplay, slotCountDisplay};
61 this.selectorDisplay = this.display.appendChild(document.createElement("div"));
62 this.selectorDisplay.style.position = "absolute";
63 this.selectorDisplay.style.top = "3px";
64 this.selectorDisplay.style.width = "50px";
65 this.selectorDisplay.style.height = "50px";
66 this.selectorDisplay.style.boxShadow = "0 0 0 5px #999999";
68 this.itemnameDisplay = this.display.appendChild(document.createElement("span"));
69 this.itemnameDisplay.style.position = "absolute";
70 this.itemnameDisplay.style.bottom = "60px";
71 this.itemnameDisplay.style.color = "white";
72 this.itemnameDisplay.style.fontSize = "20px";
79 if (++this.selectedSlot == this.slots)
80 this.selectedSlot = 0;
87 if (--this.selectedSlot == -1)
88 this.selectedSlot = this.slots - 1;
95 this.selectedSlot = i;
101 for (let i = 0; i < this.slots; i++) {
102 let itemstack = this.inventory.getSlot(i);
103 let {slotDisplay, slotCountDisplay} = this.slotDisplays[i];
105 slotDisplay.style.background = itemstack.item && dragonblocks.getTexture(itemstack.toItem().texture);
106 if (! slotDisplay.style.backgroundColor || slotDisplay.style.backgroundColor == "initial")
107 slotDisplay.style.backgroundColor = "rgba(0, 0, 0, 0.3)";
108 slotDisplay.style.backgroundSize = "cover";
110 slotCountDisplay.innerHTML = (itemstack.count <= 1) ? "" : itemstack.count;
112 if (i == this.selectedSlot) {
113 this.selectorDisplay.style.left = slotDisplay.style.left;
115 this.itemnameDisplay.innerHTML = itemstack.item ? itemstack.toItem().desc : "";
116 dblib.center(this.itemnameDisplay);
123 return this.inventory.getSlot(this.selectedSlot);