Add Catmull-Rom spline interpolator to simplepathing

This commit is contained in:
johni0702
2017-01-28 11:03:59 +01:00
parent 5cbfd2884e
commit bdbffd5c33
6 changed files with 99 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
package com.replaymod.simplepathing;
import com.replaymod.replaystudio.pathing.interpolation.CatmullRomSplineInterpolator;
import com.replaymod.replaystudio.pathing.interpolation.CubicSplineInterpolator;
import com.replaymod.replaystudio.pathing.interpolation.Interpolator;
import com.replaymod.replaystudio.pathing.interpolation.LinearInterpolator;
@@ -11,6 +12,7 @@ import java.util.function.Supplier;
@AllArgsConstructor
public enum InterpolatorType {
DEFAULT("default", null, null),
CATMULL_ROM("catmullrom", CatmullRomSplineInterpolator.class, () -> new CatmullRomSplineInterpolator(0.5)),
CUBIC("cubic", CubicSplineInterpolator.class, CubicSplineInterpolator::new),
LINEAR("linear", LinearInterpolator.class, LinearInterpolator::new);
@@ -34,7 +36,7 @@ public enum InterpolatorType {
for (InterpolatorType t : values()) {
if (t.getI18nName().equals(string)) return t;
}
return CUBIC; //the default
return CATMULL_ROM; //the default
}
public static InterpolatorType fromClass(Class<? extends Interpolator> cls) {

View File

@@ -2,6 +2,8 @@ package com.replaymod.simplepathing;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.replaymod.pathing.properties.CameraProperties;
@@ -14,6 +16,7 @@ import com.replaymod.replaystudio.pathing.change.CombinedChange;
import com.replaymod.replaystudio.pathing.change.SetInterpolator;
import com.replaymod.replaystudio.pathing.change.UpdateKeyframeProperties;
import com.replaymod.replaystudio.pathing.impl.TimelineImpl;
import com.replaymod.replaystudio.pathing.interpolation.CatmullRomSplineInterpolator;
import com.replaymod.replaystudio.pathing.interpolation.CubicSplineInterpolator;
import com.replaymod.replaystudio.pathing.interpolation.Interpolator;
import com.replaymod.replaystudio.pathing.interpolation.LinearInterpolator;
@@ -68,7 +71,7 @@ public class SPTimeline implements PathingRegistry {
@Getter
private EntityPositionTracker entityTracker;
private InterpolatorType defaultInterpolatorType = InterpolatorType.CUBIC;
private InterpolatorType defaultInterpolatorType = InterpolatorType.fromString("invalid string returns default");
public SPTimeline() {
this(createInitialTimeline());
@@ -573,6 +576,11 @@ public class SPTimeline implements PathingRegistry {
writer.value("linear");
} else if (interpolator instanceof CubicSplineInterpolator) {
writer.value("cubic-spline");
} else if (interpolator instanceof CatmullRomSplineInterpolator) {
writer.beginObject();
writer.name("type").value("catmull-rom-spline");
writer.name("alpha").value(((CatmullRomSplineInterpolator) interpolator).getAlpha());
writer.endObject();
} else {
throw new IOException("Unknown interpolator type: " + interpolator);
}
@@ -580,12 +588,30 @@ public class SPTimeline implements PathingRegistry {
@Override
public Interpolator deserializeInterpolator(JsonReader reader) throws IOException {
String type = reader.nextString();
String type;
JsonObject args;
switch (reader.peek()) {
case STRING:
type = reader.nextString();
args = null;
break;
case BEGIN_OBJECT:
args = new JsonParser().parse(reader).getAsJsonObject();
type = args.get("type").getAsString();
break;
default:
throw new IOException("Unexpected token: " + reader.peek());
}
switch (type) {
case "linear":
return new LinearInterpolator();
case "cubic-spline":
return new CubicSplineInterpolator();
case "catmull-rom-spline":
if (args == null || !args.has("alpha")) {
throw new IOException("Missing alpha value for catmull-rom-spline.");
}
return new CatmullRomSplineInterpolator(args.get("alpha").getAsDouble());
default:
throw new IOException("Unknown interpolation type: " + type);

View File

@@ -5,6 +5,7 @@ import com.replaymod.pathing.properties.TimestampProperty;
import com.replaymod.replay.ReplayModReplay;
import com.replaymod.replaystudio.pathing.change.Change;
import com.replaymod.replaystudio.pathing.change.CombinedChange;
import com.replaymod.replaystudio.pathing.interpolation.CatmullRomSplineInterpolator;
import com.replaymod.replaystudio.pathing.interpolation.CubicSplineInterpolator;
import com.replaymod.replaystudio.pathing.interpolation.Interpolator;
import com.replaymod.replaystudio.pathing.interpolation.LinearInterpolator;
@@ -317,6 +318,9 @@ public abstract class GuiEditKeyframe<T extends GuiEditKeyframe<T>> extends Abst
removeElement(this.settingsPanel);
switch (getInterpolatorTypeNoDefault(type)) {
case CATMULL_ROM:
settingsPanel = new CatmullRomSettingsPanel();
break;
case CUBIC:
settingsPanel = new CubicSettingsPanel();
break;
@@ -353,6 +357,33 @@ public abstract class GuiEditKeyframe<T extends GuiEditKeyframe<T>> extends Abst
public abstract I createInterpolator();
}
public class CatmullRomSettingsPanel extends SettingsPanel<CatmullRomSplineInterpolator, CatmullRomSettingsPanel> {
public final GuiLabel alphaLabel = new GuiLabel().setColor(Colors.BLACK)
.setI18nText("replaymod.gui.editkeyframe.interpolator.catmullrom.alpha");
public final GuiNumberField alphaField = new GuiNumberField().setSize(100, 20).setPrecision(5)
.setMinValue(0).setValidateOnFocusChange(true);
{
setLayout(new HorizontalLayout(HorizontalLayout.Alignment.CENTER));
addElements(new HorizontalLayout.Data(0.5), alphaLabel, alphaField);
}
@Override
public void loadSettings(CatmullRomSplineInterpolator interpolator) {
alphaField.setValue(interpolator.getAlpha());
}
@Override
public CatmullRomSplineInterpolator createInterpolator() {
return new CatmullRomSplineInterpolator(alphaField.getDouble());
}
@Override
protected CatmullRomSettingsPanel getThis() {
return this;
}
}
public class CubicSettingsPanel extends SettingsPanel<CubicSplineInterpolator, CubicSettingsPanel> {
@Override

View File

@@ -292,6 +292,7 @@ replaymod.gui.settings.title=Replay Mod Settings
replaymod.gui.settings.interpolation.linear=Linear
replaymod.gui.settings.interpolation.cubic=Cubic
replaymod.gui.settings.interpolation.catmullrom=Catmull Rom
replaymod.gui.settings.bitrate=Video Bitrate
replaymod.gui.settings.framerate=Video Framerate
@@ -376,6 +377,9 @@ replaymod.gui.editkeyframe.spec.method.shoulder.smoothness=Path Smoothness
replaymod.gui.editkeyframe.interpolator=Interpolator
replaymod.gui.editkeyframe.interpolator.default.name=Default Interpolator
replaymod.gui.editkeyframe.interpolator.default.desc=Uses the default interpolator defined in the Replay Mod Settings.
replaymod.gui.editkeyframe.interpolator.catmullrom.name=Catmull Rom Spline Interpolator
replaymod.gui.editkeyframe.interpolator.catmullrom.desc=Calculates a catmull rom spline with customizable alpha value for precise tightness and smoothness of the camera path.
replaymod.gui.editkeyframe.interpolator.catmullrom.alpha=Alpha:
replaymod.gui.editkeyframe.interpolator.cubic.name=Cubic Spline Interpolator
replaymod.gui.editkeyframe.interpolator.cubic.desc=Calculates a cubic equation matrix for all points for a smooth camera path.
replaymod.gui.editkeyframe.interpolator.linear.name=Linear Interpolator