1.21.8
This commit is contained in:
@@ -2,121 +2,134 @@ package com.straice.smoothdoors.client.ui;
|
||||
|
||||
import com.straice.smoothdoors.config.SddConfig;
|
||||
import com.straice.smoothdoors.config.SddConfigManager;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.gui.Element;
|
||||
import net.minecraft.client.gui.Selectable;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.ButtonWidget;
|
||||
import net.minecraft.client.gui.widget.ClickableWidget;
|
||||
import net.minecraft.client.gui.widget.ElementListWidget;
|
||||
import net.minecraft.client.gui.widget.SliderWidget;
|
||||
import net.minecraft.client.gui.widget.ThreePartsLayoutWidget;
|
||||
import net.minecraft.screen.ScreenTexts;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class SddConfigScreen extends Screen {
|
||||
private static final int ITEM_HEIGHT = 24;
|
||||
private static final int ROW_WIDTH = 310;
|
||||
private static final int COLUMN_WIDTH = 150;
|
||||
private static final int COLUMN_GAP = 10;
|
||||
|
||||
private final Screen parent;
|
||||
private final SddConfig cfg;
|
||||
private final ThreePartsLayoutWidget layout = new ThreePartsLayoutWidget(this);
|
||||
private SettingsList list;
|
||||
|
||||
public SddConfigScreen(Screen parent) {
|
||||
super(Text.literal("Smooth Double Doors"));
|
||||
super(Text.literal("Smooth Double Doors Settings"));
|
||||
this.parent = parent;
|
||||
this.cfg = SddConfigManager.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
int centerX = this.width / 2;
|
||||
initHeader();
|
||||
initBody();
|
||||
initFooter();
|
||||
|
||||
int colW = 118;
|
||||
int gap = 4;
|
||||
int leftX = centerX - colW - gap;
|
||||
int rightX = centerX + gap;
|
||||
|
||||
int y = this.height / 4;
|
||||
int h = 20;
|
||||
|
||||
// ===== DOUBLE SYNC (2 columns) =====
|
||||
|
||||
// Doors row
|
||||
addDrawableChild(toggle(leftX, y, colW, h,
|
||||
() -> "Doors | Use: " + (cfg.connectDoors ? "ON" : "OFF"),
|
||||
() -> cfg.connectDoors = !cfg.connectDoors));
|
||||
|
||||
addDrawableChild(toggle(rightX, y, colW, h,
|
||||
() -> "Doors | Redstone: " + (cfg.redstoneDoubleDoors ? "ON" : "OFF"),
|
||||
() -> cfg.redstoneDoubleDoors = !cfg.redstoneDoubleDoors));
|
||||
|
||||
y += 24;
|
||||
|
||||
// Trapdoors row
|
||||
addDrawableChild(toggle(leftX, y, colW, h,
|
||||
() -> "Traps | Use: " + (cfg.connectTrapdoors ? "ON" : "OFF"),
|
||||
() -> cfg.connectTrapdoors = !cfg.connectTrapdoors));
|
||||
|
||||
addDrawableChild(toggle(rightX, y, colW, h,
|
||||
() -> "Traps | Redstone: " + (cfg.redstoneDoubleTrapdoors ? "ON" : "OFF"),
|
||||
() -> cfg.redstoneDoubleTrapdoors = !cfg.redstoneDoubleTrapdoors));
|
||||
|
||||
y += 24;
|
||||
|
||||
// Fence gates row
|
||||
addDrawableChild(toggle(leftX, y, colW, h,
|
||||
() -> "Gates | Use: " + (cfg.connectFenceGates ? "ON" : "OFF"),
|
||||
() -> cfg.connectFenceGates = !cfg.connectFenceGates));
|
||||
|
||||
addDrawableChild(toggle(rightX, y, colW, h,
|
||||
() -> "Gates | Redstone: " + (cfg.redstoneDoubleFenceGates ? "ON" : "OFF"),
|
||||
() -> cfg.redstoneDoubleFenceGates = !cfg.redstoneDoubleFenceGates));
|
||||
|
||||
y += 34;
|
||||
|
||||
// ===== ANIMATIONS =====
|
||||
addDrawableChild(toggle(centerX - 120, y, 240, h,
|
||||
() -> "Animation Doors: " + (cfg.animateDoors ? "ON" : "OFF"),
|
||||
() -> cfg.animateDoors = !cfg.animateDoors));
|
||||
y += 24;
|
||||
|
||||
addDrawableChild(toggle(centerX - 120, y, 240, h,
|
||||
() -> "Animation Traps: " + (cfg.animateTrapdoors ? "ON" : "OFF"),
|
||||
() -> cfg.animateTrapdoors = !cfg.animateTrapdoors));
|
||||
y += 24;
|
||||
|
||||
addDrawableChild(toggle(centerX - 120, y, 240, h,
|
||||
() -> "Animation Gates: " + (cfg.animateFenceGates ? "ON" : "OFF"),
|
||||
() -> cfg.animateFenceGates = !cfg.animateFenceGates));
|
||||
y += 34;
|
||||
|
||||
// ===== SPEED =====
|
||||
addDrawableChild(new SpeedSlider(centerX - 120, y, 240, h,
|
||||
"Speed Doors", cfg.doorSpeed, v -> {
|
||||
cfg.doorSpeed = v;
|
||||
SddConfigManager.save();
|
||||
}));
|
||||
y += 24;
|
||||
|
||||
addDrawableChild(new SpeedSlider(centerX - 120, y, 240, h,
|
||||
"Speed Traps", cfg.trapdoorSpeed, v -> {
|
||||
cfg.trapdoorSpeed = v;
|
||||
SddConfigManager.save();
|
||||
}));
|
||||
y += 24;
|
||||
|
||||
addDrawableChild(new SpeedSlider(centerX - 120, y, 240, h,
|
||||
"Speed Gates", cfg.fenceGateSpeed, v -> {
|
||||
cfg.fenceGateSpeed = v;
|
||||
SddConfigManager.save();
|
||||
}));
|
||||
y += 40;
|
||||
|
||||
addDrawableChild(ButtonWidget.builder(Text.literal("Done"), btn -> {
|
||||
SddConfigManager.save();
|
||||
if (this.client != null) this.client.setScreen(parent);
|
||||
}).dimensions(centerX - 120, y, 240, h).build());
|
||||
layout.forEachChild(this::addDrawableChild);
|
||||
refreshWidgetPositions();
|
||||
}
|
||||
|
||||
private ButtonWidget toggle(int x, int y, int w, int h, java.util.function.Supplier<String> label,
|
||||
Runnable action) {
|
||||
return ButtonWidget.builder(Text.literal(label.get()), btn -> {
|
||||
action.run();
|
||||
btn.setMessage(Text.literal(label.get()));
|
||||
private void initHeader() {
|
||||
layout.addHeader(this.title, this.textRenderer);
|
||||
}
|
||||
|
||||
private void initBody() {
|
||||
if (this.client == null) return;
|
||||
list = layout.addBody(new SettingsList(this.client, this.width, layout));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
toggle(COLUMN_WIDTH, () -> "Doors | Use: " + (cfg.connectDoors ? "ON" : "OFF"),
|
||||
() -> cfg.connectDoors = !cfg.connectDoors),
|
||||
toggle(COLUMN_WIDTH, () -> "Doors | Redstone: " + (cfg.redstoneDoubleDoors ? "ON" : "OFF"),
|
||||
() -> cfg.redstoneDoubleDoors = !cfg.redstoneDoubleDoors)
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
toggle(COLUMN_WIDTH, () -> "Traps | Use: " + (cfg.connectTrapdoors ? "ON" : "OFF"),
|
||||
() -> cfg.connectTrapdoors = !cfg.connectTrapdoors),
|
||||
toggle(COLUMN_WIDTH, () -> "Traps | Redstone: " + (cfg.redstoneDoubleTrapdoors ? "ON" : "OFF"),
|
||||
() -> cfg.redstoneDoubleTrapdoors = !cfg.redstoneDoubleTrapdoors)
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
toggle(COLUMN_WIDTH, () -> "Gates | Use: " + (cfg.connectFenceGates ? "ON" : "OFF"),
|
||||
() -> cfg.connectFenceGates = !cfg.connectFenceGates),
|
||||
toggle(COLUMN_WIDTH, () -> "Gates | Redstone: " + (cfg.redstoneDoubleFenceGates ? "ON" : "OFF"),
|
||||
() -> cfg.redstoneDoubleFenceGates = !cfg.redstoneDoubleFenceGates)
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
toggle(ROW_WIDTH, () -> "Animation Doors: " + (cfg.animateDoors ? "ON" : "OFF"),
|
||||
() -> cfg.animateDoors = !cfg.animateDoors),
|
||||
null
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
toggle(ROW_WIDTH, () -> "Animation Traps: " + (cfg.animateTrapdoors ? "ON" : "OFF"),
|
||||
() -> cfg.animateTrapdoors = !cfg.animateTrapdoors),
|
||||
null
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
toggle(ROW_WIDTH, () -> "Animation Gates: " + (cfg.animateFenceGates ? "ON" : "OFF"),
|
||||
() -> cfg.animateFenceGates = !cfg.animateFenceGates),
|
||||
null
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
new SpeedSlider(ROW_WIDTH, "Speed Doors", cfg.doorSpeed, v -> {
|
||||
cfg.doorSpeed = v;
|
||||
SddConfigManager.save();
|
||||
}),
|
||||
null
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
new SpeedSlider(ROW_WIDTH, "Speed Traps", cfg.trapdoorSpeed, v -> {
|
||||
cfg.trapdoorSpeed = v;
|
||||
SddConfigManager.save();
|
||||
}),
|
||||
null
|
||||
));
|
||||
|
||||
list.addEntry(new SettingsEntry(
|
||||
new SpeedSlider(ROW_WIDTH, "Speed Gates", cfg.fenceGateSpeed, v -> {
|
||||
cfg.fenceGateSpeed = v;
|
||||
SddConfigManager.save();
|
||||
}),
|
||||
null
|
||||
));
|
||||
}
|
||||
|
||||
private void initFooter() {
|
||||
layout.addFooter(ButtonWidget.builder(ScreenTexts.DONE, btn -> {
|
||||
SddConfigManager.save();
|
||||
}).dimensions(x, y, w, h).build();
|
||||
if (this.client != null) this.client.setScreen(parent);
|
||||
}).width(200).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refreshWidgetPositions() {
|
||||
layout.refreshPositions();
|
||||
if (list != null) {
|
||||
list.position(this.width, layout);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -125,6 +138,73 @@ public class SddConfigScreen extends Screen {
|
||||
if (this.client != null) this.client.setScreen(parent);
|
||||
}
|
||||
|
||||
private ButtonWidget toggle(int width, java.util.function.Supplier<String> label, Runnable action) {
|
||||
return ButtonWidget.builder(Text.literal(label.get()), btn -> {
|
||||
action.run();
|
||||
btn.setMessage(Text.literal(label.get()));
|
||||
SddConfigManager.save();
|
||||
}).dimensions(0, 0, width, 20).build();
|
||||
}
|
||||
|
||||
private static final class SettingsList extends ElementListWidget<SettingsEntry> {
|
||||
SettingsList(MinecraftClient client, int width, ThreePartsLayoutWidget layout) {
|
||||
super(client, width, layout.getContentHeight(), layout.getHeaderHeight(), ITEM_HEIGHT);
|
||||
this.centerListVertically = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int addEntry(SettingsEntry entry) {
|
||||
return super.addEntry(entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowWidth() {
|
||||
return ROW_WIDTH;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class SettingsEntry extends ElementListWidget.Entry<SettingsEntry> {
|
||||
private final ClickableWidget left;
|
||||
private final ClickableWidget right;
|
||||
private final List<ClickableWidget> widgets;
|
||||
|
||||
SettingsEntry(ClickableWidget left, ClickableWidget right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.widgets = (right == null) ? List.of(left) : List.of(left, right);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight,
|
||||
int mouseX, int mouseY, boolean hovered, float delta) {
|
||||
if (right == null) {
|
||||
left.setX(x);
|
||||
left.setY(y);
|
||||
left.render(context, mouseX, mouseY, delta);
|
||||
return;
|
||||
}
|
||||
|
||||
int leftX = x;
|
||||
int rightX = x + COLUMN_WIDTH + COLUMN_GAP;
|
||||
left.setX(leftX);
|
||||
left.setY(y);
|
||||
right.setX(rightX);
|
||||
right.setY(y);
|
||||
left.render(context, mouseX, mouseY, delta);
|
||||
right.render(context, mouseX, mouseY, delta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Element> children() {
|
||||
return widgets;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Selectable> selectableChildren() {
|
||||
return widgets;
|
||||
}
|
||||
}
|
||||
|
||||
private static class SpeedSlider extends SliderWidget {
|
||||
private final String label;
|
||||
private final java.util.function.Consumer<Float> onApply;
|
||||
@@ -137,9 +217,9 @@ public class SddConfigScreen extends Screen {
|
||||
return v;
|
||||
}
|
||||
|
||||
SpeedSlider(int x, int y, int w, int h, String label, float speed,
|
||||
SpeedSlider(int width, String label, float speed,
|
||||
java.util.function.Consumer<Float> onApply) {
|
||||
super(x, y, w, h, Text.literal(label), toValue(speed));
|
||||
super(0, 0, width, 20, Text.literal(label), toValue(speed));
|
||||
this.label = label;
|
||||
this.onApply = onApply;
|
||||
updateMessage();
|
||||
@@ -156,4 +236,4 @@ public class SddConfigScreen extends Screen {
|
||||
onApply.accept(toSpeed(this.value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ public class SddConfig {
|
||||
public boolean redstoneDoubleDoors = true;
|
||||
|
||||
public boolean connectTrapdoors = false;
|
||||
public boolean redstoneDoubleTrapdoors = true;
|
||||
public boolean redstoneDoubleTrapdoors = false;
|
||||
|
||||
public boolean connectFenceGates = true;
|
||||
public boolean redstoneDoubleFenceGates = true;
|
||||
public boolean connectFenceGates = false;
|
||||
public boolean redstoneDoubleFenceGates = false;
|
||||
|
||||
public boolean animateDoors = true;
|
||||
public boolean animateTrapdoors = true;
|
||||
|
||||
Reference in New Issue
Block a user