Some refactoring

This commit is contained in:
Frank 2022-07-17 01:21:33 +02:00
parent ef34bc9981
commit 4aed7c6285
17 changed files with 124 additions and 83 deletions

View file

@ -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();

View file

@ -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()
);

View file

@ -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<R extends ComponentRenderer, T extends AbstractStack<R, T>> extends LayoutComponent<R> implements RelativeContainerEventHandler {
protected final List<LayoutComponent<?>> components = new LinkedList<>();
public abstract class AbstractStack<R extends ComponentRenderer, T extends AbstractStack<R, T>> extends LayoutComponent<R, T> implements RelativeContainerEventHandler {
protected final List<LayoutComponent<?, ?>> components = new LinkedList<>();
public AbstractStack(Value width, Value height) {
this(width, height, null);
@ -58,12 +59,12 @@ public abstract class AbstractStack<R extends ComponentRenderer, T extends Abstr
Rectangle clipRect
) {
super.renderInBounds(poseStack, mouseX, mouseY, deltaTicks, renderBounds, clipRect);
for (LayoutComponent<?> 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<R extends ComponentRenderer, T extends Abstr
Component component,
net.minecraft.client.gui.components.Button.OnPress onPress
) {
return addButton(width, height, component, onPress, net.minecraft.client.gui.components.Button.NO_TOOLTIP);
}
public Button addButton(
Value width, Value height,
Component component,
net.minecraft.client.gui.components.Button.OnPress onPress,
net.minecraft.client.gui.components.Button.OnTooltip onTooltip
) {
Button b = new Button(width, height, component, onPress, onTooltip);
Button b = new Button(width, height, component, onPress);
add(b);
return b;
}
public VerticalScroll<NullRenderer, VanillaScrollerRenderer> addScrollable(LayoutComponent content) {
public VerticalScroll<NullRenderer, VanillaScrollerRenderer> addScrollable(LayoutComponent<?, ?> content) {
return addScrollable(Value.fill(), Value.fill(), content);
}
public VerticalScroll<NullRenderer, VanillaScrollerRenderer> addScrollable(
Value width,
Value heihght,
LayoutComponent content
Value height,
LayoutComponent<?, ?> content
) {
VerticalScroll<NullRenderer, VanillaScrollerRenderer> 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<Integer> addRange(
Value width, Value height,
net.minecraft.network.chat.Component titleOrNull,
int minValue, int maxValue, int initialValue,
Slider.SliderValueChanged<Integer> onChange
) {
Range<Integer> r = new Range<>(width, height, titleOrNull, minValue, maxValue, initialValue, onChange);
add(r);
return r;
}
public Range<Float> addRange(
Value width, Value height,
net.minecraft.network.chat.Component titleOrNull,
float minValue, float maxValue, float initialValue,
Slider.SliderValueChanged<Float> onChange
) {
Range<Float> r = new Range<>(width, height, titleOrNull, minValue, maxValue, initialValue, onChange);
add(r);
return r;
}
public Range<Double> addRange(
Value width, Value height,
net.minecraft.network.chat.Component titleOrNull,
double minValue, double maxValue, double initialValue,
Slider.SliderValueChanged<Double> onChange
) {
Range<Double> r = new Range<>(width, height, titleOrNull, minValue, maxValue, initialValue, onChange);
add(r);
return r;
}
}

View file

@ -4,7 +4,7 @@ import org.betterx.ui.layout.values.Value;
import net.minecraft.client.gui.components.AbstractWidget;
public abstract class AbstractVanillaComponent<C extends AbstractWidget, V extends AbstractVanillaComponent<C, V>> extends LayoutComponent<AbstractVanillaComponentRenderer<C, V>> {
public abstract class AbstractVanillaComponent<C extends AbstractWidget, V extends AbstractVanillaComponent<C, V>> extends LayoutComponent<AbstractVanillaComponentRenderer<C, V>, V> {
protected C vanillaComponent;
protected final net.minecraft.network.chat.Component component;
protected float alpha = 1.0f;

View file

@ -9,18 +9,22 @@ import net.fabricmc.api.Environment;
@Environment(EnvType.CLIENT)
public class Button extends AbstractVanillaComponent<net.minecraft.client.gui.components.Button, Button> {
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

View file

@ -10,7 +10,7 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@Environment(EnvType.CLIENT)
public abstract class CustomRenderComponent<C extends CustomRenderComponent<C>> extends LayoutComponent<CustomRenderComponent.CustomRenderRenderer<C>> {
public abstract class CustomRenderComponent<C extends CustomRenderComponent<C>> extends LayoutComponent<CustomRenderComponent.CustomRenderRenderer<C>, C> {
public CustomRenderComponent(
Value width,
Value height

View file

@ -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<NullRenderer, Empty> {
public Empty(
Value width,
Value height

View file

@ -22,7 +22,7 @@ public class HorizontalStack extends AbstractStack<NullRenderer, HorizontalStack
int freeWidth = Math.max(0, myWidth - fixedWidth);
fillWidth(myWidth, freeWidth);
for (LayoutComponent<?> c : components) {
for (LayoutComponent<?, ?> c : components) {
c.updateContainerWidth(c.width.calculatedSize());
}
@ -33,7 +33,7 @@ public class HorizontalStack extends AbstractStack<NullRenderer, HorizontalStack
protected int updateContainerHeight(int containerHeight) {
int myHeight = height.calculateOrFill(containerHeight);
components.stream().forEach(c -> 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<NullRenderer, HorizontalStack
super.setRelativeBounds(left, top);
int offset = 0;
for (LayoutComponent<?> 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<NullRenderer, HorizontalStack
return components.stream().map(c -> 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();
}

View file

@ -14,10 +14,11 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@Environment(EnvType.CLIENT)
public abstract class LayoutComponent<R extends ComponentRenderer> implements ComponentWithBounds, GuiEventListener {
public abstract class LayoutComponent<R extends ComponentRenderer, L extends LayoutComponent<R, L>> 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<R extends ComponentRenderer> 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<R extends ComponentRenderer> 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<R> alignTop() {
public L alignTop() {
vAlign = Alignment.MIN;
return this;
return (L) this;
}
public LayoutComponent<R> alignBottom() {
public L alignBottom() {
vAlign = Alignment.MAX;
return this;
return (L) this;
}
public LayoutComponent<R> centerVertical() {
public L centerVertical() {
vAlign = Alignment.CENTER;
return this;
return (L) this;
}
public LayoutComponent<R> alignLeft() {
public L alignLeft() {
hAlign = Alignment.MIN;
return this;
return (L) this;
}
public LayoutComponent<R> alignRight() {
public L alignRight() {
hAlign = Alignment.MAX;
return this;
return (L) this;
}
public LayoutComponent<R> centerHorizontal() {
public L centerHorizontal() {
hAlign = Alignment.CENTER;
return this;
return (L) this;
}
public L setDebugName(String d) {
debugName = d;
return (L) this;
}
}

View file

@ -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<MultiLineText.MultiLineTextRenderer> {
public class MultiLineText extends LayoutComponent<MultiLineText.MultiLineTextRenderer, MultiLineText> {
final net.minecraft.network.chat.Component text;
int color = ColorUtil.DEFAULT_TEXT;
protected MultiLineLabel multiLineLabel;

View file

@ -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<? extends GuiEventListener> 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);
}

View file

@ -40,11 +40,7 @@ public class Range<N extends Number> extends AbstractVanillaComponent<Slider<N>,
N initialValue,
Slider.SliderValueChanged<N> 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

View file

@ -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<Text.TextRenderer> {
public class Text extends LayoutComponent<Text.TextRenderer, Text> {
final net.minecraft.network.chat.Component text;
int color = ColorUtil.DEFAULT_TEXT;
@ -18,7 +18,6 @@ public class Text extends LayoutComponent<Text.TextRenderer> {
Value width,
Value height,
net.minecraft.network.chat.Component text
) {
super(width, height, new TextRenderer());
vAlign = Alignment.CENTER;

View file

@ -14,8 +14,8 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@Environment(EnvType.CLIENT)
public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRenderer> extends LayoutComponent<R> {
protected LayoutComponent<?> child;
public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRenderer> extends LayoutComponent<R, VerticalScroll<R, RS>> {
protected LayoutComponent<?, ?> child;
protected final RS scrollerRenderer;
protected int dist;
@ -33,14 +33,14 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
this.scrollerRenderer = scrollerRenderer;
}
public static VerticalScroll<NullRenderer, VanillaScrollerRenderer> create(LayoutComponent<?> c) {
public static VerticalScroll<NullRenderer, VanillaScrollerRenderer> create(LayoutComponent<?, ?> c) {
return create(Value.relative(1), Value.relative(1), c);
}
public static VerticalScroll<NullRenderer, VanillaScrollerRenderer> create(
Value width,
Value height,
LayoutComponent<?> c
LayoutComponent<?, ?> c
) {
VerticalScroll<NullRenderer, VanillaScrollerRenderer> res = new VerticalScroll<>(
width,
@ -52,7 +52,7 @@ public class VerticalScroll<R extends ComponentRenderer, RS extends ScrollerRend
return res;
}
public void setChild(LayoutComponent<?> c) {
public void setChild(LayoutComponent<?, ?> c) {
this.child = c;
}

View file

@ -19,7 +19,7 @@ public class VerticalStack extends AbstractStack<NullRenderer, VerticalStack> 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<NullRenderer, VerticalStack> 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<NullRenderer, VerticalStack> 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<NullRenderer, VerticalStack> 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();
}

View file

@ -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);

View file

@ -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();