]> git.lizzy.rs Git - dragonblocks.git/blob - engine/inventory_group.js
c5dce148df56252197068c30789e930c3a7d35a8
[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 dragonblocks.InventoryGroup = class{
24         constructor(){
25                 this.id = dragonblocks.getToken();
26                 this._elements = [];
27                 this.opened = false;
28                 var display = document.createElement("div");
29                 display.id = "dragonblocks.inventoryGroup[" + this.id + "]";
30                 display.style.position = "fixed";
31                 display.style.backgroundColor = "#535353";
32                 display.style.visibility = "hidden";
33                 dragonblocks.Inventory.insertElement(display);
34         }
35         close(){
36                 this.opened = false;
37                 document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]").style.visibility = "hidden";
38                 dragonblocks.Inventory.getStackDisplay(dragonblocks.Inventory.out.id).style.visibility = "hidden";
39                 if(this.onNextClose)
40                         this.onNextClose();
41                 this.onNextInventoryClose = null;
42         }
43         open(){
44                 this.opened = true;
45                 document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]").style.visibility = "visible";
46                 dragonblocks.Inventory.getStackDisplay(dragonblocks.Inventory.out.id).style.visibility = "visible";
47         }
48         toggle(){
49                 this.opened ? this.close() : this.open();
50         }
51         set elements(elements){
52                 for(let element of this.elements)
53                         element.hide();
54                 this._elements = elements;
55                 let height = 0;
56                 let width = 0;
57                 let container = document.getElementById("dragonblocks.inventoryGroup[" + this.id + "]");
58                 for(let element of this.elements){
59                         element.draw(container, 0, height);
60                         height += element.calculateHeight();
61                         width = Math.max(width, element.calculateWidth());
62                         element.show();
63                 }
64                 container.style.width = width + "px"; 
65                 container.style.height = height + "px";
66                 dblib.center(container);
67                 dblib.centerVertical(container);
68         }
69         get elements(){
70                 return this._elements;
71         }
72