Skip to content

Commit

Permalink
Migrate smoothing to a more intuitive config format (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Sep 5, 2023
1 parent b772a1a commit 6ad6337
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void init() {
.useModifier(RotationModifiers.smoothing(
PITCH_SMOOTHER, YAW_SMOOTHER, ROLL_SMOOTHER,
ModConfig.INSTANCE.getSmoothing()
), ModConfig.INSTANCE::getSmoothingEnabled)
))
.useModifier(RotationModifiers::banking, ModConfig.INSTANCE::getEnableBanking),
1000, FALL_FLYING_GROUP);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,16 @@ public static Screen generateConfigScreen(Screen parent) {
.name(getText("sensitivity"))
.group(OptionGroup.createBuilder()
.name(getText("smoothing"))
.option(getBooleanOption("smoothing", "smoothing_enabled", false, false)
.binding(true, () -> ModConfig.INSTANCE.getSmoothingEnabled(), value -> ModConfig.INSTANCE.setSmoothingEnabled(value))
.build())
.option(getOption(Double.class, "smoothing", "smoothing_pitch", false, false)
.controller(option -> getDoubleSlider(option, 0.1, 5.0, 0.1))
.controller(option -> getDoubleSlider(option, 0.0, 5.0, 0.1))
.binding(1.0, () -> ModConfig.INSTANCE.getSmoothingPitch(), value -> ModConfig.INSTANCE.setSmoothingPitch(value))
.build())
.option(getOption(Double.class, "smoothing", "smoothing_yaw", false, false)
.controller(option -> getDoubleSlider(option, 0.1, 5.0, 0.1))
.binding(0.4, () -> ModConfig.INSTANCE.getSmoothingYaw(), value -> ModConfig.INSTANCE.setSmoothingYaw(value))
.controller(option -> getDoubleSlider(option, 0.0, 5.0, 0.1))
.binding(2.5, () -> ModConfig.INSTANCE.getSmoothingYaw(), value -> ModConfig.INSTANCE.setSmoothingYaw(value))
.build())
.option(getOption(Double.class, "smoothing", "smoothing_roll", false, false)
.controller(option -> getDoubleSlider(option, 0.1, 5.0, 0.1))
.controller(option -> getDoubleSlider(option, 0.0, 5.0, 0.1))
.binding(1.0, () -> ModConfig.INSTANCE.getSmoothingRoll(), value -> ModConfig.INSTANCE.setSmoothingRoll(value))
.build())
.build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package nl.enjarai.doabarrelroll.config;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public @interface MigrationValue {
class SerializationStrategy implements ExclusionStrategy {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(MigrationValue.class) != null;
}

@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}
}
45 changes: 25 additions & 20 deletions src/client/java/nl/enjarai/doabarrelroll/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class ModConfig {
public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(ExpressionParser.class, new ExpressionParserTypeAdapter())
.addSerializationExclusionStrategy(new MigrationValue.SerializationStrategy())
.setPrettyPrinting()
.create();
public static final ModConfig DEFAULT = new ModConfig();
Expand All @@ -29,7 +30,20 @@ public static void touch() {
// touch the grass
}

int format_version_do_not_edit = 1;

private void performMigrations() {
if (format_version_do_not_edit <= 1) { // configs below format version 1 use old smoothing values
var enabled = sensitivity.smoothing.smoothing_enabled;
sensitivity.camera_smoothing.pitch = enabled ? 1 / sensitivity.smoothing.smoothing_pitch : 0;
sensitivity.camera_smoothing.yaw = enabled ? 1 / sensitivity.smoothing.smoothing_yaw : 0;
sensitivity.camera_smoothing.roll = enabled ? 1 / sensitivity.smoothing.smoothing_roll : 0;
}

format_version_do_not_edit = 2; // update format version
}


int format_version_do_not_edit = 2;

General general = new General();
static class General {
Expand Down Expand Up @@ -74,6 +88,7 @@ static class Misc {

SensitivityConfig sensitivity = new SensitivityConfig();
static class SensitivityConfig {
@MigrationValue
Smoothing smoothing = new Smoothing();
static class Smoothing {
boolean smoothing_enabled = true;
Expand All @@ -82,6 +97,7 @@ static class Smoothing {
double smoothing_roll = 1.0;
}

Sensitivity camera_smoothing = new Sensitivity(1.0, 2.5, 1.0);
Sensitivity desktop = new Sensitivity();
Sensitivity controller = new Sensitivity();
}
Expand Down Expand Up @@ -169,28 +185,20 @@ public boolean getEnableEasterEggs() {
return general.misc.enable_easter_eggs;
}

public boolean getSmoothingEnabled() {
return sensitivity.smoothing.smoothing_enabled;
}

public double getSmoothingPitch() {
return sensitivity.smoothing.smoothing_pitch;
return sensitivity.camera_smoothing.pitch;
}

public double getSmoothingYaw() {
return sensitivity.smoothing.smoothing_yaw;
return sensitivity.camera_smoothing.yaw;
}

public double getSmoothingRoll() {
return sensitivity.smoothing.smoothing_roll;
return sensitivity.camera_smoothing.roll;
}

public Sensitivity getSmoothing() {
return new Sensitivity(
sensitivity.smoothing.smoothing_pitch,
sensitivity.smoothing.smoothing_yaw,
sensitivity.smoothing.smoothing_roll
);
return sensitivity.camera_smoothing;
}

public Sensitivity getDesktopSensitivity() {
Expand Down Expand Up @@ -313,20 +321,16 @@ public void setEnableEasterEggs(boolean enabled) {
general.misc.enable_easter_eggs = enabled;
}

public void setSmoothingEnabled(boolean enabled) {
sensitivity.smoothing.smoothing_enabled = enabled;
}

public void setSmoothingPitch(double pitch) {
sensitivity.smoothing.smoothing_pitch = pitch;
sensitivity.camera_smoothing.pitch = pitch;
}

public void setSmoothingYaw(double yaw) {
sensitivity.smoothing.smoothing_yaw = yaw;
sensitivity.camera_smoothing.yaw = yaw;
}

public void setSmoothingRoll(double roll) {
sensitivity.smoothing.smoothing_roll = roll;
sensitivity.camera_smoothing.roll = roll;
}

public void setDesktopSensitivity(Sensitivity sensitivity) {
Expand Down Expand Up @@ -437,6 +441,7 @@ private static ModConfig loadConfigFile(File file) {
}

// Saves the file in order to write new fields if they were added
config.performMigrations();
config.saveConfigFile(file);
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public static RollContext.ConfiguresRotation buttonControls(double power) {

public static RollContext.ConfiguresRotation smoothing(SmoothUtil pitchSmoother, SmoothUtil yawSmoother, SmoothUtil rollSmoother, Sensitivity smoothness) {
return (rotationInstant, context) -> RotationInstant.of(
pitchSmoother.smooth(rotationInstant.pitch(), smoothness.pitch * context.getRenderDelta()),
yawSmoother.smooth(rotationInstant.yaw(), smoothness.yaw * context.getRenderDelta()),
rollSmoother.smooth(rotationInstant.roll(), smoothness.roll * context.getRenderDelta())
smoothness.pitch == 0 ? rotationInstant.pitch() : pitchSmoother.smooth(rotationInstant.pitch(), 1 / smoothness.pitch * context.getRenderDelta()),
smoothness.yaw == 0 ? rotationInstant.yaw() : yawSmoother.smooth(rotationInstant.yaw(), 1 / smoothness.yaw * context.getRenderDelta()),
smoothness.roll == 0 ? rotationInstant.roll() : rollSmoother.smooth(rotationInstant.roll(), 1 / smoothness.roll * context.getRenderDelta())
);
}

Expand Down

0 comments on commit 6ad6337

Please sign in to comment.