Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.caffeinemc.mods.sodium.api.config.option;

/**
* The control style used by an integer option.
*/
public enum IntegerOptionControlStyle {
SLIDER,
TEXT_BOX
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,19 @@ public interface IntegerOptionBuilder extends StatefulOptionBuilder<Integer> {
*/
IntegerOptionBuilder setValidatorProvider(Function<ConfigState, ? extends SteppedValidator> provider, Identifier... dependencies);

/**
* Sets the control style for this integer option.
*
* @param control The control style to use.
* @return The current builder instance.
*/
IntegerOptionBuilder setControlStyle(IntegerOptionControlStyle control);

/**
* Sets the value formatter for this integer option.
*
* @param formatter The formatter to format the integer value of this option.
* @return The current builder instance.
*/
IntegerOptionBuilder setValueFormatter(ControlValueFormatter formatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

class IntegerOptionBuilderImpl extends StatefulOptionBuilderImpl<IntegerOption, Integer> implements IntegerOptionBuilder {
private DependentValue<? extends SteppedValidator> validatorProvider;
private IntegerOptionControlStyle control = IntegerOptionControlStyle.SLIDER;
private ControlValueFormatter valueFormatter;

IntegerOptionBuilderImpl(Identifier id) {
Expand All @@ -31,6 +32,7 @@ void validateData() {

Validate.notNull(this.getValidatorProvider(), "Validator provider must be set");
Validate.notNull(this.getValueFormatter(), "Value formatter must be set");
Validate.notNull(this.getControlStyle(), "Control style must be set");
}

@Override
Expand All @@ -51,6 +53,7 @@ IntegerOption build() {
this.getBinding(),
this.getApplyHook(),
this.getValidatorProvider(),
this.getControlStyle(),
this.getValueFormatter());
}

Expand All @@ -70,6 +73,10 @@ DependentValue<? extends SteppedValidator> getValidatorProvider() {
return getFirstNotNull(this.validatorProvider, IntegerOption::getValidatorProvider);
}

IntegerOptionControlStyle getControlStyle() {
return getFirstNotNull(this.control, IntegerOption::getControlStyle);
}

ControlValueFormatter getValueFormatter() {
return getFirstNotNull(this.valueFormatter, IntegerOption::getValueFormatter);
}
Expand Down Expand Up @@ -193,6 +200,12 @@ public IntegerOptionBuilder setValidatorProvider(Function<ConfigState, ? extends
return this;
}

@Override
public IntegerOptionBuilder setControlStyle(IntegerOptionControlStyle control) {
this.control = control;
return this;
}

@Override
public IntegerOptionBuilder setValueFormatter(ControlValueFormatter formatter) {
this.valueFormatter = formatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import net.caffeinemc.mods.sodium.api.config.ConfigState;
import net.caffeinemc.mods.sodium.api.config.StorageEventHandler;
import net.caffeinemc.mods.sodium.api.config.option.ControlValueFormatter;
import net.caffeinemc.mods.sodium.api.config.option.OptionBinding;
import net.caffeinemc.mods.sodium.api.config.option.OptionImpact;
import net.caffeinemc.mods.sodium.api.config.option.SteppedValidator;
import net.caffeinemc.mods.sodium.api.config.option.*;
import net.caffeinemc.mods.sodium.client.config.value.DependentValue;
import net.caffeinemc.mods.sodium.client.gui.options.control.Control;
import net.caffeinemc.mods.sodium.client.gui.options.control.IntegerTextBoxControl;
import net.caffeinemc.mods.sodium.client.gui.options.control.SliderControl;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
Expand All @@ -19,6 +17,7 @@

public class IntegerOption extends StatefulOption<Integer> {
private final DependentValue<? extends SteppedValidator> validator;
private final IntegerOptionControlStyle controlStyle;
private final ControlValueFormatter valueFormatter;

public IntegerOption(
Expand All @@ -35,10 +34,12 @@ public IntegerOption(
OptionBinding<Integer> binding,
Consumer<ConfigState> applyHook,
DependentValue<? extends SteppedValidator> validator,
IntegerOptionControlStyle controlStyle,
ControlValueFormatter valueFormatter
) {
super(id, dependencies, name, enabled, storage, tooltipProvider, impact, flags, defaultValue, controlHiddenWhenDisabled, binding, applyHook);
this.validator = validator;
this.controlStyle = controlStyle;
this.valueFormatter = valueFormatter;
}

Expand All @@ -59,7 +60,10 @@ Integer validateValue(Integer value) {

@Override
Control createControl() {
return new SliderControl(this);
return switch (this.controlStyle) {
case SLIDER -> new SliderControl(this);
case TEXT_BOX -> new IntegerTextBoxControl(this);
};
}

public SteppedValidator getSteppedValidator() {
Expand All @@ -74,6 +78,10 @@ public DependentValue<? extends SteppedValidator> getValidatorProvider() {
return this.validator;
}

public IntegerOptionControlStyle getControlStyle() {
return this.controlStyle;
}

public ControlValueFormatter getValueFormatter() {
return this.valueFormatter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.caffeinemc.mods.sodium.api.config.ConfigEntryPoint;
import net.caffeinemc.mods.sodium.api.config.ConfigState;
import net.caffeinemc.mods.sodium.api.config.StorageEventHandler;
import net.caffeinemc.mods.sodium.api.config.option.IntegerOptionControlStyle;
import net.caffeinemc.mods.sodium.api.config.option.OptionFlag;
import net.caffeinemc.mods.sodium.api.config.option.OptionImpact;
import net.caffeinemc.mods.sodium.api.config.option.Range;
Expand Down Expand Up @@ -47,6 +48,9 @@
public class SodiumConfigBuilder implements ConfigEntryPoint {
private static final Identifier SODIUM_ICON = Identifier.fromNamespaceAndPath("sodium", "textures/gui/config-icon.png");
private static final SodiumOptions DEFAULTS = SodiumOptions.defaults();
public static final int FRAMERATE_LIMIT_MIN = 10;
public static final int FRAMERATE_LIMIT_MAX = 1_000_000;
public static final int FRAMERATE_LIMIT_DEFAULT = 60;

private final Options vanillaOpts;
private final StorageEventHandler vanillaStorage;
Expand Down Expand Up @@ -309,9 +313,10 @@ private OptionPageBuilder buildGeneralPage(ConfigBuilder builder) {
.setStorageHandler(this.vanillaStorage)
.setName(Component.translatable("options.framerateLimit"))
.setTooltip(Component.translatable("sodium.options.fps_limit.tooltip"))
.setValueFormatter(ControlValueFormatterImpls.fpsLimit())
.setRange(10, 260, 10)
.setDefaultValue(60)
.setValueFormatter(ControlValueFormatterImpls.fpsLimit(FRAMERATE_LIMIT_MAX))
.setRange(FRAMERATE_LIMIT_MIN, FRAMERATE_LIMIT_MAX, 1)
.setDefaultValue(FRAMERATE_LIMIT_DEFAULT)
.setControlStyle(IntegerOptionControlStyle.TEXT_BOX)
.setBinding(this.vanillaOpts.framerateLimit()::set, this.vanillaOpts.framerateLimit()::get)
)
);
Expand Down Expand Up @@ -726,5 +731,4 @@ private OptionPageBuilder buildAdvancedPage(ConfigBuilder builder) {
);
return advancedPage;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.caffeinemc.mods.sodium.client.gui.ColorTheme;
import net.caffeinemc.mods.sodium.client.gui.Colors;
import net.caffeinemc.mods.sodium.client.gui.Layout;
import net.caffeinemc.mods.sodium.client.gui.widgets.AbstractWidget;
import net.caffeinemc.mods.sodium.client.gui.widgets.AbstractParentWidget;
import net.caffeinemc.mods.sodium.client.util.Dim2i;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.ComponentPath;
Expand All @@ -15,7 +15,7 @@
import net.minecraft.network.chat.Style;
import org.jspecify.annotations.Nullable;

public abstract class ControlElement extends AbstractWidget {
public abstract class ControlElement extends AbstractParentWidget {
protected final AbstractOptionList list;
protected final ColorTheme theme;

Expand Down Expand Up @@ -83,6 +83,16 @@ public int getY() {
if (!this.getOption().isEnabled()) {
return null;
}

if (this.children().isEmpty()) {
return ComponentPath.leaf(this);
}

return super.nextFocusPath(event);
}

@Override
public boolean isFocused() {
return super.isFocused() || this.getFocused() != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static ControlValueFormatter resolution() {
};
}

public static ControlValueFormatter fpsLimit() {
return (v) -> (v == 260) ? Component.translatable("options.framerateLimit.max") : Component.translatable("options.framerate", v);
public static ControlValueFormatter fpsLimit(int maximum) {
return (v) -> (v == maximum) ? Component.translatable("options.framerateLimit.max") : Component.translatable("options.framerate", v);
}

public static ControlValueFormatter brightness() {
Expand Down
Loading