-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: movement speed sliders (#190)
- Loading branch information
Showing
8 changed files
with
249 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
common/src/main/java/net/xolt/freecam/config/gui/AutoConfigExtensions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package net.xolt.freecam.config.gui; | ||
|
||
import me.shedaniel.autoconfig.AutoConfig; | ||
import me.shedaniel.autoconfig.ConfigData; | ||
import me.shedaniel.autoconfig.gui.DefaultGuiProviders; | ||
import me.shedaniel.autoconfig.gui.DefaultGuiTransformers; | ||
import me.shedaniel.autoconfig.gui.registry.GuiRegistry; | ||
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; | ||
import net.minecraft.network.chat.Component; | ||
|
||
/** | ||
* Extensions and modifications to AutoConfig. | ||
* | ||
* @see DefaultGuiProviders | ||
* @see DefaultGuiTransformers | ||
*/ | ||
public class AutoConfigExtensions { | ||
static final Component RESET_TEXT = Component.translatable("text.cloth-config.reset_value"); | ||
static final ConfigEntryBuilder ENTRY_BUILDER = ConfigEntryBuilder.create(); | ||
|
||
private AutoConfigExtensions() {} | ||
|
||
public static void apply(Class<? extends ConfigData> configClass) { | ||
GuiRegistry registry = AutoConfig.getGuiRegistry(configClass); | ||
VariantTooltipImpl.apply(registry); | ||
BoundedContinuousImpl.apply(registry); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
common/src/main/java/net/xolt/freecam/config/gui/BoundedContinuous.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.xolt.freecam.config.gui; | ||
|
||
import me.shedaniel.autoconfig.annotation.ConfigEntry; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* A continuous version of {@link ConfigEntry.BoundedDiscrete}. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface BoundedContinuous { | ||
|
||
/** | ||
* The number of decimal places the slider will round to. | ||
*/ | ||
int precision() default 2; | ||
|
||
double min() default 0; | ||
double max(); | ||
} |
31 changes: 31 additions & 0 deletions
31
common/src/main/java/net/xolt/freecam/config/gui/BoundedContinuousImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.xolt.freecam.config.gui; | ||
|
||
import me.shedaniel.autoconfig.gui.registry.GuiRegistry; | ||
import me.shedaniel.autoconfig.util.Utils; | ||
import net.minecraft.network.chat.Component; | ||
|
||
import java.util.Collections; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
import static net.xolt.freecam.config.gui.AutoConfigExtensions.RESET_TEXT; | ||
|
||
class BoundedContinuousImpl { | ||
|
||
private BoundedContinuousImpl() {} | ||
|
||
static void apply(GuiRegistry registry) { | ||
|
||
registry.registerAnnotationProvider( | ||
(i18n, field, config, defaults, guiProvider) -> { | ||
Consumer<Double> save = newValue -> Utils.setUnsafely(field, config, newValue); | ||
Supplier<Double> defaultValue = () -> Utils.getUnsafely(field, defaults); | ||
double value = Utils.getUnsafely(field, config, defaultValue.get()); | ||
BoundedContinuous bounds = field.getAnnotation(BoundedContinuous.class); | ||
return Collections.singletonList(new DoubleSliderEntry(Component.translatable(i18n), bounds.precision(), bounds.min(), bounds.max(), value, RESET_TEXT, defaultValue, save)); | ||
}, | ||
field -> field.getType() == Double.TYPE || field.getType() == Double.class, | ||
BoundedContinuous.class | ||
); | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
common/src/main/java/net/xolt/freecam/config/gui/DoubleSliderEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package net.xolt.freecam.config.gui; | ||
|
||
import com.google.common.util.concurrent.AtomicDouble; | ||
import com.mojang.blaze3d.platform.Window; | ||
import me.shedaniel.clothconfig2.gui.entries.TooltipListEntry; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.gui.components.AbstractSliderButton; | ||
import net.minecraft.client.gui.components.AbstractWidget; | ||
import net.minecraft.client.gui.components.Button; | ||
import net.minecraft.client.gui.components.events.GuiEventListener; | ||
import net.minecraft.client.gui.narration.NarratableEntry; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.util.Mth; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.text.DecimalFormat; | ||
import java.text.NumberFormat; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
|
||
import static net.xolt.freecam.Freecam.MC; | ||
|
||
/** | ||
* {@link IntegerSliderEntry} ported from {@code int} to {@code double}. | ||
*/ | ||
class DoubleSliderEntry extends TooltipListEntry<Double> { | ||
private final Slider sliderWidget; | ||
private final Button resetButton; | ||
private final AtomicDouble value; | ||
private final double original; | ||
private final int precision; | ||
private final double minimum; | ||
private final double maximum; | ||
private final Supplier<Double> defaultValue; | ||
private final List<AbstractWidget> widgets; | ||
|
||
DoubleSliderEntry(Component fieldName, int precision, double minimum, double maximum, double value, Component resetText, Supplier<Double> defaultValue, @Nullable Consumer<Double> save) { | ||
//noinspection deprecation,UnstableApiUsage | ||
super(fieldName, null); | ||
this.value = new AtomicDouble(value); | ||
this.original = value; | ||
this.defaultValue = defaultValue; | ||
this.maximum = maximum; | ||
this.minimum = minimum; | ||
this.precision = precision; | ||
this.saveCallback = save; | ||
this.sliderWidget = new Slider(0, 0, 152, 20, (this.value.get() - minimum) / (maximum - minimum)); | ||
this.sliderWidget.updateMessage(); | ||
this.resetButton = Button.builder(resetText, widget -> this.setValue(this.defaultValue.get())) | ||
.width(MC.font.width(resetText) + 6) | ||
.build(); | ||
this.widgets = List.of(this.sliderWidget, this.resetButton); | ||
} | ||
|
||
@Override | ||
public Double getValue() { | ||
return value.get(); | ||
} | ||
|
||
public void setValue(double value) { | ||
double clamped = Mth.clamp(value, minimum, maximum); | ||
this.value.set(clamped); | ||
sliderWidget.setValue((clamped - minimum) / (maximum - minimum)); | ||
sliderWidget.updateMessage(); | ||
} | ||
|
||
@Override | ||
public boolean isEdited() { | ||
return super.isEdited() || getValue() != original; | ||
} | ||
|
||
@Override | ||
public Optional<Double> getDefaultValue() { | ||
return Optional.ofNullable(defaultValue).map(Supplier::get); | ||
} | ||
|
||
@Override | ||
public @NotNull List<? extends GuiEventListener> children() { | ||
return widgets; | ||
} | ||
|
||
@Override | ||
public List<? extends NarratableEntry> narratables() { | ||
return widgets; | ||
} | ||
|
||
@Override | ||
public void render(GuiGraphics graphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isHovered, float delta) { | ||
super.render(graphics, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isHovered, delta); | ||
Window window = MC.getWindow(); | ||
resetButton.active = isEditable() && getDefaultValue().isPresent() && defaultValue.get() != value.get(); | ||
resetButton.setY(y); | ||
sliderWidget.active = isEditable(); | ||
sliderWidget.setY(y); | ||
|
||
Component name = getDisplayedFieldName(); | ||
if (MC.font.isBidirectional()) { | ||
graphics.drawString(MC.font, name.getVisualOrderText(), window.getGuiScaledWidth() - x - MC.font.width(name), y + 6, getPreferredTextColor()); | ||
resetButton.setX(x); | ||
sliderWidget.setX(x + resetButton.getWidth() + 1); | ||
} else { | ||
graphics.drawString(MC.font, name.getVisualOrderText(), x, y + 6, getPreferredTextColor()); | ||
resetButton.setX(x + entryWidth - resetButton.getWidth()); | ||
sliderWidget.setX(x + entryWidth - 150); | ||
} | ||
|
||
sliderWidget.setWidth(150 - resetButton.getWidth() - 2); | ||
resetButton.render(graphics, mouseX, mouseY, delta); | ||
sliderWidget.render(graphics, mouseX, mouseY, delta); | ||
} | ||
|
||
private final class Slider extends AbstractSliderButton { | ||
private Slider(int x, int y, int width, int height, double value) { | ||
super(x, y, width, height, Component.empty(), value); | ||
} | ||
|
||
@Override | ||
public void updateMessage() { | ||
NumberFormat fmt = DecimalFormat.getInstance(); | ||
fmt.setMinimumIntegerDigits(1); | ||
fmt.setMinimumFractionDigits(precision); | ||
fmt.setMaximumFractionDigits(precision); | ||
setMessage(Component.literal("Value: " + fmt.format(DoubleSliderEntry.this.value.get()))); | ||
} | ||
|
||
@Override | ||
protected void applyValue() { | ||
double rounded = BigDecimal.valueOf(DoubleSliderEntry.this.minimum + (DoubleSliderEntry.this.maximum - DoubleSliderEntry.this.minimum) * this.value) | ||
.setScale(DoubleSliderEntry.this.precision, RoundingMode.HALF_UP) | ||
.doubleValue(); | ||
DoubleSliderEntry.this.value.set(rounded); | ||
} | ||
|
||
@Override | ||
public boolean keyPressed(int keyCode, int scanCode, int modifiers) { | ||
return DoubleSliderEntry.this.isEditable() && super.keyPressed(keyCode, scanCode, modifiers); | ||
} | ||
|
||
@Override | ||
public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) { | ||
return DoubleSliderEntry.this.isEditable() && super.mouseDragged(mouseX, mouseY, button, dragX, dragY); | ||
} | ||
|
||
public void setValue(double value) { | ||
this.value = value; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...t/xolt/freecam/config/VariantTooltip.java → ...lt/freecam/config/gui/VariantTooltip.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters