From 51e0a88001ee000148f086a5360067eeb3605a4d Mon Sep 17 00:00:00 2001 From: Liyan Zhao Date: Wed, 13 Sep 2023 15:19:47 +0800 Subject: [PATCH] feat: Updatable --- .../owo/ui/container/Containers.java | 25 ++++++++++++++++--- .../owo/ui/container/FlowLayout.java | 19 ++++++++++++-- .../owo/ui/container/GridLayout.java | 21 +++++++++++++--- .../owo/ui/util/ElementConfigurer.java | 5 ++++ .../io/wispforest/owo/ui/util/Updatable.java | 5 ++++ 5 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 src/main/java/io/wispforest/owo/ui/util/ElementConfigurer.java create mode 100644 src/main/java/io/wispforest/owo/ui/util/Updatable.java diff --git a/src/main/java/io/wispforest/owo/ui/container/Containers.java b/src/main/java/io/wispforest/owo/ui/container/Containers.java index 9ece7286..5dbda3a2 100644 --- a/src/main/java/io/wispforest/owo/ui/container/Containers.java +++ b/src/main/java/io/wispforest/owo/ui/container/Containers.java @@ -2,6 +2,7 @@ import io.wispforest.owo.ui.core.Component; import io.wispforest.owo.ui.core.Sizing; +import io.wispforest.owo.ui.util.ElementConfigurer; import net.minecraft.text.Text; public final class Containers { @@ -13,19 +14,35 @@ private Containers() {} // ------ public static GridLayout grid(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns) { - return new GridLayout(horizontalSizing, verticalSizing, rows, columns); + return new GridLayout(horizontalSizing, verticalSizing, rows, columns, null); + } + + public static GridLayout grid(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns, ElementConfigurer configurer) { + return new GridLayout(horizontalSizing, verticalSizing, rows, columns, configurer); } public static FlowLayout verticalFlow(Sizing horizontalSizing, Sizing verticalSizing) { - return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.VERTICAL); + return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.VERTICAL, null); } public static FlowLayout horizontalFlow(Sizing horizontalSizing, Sizing verticalSizing) { - return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.HORIZONTAL); + return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.HORIZONTAL, null); } public static FlowLayout ltrTextFlow(Sizing horizontalSizing, Sizing verticalSizing) { - return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.LTR_TEXT); + return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.LTR_TEXT, null); + } + + public static FlowLayout verticalFlow(Sizing horizontalSizing, Sizing verticalSizing, ElementConfigurer configurer) { + return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.VERTICAL, configurer); + } + + public static FlowLayout horizontalFlow(Sizing horizontalSizing, Sizing verticalSizing, ElementConfigurer configurer) { + return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.HORIZONTAL, configurer); + } + + public static FlowLayout ltrTextFlow(Sizing horizontalSizing, Sizing verticalSizing, ElementConfigurer configurer) { + return new FlowLayout(horizontalSizing, verticalSizing, FlowLayout.Algorithm.LTR_TEXT, configurer); } public static StackLayout stack(Sizing horizontalSizing, Sizing verticalSizing) { diff --git a/src/main/java/io/wispforest/owo/ui/container/FlowLayout.java b/src/main/java/io/wispforest/owo/ui/container/FlowLayout.java index 42d21ff4..e606f0ba 100644 --- a/src/main/java/io/wispforest/owo/ui/container/FlowLayout.java +++ b/src/main/java/io/wispforest/owo/ui/container/FlowLayout.java @@ -4,7 +4,9 @@ import io.wispforest.owo.ui.core.*; import io.wispforest.owo.ui.parsing.UIModel; import io.wispforest.owo.ui.parsing.UIParsing; +import io.wispforest.owo.ui.util.ElementConfigurer; import io.wispforest.owo.ui.util.MountingHelper; +import io.wispforest.owo.ui.util.Updatable; import io.wispforest.owo.util.Observable; import org.apache.commons.lang3.mutable.MutableInt; import org.w3c.dom.Element; @@ -12,7 +14,7 @@ import java.util.*; -public class FlowLayout extends BaseParentComponent { +public class FlowLayout extends BaseParentComponent implements Updatable { protected final List children = new ArrayList<>(); protected final List childrenView = Collections.unmodifiableList(this.children); @@ -20,14 +22,27 @@ public class FlowLayout extends BaseParentComponent { protected Size contentSize = Size.zero(); protected Observable gap = Observable.of(0); + protected ElementConfigurer configurer; - protected FlowLayout(Sizing horizontalSizing, Sizing verticalSizing, Algorithm algorithm) { + protected FlowLayout(Sizing horizontalSizing, Sizing verticalSizing, Algorithm algorithm, ElementConfigurer configurer) { super(horizontalSizing, verticalSizing); this.algorithm = algorithm; this.gap.observe(integer -> this.updateLayout()); + this.configurer = configurer; + update(); } + @Override + public void update() { + if (configurer != null) { + children.clear(); + configurer.configure(this); + updateLayout(); + } + } + + @Override protected int determineHorizontalContentSize(Sizing sizing) { return this.contentSize.width() + this.padding.get().horizontal(); diff --git a/src/main/java/io/wispforest/owo/ui/container/GridLayout.java b/src/main/java/io/wispforest/owo/ui/container/GridLayout.java index 6c9ff769..e0c5132b 100644 --- a/src/main/java/io/wispforest/owo/ui/container/GridLayout.java +++ b/src/main/java/io/wispforest/owo/ui/container/GridLayout.java @@ -8,6 +8,8 @@ import io.wispforest.owo.ui.parsing.UIModel; import io.wispforest.owo.ui.parsing.UIModelParsingException; import io.wispforest.owo.ui.parsing.UIParsing; +import io.wispforest.owo.ui.util.ElementConfigurer; +import io.wispforest.owo.ui.util.Updatable; import org.apache.commons.lang3.mutable.MutableInt; import org.jetbrains.annotations.Nullable; import org.w3c.dom.Element; @@ -15,23 +17,34 @@ import java.util.*; -public class GridLayout extends BaseParentComponent { +public class GridLayout extends BaseParentComponent implements Updatable { protected final int rows, columns; - protected final Component[] children; + protected Component[] children; protected final List nonNullChildren = new ArrayList<>(); protected final List nonNullChildrenView = Collections.unmodifiableList(this.nonNullChildren); + protected ElementConfigurer configurer; protected Size contentSize = Size.zero(); - protected GridLayout(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns) { + protected GridLayout(Sizing horizontalSizing, Sizing verticalSizing, int rows, int columns, ElementConfigurer configurer) { super(horizontalSizing, verticalSizing); this.rows = rows; this.columns = columns; + this.configurer = configurer; this.children = new Component[rows * columns]; + update(); + } + + @Override + public void update() { + if (configurer != null) { + children = new Component[rows * columns]; + configurer.configure(this); + } } @Override @@ -193,6 +206,6 @@ public static GridLayout parse(Element element) { int rows = UIParsing.parseUnsignedInt(element.getAttributeNode("rows")); int columns = UIParsing.parseUnsignedInt(element.getAttributeNode("columns")); - return new GridLayout(Sizing.content(), Sizing.content(), rows, columns); + return new GridLayout(Sizing.content(), Sizing.content(), rows, columns, null); } } diff --git a/src/main/java/io/wispforest/owo/ui/util/ElementConfigurer.java b/src/main/java/io/wispforest/owo/ui/util/ElementConfigurer.java new file mode 100644 index 00000000..51f73426 --- /dev/null +++ b/src/main/java/io/wispforest/owo/ui/util/ElementConfigurer.java @@ -0,0 +1,5 @@ +package io.wispforest.owo.ui.util; + +public interface ElementConfigurer { + void configure(T element); +} diff --git a/src/main/java/io/wispforest/owo/ui/util/Updatable.java b/src/main/java/io/wispforest/owo/ui/util/Updatable.java new file mode 100644 index 00000000..2f3a5115 --- /dev/null +++ b/src/main/java/io/wispforest/owo/ui/util/Updatable.java @@ -0,0 +1,5 @@ +package io.wispforest.owo.ui.util; + +public interface Updatable { + void update(); +}