]> git.lizzy.rs Git - dragonblocks.git/blobdiff - engine/inventory.js
Rework inventory rendering
[dragonblocks.git] / engine / inventory.js
index b7142458c6f5455e8ace485fe38bb5608d83c867..d6e5b2e9e9ccd88eb576021f0b6c58db21936110 100644 (file)
@@ -26,7 +26,6 @@ dragonblocks.Inventory = class extends EventTarget
        constructor(slots, columns)
        {
                super();
-               this.id = dragonblocks.getToken();
 
                this.slots = slots;
                this.columns = columns;
@@ -102,54 +101,21 @@ dragonblocks.Inventory = class extends EventTarget
 
        draw(parent, x, y)
        {
-               if (this.display) {
-                       let display = this.getDisplay();
-                       if (display.parentElement == parent) {
-                               display.style.left = x + "px";
-                               display.style.top = y + "px";
-                               return false;
-                       } else {
-                               this.remove();
-                       }
-               }
-
-               this.display = true;
-
-               let display = parent.appendChild(document.createElement("div"));
-               display.id = "dragonblocks.inventory[" + this.id + "]";
-               display.style.position = "absolute";
-               display.style.left = x + "px";
-               display.style.top = y + "px";
-               display.style.width =  this.calculateWidth() + "px";
-               display.style.height = this.calculateHeight() + "px";
-
-               let scale = dragonblocks.settings.inventory.scale * 1.1;
-               let offset = dragonblocks.settings.inventory.scale * 0.1;
+               if (! this.display)
+                       this.initGraphics();
 
-               for (let i in this.list) {
-                       let x = i % this.columns;
-                       let y = (i - x) / this.columns;
-                       this.list[i].draw(display, offset + x * scale, offset + y * scale);
-               }
-
-               return true;
-       }
+               if (this.display.parentElement != parent)
+                       this.display = parent.appendChild(this.display);
 
-       remove()
-       {
-               this.getDisplay().remove();
-               this.display = false;
-       }
+               this.display.style.left = x + "px";
+               this.display.style.top = y + "px";
 
-       show()
-       {
-               this.getDisplay().style.visibility = "inherit";
                this.update();
        }
 
-       hide()
+       remove()
        {
-               this.getDisplay().style.visibility = "hidden";
+               this.display.remove();
        }
 
        update()
@@ -163,9 +129,21 @@ dragonblocks.Inventory = class extends EventTarget
                return this.list[i];
        }
 
-       getDisplay()
+       initGraphics()
        {
-               return document.getElementById("dragonblocks.inventory[" + this.id + "]");
+               this.display = document.createElement("div");
+               this.display.style.position = "absolute";
+               this.display.style.width =  this.calculateWidth() + "px";
+               this.display.style.height = this.calculateHeight() + "px";
+
+               let scale = dragonblocks.settings.inventory.scale * 1.1;
+               let offset = dragonblocks.settings.inventory.scale * 0.1;
+
+               for (let i in this.list) {
+                       let x = i % this.columns;
+                       let y = (i - x) / this.columns;
+                       this.list[i].draw(this.display, offset + x * scale, offset + y * scale);
+               }
        }
 };