Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

options screen with save button rework #30

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G

# minecraft, mappings and loader dependencies
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.20.6
quilt_mappings=6
minecraft_version=1.21-pre2
quilt_mappings=3
loader_version=0.15.11

# mod properties
mod_version=1.3.1+mc1.20.6
mod_version=1.3.2+mc1.21
maven_group=rainglow
archives_base_name=rainglow

# other dependencies
java_version=21
mod_menu_version=10.0.0-beta.1
fabric_api_version=0.98.0+1.20.6
mod_menu_version=11.0.0-beta.1
fabric_api_version=0.99.4+1.21
2 changes: 1 addition & 1 deletion src/main/java/io/ix0rai/rainglow/Rainglow.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void onInitialize() {
}

public static Identifier id(String id) {
return new Identifier(MOD_ID, id);
return Identifier.method_60655(MOD_ID, id);
}

public static String generateRandomColourId(World world, RandomGenerator random) {
Expand Down
120 changes: 31 additions & 89 deletions src/main/java/io/ix0rai/rainglow/config/CustomModeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,120 +4,62 @@
import io.ix0rai.rainglow.data.RainglowColour;
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.button.ButtonWidget;
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.option.Option;
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.List;

public class CustomModeScreen extends GameOptionsScreen implements ScreenWithUnsavedWarning {
private final ButtonWidget saveButton;
private final List<DeferredSaveOption<Boolean>> options = new ArrayList<>();
private boolean isConfirming;

public class CustomModeScreen extends SaveableGameOptionsScreen {
private static final Text TITLE = Rainglow.translatableText("config.custom");

public CustomModeScreen(Screen parent) {
super(parent, MinecraftClient.getInstance().options, TITLE);
this.saveButton = ButtonWidget.builder(Rainglow.translatableText("config.save"), button -> {
boolean hasColourSelected = false;
for (DeferredSaveOption<Boolean> option : this.options) {
if (option.deferredValue) {
hasColourSelected = true;
break;
}
}

if (!hasColourSelected) {
sendNoColoursToast();
} else {
this.save();
}
}).build();
this.saveButton.active = false;
super(parent, TITLE);
}

private void createColourToggles() {
this.options.clear();
@Override
protected boolean validate() {
boolean hasColourSelected = false;
for (DeferredSaveOption<?> option : this.options) {
if ((boolean) option.deferredValue) {
hasColourSelected = true;
break;
}
}

for (RainglowColour colour : RainglowColour.values()) {
this.options.add(DeferredSaveOption.createDeferredBoolean(
"colour." + colour.getId(),
null,
Rainglow.CONFIG.customColours.getRealValue().contains(colour.getId()),
enabled -> {
if (enabled) {
Rainglow.CONFIG.customColours.getRealValue().add(colour.getId());
}
},
enabled -> this.saveButton.active = true
));
if (!hasColourSelected) {
sendNoColoursToast();
}
return hasColourSelected;
}

private void save() {
@Override
protected void save() {
Rainglow.CONFIG.customColours.getRealValue().clear();

for (DeferredSaveOption<?> option : this.options) {
option.save();
}

super.save();
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));
createColourToggles();
buttonListWidget.addEntries(this.options.toArray(new Option<?>[0]));

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);
protected void method_60325() {
for (RainglowColour colour : RainglowColour.values()) {
this.options.add(DeferredSaveOption.createDeferredBoolean(
"colour." + colour.getId(),
null,
Rainglow.CONFIG.customColours.getRealValue().contains(colour.getId()),
enabled -> {
if (enabled) {
Rainglow.CONFIG.customColours.getRealValue().add(colour.getId());
}
},
enabled -> this.saveButton.active = true
));
}

headerFooterWidget.visitWidgets(this::addDrawableSelectableElement);
headerFooterWidget.arrangeElements();
this.field_51824.addEntries(this.options.toArray(new Option<?>[0]));
}

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);
}
}
}
148 changes: 48 additions & 100 deletions src/main/java/io/ix0rai/rainglow/config/RainglowConfigScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,25 @@
import net.minecraft.client.gui.widget.button.ButtonWidget;
import net.minecraft.client.gui.widget.button.CyclingButtonWidget;
import net.minecraft.client.gui.widget.layout.GridWidget;
import net.minecraft.client.gui.widget.layout.HeaderFooterLayoutWidget;
import net.minecraft.client.gui.widget.layout.LayoutSettings;
import net.minecraft.client.gui.widget.layout.LinearLayoutWidget;
import net.minecraft.client.gui.widget.text.TextWidget;
import net.minecraft.client.option.Option;
import net.minecraft.text.CommonTexts;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Language;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class RainglowConfigScreen extends Screen implements ScreenWithUnsavedWarning {
public class RainglowConfigScreen extends SaveableGameOptionsScreen {
private static final Text TITLE = Rainglow.translatableText("config.title");
public static final Text YES = Text.translatable("gui.yes").styled(style -> style.withColor(0x00FF00));
public static final Text NO = Text.translatable("gui.no").styled(style -> style.withColor(0xFF0000));

private final Screen parent;
private final Map<RainglowEntity, DeferredSaveOption<Boolean>> toggles = new HashMap<>();
private final Map<RainglowEntity, DeferredSaveOption<Integer>> sliders = new HashMap<>();
private final ButtonWidget saveButton;

private RainglowMode mode;
private boolean isConfirming;

public RainglowConfigScreen(@Nullable Screen parent) {
super(TITLE);
this.parent = parent;
super(parent, TITLE);

this.mode = getMode();
this.saveButton = ButtonWidget.builder(Rainglow.translatableText("config.save"), button -> this.save()).build();
this.saveButton.active = false;
}

private void setMode(RainglowMode mode) {
Expand Down Expand Up @@ -74,68 +58,66 @@ private TextWidget getInfoText() {
}

@Override
public void init() {
HeaderFooterLayoutWidget headerFooterWidget = new HeaderFooterLayoutWidget(this, 61, 33);
LinearLayoutWidget headerLayout = headerFooterWidget.addToHeader(LinearLayoutWidget.createVertical().setSpacing(8));

if (!this.isConfirming) {
// header
headerLayout.add(new TextWidget(TITLE, this.textRenderer), settings -> settings.alignHorizontallyCenter().alignVerticallyTop().setPadding(12));
headerLayout.add(createModeButton(), LayoutSettings::alignVerticallyBottom);
headerLayout.add(getInfoText(), LayoutSettings::alignHorizontallyCenter);

// contents
LinearLayoutWidget contentLayout = LinearLayoutWidget.createVertical();

GridWidget gridWidget = new GridWidget();
gridWidget.getDefaultSettings().setHorizontalPadding(4).setBottomPadding(4).alignHorizontallyCenter();
protected void save() {
super.save();
this.setMode(this.mode);
}

GridWidget.AdditionHelper mainAdditionHelper = gridWidget.createAdditionHelper(2);
for (RainglowEntity entity : RainglowEntity.values()) {
DeferredSaveOption<Boolean> entityToggle = createEntityToggle(entity);
mainAdditionHelper.add(entityToggle.createButton(MinecraftClient.getInstance().options));
entityToggle.set(entityToggle.deferredValue);
@Override
protected void method_60329() {
LinearLayoutWidget contentLayout = LinearLayoutWidget.createVertical().setSpacing(8);

mainAdditionHelper.add(createColourRaritySlider(entity).createButton(MinecraftClient.getInstance().options));
}
contentLayout.add(createModeButton(), LayoutSettings::alignHorizontallyCenter);
contentLayout.add(getInfoText(), LayoutSettings::alignHorizontallyCenter);

contentLayout.add(gridWidget);
contentLayout.add(ButtonWidget.builder(Rainglow.translatableText("config.custom"), button -> MinecraftClient.getInstance().setScreen(new CustomModeScreen(this))).width(308).position(4, 0).build(), LayoutSettings.create().setPadding(4, 0));
GridWidget gridWidget = new GridWidget();
gridWidget.getDefaultSettings().setHorizontalPadding(4).setBottomPadding(4).alignHorizontallyCenter();

headerFooterWidget.addToContents(contentLayout);
GridWidget.AdditionHelper mainAdditionHelper = gridWidget.createAdditionHelper(2);
for (RainglowEntity entity : RainglowEntity.values()) {
DeferredSaveOption<Boolean> entityToggle = createEntityToggle(entity);
mainAdditionHelper.add(entityToggle.createButton(MinecraftClient.getInstance().options));
entityToggle.set(entityToggle.deferredValue);
this.options.add(entityToggle);

// footer
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);
DeferredSaveOption<Integer> raritySlider = createColourRaritySlider(entity);
mainAdditionHelper.add(raritySlider.createButton(MinecraftClient.getInstance().options));
this.options.add(raritySlider);
}

headerFooterWidget.visitWidgets(this::addDrawableSelectableElement);
headerFooterWidget.arrangeElements();
contentLayout.add(gridWidget);
contentLayout.add(ButtonWidget.builder(
Rainglow.translatableText("config.custom"),
button -> MinecraftClient.getInstance().setScreen(new CustomModeScreen(this))
).width(308).build(),
LayoutSettings::alignHorizontallyCenter);

this.field_49503.addToContents(contentLayout);
}

@Override
protected void method_60325() {}

private DeferredSaveOption<Boolean> createEntityToggle(RainglowEntity entity) {
return toggles.computeIfAbsent(entity, e -> DeferredSaveOption.createDeferredBoolean(
"config.enable_" + e.getId(),
return DeferredSaveOption.createDeferredBoolean(
"config.enable_" + entity.getId(),
"tooltip.entity_toggle",
Rainglow.CONFIG.toggles.getRealValue().get(e.getId()),
enabled -> Rainglow.CONFIG.toggles.getRealValue().put(e.getId(), enabled),
Rainglow.CONFIG.toggles.getRealValue().get(entity.getId()),
enabled -> Rainglow.CONFIG.toggles.getRealValue().put(entity.getId(), enabled),
enabled -> this.saveButton.active = true
));
);
}

private DeferredSaveOption<Integer> createColourRaritySlider(RainglowEntity entity) {
return sliders.computeIfAbsent(entity, e -> DeferredSaveOption.createDeferredRangedInt(
"config." + e.getId() + "_rarity",
"tooltip.rarity",
Rainglow.CONFIG.rarities.getRealValue().get(e.getId()),
0,
100,
rarity -> Rainglow.CONFIG.rarities.getRealValue().put(e.getId(), rarity),
return DeferredSaveOption.createDeferredRangedInt(
"config." + entity.getId() + "_rarity",
"tooltip.rarity",
Rainglow.CONFIG.rarities.getRealValue().get(entity.getId()),
0,
100,
rarity -> Rainglow.CONFIG.rarities.getRealValue().put(entity.getId(), rarity),
rarity -> this.saveButton.active = true
));
);
}

public CyclingButtonWidget<RainglowMode> createModeButton() {
Expand All @@ -151,25 +133,11 @@ public CyclingButtonWidget<RainglowMode> createModeButton() {
Rainglow.translatableText("config.mode"),
(cyclingButtonWidget, mode) -> {
this.saveButton.active = true;
RainglowConfigScreen.this.mode = mode;
this.mode = mode;
}
);
}

private void save() {
Collection<Option<?>> options = new ArrayList<>(this.sliders.values());
options.addAll(this.toggles.values());

for (Option<?> option : options) {
if (option instanceof DeferredSaveOption) {
((DeferredSaveOption<?>) option).save();
}
}

this.setMode(this.mode);
this.saveButton.active = false;
}

private 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")));
Expand Down Expand Up @@ -199,24 +167,4 @@ private Tooltip createColourListLabel(RainglowMode mode) {
Style style = Style.EMPTY.withColor(mode.getText().getStyle().getColor());
return Tooltip.create(Text.literal(text.toString()).setStyle(style));
}

@Override
public void closeScreen() {
if (this.saveButton.active) {
this.isConfirming = true;
this.clearAndInit();
} else {
MinecraftClient.getInstance().setScreen(this.parent);
}
}

@Override
public void setConfirming(boolean confirming) {
this.isConfirming = confirming;
}

@Override
public void clearAndInit() {
super.clearAndInit();
}
}
Loading
Loading