From 7122942eead07faec0770fe3f32132b6730d1f69 Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Tue, 16 Aug 2022 15:42:25 +0300 Subject: Fix #169 --- .../cottonmc/cotton/gui/widget/WListPanel.java | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/main/java') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java index 5440bd5..39a7a26 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java @@ -4,8 +4,10 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; +import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.widget.data.Axis; import io.github.cottonmc.cotton.gui.widget.data.InputResult; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.HashMap; @@ -58,6 +60,11 @@ public class WListPanel extends WClippedPanel { protected WScrollBar scrollBar = new WScrollBar(Axis.VERTICAL); private int lastScroll = -1; + /** + * The widgets whose host hasn't been set yet. + */ + private final List requiresHost = new ArrayList<>(); + /** * Constructs a list panel. * @@ -104,10 +111,31 @@ public class WListPanel extends WClippedPanel { // setHost instead of validate since we cannot have independent validations // TODO: System for independently validating widgets? child.setHost(host); + } else { + requiresHost.add(child); } return child; } + @Override + public void validate(GuiDescription c) { + super.validate(c); + setRequiredHosts(c); + } + + @Override + public void setHost(@Nullable GuiDescription host) { + super.setHost(host); + if (host != null) setRequiredHosts(host); + } + + private void setRequiredHosts(GuiDescription host) { + for (W widget : requiresHost) { + widget.setHost(host); + } + requiresHost.clear(); + } + @Override public void layout() { -- cgit