]> git.lizzy.rs Git - dragonblocks.git/blob - engine/skin.js
ea1e1f7bf6cb4b6b70db52053ad4bcbfdd0b1ab3
[dragonblocks.git] / engine / skin.js
1 /*
2  * skin.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.registeredSkins = {};
25
26 dragonblocks.registerSkin = def => {
27         if (! def || ! def.name || ! def.texture)
28                 dragonblocks.error("Cannot register skin");
29
30         dragonblocks.registeredSkins[def.name] = def;
31 };
32
33 {
34         let gui = new dragonblocks.gui.Box({keylock: true});
35
36         let headline = gui.create("h1");
37         headline.innerHTML = "Skins";
38         headline.align = "center";
39
40         let status = gui.create("span");
41         status.style.position = "absolute";
42         status.style.top = "5px";
43         status.style.left = "5px";
44
45         let columns = parseInt(parseInt(gui.display.style.width) / (dragonblocks.settings.mapDisplay.scale * 1.5));
46
47         let container = gui.create("div");
48         container.style.width = parseInt(columns * dragonblocks.settings.mapDisplay.scale * 1.5) + "px";
49         container.style.position = "absolute";
50         container.style.top = "80px";
51         dblib.center(container);
52
53         dragonblocks.registerOnStarted(_ => {
54                 status.innerHTML = dragonblocks.player.skin;
55
56                 let i = 0;
57
58                 for (let skin in dragonblocks.registeredSkins) {
59                         let x = i % columns;
60                         let y = (i - x) / columns;
61
62                         i++;
63
64                         let def = dragonblocks.registeredSkins[skin];
65
66                         let skinDisplay = container.appendChild(document.createElement("div"));
67                         skinDisplay.style.position = "absolute";
68                         skinDisplay.style.left = parseInt(x * dragonblocks.settings.map.scale * 1.5) + "px";
69                         skinDisplay.style.top = parseInt(y * dragonblocks.settings.map.scale * 2 * 1.5) + "px";
70                         skinDisplay.style.width = parseInt(dragonblocks.settings.map.scale) + "px";
71                         skinDisplay.style.height = parseInt(dragonblocks.settings.map.scale * 2) + "px";
72                         skinDisplay.style.background = dragonblocks.getTexture(def.texture);
73                         skinDisplay.style.backgroundSize = "cover";
74                         skinDisplay.title = def.name + (def.desc ? "\n" + def.desc : "");
75
76                         if (dragonblocks.player.skin == def.name)
77                                 skinDisplay.style.boxShadow = "0 0 0 3px #BEBEBE";
78
79                         skinDisplay.addEventListener("click", event => {
80                                 event.srcElement.style.boxShadow = "0 0 0 3px #BEBEBE";
81
82                                 dragonblocks.player.skin = def.name;
83                                 status.innerHTML = dragonblocks.player.skin;
84
85                                 container.dispatchEvent(new Event("update"));
86                         });
87
88                         container.addEventListener("update", event => {
89                                 if (dragonblocks.player.skin != def.name)
90                                         skinDisplay.style.boxShadow = "none";
91                         });
92
93                         skinDisplay.addEventListener("mouseover", event => {
94                                 if (dragonblocks.player.skin != def.name)
95                                         event.srcElement.style.boxShadow = "0 0 0 1px black";
96                         });
97
98                         skinDisplay.addEventListener("mouseleave", event => {
99                                 if (dragonblocks.player.skin != def.name)
100                                         event.srcElement.style.boxShadow = "none";
101                         });
102                 }
103         });
104
105         dragonblocks.menu.addButton("Change Skin", _ => {
106                 gui.open();
107         });
108 }