]> git.lizzy.rs Git - dragonblocks.git/blob - engine/inventory_group.js
098f993a60a99940b666f43905e059aed278f152
[dragonblocks.git] / engine / inventory_group.js
1 /*
2  * inventory_group.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.InventoryGroup = class
25 {
26         constructor()
27         {
28                 this.id = dragonblocks.getToken();
29
30                 this._elements = [];
31                 this.opened = false;
32
33                 let display = dragonblocks.addInventoryMenuDisplay(document.createElement("div"));
34                 display.id = "dragonblocks.inventoryGroup[" + this.id + "]";
35                 display.style.position = "fixed";
36                 display.style.backgroundColor = "#535353";
37                 display.style.visibility = "hidden";
38         }
39
40         close()
41         {
42                 this.opened = false;
43
44                 document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]").style.visibility = "hidden";
45                 dragonblocks.outStack.getDisplay().style.visibility = "hidden";
46
47                 if(this.onNextClose)
48                         this.onNextClose();
49
50                 this.onNextClose = null;
51         }
52
53         open()
54         {
55                 this.opened = true;
56
57                 document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]").style.visibility = "inherit";
58                 dragonblocks.outStack.getDisplay().style.visibility = "visible";
59         }
60
61         toggle()
62         {
63                 this.opened ? this.close() : this.open();
64         }
65
66         set elements(elements)
67         {
68                 for (let element of this.elements)
69                         element.hide();
70
71                 this._elements = elements;
72
73                 let container = document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]");
74
75                 let height = 0;
76                 let width = 0;
77
78                 for (let element of this.elements) {
79                         element.draw(container, 0, height);
80                         height += element.calculateHeight();
81                         width = Math.max(width, element.calculateWidth());
82                         element.show();
83                 }
84
85                 container.style.width = width + "px";
86                 container.style.height = height + "px";
87
88                 dblib.center(container);
89                 dblib.centerVertical(container);
90         }
91
92         get elements()
93         {
94                 return this._elements;
95         }
96 };