Public Release

This commit is contained in:
Moulberry committed 2024-08-24 04:41:01 +08:00
1 parent 666398e480
commit 61bb497a22
126 files changed
+8435 -1240

No files matched your search

@@ -1,36 +1,78 @@
package com.moulberry.flashback.state;
import com.google.gson.*;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.FlashbackGson;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
import com.moulberry.flashback.keyframe.impl.FOVKeyframe;
import com.moulberry.flashback.keyframe.impl.TickrateKeyframe;
import com.moulberry.flashback.keyframe.impl.TimeOfDayKeyframe;
import com.moulberry.flashback.serialization.QuaternionTypeAdapater;
import com.moulberry.flashback.serialization.Vector3fTypeAdapater;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.visuals.ReplayVisuals;
import org.jetbrains.annotations.Nullable;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
public class EditorState {
public transient boolean dirty = false;
transient boolean dirty = false;
public transient int modCount = ThreadLocalRandom.current().nextInt();
public final List<KeyframeTrack> keyframeTracks = new ArrayList<>();
public final ReplayVisuals replayVisuals = new ReplayVisuals();
private final EditorStateHistory history = new EditorStateHistory();
public float zoomMin = 0.0f;
public float zoomMax = 1.0f;
public double zoomMin = 0.0;
public double zoomMax = 1.0;
public int exportStartTicks = -1;
public int exportEndTicks = -1;
public void setKeyframe(int trackIndex, int tick, Keyframe keyframe) {
if (trackIndex >= this.keyframeTracks.size()) {
return;
}
List<EditorStateHistoryAction> undo = new ArrayList<>();
List<EditorStateHistoryAction> redo = new ArrayList<>();
String description;
KeyframeTrack track = this.keyframeTracks.get(trackIndex);
Keyframe old = track.keyframesByTick.get(tick);
if (old != null) {
undo.add(new EditorStateHistoryAction.SetKeyframe(track.keyframeType, trackIndex, tick, old.copy()));
description = "Replaced " + track.keyframeType + " keyframe";
} else {
undo.add(new EditorStateHistoryAction.RemoveKeyframe(track.keyframeType, trackIndex, tick));
description = "Added " + track.keyframeType + " keyframe";
}
redo.add(new EditorStateHistoryAction.SetKeyframe(track.keyframeType, trackIndex, tick, keyframe.copy()));
this.push(new EditorStateHistoryEntry(undo, redo, description));
}
public void push(EditorStateHistoryEntry entry) {
if (entry.undo().isEmpty() && entry.redo().isEmpty()) {
return;
}
this.history.push(this, entry);
this.markDirty();
}
public void undo(Consumer<String> descriptionConsumer) {
this.history.undo(this, descriptionConsumer);
this.markDirty();
}
public void redo(Consumer<String> descriptionConsumer) {
this.history.redo(this, descriptionConsumer);
this.markDirty();
}
public void setExportTicks(int start, int end, int totalTicks) {
if (this.exportEndTicks < 0) {
this.exportEndTicks = totalTicks;
@@ -52,7 +94,7 @@ public class EditorState {
}
}
if (this.exportStartTicks == 0 && this.exportEndTicks == totalTicks) {
if (this.exportStartTicks <= 0 && this.exportEndTicks >= totalTicks) {
this.exportStartTicks = -1;
this.exportEndTicks = -1;
}
@@ -60,6 +102,11 @@ public class EditorState {
this.dirty = true;
}
public void markDirty() {
this.dirty = true;
this.modCount += 1;
}
public void save(Path path) {
this.dirty = false;
@@ -80,16 +127,23 @@ public class EditorState {
return null;
}
String serialized = null;
try {
String serialized = Files.readString(path);
serialized = Files.readString(path);
return FlashbackGson.PRETTY.fromJson(serialized, EditorState.class);
} catch (Exception e) {
e.printStackTrace();
Flashback.LOGGER.error("Error loading editor state", e);
Flashback.LOGGER.error("JSON: {}", serialized);
return null;
}
}
public <T> void applyKeyframes(T t, float tick) {
public EditorState copy() {
String serialized = FlashbackGson.COMPRESSED.toJson(this, EditorState.class);
return FlashbackGson.COMPRESSED.fromJson(serialized, EditorState.class);
}
public void applyKeyframes(KeyframeHandler keyframeHandler, float tick) {
EnumSet<KeyframeType> applied = EnumSet.noneOf(KeyframeType.class);
for (KeyframeTrack keyframeTrack : this.keyframeTracks) {
// Ignore lines that are disabled
@@ -97,14 +151,16 @@ public class EditorState {
continue;
}
KeyframeType baseType = keyframeTrack.keyframeType == KeyframeType.CAMERA_ORBIT ? KeyframeType.CAMERA : keyframeTrack.keyframeType;
// Already applied a keyframe of this type earlier, skip
if (applied.contains(keyframeTrack.keyframeType)) {
if (applied.contains(baseType)) {
continue;
}
// Try to apply keyframes, mark applied if successful
if (keyframeTrack.tryApplyKeyframes(t, tick)) {
applied.add(keyframeTrack.keyframeType);
if (keyframeTrack.tryApplyKeyframes(keyframeHandler, tick)) {
applied.add(baseType);
}
}
}
@@ -0,0 +1,56 @@
package com.moulberry.flashback.state;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class EditorStateHistory {
private final List<EditorStateHistoryEntry> entries = new ArrayList<>();
private int position = 0;
public void push(EditorState editorState, EditorStateHistoryEntry entry) {
while (this.entries.size() > this.position) {
this.entries.removeLast();
}
while (this.entries.size() >= 256) {
this.entries.removeFirst();
this.position -= 1;
}
for (EditorStateHistoryAction redo : entry.redo()) {
redo.apply(editorState);
}
this.entries.add(entry);
this.position += 1;
}
public void undo(EditorState editorState, Consumer<String> descriptionConsumer) {
if (this.position == 0) {
return;
}
this.position -= 1;
EditorStateHistoryEntry entry = this.entries.get(this.position);
for (EditorStateHistoryAction undo : entry.undo()) {
undo.apply(editorState);
}
descriptionConsumer.accept("Undo '" + entry.description() + "'");
}
public void redo(EditorState editorState, Consumer<String> descriptionConsumer) {
if (this.position >= this.entries.size()) {
return;
}
EditorStateHistoryEntry entry = this.entries.get(this.position);
for (EditorStateHistoryAction redo : entry.redo()) {
redo.apply(editorState);
}
descriptionConsumer.accept("Redo '" + entry.description() + "'");
this.position += 1;
}
}
@@ -0,0 +1,186 @@
package com.moulberry.flashback.state;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import java.lang.reflect.Type;
public interface EditorStateHistoryAction {
void apply(EditorState editorState);
record SetKeyframe(KeyframeType type, int trackIndex, int tick, Keyframe keyframe) implements EditorStateHistoryAction {
@Override
public void apply(EditorState editorState) {
if (this.trackIndex < editorState.keyframeTracks.size()) {
KeyframeTrack track = editorState.keyframeTracks.get(this.trackIndex);
if (track.keyframeType == this.type) {
track.keyframesByTick.put(this.tick, this.keyframe.copy());
}
}
}
public static class TypeAdapter implements JsonSerializer<SetKeyframe>, JsonDeserializer<SetKeyframe> {
@Override
public SetKeyframe deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
KeyframeType type = context.deserialize(jsonObject.get("keyframe_type"), KeyframeType.class);
int trackIndex = jsonObject.get("trackIndex").getAsInt();
int tick = jsonObject.get("tick").getAsInt();
Keyframe keyframe = context.deserialize(jsonObject.get("keyframe"), Keyframe.class);
return new SetKeyframe(type, trackIndex, tick, keyframe);
}
@Override
public JsonElement serialize(SetKeyframe src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action_type", "set_keyframe");
jsonObject.add("keyframe_type", context.serialize(src.type));
jsonObject.addProperty("trackIndex", src.trackIndex);
jsonObject.addProperty("tick", src.tick);
jsonObject.add("keyframe", context.serialize(src.keyframe));
return jsonObject;
}
}
}
record RemoveKeyframe(KeyframeType type, int trackIndex, int tick) implements EditorStateHistoryAction {
@Override
public void apply(EditorState editorState) {
if (this.trackIndex < editorState.keyframeTracks.size()) {
KeyframeTrack track = editorState.keyframeTracks.get(this.trackIndex);
if (track.keyframeType == this.type) {
track.keyframesByTick.remove(this.tick);
}
}
}
public static class TypeAdapter implements JsonSerializer<RemoveKeyframe>, JsonDeserializer<RemoveKeyframe> {
@Override
public RemoveKeyframe deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
KeyframeType type = context.deserialize(jsonObject.get("keyframe_type"), KeyframeType.class);
int trackIndex = jsonObject.get("trackIndex").getAsInt();
int tick = jsonObject.get("tick").getAsInt();
return new RemoveKeyframe(type, trackIndex, tick);
}
@Override
public JsonElement serialize(RemoveKeyframe src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action_type", "remove_keyframe");
jsonObject.add("keyframe_type", context.serialize(src.type));
jsonObject.addProperty("trackIndex", src.trackIndex);
jsonObject.addProperty("tick", src.tick);
return jsonObject;
}
}
}
record AddTrack(KeyframeType type, int trackIndex) implements EditorStateHistoryAction {
@Override
public void apply(EditorState editorState) {
if (this.trackIndex <= editorState.keyframeTracks.size()) {
editorState.keyframeTracks.add(this.trackIndex, new KeyframeTrack(this.type));
}
}
public static class TypeAdapter implements JsonSerializer<AddTrack>, JsonDeserializer<AddTrack> {
@Override
public AddTrack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
KeyframeType type = context.deserialize(jsonObject.get("keyframe_type"), KeyframeType.class);
int trackIndex = jsonObject.get("trackIndex").getAsInt();
return new AddTrack(type, trackIndex);
}
@Override
public JsonElement serialize(AddTrack src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action_type", "add_track");
jsonObject.add("keyframe_type", context.serialize(src.type));
jsonObject.addProperty("trackIndex", src.trackIndex);
return jsonObject;
}
}
}
record RemoveTrack(KeyframeType type, int trackIndex) implements EditorStateHistoryAction {
@Override
public void apply(EditorState editorState) {
if (this.trackIndex < editorState.keyframeTracks.size()) {
KeyframeTrack keyframeTrack = editorState.keyframeTracks.get(this.trackIndex);
if (keyframeTrack.keyframeType == type) {
editorState.keyframeTracks.remove(this.trackIndex);
}
}
}
public static class TypeAdapter implements JsonSerializer<RemoveTrack>, JsonDeserializer<RemoveTrack> {
@Override
public RemoveTrack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
KeyframeType type = context.deserialize(jsonObject.get("keyframe_type"), KeyframeType.class);
int trackIndex = jsonObject.get("trackIndex").getAsInt();
return new RemoveTrack(type, trackIndex);
}
@Override
public JsonElement serialize(RemoveTrack src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("action_type", "remove_track");
jsonObject.add("keyframe_type", context.serialize(src.type));
jsonObject.addProperty("trackIndex", src.trackIndex);
return jsonObject;
}
}
}
class TypeAdapter implements JsonSerializer<EditorStateHistoryAction>, JsonDeserializer<EditorStateHistoryAction> {
@Override
public EditorStateHistoryAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String type = jsonObject.get("action_type").getAsString();
return switch (type) {
case "set_keyframe" -> context.deserialize(json, SetKeyframe.class);
case "remove_keyframe" -> context.deserialize(json, RemoveKeyframe.class);
case "add_track" -> context.deserialize(json, AddTrack.class);
case "remove_track" -> context.deserialize(json, RemoveTrack.class);
default -> throw new IllegalStateException("Unknown action type: " + type);
};
}
@Override
public JsonElement serialize(EditorStateHistoryAction src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObject;
switch (src) {
case SetKeyframe setKeyframe -> {
jsonObject = (JsonObject) context.serialize(setKeyframe);
jsonObject.addProperty("action_type", "set_keyframe");
}
case RemoveKeyframe removeKeyframe -> {
jsonObject = (JsonObject) context.serialize(removeKeyframe);
jsonObject.addProperty("action_type", "remove_keyframe");
}
case AddTrack addTrack -> {
jsonObject = (JsonObject) context.serialize(addTrack);
jsonObject.addProperty("action_type", "add_track");
}
case RemoveTrack removeTrack -> {
jsonObject = (JsonObject) context.serialize(removeTrack);
jsonObject.addProperty("action_type", "remove_track");
}
default -> throw new IllegalStateException("Unknown action type: " + src.getClass());
}
return jsonObject;
}
}
}
@@ -0,0 +1,6 @@
package com.moulberry.flashback.state;
import java.util.List;
public record EditorStateHistoryEntry(List<EditorStateHistoryAction> undo, List<EditorStateHistoryAction> redo, String description) {
}
@@ -11,7 +11,7 @@ import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;
public class EditorStateCache {
public class EditorStateManager {
private static long AUTOSAVE_INTERVAL_MILLIS = 30 * 1000; // 30 seconds
@@ -106,6 +106,12 @@ public class EditorStateCache {
@Nullable
public static EditorState getCurrent() {
if (Flashback.isExporting()) {
return Flashback.EXPORT_JOB.getSettings().editorState();
}
if (!Flashback.isInReplay()) {
return null;
}
return current;
}
@@ -133,8 +139,8 @@ public class EditorStateCache {
if (old) {
filename += ".old";
}
return Flashback.getConfigDirectory()
.resolve("replay_states")
return Flashback.getDataDirectory()
.resolve("editor_states")
.resolve(filename);
}
@@ -1,7 +1,11 @@
package com.moulberry.flashback.state;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.editor.ui.ReplayUI;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.impl.TimelapseKeyframe;
import com.moulberry.flashback.keyframe.interpolation.SidedInterpolationType;
import java.util.Map;
@@ -10,31 +14,39 @@ import java.util.TreeMap;
public class KeyframeTrack {
public final KeyframeType keyframeType;
public TreeMap<Integer, Keyframe<?>> keyframesByTick = new TreeMap<>();
public TreeMap<Integer, Keyframe> keyframesByTick = new TreeMap<>();
public boolean enabled = true;
public KeyframeTrack(KeyframeType keyframeType) {
this.keyframeType = keyframeType;
}
@SuppressWarnings("unchecked")
public <T> boolean tryApplyKeyframes(T t, float tick) {
public boolean tryApplyKeyframes(KeyframeHandler t, float tick) {
if (!t.supportedKeyframes().contains(this.keyframeType)) {
return false;
}
if (this.keyframeType == KeyframeType.TIMELAPSE) {
return this.tryApplyKeyframesTimelapse(t, tick);
}
// Skip if empty
TreeMap<Integer, Keyframe<?>> keyframeTimes = this.keyframesByTick;
TreeMap<Integer, Keyframe> keyframeTimes = this.keyframesByTick;
if (keyframeTimes.isEmpty()) {
return false;
}
Map.Entry<Integer, Keyframe<?>> lowerEntry = keyframeTimes.floorEntry((int) tick);
Map.Entry<Integer, Keyframe> lowerEntry = keyframeTimes.floorEntry((int) tick);
if (lowerEntry == null) {
return false;
}
if (lowerEntry.getValue().operandClass != t.getClass()) {
return false;
}
Keyframe lowerKeyframe = lowerEntry.getValue();
Keyframe<T> lowerKeyframe = (Keyframe<T>) lowerEntry.getValue();
if (tick == lowerEntry.getKey()) {
lowerKeyframe.apply(t);
return true;
}
SidedInterpolationType leftInterpolation = lowerEntry.getValue().interpolationType().rightSide;
@@ -45,8 +57,14 @@ public class KeyframeTrack {
}
// Get next entry, skip if tick is not between two keyframes
Map.Entry<Integer, Keyframe<?>> ceilEntry = keyframeTimes.ceilingEntry(lowerEntry.getKey() + 1);
Map.Entry<Integer, Keyframe> ceilEntry = keyframeTimes.ceilingEntry(lowerEntry.getKey() + 1);
if (ceilEntry == null) {
if ((int) tick == lowerEntry.getKey()) {
lowerKeyframe.apply(t);
return true;
} else if (t.alwaysApplyLastKeyframe()) {
lowerKeyframe.apply(t);
}
return false;
}
@@ -61,12 +79,12 @@ public class KeyframeTrack {
rightInterpolation == SidedInterpolationType.SMOOTH;
if (isUsingSmooth) {
Map.Entry<Integer, Keyframe<?>> beforeEntry = keyframeTimes.floorEntry(lowerEntry.getKey() - 1);
Map.Entry<Integer, Keyframe> beforeEntry = keyframeTimes.floorEntry(lowerEntry.getKey() - 1);
if (beforeEntry == null) {
beforeEntry = lowerEntry;
}
Map.Entry<Integer, Keyframe<?>> afterAfterEntry = keyframeTimes.ceilingEntry(ceilEntry.getKey() + 1);
Map.Entry<Integer, Keyframe> afterAfterEntry = keyframeTimes.ceilingEntry(ceilEntry.getKey() + 1);
if (afterAfterEntry == null) {
afterAfterEntry = ceilEntry;
}
@@ -84,7 +102,7 @@ public class KeyframeTrack {
lerpFromRight = false;
}
((Keyframe<T>)beforeEntry.getValue()).applyInterpolatedSmooth(t, lowerEntry.getValue(), ceilEntry.getValue(), afterAfterEntry.getValue(),
beforeEntry.getValue().applyInterpolatedSmooth(t, lowerEntry.getValue(), ceilEntry.getValue(), afterAfterEntry.getValue(),
beforeEntry.getKey(), lowerEntry.getKey(), ceilEntry.getKey(), afterAfterEntry.getKey(), amount, lerpAmount, lerpFromRight);
} else {
amount = SidedInterpolationType.interpolate(leftInterpolation, rightInterpolation, amount);
@@ -99,4 +117,37 @@ public class KeyframeTrack {
return true;
}
private boolean tryApplyKeyframesTimelapse(KeyframeHandler t, float tick) {
// Skip if empty
TreeMap<Integer, Keyframe> keyframeTimes = this.keyframesByTick;
if (keyframeTimes.isEmpty()) {
return false;
}
Map.Entry<Integer, Keyframe> lowerEntry = keyframeTimes.floorEntry((int) tick);
Map.Entry<Integer, Keyframe> ceilEntry = keyframeTimes.ceilingEntry(((int) tick) + 1);
if (ceilEntry == null && lowerEntry != null && lowerEntry.getKey() == (int) tick) {
ceilEntry = lowerEntry;
lowerEntry = keyframeTimes.floorEntry((int) tick - 1);
}
if (lowerEntry != null && ceilEntry != null) {
int lowerTicks = ((TimelapseKeyframe) lowerEntry.getValue()).ticks;
int ceilTicks = ((TimelapseKeyframe) ceilEntry.getValue()).ticks;
if (ceilTicks <= lowerTicks) {
ReplayUI.setInfoOverlayShort("Unable to timelapse. Right keyframe's time must be greater than left keyframe's time");
t.applyTickrate(20);
} else {
double tickrate = (double) (ceilEntry.getKey() - lowerEntry.getKey()) / (ceilTicks - lowerTicks) * 20;
t.applyTickrate((float) tickrate);
}
return true;
}
return false;
}
}