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,
23 dragonblocks.Hudbar = class{
24 constructor(inventory, slots){
25 this.id = dragonblocks.getToken();
26 this.inventory = inventory;
28 this.selectedSlot = 0;
29 let display = document.createElement("div");
30 display.id = "dragonblocks.hudbar[" + this.id + "]";
31 display.style.position = "fixed";
32 display.style.bottom = "5px";
33 display.style.height = "60px";
34 display.style.width = "445px";
35 for(let i = 0; i < this.slots; i++){
36 let slotDisplay = document.createElement("div");
37 slotDisplay.id = "dragonblocks.hudbar[" + this.id + "].slot[" + i + "]";
38 slotDisplay.style.position = "absolute";
39 slotDisplay.style.top = "3px";
40 slotDisplay.style.left = i * 53 + "px";
41 slotDisplay.style.width = "50px";
42 slotDisplay.style.height = "50px";
43 slotDisplay.style.backgroundColor = "black";
44 slotDisplay.style.boxShadow = "0 0 0 3px #C5C5C5";
45 let slotCountDisplay = document.createElement("span");
46 slotCountDisplay.id = slotDisplay.id + ".count";
47 slotCountDisplay.style.position = "absolute";
48 slotCountDisplay.style.right = "5px";
49 slotCountDisplay.style.bottom = "5px";
50 slotCountDisplay.style.color = "white";
51 slotDisplay.appendChild(slotCountDisplay);
52 display.appendChild(slotDisplay);
54 let selectorDisplay = document.createElement("div");
55 selectorDisplay.id = "dragonblocks.hudbar[" + this.id + "].selector";
56 selectorDisplay.style.position = "absolute";
57 selectorDisplay.style.top = "3px";
58 selectorDisplay.style.width = "50px";
59 selectorDisplay.style.height = "50px";
60 selectorDisplay.style.boxShadow = "0 0 0 5px #999999";
61 display.appendChild(selectorDisplay);
62 let itemnameDisplay = document.createElement("span");
63 itemnameDisplay.id = "dragonblocks.hudbar[" + this.id + "].itemname";
64 itemnameDisplay.style.position = "absolute";
65 itemnameDisplay.style.bottom = "60px";
66 itemnameDisplay.style.color = "white";
67 itemnameDisplay.style.fontSize = "20px";
68 display.appendChild(itemnameDisplay);
69 display = document.body.insertBefore(display, document.getElementById("dragonblocks.map").nextSibling);
70 dblib.center(display);
74 (this.selectedSlot++ == 7) && (this.selectedSlot = 0);
78 (this.selectedSlot-- == 0) && (this.selectedSlot = 7);
82 this.selectedSlot = i;
86 let display = document.getElementById("dragonblocks.hudbar[" + this.id + "]");
89 for(let i = 0; i < this.slots; i++){
90 let itemstack = this.inventory.getSlot(i);
91 let slotDisplay = document.getElementById("dragonblocks.hudbar[" + this.id + "].slot[" + i + "]");
92 slotDisplay.style.background = itemstack.item ? dragonblocks.getTexture(itemstack.toItem().texture) : "black";
93 slotDisplay.style.backgroundSize = "cover";
94 slotDisplay.style.opacity = itemstack.item ? 1 : 0.3;
95 document.getElementById(slotDisplay.id + ".count").innerHTML = (itemstack.count <= 1) ? "" : itemstack.count;
96 if(i == this.selectedSlot){
97 document.getElementById("dragonblocks.hudbar[" + this.id + "].selector").style.left = slotDisplay.style.left;
98 document.getElementById("dragonblocks.hudbar[" + this.id + "].itemname").innerHTML = itemstack.item ? itemstack.toItem().desc : "";
99 dblib.center(document.getElementById("dragonblocks.hudbar[" + this.id + "].itemname"));
104 return this.inventory.getSlot(this.selectedSlot);