Browse Source

✨ Add visibility parameter to Object2D and UIObject

- Add show, hide and setVisibility methods, that recursively change the visibility of all children of the object,
- Add getVisibility method.
Pedro Schneider 3 years ago
parent
commit
96cd31720a

+ 41 - 0
pandora/game_objects/2d_objects/Object2D.js

@@ -28,10 +28,51 @@ class Object2D extends GameObject
         this.position = Vector2.ZERO();
         this.rotationDegrees = 0;
         this.scale = Vector2.ONE();
+        this.visible = true;
+    }
+
+    show()
+    {
+        this.visible = true;
+
+        for (let i = 0; i < this.children.length; i++)
+        {
+            if(!this.children[i].show) continue;
+            this.children[i].show();
+        }
+    }
+
+    hide()
+    {
+        this.visible = false;
+
+        for (let i = 0; i < this.children.length; i++)
+        {
+            if(!this.children[i].hide) continue;
+            this.children[i].hide();
+        }
+    }
+
+    setVisibility(val)
+    {
+        this.visible = val;
+
+        for (let i = 0; i < this.children.length; i++)
+        {
+            if(!this.children[i].setVisibility) continue;
+            this.children[i].setVisibility(val);
+        }
+    }
+
+    getVisibility()
+    {
+        return this.visible;
     }
 
     draw(delta, db)
     {
+        if (!this.visible) return;
+
         db.push();
         db.translate(this.position.x, this.position.y);
         db.rotate(this.rotationDegrees);

+ 21 - 2
pandora/game_objects/ui_objects/UIObject.js

@@ -116,9 +116,28 @@ class UIObject extends GameObject
     }
 
     // Methods
-    toggleVisibility()
+    show()
     {
-        this.setVisibility(!this.visible);
+        this.visible = true;
+        this.P5Element.show();
+
+        for (let i = 0; i < this.children.length; i++)
+        {
+            if (!this.children[i].show) continue;
+            this.children[i].show();
+        }
+    }
+
+    hide()
+    {
+        this.visible = false;
+        this.P5Element.hide();
+        
+        for (let i = 0; i < this.children.length; i++)
+        {
+            if (!this.children[i].hide) continue;
+            this.children[i].hide();
+        }
     }
 
     addChild(child)

+ 6 - 22
src/sketch.js

@@ -4,18 +4,12 @@ class TestObject extends Object2D
 {
     _setup()
     {
-        this.visible = true;
         this.position = new Vector2(100, 100);
-        this.getChildByIndex(0).connect("buttonPressed", this, "_onSignal");
     }
 
     _onSignal(param1, param2, param3, param4)
     {
-        this.visible = !this.visible;
-        console.log(param1);
-        console.log(param2);
-        console.log(param3);
-        console.log(param4);
+        this.setVisibility(!this.getVisibility())
     }
 
     _update(delta)
@@ -25,7 +19,7 @@ class TestObject extends Object2D
 
     _draw(delta, db)
     {
-        if (this.visible) db.ellipse(0, 0, 50);
+        db.ellipse(0, 0, 50);
     }
 }
 
@@ -34,17 +28,7 @@ class TestObject2 extends Object2D
     _setup()
     {
         this.visible = true;
-        this.position = new Vector2(200, 100);
-        but.connect("buttonPressed", this, "_onSignal");
-    }
-
-    _onSignal(param1, param2, param3, param4)
-    {
-        this.visible = !this.visible;
-        console.log(param1);
-        console.log(param2);
-        console.log(param3);
-        console.log(param4);
+        this.position = new Vector2(100, 0);
     }
 
     _update(delta)
@@ -88,15 +72,15 @@ function preload()
 function setup()
 {
     GameHandler.drawDebugFPS(true);
-    // GameHandler.setRenderMode(RENDER_MODES.WEBGL);
     GameHandler.init();
     textFont(AssetHandler.getP5FontByName("Lato"));
 
     test = new TestObject("myTest");
     but = new TestButton("b1", "Emit signal");
-    test.addChild(but);
+    but.connect("buttonPressed", test, "_onSignal");
+    GameHandler.addRootObject(but);
     GameHandler.addRootObject(test);
-    GameHandler.addRootObject(new TestObject2("myTest2"));
+    test.addChild(new TestObject2("myTest2"));
 }
 
 function draw()