From bf2ab4d0454a067cd34ef4268eb41698f3e01706 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 3 Oct 2024 14:37:37 -0400 Subject: [PATCH] Dynamic spacer class --- arc-core/src/arc/scene/ui/layout/Spacer.java | 38 ++++++++++++++++++++ arc-core/src/arc/scene/ui/layout/Table.java | 12 +++++++ 2 files changed, 50 insertions(+) create mode 100644 arc-core/src/arc/scene/ui/layout/Spacer.java diff --git a/arc-core/src/arc/scene/ui/layout/Spacer.java b/arc-core/src/arc/scene/ui/layout/Spacer.java new file mode 100644 index 00000000..a2c24c1a --- /dev/null +++ b/arc-core/src/arc/scene/ui/layout/Spacer.java @@ -0,0 +1,38 @@ +package arc.scene.ui.layout; + +import arc.func.*; +import arc.scene.*; + +public class Spacer extends Element{ + Floatp widthFunc, heightFunc; + + public Spacer(Floatp widthFunc, Floatp heightFunc){ + this.widthFunc = widthFunc; + this.heightFunc = heightFunc; + + width = Scl.scl(widthFunc.get()); + height = Scl.scl(heightFunc.get()); + } + + @Override + public void act(float delta){ + super.act(delta); + + float w = Scl.scl(widthFunc.get()), h = Scl.scl(heightFunc.get()); + if(w != width || h != height){ + width = w; + height = h; + invalidateHierarchy(); + } + } + + @Override + public float getPrefHeight(){ + return heightFunc.get(); + } + + @Override + public float getPrefWidth(){ + return widthFunc.get(); + } +} diff --git a/arc-core/src/arc/scene/ui/layout/Table.java b/arc-core/src/arc/scene/ui/layout/Table.java index d794218d..c867cc82 100644 --- a/arc-core/src/arc/scene/ui/layout/Table.java +++ b/arc-core/src/arc/scene/ui/layout/Table.java @@ -366,6 +366,18 @@ public Cell stack(Element... elements){ return add(stack); } + public Cell spacer(Floatp width, Floatp height){ + return add(new Spacer(width, height)); + } + + public Cell spacerX(Floatp width){ + return add(new Spacer(width, () -> 0f)); + } + + public Cell spacerY(Floatp height){ + return add(new Spacer(() -> 0f, height)); + } + public Cell image(Prov reg){ return add(new Image(reg.get())).update(i -> { ((TextureRegionDrawable)i.getDrawable()).setRegion(reg.get());