Skip to content

Commit

Permalink
allow gui scale to be changed by holding down control and scrolling,
Browse files Browse the repository at this point in the history
refactor option applying, move flag processing into Config,
  • Loading branch information
douira committed Nov 2, 2024
1 parent 9d65347 commit c18e6c8
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package net.caffeinemc.mods.sodium.api.config.option;

public record Range(int min, int max, int step) {
public Range {
if (min > max) {
throw new IllegalArgumentException("Min must be less than or equal to max");
}
if (step <= 0) {
throw new IllegalArgumentException("Step must be greater than 0");
}
}

public boolean isValueValid(int value) {
return value >= this.min && value <= this.max && (value - this.min) % this.step == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.EnumSet;
import java.util.function.Function;

class BooleanOption extends StatefulOption<Boolean> {
public class BooleanOption extends StatefulOption<Boolean> {
BooleanOption(ResourceLocation id, Collection<ResourceLocation> dependencies, Component name, DependentValue<Boolean> enabled, StorageEventHandler storage, Function<Boolean, Component> tooltipProvider, OptionImpact impact, EnumSet<OptionFlag> flags, DependentValue<Boolean> defaultValue, OptionBinding<Boolean> binding) {
super(id, dependencies, name, enabled, storage, tooltipProvider, impact, flags, defaultValue, binding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import net.caffeinemc.mods.sodium.api.config.ConfigState;
import net.caffeinemc.mods.sodium.api.config.option.OptionFlag;
import net.caffeinemc.mods.sodium.api.config.StorageEventHandler;
import net.caffeinemc.mods.sodium.client.console.Console;
import net.caffeinemc.mods.sodium.client.console.message.MessageLevel;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;

import java.util.Collection;
Expand Down Expand Up @@ -76,7 +79,7 @@ public void resetAllOptions() {
}
}

public Collection<OptionFlag> applyAllOptions() {
public void applyAllOptions() {
var flags = EnumSet.noneOf(OptionFlag.class);

for (var option : this.options.values()) {
Expand All @@ -87,7 +90,20 @@ public Collection<OptionFlag> applyAllOptions() {

this.flushStorageHandlers();

return flags;
processFlags(flags);
}

public void applyOption(ResourceLocation id) {
var flags = EnumSet.noneOf(OptionFlag.class);

var option = this.options.get(id);
if (option != null && option.applyChanges()) {
flags.addAll(option.getFlags());
}

this.flushStorageHandlers();

processFlags(flags);
}

public boolean anyOptionChanged() {
Expand All @@ -111,6 +127,10 @@ void flushStorageHandlers() {
this.pendingStorageHandlers.clear();
}

public Option getOption(ResourceLocation id) {
return this.options.get(id);
}

public ImmutableList<ModOptions> getModOptions() {
return this.modOptions;
}
Expand Down Expand Up @@ -148,4 +168,30 @@ public <E extends Enum<E>> E readEnumOption(ResourceLocation id, Class<E> enumCl

throw new IllegalArgumentException("Can't read enum value from option with id " + id);
}

private static void processFlags(Collection<OptionFlag> flags) {
Minecraft client = Minecraft.getInstance();

if (client.level != null) {
if (flags.contains(OptionFlag.REQUIRES_RENDERER_RELOAD)) {
client.levelRenderer.allChanged();
} else if (flags.contains(OptionFlag.REQUIRES_RENDERER_UPDATE)) {
client.levelRenderer.needsUpdate();
}
}

if (flags.contains(OptionFlag.REQUIRES_ASSET_RELOAD)) {
client.updateMaxMipLevel(client.options.mipmapLevels().get());
client.delayTextureReload();
}

if (flags.contains(OptionFlag.REQUIRES_VIDEOMODE_RELOAD)) {
client.getWindow().changeFullscreenVideoMode();
}

if (flags.contains(OptionFlag.REQUIRES_GAME_RESTART)) {
Console.instance().logMessage(MessageLevel.WARN,
"sodium.console.game_restart", true, 10.0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class EnumOption<E extends Enum<E>> extends StatefulOption<E> {
}

@Override
boolean isValueValid(E value) {
public boolean isValueValid(E value) {
return this.allowedValues.get(this.state).contains(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.EnumSet;
import java.util.function.Function;

class IntegerOption extends StatefulOption<Integer> {
public class IntegerOption extends StatefulOption<Integer> {
private final DependentValue<Range> range;
private final ControlValueFormatter valueFormatter;

Expand All @@ -23,7 +23,7 @@ class IntegerOption extends StatefulOption<Integer> {
}

@Override
boolean isValueValid(Integer value) {
public boolean isValueValid(Integer value) {
return this.range.get(this.state).isValueValid(value);
}

Expand All @@ -32,5 +32,9 @@ Control createControl() {
var range = this.range.get(this.state);
return new SliderControl(this, range.min(), range.max(), range.step(), this.valueFormatter);
}

public Range getRange() {
return this.range.get(this.state);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean hasChanged() {
return false;
}

public boolean applyChanges() {
boolean applyChanges() {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public boolean hasChanged() {
}

@Override
public boolean applyChanges() {
boolean applyChanges() {
if (this.hasChanged()) {
this.value = this.modifiedValue;
this.binding.save(this.value);
Expand All @@ -77,7 +77,7 @@ public boolean applyChanges() {
return false;
}

boolean isValueValid(V value) {
public boolean isValueValid(V value) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package net.caffeinemc.mods.sodium.client.gui;

import net.caffeinemc.mods.sodium.api.config.option.OptionFlag;
import net.caffeinemc.mods.sodium.api.config.option.OptionImpact;
import net.caffeinemc.mods.sodium.client.config.structure.Page;
import net.caffeinemc.mods.sodium.client.SodiumClientMod;
import net.caffeinemc.mods.sodium.client.config.*;
import net.caffeinemc.mods.sodium.client.config.structure.*;
import net.caffeinemc.mods.sodium.client.config.ConfigManager;
import net.caffeinemc.mods.sodium.client.config.structure.IntegerOption;
import net.caffeinemc.mods.sodium.client.config.structure.ModOptions;
import net.caffeinemc.mods.sodium.client.config.structure.OptionPage;
import net.caffeinemc.mods.sodium.client.config.structure.Page;
import net.caffeinemc.mods.sodium.client.data.fingerprint.HashedFingerprint;
import net.caffeinemc.mods.sodium.client.console.Console;
import net.caffeinemc.mods.sodium.client.console.message.MessageLevel;
import net.caffeinemc.mods.sodium.client.gui.options.control.ControlElement;
import net.caffeinemc.mods.sodium.client.gui.prompt.ScreenPrompt;
import net.caffeinemc.mods.sodium.client.gui.prompt.ScreenPromptable;
Expand All @@ -24,9 +23,9 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -196,7 +195,7 @@ private void rebuildGUI() {
this.pageList = new PageListWidget(this, new Dim2i(0, 0, 125, this.height));

this.undoButton = new FlatButtonWidget(new Dim2i(270, Layout.INNER_MARGIN, Layout.BUTTON_LONG, Layout.BUTTON_SHORT), Component.translatable("sodium.options.buttons.undo"), this::undoChanges, true, false);
this.applyButton = new FlatButtonWidget(new Dim2i(130, Layout.INNER_MARGIN, Layout.BUTTON_LONG, Layout.BUTTON_SHORT), Component.translatable("sodium.options.buttons.apply"), this::applyChanges, true, false);
this.applyButton = new FlatButtonWidget(new Dim2i(130, Layout.INNER_MARGIN, Layout.BUTTON_LONG, Layout.BUTTON_SHORT), Component.translatable("sodium.options.buttons.apply"), ConfigManager.CONFIG::applyAllOptions, true, false);
this.closeButton = new FlatButtonWidget(new Dim2i(200, Layout.INNER_MARGIN, Layout.BUTTON_LONG, Layout.BUTTON_SHORT), Component.translatable("gui.done"), this::onClose, true, false);

this.donateButton = new FlatButtonWidget(new Dim2i(this.width - 128, Layout.INNER_MARGIN, 100, Layout.BUTTON_SHORT), Component.translatable("sodium.options.buttons.donate"), this::openDonationPage, true, false);
Expand Down Expand Up @@ -292,7 +291,7 @@ private void renderOptionTooltip(GuiGraphics graphics, ControlElement element) {

var option = element.getOption();
var splitWidth = boxWidth - (textPadding * 2);
List<FormattedCharSequence> tooltip = new ArrayList<>(this.font.split(option.getTooltip(),splitWidth));
List<FormattedCharSequence> tooltip = new ArrayList<>(this.font.split(option.getTooltip(), splitWidth));

OptionImpact impact = option.getImpact();

Expand All @@ -318,34 +317,6 @@ private void renderOptionTooltip(GuiGraphics graphics, ControlElement element) {
}
}

private void applyChanges() {
var flags = ConfigManager.CONFIG.applyAllOptions();

Minecraft client = Minecraft.getInstance();

if (client.level != null) {
if (flags.contains(OptionFlag.REQUIRES_RENDERER_RELOAD)) {
client.levelRenderer.allChanged();
} else if (flags.contains(OptionFlag.REQUIRES_RENDERER_UPDATE)) {
client.levelRenderer.needsUpdate();
}
}

if (flags.contains(OptionFlag.REQUIRES_ASSET_RELOAD)) {
client.updateMaxMipLevel(client.options.mipmapLevels().get());
client.delayTextureReload();
}

if (flags.contains(OptionFlag.REQUIRES_VIDEOMODE_RELOAD)) {
client.getWindow().changeFullscreenVideoMode();
}

if (flags.contains(OptionFlag.REQUIRES_GAME_RESTART)) {
Console.instance().logMessage(MessageLevel.WARN,
"sodium.console.game_restart", true, 10.0);
}
}

private void undoChanges() {
ConfigManager.CONFIG.resetAllOptions();
}
Expand Down Expand Up @@ -386,6 +357,41 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
return true;
}

@Override
public boolean mouseScrolled(double d, double e, double f, double amount) {
if (Screen.hasControlDown()) {
var location = ResourceLocation.parse("sodium:general.gui_scale");
var option = ConfigManager.CONFIG.getOption(location);
if (option instanceof IntegerOption guiScaleOption) {
var value = guiScaleOption.getValidatedValue();
if (value instanceof Integer intValue) {
var range = guiScaleOption.getRange();
var top = range.max() + 1;
var auto = range.min();

// re-maps the auto value (presumably 0) to be at the top of the scroll range
if (intValue == auto) {
intValue = top;
}
var newValue = Math.clamp(intValue + (int) Math.signum(amount), auto + 1, top);
if (newValue != intValue) {
if (newValue == top) {
newValue = auto;
}
if (range.isValueValid(newValue)) {
guiScaleOption.modifyValue(newValue);
ConfigManager.CONFIG.applyOption(location);
return true;
}
}
}
}
return false;
} else {
return super.mouseScrolled(d, e, f, amount);
}
}

@Override
public boolean shouldCloseOnEsc() {
return !this.hasPendingChanges;
Expand Down

0 comments on commit c18e6c8

Please sign in to comment.