]> git.lizzy.rs Git - dragonblocks.git/blob - engine/hotbar.js
Rename hudbar to hotbar; Use DOM references for hotbar
[dragonblocks.git] / engine / hotbar.js
1 /*
2  * hotbar.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
24 dragonblocks.Hotbar = class
25 {
26         constructor(inventory, slots)
27         {
28                 this.inventory = inventory;
29                 this.slots = slots;
30
31                 this.selectedSlot = 0;
32
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";
38
39                 dblib.center(this.display);
40
41                 this.slotDisplays = [];
42
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";
51
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";
57
58                         this.slotDisplays[i] = {slotDisplay, slotCountDisplay};
59                 }
60
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";
67
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";
73
74                 this.update();
75         }
76
77         nextItem()
78         {
79                 if (++this.selectedSlot == this.slots)
80                         this.selectedSlot = 0;
81
82                 this.update();
83         }
84
85         previousItem()
86         {
87                 if (--this.selectedSlot == -1)
88                         this.selectedSlot = this.slots - 1;
89
90                 this.update();
91         }
92
93         select(i)
94         {
95                 this.selectedSlot = i;
96                 this.update();
97         }
98
99         update()
100         {
101                 for (let i = 0; i < this.slots; i++) {
102                         let itemstack = this.inventory.getSlot(i);
103                         let {slotDisplay, slotCountDisplay} = this.slotDisplays[i];
104
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";
109
110                         slotCountDisplay.innerHTML = (itemstack.count <= 1) ? "" : itemstack.count;
111
112                         if (i == this.selectedSlot) {
113                                 this.selectorDisplay.style.left = slotDisplay.style.left;
114
115                                 this.itemnameDisplay.innerHTML = itemstack.item ? itemstack.toItem().desc : "";
116                                 dblib.center(this.itemnameDisplay);
117                         }
118                 }
119         }
120
121         getSelectedItem()
122         {
123                 return this.inventory.getSlot(this.selectedSlot);
124         }
125 };