mirror of
https://github.com/darksaid98/Flashback.git
synced 2026-12-01 22:16:01 +00:00
Implement scene switcher
This commit is contained in:
1 parent
3600e535d5
commit
775b49bd8a
15 files changed
+473
-314
No files matched your search
@@ -0,0 +1,90 @@
|
||||
package com.moulberry.flashback.state;
|
||||
|
||||
import com.moulberry.flashback.keyframe.Keyframe;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class EditorScene {
|
||||
|
||||
public String name;
|
||||
public final List<KeyframeTrack> keyframeTracks = new ArrayList<>();
|
||||
public int exportStartTicks = -1;
|
||||
public int exportEndTicks = -1;
|
||||
|
||||
private final EditorSceneHistory history = new EditorSceneHistory();
|
||||
|
||||
public EditorScene(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setKeyframe(int trackIndex, int tick, Keyframe keyframe) {
|
||||
if (trackIndex >= this.keyframeTracks.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<EditorSceneHistoryAction> undo = new ArrayList<>();
|
||||
List<EditorSceneHistoryAction> redo = new ArrayList<>();
|
||||
|
||||
String description;
|
||||
|
||||
KeyframeTrack track = this.keyframeTracks.get(trackIndex);
|
||||
Keyframe old = track.keyframesByTick.get(tick);
|
||||
if (old != null) {
|
||||
undo.add(new EditorSceneHistoryAction.SetKeyframe(track.keyframeType, trackIndex, tick, old.copy()));
|
||||
description = "Replaced " + track.keyframeType.name() + " keyframe";
|
||||
} else {
|
||||
undo.add(new EditorSceneHistoryAction.RemoveKeyframe(track.keyframeType, trackIndex, tick));
|
||||
description = "Added " + track.keyframeType.name() + " keyframe";
|
||||
}
|
||||
redo.add(new EditorSceneHistoryAction.SetKeyframe(track.keyframeType, trackIndex, tick, keyframe.copy()));
|
||||
|
||||
this.push(new EditorSceneHistoryEntry(undo, redo, description));
|
||||
}
|
||||
|
||||
public void push(EditorSceneHistoryEntry entry) {
|
||||
if (entry.undo().isEmpty() && entry.redo().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.history.push(this, entry);
|
||||
}
|
||||
|
||||
public void undo(Consumer<String> descriptionConsumer) {
|
||||
this.history.undo(this, descriptionConsumer);
|
||||
}
|
||||
|
||||
public void redo(Consumer<String> descriptionConsumer) {
|
||||
this.history.redo(this, descriptionConsumer);
|
||||
}
|
||||
|
||||
public void setExportTicks(int start, int end, int totalTicks) {
|
||||
if (this.exportEndTicks < 0) {
|
||||
this.exportEndTicks = totalTicks;
|
||||
}
|
||||
this.exportStartTicks = Math.max(0, Math.min(totalTicks, this.exportStartTicks));
|
||||
this.exportEndTicks = Math.max(0, Math.min(totalTicks, this.exportEndTicks));
|
||||
|
||||
if (start >= 0) {
|
||||
this.exportStartTicks = start;
|
||||
if (this.exportEndTicks < start) {
|
||||
this.exportEndTicks = start;
|
||||
}
|
||||
}
|
||||
|
||||
if (end >= 0) {
|
||||
this.exportEndTicks = end;
|
||||
if (this.exportStartTicks > end) {
|
||||
this.exportStartTicks = end;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.exportStartTicks <= 0 && this.exportEndTicks >= totalTicks) {
|
||||
this.exportStartTicks = -1;
|
||||
this.exportEndTicks = -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+13
-13
@@ -4,12 +4,12 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class EditorStateHistory {
|
||||
public class EditorSceneHistory {
|
||||
|
||||
private final List<EditorStateHistoryEntry> entries = new ArrayList<>();
|
||||
private final List<EditorSceneHistoryEntry> entries = new ArrayList<>();
|
||||
private int position = 0;
|
||||
|
||||
public void push(EditorState editorState, EditorStateHistoryEntry entry) {
|
||||
public void push(EditorScene editorScene, EditorSceneHistoryEntry entry) {
|
||||
while (this.entries.size() > this.position) {
|
||||
this.entries.removeLast();
|
||||
}
|
||||
@@ -18,35 +18,35 @@ public class EditorStateHistory {
|
||||
this.position -= 1;
|
||||
}
|
||||
|
||||
for (EditorStateHistoryAction redo : entry.redo()) {
|
||||
redo.apply(editorState);
|
||||
for (EditorSceneHistoryAction redo : entry.redo()) {
|
||||
redo.apply(editorScene);
|
||||
}
|
||||
|
||||
this.entries.add(entry);
|
||||
this.position += 1;
|
||||
}
|
||||
|
||||
public void undo(EditorState editorState, Consumer<String> descriptionConsumer) {
|
||||
public void undo(EditorScene editorScene, 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);
|
||||
EditorSceneHistoryEntry entry = this.entries.get(this.position);
|
||||
for (EditorSceneHistoryAction undo : entry.undo()) {
|
||||
undo.apply(editorScene);
|
||||
}
|
||||
descriptionConsumer.accept("Undo '" + entry.description() + "'");
|
||||
}
|
||||
|
||||
public void redo(EditorState editorState, Consumer<String> descriptionConsumer) {
|
||||
public void redo(EditorScene editorScene, 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);
|
||||
EditorSceneHistoryEntry entry = this.entries.get(this.position);
|
||||
for (EditorSceneHistoryAction redo : entry.redo()) {
|
||||
redo.apply(editorScene);
|
||||
}
|
||||
descriptionConsumer.accept("Redo '" + entry.description() + "'");
|
||||
|
||||
+22
-22
@@ -12,15 +12,15 @@ import com.moulberry.flashback.keyframe.KeyframeType;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public interface EditorStateHistoryAction {
|
||||
public interface EditorSceneHistoryAction {
|
||||
|
||||
void apply(EditorState editorState);
|
||||
void apply(EditorScene editorScene);
|
||||
|
||||
record SetKeyframe(KeyframeType<?> type, int trackIndex, int tick, Keyframe keyframe) implements EditorStateHistoryAction {
|
||||
record SetKeyframe(KeyframeType<?> type, int trackIndex, int tick, Keyframe keyframe) implements EditorSceneHistoryAction {
|
||||
@Override
|
||||
public void apply(EditorState editorState) {
|
||||
if (this.trackIndex < editorState.keyframeTracks.size()) {
|
||||
KeyframeTrack track = editorState.keyframeTracks.get(this.trackIndex);
|
||||
public void apply(EditorScene editorScene) {
|
||||
if (this.trackIndex < editorScene.keyframeTracks.size()) {
|
||||
KeyframeTrack track = editorScene.keyframeTracks.get(this.trackIndex);
|
||||
if (track.keyframeType == this.type) {
|
||||
track.keyframesByTick.put(this.tick, this.keyframe.copy());
|
||||
}
|
||||
@@ -51,11 +51,11 @@ public interface EditorStateHistoryAction {
|
||||
}
|
||||
}
|
||||
|
||||
record RemoveKeyframe(KeyframeType<?> type, int trackIndex, int tick) implements EditorStateHistoryAction {
|
||||
record RemoveKeyframe(KeyframeType<?> type, int trackIndex, int tick) implements EditorSceneHistoryAction {
|
||||
@Override
|
||||
public void apply(EditorState editorState) {
|
||||
if (this.trackIndex < editorState.keyframeTracks.size()) {
|
||||
KeyframeTrack track = editorState.keyframeTracks.get(this.trackIndex);
|
||||
public void apply(EditorScene editorScene) {
|
||||
if (this.trackIndex < editorScene.keyframeTracks.size()) {
|
||||
KeyframeTrack track = editorScene.keyframeTracks.get(this.trackIndex);
|
||||
if (track.keyframeType == this.type) {
|
||||
track.keyframesByTick.remove(this.tick);
|
||||
}
|
||||
@@ -84,11 +84,11 @@ public interface EditorStateHistoryAction {
|
||||
}
|
||||
}
|
||||
|
||||
record AddTrack(KeyframeType<?> type, int trackIndex) implements EditorStateHistoryAction {
|
||||
record AddTrack(KeyframeType<?> type, int trackIndex) implements EditorSceneHistoryAction {
|
||||
@Override
|
||||
public void apply(EditorState editorState) {
|
||||
if (this.trackIndex <= editorState.keyframeTracks.size()) {
|
||||
editorState.keyframeTracks.add(this.trackIndex, new KeyframeTrack(this.type));
|
||||
public void apply(EditorScene editorScene) {
|
||||
if (this.trackIndex <= editorScene.keyframeTracks.size()) {
|
||||
editorScene.keyframeTracks.add(this.trackIndex, new KeyframeTrack(this.type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,13 +112,13 @@ public interface EditorStateHistoryAction {
|
||||
}
|
||||
}
|
||||
|
||||
record RemoveTrack(KeyframeType<?> type, int trackIndex) implements EditorStateHistoryAction {
|
||||
record RemoveTrack(KeyframeType<?> type, int trackIndex) implements EditorSceneHistoryAction {
|
||||
@Override
|
||||
public void apply(EditorState editorState) {
|
||||
if (this.trackIndex < editorState.keyframeTracks.size()) {
|
||||
KeyframeTrack keyframeTrack = editorState.keyframeTracks.get(this.trackIndex);
|
||||
public void apply(EditorScene editorScene) {
|
||||
if (this.trackIndex < editorScene.keyframeTracks.size()) {
|
||||
KeyframeTrack keyframeTrack = editorScene.keyframeTracks.get(this.trackIndex);
|
||||
if (keyframeTrack.keyframeType == type) {
|
||||
editorState.keyframeTracks.remove(this.trackIndex);
|
||||
editorScene.keyframeTracks.remove(this.trackIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,9 +143,9 @@ public interface EditorStateHistoryAction {
|
||||
}
|
||||
}
|
||||
|
||||
class TypeAdapter implements JsonSerializer<EditorStateHistoryAction>, JsonDeserializer<EditorStateHistoryAction> {
|
||||
class TypeAdapter implements JsonSerializer<EditorSceneHistoryAction>, JsonDeserializer<EditorSceneHistoryAction> {
|
||||
@Override
|
||||
public EditorStateHistoryAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
public EditorSceneHistoryAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
String type = jsonObject.get("action_type").getAsString();
|
||||
return switch (type) {
|
||||
@@ -158,7 +158,7 @@ public interface EditorStateHistoryAction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(EditorStateHistoryAction src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
public JsonElement serialize(EditorSceneHistoryAction src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject jsonObject;
|
||||
switch (src) {
|
||||
case SetKeyframe setKeyframe -> {
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.moulberry.flashback.state;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record EditorSceneHistoryEntry(List<EditorSceneHistoryAction> undo, List<EditorSceneHistoryAction> redo, String description) {
|
||||
}
|
||||
@@ -35,14 +35,13 @@ public class EditorState {
|
||||
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 final List<EditorScene> scenes;
|
||||
public int sceneIndex = 0;
|
||||
|
||||
public double zoomMin = 0.0;
|
||||
public double zoomMax = 1.0;
|
||||
public int exportStartTicks = -1;
|
||||
public int exportEndTicks = -1;
|
||||
|
||||
public UUID audioSourceEntity = null;
|
||||
public Set<UUID> hideDuringExport = new HashSet<>();
|
||||
@@ -50,28 +49,16 @@ public class EditorState {
|
||||
public Map<UUID, GameProfile> skinOverride = new HashMap<>();
|
||||
public Map<UUID, String> nameOverride = new HashMap<>();
|
||||
|
||||
public void setKeyframe(int trackIndex, int tick, Keyframe keyframe) {
|
||||
if (trackIndex >= this.keyframeTracks.size()) {
|
||||
return;
|
||||
public EditorState() {
|
||||
this.scenes = new ArrayList<>();
|
||||
this.scenes.add(new EditorScene("Scene 1"));
|
||||
}
|
||||
|
||||
public EditorScene currentScene() {
|
||||
if (this.scenes.isEmpty()) {
|
||||
this.scenes.add(new EditorScene("Scene 1"));
|
||||
}
|
||||
|
||||
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.name() + " keyframe";
|
||||
} else {
|
||||
undo.add(new EditorStateHistoryAction.RemoveKeyframe(track.keyframeType, trackIndex, tick));
|
||||
description = "Added " + track.keyframeType.name() + " keyframe";
|
||||
}
|
||||
redo.add(new EditorStateHistoryAction.SetKeyframe(track.keyframeType, trackIndex, tick, keyframe.copy()));
|
||||
|
||||
this.push(new EditorStateHistoryEntry(undo, redo, description));
|
||||
return this.scenes.get(this.sceneIndex);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -96,54 +83,6 @@ public class EditorState {
|
||||
return dummyCamera;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
this.exportStartTicks = Math.max(0, Math.min(totalTicks, this.exportStartTicks));
|
||||
this.exportEndTicks = Math.max(0, Math.min(totalTicks, this.exportEndTicks));
|
||||
|
||||
if (start >= 0) {
|
||||
this.exportStartTicks = start;
|
||||
if (this.exportEndTicks < start) {
|
||||
this.exportEndTicks = start;
|
||||
}
|
||||
}
|
||||
|
||||
if (end >= 0) {
|
||||
this.exportEndTicks = end;
|
||||
if (this.exportStartTicks > end) {
|
||||
this.exportStartTicks = end;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.exportStartTicks <= 0 && this.exportEndTicks >= totalTicks) {
|
||||
this.exportStartTicks = -1;
|
||||
this.exportEndTicks = -1;
|
||||
}
|
||||
|
||||
this.dirty = true;
|
||||
}
|
||||
|
||||
public void markDirty() {
|
||||
this.dirty = true;
|
||||
this.modCount += 1;
|
||||
@@ -187,7 +126,7 @@ public class EditorState {
|
||||
|
||||
public void applyKeyframes(KeyframeHandler keyframeHandler, float tick) {
|
||||
Set<KeyframeType<?>> applied = new HashSet<>();
|
||||
for (KeyframeTrack keyframeTrack : this.keyframeTracks) {
|
||||
for (KeyframeTrack keyframeTrack : this.currentScene().keyframeTracks) {
|
||||
// Ignore lines that are disabled
|
||||
if (!keyframeTrack.enabled) {
|
||||
continue;
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.moulberry.flashback.state;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record EditorStateHistoryEntry(List<EditorStateHistoryAction> undo, List<EditorStateHistoryAction> redo, String description) {
|
||||
}
|
||||
Reference in new issue
Block a user