[Change] Migrated ModMenu Screen to new Layout Engine

This commit is contained in:
Frank 2022-07-16 13:55:02 +02:00
parent 399a2c8a4e
commit bebd7e1c70
21 changed files with 289 additions and 152 deletions

View file

@ -1,60 +1,65 @@
package org.betterx.bclib.client.gui.modmenu;
import org.betterx.bclib.client.gui.gridlayout.*;
import org.betterx.bclib.BCLib;
import org.betterx.bclib.config.ConfigKeeper;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.config.NamedPathConfig;
import org.betterx.bclib.config.NamedPathConfig.ConfigTokenDescription;
import org.betterx.bclib.config.NamedPathConfig.DependendConfigToken;
import org.betterx.ui.layout.components.*;
import org.betterx.ui.layout.values.Value;
import org.betterx.ui.vanilla.LayoutScreenWithIcon;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import org.jetbrains.annotations.Nullable;
public class MainScreen extends GridScreen {
public class MainScreen extends LayoutScreenWithIcon {
static final ResourceLocation BCLIB_LOGO_LOCATION = new ResourceLocation(BCLib.MOD_ID, "icon.png");
public MainScreen(@Nullable Screen parent) {
super(parent, Component.translatable("title.bclib.modmenu.main"), 10, false);
super(parent, BCLIB_LOGO_LOCATION, Component.translatable("title.bclib.modmenu.main"), 10, 10, 20);
}
protected <T> Component getComponent(NamedPathConfig config, ConfigTokenDescription<T> option, String type) {
return Component.translatable(type + ".config." + config.configID + option.getPath());
}
Map<GridWidgetWithEnabledState, Supplier<Boolean>> dependentWidgets = new HashMap<>();
Map<Checkbox, Supplier<Boolean>> dependentWidgets = new HashMap<>();
protected void updateEnabledState() {
dependentWidgets.forEach((cb, supl) -> cb.setEnabled(supl.get()));
}
@SuppressWarnings("unchecked")
protected <T> void addRow(GridColumn grid, NamedPathConfig config, ConfigTokenDescription<T> option) {
protected <T> void addRow(VerticalStack grid, NamedPathConfig config, ConfigTokenDescription<T> option) {
if (ConfigKeeper.BooleanEntry.class.isAssignableFrom(option.token.type)) {
addCheckbox(grid, config, (ConfigTokenDescription<Boolean>) option);
}
grid.addSpacerRow(2);
grid.addSpacer(2);
}
protected void addCheckbox(GridColumn grid, NamedPathConfig config, ConfigTokenDescription<Boolean> option) {
protected void addCheckbox(VerticalStack grid, NamedPathConfig config, ConfigTokenDescription<Boolean> option) {
if (option.topPadding > 0) {
grid.addSpacerRow(option.topPadding);
grid.addSpacer(option.topPadding);
}
GridRow row = grid.addRow();
HorizontalStack row = grid.addRow();
if (option.leftPadding > 0) {
row.addSpacer(option.leftPadding);
}
GridCheckboxCell cb = row.addCheckbox(
Checkbox cb = row.addCheckbox(
Value.fit(), Value.fit(),
getComponent(config, option, "title"),
config.getRaw(option.token),
font,
(state) -> {
(caller, state) -> {
config.set(option.token, state);
updateEnabledState();
}
@ -71,34 +76,39 @@ public class MainScreen extends GridScreen {
return false;
}
@Override
protected void initLayout() {
final int BUTTON_HEIGHT = 20;
protected LayoutComponent initContent() {
VerticalStack content = new VerticalStack(Value.fit(), Value.fit());
Configs.GENERATOR_CONFIG.getAllOptions()
.stream()
.filter(o -> !o.hidden)
.forEach(o -> addRow(grid, Configs.GENERATOR_CONFIG, o));
grid.addSpacerRow(12);
.forEach(o -> addRow(content, Configs.GENERATOR_CONFIG, o));
content.addSpacer(12);
Configs.MAIN_CONFIG.getAllOptions()
.stream()
.filter(o -> !o.hidden)
.forEach(o -> addRow(grid, Configs.MAIN_CONFIG, o));
grid.addSpacerRow(12);
.forEach(o -> addRow(content, Configs.MAIN_CONFIG, o));
content.addSpacer(12);
Configs.CLIENT_CONFIG.getAllOptions()
.stream()
.filter(o -> !o.hidden)
.forEach(o -> addRow(grid, Configs.CLIENT_CONFIG, o));
.forEach(o -> addRow(content, Configs.CLIENT_CONFIG, o));
grid.addSpacerRow(15);
GridRow row = grid.addRow();
VerticalStack grid = new VerticalStack(Value.fill(), Value.fill());
grid.add(VerticalScroll.create(Value.fill(), Value.fill(), content));
grid.addSpacer(8);
HorizontalStack row = grid.addRow();
row.addFiller();
row.addButton(CommonComponents.GUI_DONE, BUTTON_HEIGHT, font, (button) -> {
grid.addButton(Value.fit(), Value.fit(), CommonComponents.GUI_DONE, (button) -> {
Configs.CLIENT_CONFIG.saveChanges();
Configs.GENERATOR_CONFIG.saveChanges();
Configs.MAIN_CONFIG.saveChanges();
onClose();
});
grid.addSpacerRow(10);
}).alignRight();
return grid;
}
}

View file

@ -3,8 +3,8 @@ package org.betterx.bclib.client.gui.modmenu;
import org.betterx.bclib.BCLib;
import org.betterx.ui.ColorUtil;
import org.betterx.ui.layout.components.*;
import org.betterx.ui.layout.values.DynamicSize;
import org.betterx.ui.layout.values.Size;
import org.betterx.ui.layout.values.Value;
import org.betterx.ui.vanilla.LayoutScreen;
import net.minecraft.client.gui.screens.Screen;
@ -24,36 +24,36 @@ public class TestScreen extends LayoutScreen {
}
@Override
protected org.betterx.ui.layout.components.Component<?> initContent() {
VerticalStack rows = new VerticalStack(DynamicSize.fit(), DynamicSize.fitOrFill());
protected LayoutComponent<?> initContent() {
VerticalStack rows = new VerticalStack(Value.fit(), Value.fitOrFill());
rows.addFiller();
rows.add(new Text(
DynamicSize.fitOrFill(), DynamicSize.fixed(20),
Value.fitOrFill(), Value.fixed(20),
Component.literal("Some Text")
).alignRight()
);
rows.add(new Text(
DynamicSize.fitOrFill(), DynamicSize.fixed(20),
Value.fitOrFill(), Value.fixed(20),
Component.literal("Some other, longer Text")
).centerHorizontal()
);
rows.addSpacer(16);
rows.add(new Image(
DynamicSize.fixed(24), DynamicSize.fixed(24),
Value.fixed(24), Value.fixed(24),
BCLib.makeID("icon.png"),
new Size(512, 512)
).centerHorizontal()
);
rows.add(new MultiLineText(
DynamicSize.fill(), DynamicSize.fit(),
Value.fill(), Value.fit(),
Component.literal(
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
).setColor(ColorUtil.LIGHT_PURPLE).centerHorizontal()
);
rows.addSpacer(16);
rows.add(new Range<>(
DynamicSize.fill(), DynamicSize.fit(),
Value.fill(), Value.fit(),
Component.literal("Integer"),
10, 90, 20,
(slider, value) -> {
@ -62,7 +62,7 @@ public class TestScreen extends LayoutScreen {
));
rows.addSpacer(8);
rows.add(new Range<>(
DynamicSize.fill(), DynamicSize.fit(),
Value.fill(), Value.fit(),
Component.literal("Float"),
10f, 90f, 20f,
(slider, value) -> {
@ -71,7 +71,7 @@ public class TestScreen extends LayoutScreen {
));
rows.addSpacer(16);
Checkbox cb1 = new Checkbox(
DynamicSize.fit(), DynamicSize.fit(),
Value.fit(), Value.fit(),
Component.literal("Some Sub-State"),
false, true,
(checkbox, value) -> {
@ -79,7 +79,7 @@ public class TestScreen extends LayoutScreen {
}
);
rows.add(new Checkbox(
DynamicSize.fit(), DynamicSize.fit(),
Value.fit(), Value.fit(),
Component.literal("Some Selectable State"),
false, true,
(checkbox, value) -> {
@ -90,7 +90,7 @@ public class TestScreen extends LayoutScreen {
rows.add(cb1);
rows.addSpacer(16);
rows.add(new Button(
DynamicSize.fit(), DynamicSize.fit(),
Value.fit(), Value.fit(),
Component.literal("test"),
(bt) -> {
System.out.println("clicked test");
@ -101,7 +101,7 @@ public class TestScreen extends LayoutScreen {
);
rows.addSpacer(8);
rows.add(new Button(
DynamicSize.fit(), DynamicSize.fit(),
Value.fit(), Value.fit(),
Component.literal("Hello World"),
(bt) -> {
System.out.println("clicked hello");
@ -112,6 +112,6 @@ public class TestScreen extends LayoutScreen {
);
rows.addFiller();
return HorizontalStack.centered(VerticalScroll.create(DynamicSize.fit(), DynamicSize.relative(1), rows));
return HorizontalStack.centered(VerticalScroll.create(Value.fit(), Value.relative(1), rows));
}
}