[Changes] Converted Screens to new Layout Engine

This commit is contained in:
Frank 2022-07-21 22:42:42 +02:00
parent 96bfdc5afa
commit 2d997c53ce
31 changed files with 475 additions and 375 deletions

View file

@ -42,7 +42,7 @@ public class ColorUtil {
public static final int LIGHT_PURPLE = ChatFormatting.LIGHT_PURPLE.getColor() | 0xFF000000;
public static final int YELLOW = ChatFormatting.YELLOW.getColor() | 0xFF000000;
public static final int WHITE = ChatFormatting.WHITE.getColor() | 0xFF000000;
public static final int DEFAULT_TEXT = 0xFFA0A0A0;
public static final int DEFAULT_TEXT = WHITE;
private static final float[] FLOAT_BUFFER = new float[4];
private static final int ALPHA = 255 << 24;

View file

@ -29,6 +29,12 @@ public abstract class LayoutComponent<R extends ComponentRenderer, L extends Lay
this.renderer = renderer;
}
public void reCalculateLayout() {
updateContainerWidth(relativeBounds.width);
updateContainerHeight(relativeBounds.height);
setRelativeBounds(relativeBounds.left, relativeBounds.top);
}
protected int updateContainerWidth(int containerWidth) {
return width.setCalculatedSize(containerWidth);
}

View file

@ -9,11 +9,13 @@ import org.betterx.ui.layout.values.Value;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.components.MultiLineLabel;
import net.minecraft.network.chat.Component;
public class MultiLineText extends LayoutComponent<MultiLineText.MultiLineTextRenderer, MultiLineText> {
final net.minecraft.network.chat.Component text;
net.minecraft.network.chat.Component text;
int color = ColorUtil.DEFAULT_TEXT;
protected MultiLineLabel multiLineLabel;
int bufferedContentWidth = 0;
public MultiLineText(
Value width,
@ -23,6 +25,7 @@ public class MultiLineText extends LayoutComponent<MultiLineText.MultiLineTextRe
super(width, height, new MultiLineTextRenderer());
renderer.linkedComponent = this;
this.text = text;
updatedContentWidth();
}
public MultiLineText setColor(int cl) {
@ -30,6 +33,17 @@ public class MultiLineText extends LayoutComponent<MultiLineText.MultiLineTextRe
return this;
}
public MultiLineText setText(Component text) {
this.text = text;
this.updatedContentWidth();
if (multiLineLabel != null) {
multiLineLabel = createVanillaComponent();
}
return this;
}
protected MultiLineLabel createVanillaComponent() {
return MultiLineLabel.create(
renderer.getFont(),
@ -38,6 +52,19 @@ public class MultiLineText extends LayoutComponent<MultiLineText.MultiLineTextRe
);
}
protected void updatedContentWidth() {
String[] lines = text.getString().split("\n");
if (lines.length == 0) bufferedContentWidth = 0;
else {
String line = lines[0];
for (int i = 1; i < lines.length; i++)
if (lines[i].length() > line.length())
line = lines[i];
bufferedContentWidth = renderer.getWidth(Component.literal(line));
}
}
@Override
void setRelativeBounds(int left, int top) {
super.setRelativeBounds(left, top);
@ -46,7 +73,7 @@ public class MultiLineText extends LayoutComponent<MultiLineText.MultiLineTextRe
@Override
public int getContentWidth() {
return renderer.getWidth(text);
return bufferedContentWidth;
}
@Override
@ -57,6 +84,11 @@ public class MultiLineText extends LayoutComponent<MultiLineText.MultiLineTextRe
protected static class MultiLineTextRenderer implements ComponentRenderer, TextProvider {
MultiLineText linkedComponent;
@Override
public int getWidth(Component c) {
return getFont().width(c.getVisualOrderText());
}
@Override
public int getHeight(net.minecraft.network.chat.Component c) {
if (linkedComponent == null) return 20;

View file

@ -99,4 +99,6 @@ public class Panel implements ComponentWithBounds, RelativeContainerEventHandler
child.render(poseStack, mouseX - bounds.left, mouseY - bounds.top, deltaTicks, bounds, bounds);
}
}
}

View file

@ -9,9 +9,10 @@ import org.betterx.ui.layout.values.Value;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.network.chat.Component;
public class Text extends LayoutComponent<Text.TextRenderer, Text> {
final net.minecraft.network.chat.Component text;
net.minecraft.network.chat.Component text;
int color = ColorUtil.DEFAULT_TEXT;
public Text(
@ -30,6 +31,11 @@ public class Text extends LayoutComponent<Text.TextRenderer, Text> {
return this;
}
public Text setText(Component text) {
this.text = text;
return this;
}
@Override
public int getContentWidth() {
return renderer.getWidth(text);

View file

@ -59,7 +59,7 @@ public abstract class LayoutScreen extends Screen {
}
this.minecraft.setScreen(this);
}, uri, true);
Minecraft.getInstance().setScreen(cls);
}
@ -74,14 +74,14 @@ public abstract class LayoutScreen extends Screen {
}
protected LayoutComponent<?, ?> buildTitle() {
var text = new Text(Value.fit(), Value.fit(), title).centerHorizontal()
.setColor(ColorUtil.WHITE)
.setDebugName("title");
var text = new Text(fit(), fit(), title).centerHorizontal()
.setColor(ColorUtil.WHITE)
.setDebugName("title");
return text;
}
protected LayoutComponent<?, ?> addTitle(LayoutComponent<?, ?> content) {
VerticalStack rows = new VerticalStack(Value.fill(), Value.fill()).setDebugName("title stack");
VerticalStack rows = new VerticalStack(fill(), fill()).setDebugName("title stack");
if (topPadding > 0) rows.addSpacer(topPadding);
rows.add(buildTitle());
@ -91,7 +91,7 @@ public abstract class LayoutScreen extends Screen {
if (sidePadding <= 0) return rows;
HorizontalStack cols = new HorizontalStack(Value.fill(), Value.fill()).setDebugName("padded side");
HorizontalStack cols = new HorizontalStack(fill(), fill()).setDebugName("padded side");
cols.addSpacer(sidePadding);
cols.add(rows);
cols.addSpacer(sidePadding);
@ -118,4 +118,24 @@ public abstract class LayoutScreen extends Screen {
public boolean isPauseScreen() {
return true;
}
public static Value fit() {
return Value.fit();
}
public static Value fitOrFill() {
return Value.fitOrFill();
}
public static Value fill() {
return Value.fill();
}
public static Value fixed(int size) {
return Value.fixed(size);
}
public static Value relative(double percentage) {
return Value.relative(percentage);
}
}

View file

@ -3,7 +3,6 @@ package org.betterx.ui.vanilla;
import org.betterx.ui.layout.components.HorizontalStack;
import org.betterx.ui.layout.components.LayoutComponent;
import org.betterx.ui.layout.values.Size;
import org.betterx.ui.layout.values.Value;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
@ -41,7 +40,7 @@ public abstract class LayoutScreenWithIcon extends LayoutScreen {
@Override
protected LayoutComponent<?, ?> buildTitle() {
LayoutComponent<?, ?> title = super.buildTitle();
HorizontalStack row = new HorizontalStack(Value.fill(), Value.fit()).setDebugName("title bar");
HorizontalStack row = new HorizontalStack(fill(), fit()).setDebugName("title bar");
row.addFiller();
row.addIcon(icon, Size.of(512)).setDebugName("icon");
row.addSpacer(4);