From 4aed7c62853a7b69dd5cc6ed92ca87ac85d6ce81 Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 17 Jul 2022 01:21:33 +0200 Subject: [PATCH] Some refactoring --- .../bclib/client/gui/modmenu/MainScreen.java | 6 +- .../bclib/client/gui/modmenu/TestScreen.java | 6 +- .../ui/layout/components/AbstractStack.java | 71 ++++++++++++++----- .../components/AbstractVanillaComponent.java | 2 +- .../betterx/ui/layout/components/Button.java | 10 ++- .../components/CustomRenderComponent.java | 2 +- .../betterx/ui/layout/components/Empty.java | 3 +- .../ui/layout/components/HorizontalStack.java | 10 +-- .../ui/layout/components/LayoutComponent.java | 41 +++++++---- .../ui/layout/components/MultiLineText.java | 2 +- .../betterx/ui/layout/components/Panel.java | 4 +- .../betterx/ui/layout/components/Range.java | 6 +- .../betterx/ui/layout/components/Text.java | 3 +- .../ui/layout/components/VerticalScroll.java | 10 +-- .../ui/layout/components/VerticalStack.java | 10 +-- .../org/betterx/ui/vanilla/LayoutScreen.java | 13 ++-- .../ui/vanilla/LayoutScreenWithIcon.java | 8 +-- 17 files changed, 124 insertions(+), 83 deletions(-) diff --git a/src/main/java/org/betterx/bclib/client/gui/modmenu/MainScreen.java b/src/main/java/org/betterx/bclib/client/gui/modmenu/MainScreen.java index eb1437b7..d25eaaef 100644 --- a/src/main/java/org/betterx/bclib/client/gui/modmenu/MainScreen.java +++ b/src/main/java/org/betterx/bclib/client/gui/modmenu/MainScreen.java @@ -83,7 +83,7 @@ public class MainScreen extends LayoutScreenWithIcon { @Override protected LayoutComponent initContent() { - VerticalStack content = new VerticalStack(Value.fit(), Value.fit()); + VerticalStack content = new VerticalStack(Value.fit(), Value.fit()).setDebugName("content"); Configs.GENERATOR_CONFIG.getAllOptions() .stream() @@ -101,11 +101,9 @@ public class MainScreen extends LayoutScreenWithIcon { .forEach(o -> addRow(content, Configs.CLIENT_CONFIG, o)); - VerticalStack grid = new VerticalStack(Value.fill(), Value.fill()); + VerticalStack grid = new VerticalStack(Value.fill(), Value.fill()).setDebugName("main grid"); grid.addScrollable(content); grid.addSpacer(8); - HorizontalStack row = grid.addRow(); - row.addFiller(); grid.addButton(Value.fit(), Value.fit(), CommonComponents.GUI_DONE, (button) -> { Configs.CLIENT_CONFIG.saveChanges(); Configs.GENERATOR_CONFIG.saveChanges(); diff --git a/src/main/java/org/betterx/bclib/client/gui/modmenu/TestScreen.java b/src/main/java/org/betterx/bclib/client/gui/modmenu/TestScreen.java index a9dea023..3c553009 100644 --- a/src/main/java/org/betterx/bclib/client/gui/modmenu/TestScreen.java +++ b/src/main/java/org/betterx/bclib/client/gui/modmenu/TestScreen.java @@ -24,7 +24,7 @@ public class TestScreen extends LayoutScreen { } @Override - protected LayoutComponent initContent() { + protected LayoutComponent initContent() { VerticalStack rows = new VerticalStack(Value.fit(), Value.fitOrFill()); rows.addFiller(); @@ -94,8 +94,6 @@ public class TestScreen extends LayoutScreen { Component.literal("test"), (bt) -> { System.out.println("clicked test"); - }, - (bt, pose, x, y) -> { } ).centerHorizontal() ); @@ -105,8 +103,6 @@ public class TestScreen extends LayoutScreen { Component.literal("Hello World"), (bt) -> { System.out.println("clicked hello"); - }, - (bt, pose, x, y) -> { } ).centerHorizontal() ); diff --git a/src/main/java/org/betterx/ui/layout/components/AbstractStack.java b/src/main/java/org/betterx/ui/layout/components/AbstractStack.java index b27b692a..b85326a2 100644 --- a/src/main/java/org/betterx/ui/layout/components/AbstractStack.java +++ b/src/main/java/org/betterx/ui/layout/components/AbstractStack.java @@ -6,6 +6,7 @@ import org.betterx.ui.layout.components.render.NullRenderer; import org.betterx.ui.layout.values.Rectangle; import org.betterx.ui.layout.values.Size; import org.betterx.ui.layout.values.Value; +import org.betterx.ui.vanilla.Slider; import org.betterx.ui.vanilla.VanillaScrollerRenderer; import com.mojang.blaze3d.vertex.PoseStack; @@ -21,8 +22,8 @@ import java.util.List; import org.jetbrains.annotations.Nullable; @Environment(EnvType.CLIENT) -public abstract class AbstractStack> extends LayoutComponent implements RelativeContainerEventHandler { - protected final List> components = new LinkedList<>(); +public abstract class AbstractStack> extends LayoutComponent implements RelativeContainerEventHandler { + protected final List> components = new LinkedList<>(); public AbstractStack(Value width, Value height) { this(width, height, null); @@ -58,12 +59,12 @@ public abstract class AbstractStack c : components) { + for (LayoutComponent c : components) { c.render(poseStack, mouseX, mouseY, deltaTicks, renderBounds, clipRect); } } - public T add(LayoutComponent c) { + public T add(LayoutComponent c) { this.components.add(c); return (T) this; } @@ -144,32 +145,68 @@ public abstract class AbstractStack addScrollable(LayoutComponent content) { + public VerticalScroll addScrollable(LayoutComponent content) { return addScrollable(Value.fill(), Value.fill(), content); } public VerticalScroll addScrollable( Value width, - Value heihght, - LayoutComponent content + Value height, + LayoutComponent content ) { VerticalScroll s = VerticalScroll.create(width, height, content); add(s); return s; } + + public Text addText(Value width, Value height, net.minecraft.network.chat.Component text) { + Text t = new Text(width, height, text); + add(t); + return t; + } + + public MultiLineText addMultilineText(Value width, Value height, net.minecraft.network.chat.Component text) { + MultiLineText t = new MultiLineText(width, height, text); + add(t); + return t; + } + + public Range addRange( + Value width, Value height, + net.minecraft.network.chat.Component titleOrNull, + int minValue, int maxValue, int initialValue, + Slider.SliderValueChanged onChange + ) { + Range r = new Range<>(width, height, titleOrNull, minValue, maxValue, initialValue, onChange); + add(r); + return r; + } + + public Range addRange( + Value width, Value height, + net.minecraft.network.chat.Component titleOrNull, + float minValue, float maxValue, float initialValue, + Slider.SliderValueChanged onChange + ) { + Range r = new Range<>(width, height, titleOrNull, minValue, maxValue, initialValue, onChange); + add(r); + return r; + } + + public Range addRange( + Value width, Value height, + net.minecraft.network.chat.Component titleOrNull, + double minValue, double maxValue, double initialValue, + Slider.SliderValueChanged onChange + ) { + Range r = new Range<>(width, height, titleOrNull, minValue, maxValue, initialValue, onChange); + add(r); + return r; + } } diff --git a/src/main/java/org/betterx/ui/layout/components/AbstractVanillaComponent.java b/src/main/java/org/betterx/ui/layout/components/AbstractVanillaComponent.java index 92445ef0..24a7188f 100644 --- a/src/main/java/org/betterx/ui/layout/components/AbstractVanillaComponent.java +++ b/src/main/java/org/betterx/ui/layout/components/AbstractVanillaComponent.java @@ -4,7 +4,7 @@ import org.betterx.ui.layout.values.Value; import net.minecraft.client.gui.components.AbstractWidget; -public abstract class AbstractVanillaComponent> extends LayoutComponent> { +public abstract class AbstractVanillaComponent> extends LayoutComponent, V> { protected C vanillaComponent; protected final net.minecraft.network.chat.Component component; protected float alpha = 1.0f; diff --git a/src/main/java/org/betterx/ui/layout/components/Button.java b/src/main/java/org/betterx/ui/layout/components/Button.java index 0cecdcd3..6f7c5940 100644 --- a/src/main/java/org/betterx/ui/layout/components/Button.java +++ b/src/main/java/org/betterx/ui/layout/components/Button.java @@ -9,18 +9,22 @@ import net.fabricmc.api.Environment; @Environment(EnvType.CLIENT) public class Button extends AbstractVanillaComponent { final net.minecraft.client.gui.components.Button.OnPress onPress; - final net.minecraft.client.gui.components.Button.OnTooltip onTooltip; + net.minecraft.client.gui.components.Button.OnTooltip onTooltip; public Button( Value width, Value height, net.minecraft.network.chat.Component component, - net.minecraft.client.gui.components.Button.OnPress onPress, - net.minecraft.client.gui.components.Button.OnTooltip onTooltip + net.minecraft.client.gui.components.Button.OnPress onPress ) { super(width, height, new ButtonRenderer(), component); this.onPress = onPress; + this.onTooltip = net.minecraft.client.gui.components.Button.NO_TOOLTIP; + } + + public Button setOnToolTip(net.minecraft.client.gui.components.Button.OnTooltip onTooltip) { this.onTooltip = onTooltip; + return this; } @Override diff --git a/src/main/java/org/betterx/ui/layout/components/CustomRenderComponent.java b/src/main/java/org/betterx/ui/layout/components/CustomRenderComponent.java index d48e8a19..d7a234f3 100644 --- a/src/main/java/org/betterx/ui/layout/components/CustomRenderComponent.java +++ b/src/main/java/org/betterx/ui/layout/components/CustomRenderComponent.java @@ -10,7 +10,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @Environment(EnvType.CLIENT) -public abstract class CustomRenderComponent> extends LayoutComponent> { +public abstract class CustomRenderComponent> extends LayoutComponent, C> { public CustomRenderComponent( Value width, Value height diff --git a/src/main/java/org/betterx/ui/layout/components/Empty.java b/src/main/java/org/betterx/ui/layout/components/Empty.java index e2e512d5..bed8da94 100644 --- a/src/main/java/org/betterx/ui/layout/components/Empty.java +++ b/src/main/java/org/betterx/ui/layout/components/Empty.java @@ -1,12 +1,13 @@ package org.betterx.ui.layout.components; +import org.betterx.ui.layout.components.render.NullRenderer; import org.betterx.ui.layout.values.Value; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @Environment(EnvType.CLIENT) -public class Empty extends LayoutComponent { +public class Empty extends LayoutComponent { public Empty( Value width, Value height diff --git a/src/main/java/org/betterx/ui/layout/components/HorizontalStack.java b/src/main/java/org/betterx/ui/layout/components/HorizontalStack.java index 8fb141ee..cc4d5030 100644 --- a/src/main/java/org/betterx/ui/layout/components/HorizontalStack.java +++ b/src/main/java/org/betterx/ui/layout/components/HorizontalStack.java @@ -22,7 +22,7 @@ public class HorizontalStack extends AbstractStack c : components) { + for (LayoutComponent c : components) { c.updateContainerWidth(c.width.calculatedSize()); } @@ -33,7 +33,7 @@ public class HorizontalStack extends AbstractStack c.height.calculateOrFill(myHeight)); - for (LayoutComponent c : components) { + for (LayoutComponent c : components) { c.updateContainerHeight(c.height.calculatedSize()); } return myHeight; @@ -45,7 +45,7 @@ public class HorizontalStack extends AbstractStack c : components) { + for (LayoutComponent c : components) { int delta = relativeBounds.height - c.height.calculatedSize(); if (c.hAlign == Alignment.MIN) delta = 0; else if (c.hAlign == Alignment.CENTER) delta /= 2; @@ -67,11 +67,11 @@ public class HorizontalStack extends AbstractStack c.height.calculateFixed()).reduce(0, Integer::max); } - public static HorizontalStack centered(LayoutComponent c) { + public static HorizontalStack centered(LayoutComponent c) { return new HorizontalStack(Value.fill(), Value.fill()).addFiller().add(c).addFiller(); } - public static HorizontalStack bottom(LayoutComponent c) { + public static HorizontalStack bottom(LayoutComponent c) { return new HorizontalStack(Value.fill(), Value.fill()).add(c).addFiller(); } diff --git a/src/main/java/org/betterx/ui/layout/components/LayoutComponent.java b/src/main/java/org/betterx/ui/layout/components/LayoutComponent.java index 1d63c761..7ddaaa45 100644 --- a/src/main/java/org/betterx/ui/layout/components/LayoutComponent.java +++ b/src/main/java/org/betterx/ui/layout/components/LayoutComponent.java @@ -14,10 +14,11 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @Environment(EnvType.CLIENT) -public abstract class LayoutComponent implements ComponentWithBounds, GuiEventListener { +public abstract class LayoutComponent> implements ComponentWithBounds, GuiEventListener { protected final R renderer; protected final Value width; protected final Value height; + protected String debugName; protected Rectangle relativeBounds; protected Alignment vAlign = Alignment.MIN; protected Alignment hAlign = Alignment.MIN; @@ -94,7 +95,8 @@ public abstract class LayoutComponent implements Co Rectangle clip = r.intersect(clipRect); poseStack.pushPose(); poseStack.translate(relativeBounds.left, relativeBounds.top, 0); - if (r.overlaps(clip)) { + //if (r.overlaps(clip)) + { renderInBounds(poseStack, mouseX - relativeBounds.left, mouseY - relativeBounds.top, deltaTicks, r, clip); } poseStack.popPose(); @@ -117,36 +119,45 @@ public abstract class LayoutComponent implements Co @Override public String toString() { - return super.toString() + "(" + relativeBounds + ", " + width.calculatedSize() + "x" + height.calculatedSize() + ")"; + return super.toString() + "(" + + (debugName == null ? "" : debugName + " - ") + + relativeBounds + ", " + + width.calculatedSize() + "x" + height.calculatedSize() + + ")"; } - public LayoutComponent alignTop() { + public L alignTop() { vAlign = Alignment.MIN; - return this; + return (L) this; } - public LayoutComponent alignBottom() { + public L alignBottom() { vAlign = Alignment.MAX; - return this; + return (L) this; } - public LayoutComponent centerVertical() { + public L centerVertical() { vAlign = Alignment.CENTER; - return this; + return (L) this; } - public LayoutComponent alignLeft() { + public L alignLeft() { hAlign = Alignment.MIN; - return this; + return (L) this; } - public LayoutComponent alignRight() { + public L alignRight() { hAlign = Alignment.MAX; - return this; + return (L) this; } - public LayoutComponent centerHorizontal() { + public L centerHorizontal() { hAlign = Alignment.CENTER; - return this; + return (L) this; + } + + public L setDebugName(String d) { + debugName = d; + return (L) this; } } diff --git a/src/main/java/org/betterx/ui/layout/components/MultiLineText.java b/src/main/java/org/betterx/ui/layout/components/MultiLineText.java index e1cf832c..ea98d178 100644 --- a/src/main/java/org/betterx/ui/layout/components/MultiLineText.java +++ b/src/main/java/org/betterx/ui/layout/components/MultiLineText.java @@ -10,7 +10,7 @@ import org.betterx.ui.layout.values.Value; import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.client.gui.components.MultiLineLabel; -public class MultiLineText extends LayoutComponent { +public class MultiLineText extends LayoutComponent { final net.minecraft.network.chat.Component text; int color = ColorUtil.DEFAULT_TEXT; protected MultiLineLabel multiLineLabel; diff --git a/src/main/java/org/betterx/ui/layout/components/Panel.java b/src/main/java/org/betterx/ui/layout/components/Panel.java index 614eb0f7..21b3b10c 100644 --- a/src/main/java/org/betterx/ui/layout/components/Panel.java +++ b/src/main/java/org/betterx/ui/layout/components/Panel.java @@ -18,7 +18,7 @@ import org.jetbrains.annotations.Nullable; @Environment(EnvType.CLIENT) public class Panel implements ComponentWithBounds, RelativeContainerEventHandler, NarratableEntry, Widget { - protected LayoutComponent child; + protected LayoutComponent child; List listeners = List.of(); public final Rectangle bounds; @@ -30,7 +30,7 @@ public class Panel implements ComponentWithBounds, RelativeContainerEventHandler bounds = new Rectangle(left, top, width, height); } - public void setChild(LayoutComponent c) { + public void setChild(LayoutComponent c) { this.child = c; listeners = List.of(c); } diff --git a/src/main/java/org/betterx/ui/layout/components/Range.java b/src/main/java/org/betterx/ui/layout/components/Range.java index ab14116c..9a1892ff 100644 --- a/src/main/java/org/betterx/ui/layout/components/Range.java +++ b/src/main/java/org/betterx/ui/layout/components/Range.java @@ -40,11 +40,7 @@ public class Range extends AbstractVanillaComponent, N initialValue, Slider.SliderValueChanged onChange ) { - super(width, height, new RangeRenderer<>(), null); - this.onChange = onChange; - this.minValue = minValue; - this.maxValue = maxValue; - this.initialValue = initialValue; + this(width, height, null, minValue, maxValue, initialValue, onChange); } @Override diff --git a/src/main/java/org/betterx/ui/layout/components/Text.java b/src/main/java/org/betterx/ui/layout/components/Text.java index 53d05da6..b22b4abc 100644 --- a/src/main/java/org/betterx/ui/layout/components/Text.java +++ b/src/main/java/org/betterx/ui/layout/components/Text.java @@ -10,7 +10,7 @@ import org.betterx.ui.layout.values.Value; import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.client.gui.GuiComponent; -public class Text extends LayoutComponent { +public class Text extends LayoutComponent { final net.minecraft.network.chat.Component text; int color = ColorUtil.DEFAULT_TEXT; @@ -18,7 +18,6 @@ public class Text extends LayoutComponent { Value width, Value height, net.minecraft.network.chat.Component text - ) { super(width, height, new TextRenderer()); vAlign = Alignment.CENTER; diff --git a/src/main/java/org/betterx/ui/layout/components/VerticalScroll.java b/src/main/java/org/betterx/ui/layout/components/VerticalScroll.java index deac41d6..d6c3da28 100644 --- a/src/main/java/org/betterx/ui/layout/components/VerticalScroll.java +++ b/src/main/java/org/betterx/ui/layout/components/VerticalScroll.java @@ -14,8 +14,8 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @Environment(EnvType.CLIENT) -public class VerticalScroll extends LayoutComponent { - protected LayoutComponent child; +public class VerticalScroll extends LayoutComponent> { + protected LayoutComponent child; protected final RS scrollerRenderer; protected int dist; @@ -33,14 +33,14 @@ public class VerticalScroll create(LayoutComponent c) { + public static VerticalScroll create(LayoutComponent c) { return create(Value.relative(1), Value.relative(1), c); } public static VerticalScroll create( Value width, Value height, - LayoutComponent c + LayoutComponent c ) { VerticalScroll res = new VerticalScroll<>( width, @@ -52,7 +52,7 @@ public class VerticalScroll c) { + public void setChild(LayoutComponent c) { this.child = c; } diff --git a/src/main/java/org/betterx/ui/layout/components/VerticalStack.java b/src/main/java/org/betterx/ui/layout/components/VerticalStack.java index 206a5236..995e1765 100644 --- a/src/main/java/org/betterx/ui/layout/components/VerticalStack.java +++ b/src/main/java/org/betterx/ui/layout/components/VerticalStack.java @@ -19,7 +19,7 @@ public class VerticalStack extends AbstractStack im protected int updateContainerWidth(int containerWidth) { int myWidth = width.calculateOrFill(containerWidth); components.stream().forEach(c -> c.width.calculateOrFill(myWidth)); - for (LayoutComponent c : components) { + for (LayoutComponent c : components) { c.updateContainerWidth(c.width.calculatedSize()); } return myWidth; @@ -33,7 +33,7 @@ public class VerticalStack extends AbstractStack im int freeHeight = Math.max(0, myHeight - fixedHeight); fillHeight(myHeight, freeHeight); - for (LayoutComponent c : components) { + for (LayoutComponent c : components) { c.updateContainerHeight(c.height.calculatedSize()); } @@ -45,7 +45,7 @@ public class VerticalStack extends AbstractStack im super.setRelativeBounds(left, top); int offset = 0; - for (LayoutComponent c : components) { + for (LayoutComponent c : components) { int delta = relativeBounds.width - c.width.calculatedSize(); if (c.hAlign == Alignment.MIN) delta = 0; else if (c.hAlign == Alignment.CENTER) delta /= 2; @@ -67,11 +67,11 @@ public class VerticalStack extends AbstractStack im return (int) (fixedHeight / (1 - percentage)); } - public static VerticalStack centered(LayoutComponent c) { + public static VerticalStack centered(LayoutComponent c) { return new VerticalStack(Value.relative(1), Value.relative(1)).addFiller().add(c).addFiller(); } - public static VerticalStack bottom(LayoutComponent c) { + public static VerticalStack bottom(LayoutComponent c) { return new VerticalStack(Value.relative(1), Value.relative(1)).add(c).addFiller(); } diff --git a/src/main/java/org/betterx/ui/vanilla/LayoutScreen.java b/src/main/java/org/betterx/ui/vanilla/LayoutScreen.java index 53cd651b..2f6bb397 100644 --- a/src/main/java/org/betterx/ui/vanilla/LayoutScreen.java +++ b/src/main/java/org/betterx/ui/vanilla/LayoutScreen.java @@ -46,7 +46,7 @@ public abstract class LayoutScreen extends Screen { @Nullable public final Screen parent; - protected abstract LayoutComponent initContent(); + protected abstract LayoutComponent initContent(); @Override protected final void init() { @@ -58,14 +58,13 @@ public abstract class LayoutScreen extends Screen { addRenderableWidget(main); } - protected LayoutComponent buildTitle() { - - var text = new Text(Value.fill(), Value.fit(), title).centerHorizontal(); + protected LayoutComponent buildTitle() { + var text = new Text(Value.fill(), Value.fit(), title).centerHorizontal().setDebugName("title"); return text; } - protected LayoutComponent addTitle(LayoutComponent content) { - VerticalStack rows = new VerticalStack(Value.fill(), Value.fill()); + protected LayoutComponent addTitle(LayoutComponent content) { + VerticalStack rows = new VerticalStack(Value.fill(), Value.fill()).setDebugName("title stack"); if (topPadding > 0) rows.addSpacer(topPadding); rows.add(buildTitle()); @@ -75,7 +74,7 @@ public abstract class LayoutScreen extends Screen { if (sidePadding <= 0) return rows; - HorizontalStack cols = new HorizontalStack(Value.fill(), Value.fill()); + HorizontalStack cols = new HorizontalStack(Value.fill(), Value.fill()).setDebugName("padded side"); cols.addSpacer(sidePadding); cols.add(rows); cols.addSpacer(sidePadding); diff --git a/src/main/java/org/betterx/ui/vanilla/LayoutScreenWithIcon.java b/src/main/java/org/betterx/ui/vanilla/LayoutScreenWithIcon.java index 471d80be..7c5ab813 100644 --- a/src/main/java/org/betterx/ui/vanilla/LayoutScreenWithIcon.java +++ b/src/main/java/org/betterx/ui/vanilla/LayoutScreenWithIcon.java @@ -39,11 +39,11 @@ public abstract class LayoutScreenWithIcon extends LayoutScreen { } @Override - protected LayoutComponent buildTitle() { - LayoutComponent title = super.buildTitle(); - HorizontalStack row = new HorizontalStack(Value.fill(), Value.fit()); + protected LayoutComponent buildTitle() { + LayoutComponent title = super.buildTitle(); + HorizontalStack row = new HorizontalStack(Value.fill(), Value.fit()).setDebugName("title bar"); row.addFiller(); - row.addIcon(icon, Size.of(512)); + row.addIcon(icon, Size.of(512)).setDebugName("icon"); row.addSpacer(4); row.add(title); row.addFiller();