Skip to content

Commit

Permalink
initial per-entity mode config screen
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Jun 6, 2024
1 parent 1a40700 commit fbccae9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
112 changes: 112 additions & 0 deletions src/main/java/io/ix0rai/rainglow/config/ModeConfigScreen.java
Original file line number Diff line number Diff line change
@@ -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<RainglowEntity, RainglowMode> 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<RainglowEntity, RainglowMode> 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);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fbccae9

Please sign in to comment.