From fbccae9194e1b1a7a92ead042be5971269e7c2b2 Mon Sep 17 00:00:00 2001 From: ix0rai Date: Thu, 6 Jun 2024 15:10:20 -0500 Subject: [PATCH] initial per-entity mode config screen --- .../rainglow/config/ModeConfigScreen.java | 112 ++++++++++++++++++ .../rainglow/config/RainglowConfigScreen.java | 7 +- 2 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/ix0rai/rainglow/config/ModeConfigScreen.java diff --git a/src/main/java/io/ix0rai/rainglow/config/ModeConfigScreen.java b/src/main/java/io/ix0rai/rainglow/config/ModeConfigScreen.java new file mode 100644 index 0000000..aab2a85 --- /dev/null +++ b/src/main/java/io/ix0rai/rainglow/config/ModeConfigScreen.java @@ -0,0 +1,112 @@ +package io.ix0rai.rainglow.config; + +import io.ix0rai.rainglow.Rainglow; +import io.ix0rai.rainglow.data.RainglowEntity; +import io.ix0rai.rainglow.data.RainglowMode; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.option.GameOptionsScreen; +import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.gui.widget.button.ButtonWidget; +import net.minecraft.client.gui.widget.button.CyclingButtonWidget; +import net.minecraft.client.gui.widget.layout.HeaderFooterLayoutWidget; +import net.minecraft.client.gui.widget.layout.LinearLayoutWidget; +import net.minecraft.client.gui.widget.list.ButtonListWidget; +import net.minecraft.client.gui.widget.text.TextWidget; +import net.minecraft.client.toast.SystemToast; +import net.minecraft.client.toast.Toast; +import net.minecraft.text.CommonTexts; +import net.minecraft.text.Text; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ModeConfigScreen extends GameOptionsScreen implements ScreenWithUnsavedWarning { + private final ButtonWidget saveButton; + private final Map updatedModes = new HashMap<>(); + private boolean isConfirming; + + private static final Text TITLE = Rainglow.translatableText("config.custom"); + + public ModeConfigScreen(Screen parent) { + super(parent, MinecraftClient.getInstance().options, TITLE); + this.saveButton = ButtonWidget.builder(Rainglow.translatableText("config.save"), button -> this.save()).build(); + this.saveButton.active = false; + } + + private ClickableWidget createEntityCyclingWidget(RainglowEntity entity) { + Text text = Text.translatable("entity.minecraft." + entity.getId()); + + return CyclingButtonWidget.builder(RainglowMode::getText) + .values(RainglowMode.values()) + // todo this will crash if the world is null + .initially(Rainglow.MODE_CONFIG.getMode(MinecraftClient.getInstance().world, entity)) + .tooltip(RainglowConfigScreen::createColourListLabel) + .build( + text, + (cyclingButtonWidget, mode) -> { + this.saveButton.active = true; + this.updatedModes.put(entity, mode); + } + ); + } + + private void save() { + for (Map.Entry entry : this.updatedModes.entrySet()) { + Rainglow.MODE_CONFIG.setMode(MinecraftClient.getInstance().world, entry.getValue(), entry.getKey()); + } + + Rainglow.CONFIG.save(); + this.saveButton.active = false; + } + + @Override + public void init() { + HeaderFooterLayoutWidget headerFooterWidget = new HeaderFooterLayoutWidget(this, 61, 33); + headerFooterWidget.addToHeader(new TextWidget(TITLE, this.textRenderer), settings -> settings.alignHorizontallyCenter().setBottomPadding(28)); + + if (!this.isConfirming) { + ButtonListWidget buttonListWidget = headerFooterWidget.addToContents(new ButtonListWidget(this.client, this.width, this.height, this)); + for (RainglowEntity entity : RainglowEntity.values()) { + buttonListWidget.method_58227(List.of(this.createEntityCyclingWidget(entity))); + } + + LinearLayoutWidget linearLayout = headerFooterWidget.addToFooter(LinearLayoutWidget.createHorizontal().setSpacing(8)); + linearLayout.add(ButtonWidget.builder(CommonTexts.DONE, button -> this.closeScreen()).build()); + linearLayout.add(this.saveButton); + } else { + this.setUpUnsavedWarning(headerFooterWidget, this.textRenderer, this.parent); + } + + headerFooterWidget.visitWidgets(this::addDrawableSelectableElement); + headerFooterWidget.arrangeElements(); + } + + private static void sendNoColoursToast() { + Toast toast = new SystemToast(SystemToast.Id.PACK_LOAD_FAILURE, Rainglow.translatableText("config.no_custom_colours"), Rainglow.translatableText("config.no_custom_colours_description")); + MinecraftClient.getInstance().getToastManager().add(toast); + } + + @Override + public void setConfirming(boolean confirming) { + this.isConfirming = confirming; + } + + @Override + public void clearAndInit() { + super.clearAndInit(); + } + + @Override + public void closeScreen() { + if (this.saveButton.active) { + this.isConfirming = true; + this.clearAndInit(); + } else { + MinecraftClient.getInstance().setScreen(this.parent); + } + } +} + diff --git a/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java b/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java index 5163ddf..df742f2 100644 --- a/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java +++ b/src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java @@ -94,8 +94,7 @@ public void init() { modeLayout.add(createModeButton(), LayoutSettings::alignVerticallyBottom); // todo link to page with per-entity mode editing modeLayout.add(ButtonWidget.builder(Text.literal("grind"), button -> { - //this.mode = RainglowMode.get("grind"); - // this.saveButton.active = true; + this.client.setScreen(new ModeConfigScreen(this)); }).width(100).build(), LayoutSettings::alignVerticallyBottom); headerLayout.add(getInfoText(), settings -> settings.alignHorizontallyCenter().alignVerticallyBottom().setBottomPadding(1)); @@ -158,7 +157,7 @@ public ClickableWidget createModeButton() { return CyclingButtonWidget.builder(RainglowMode::getText) .values(RainglowMode.values()) .initially(this.mode) - .tooltip(this::createColourListLabel) + .tooltip(RainglowConfigScreen::createColourListLabel) .build( 0, 0, @@ -202,7 +201,7 @@ private void save() { this.saveButton.active = false; } - private Tooltip createColourListLabel(RainglowMode mode) { + static Tooltip createColourListLabel(RainglowMode mode) { // creates a label and appends all the colours that will be applied in the given mode StringBuilder text = new StringBuilder(Language.getInstance().get(Rainglow.translatableTextKey("config.colours_to_apply"))); int maxDisplayedColourCount = 16;