Implement scene switcher

This commit is contained in:
Moulberry committed 2024-11-15 12:43:06 +08:00
1 parent 3600e535d5
commit 775b49bd8a
15 files changed
+473 -314

No files matched your search

@@ -2,9 +2,10 @@ package com.moulberry.flashback.editor;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.state.EditorScene;
import com.moulberry.flashback.state.EditorSceneHistoryAction;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateHistoryAction;
import com.moulberry.flashback.state.EditorStateHistoryEntry;
import com.moulberry.flashback.state.EditorSceneHistoryEntry;
import com.moulberry.flashback.state.KeyframeTrack;
import org.jetbrains.annotations.Nullable;
@@ -15,37 +16,37 @@ import java.util.TreeMap;
public record SavedTrack(KeyframeType<?> type, int track, boolean copiedFromDisabled,
TreeMap<Integer, Keyframe> keyframes) {
public int applyToEditorState(EditorState editorState, int cursorTicks, int totalTicks) {
public int applyToScene(EditorScene editorScene, int cursorTicks, int totalTicks) {
if (this.keyframes == null || this.keyframes.isEmpty()) {
return 0;
}
// Try to apply directly to copied track
if (this.track >= 0 && this.track < editorState.keyframeTracks.size()) {
KeyframeTrack keyframeTrack = editorState.keyframeTracks.get(this.track);
if (this.track >= 0 && this.track < editorScene.keyframeTracks.size()) {
KeyframeTrack keyframeTrack = editorScene.keyframeTracks.get(this.track);
if ((keyframeTrack.enabled || this.copiedFromDisabled) && keyframeTrack.keyframeType == this.type) {
return this.applyTo(editorState, this.type, keyframeTrack, this.track, cursorTicks, totalTicks);
return this.applyTo(editorScene, this.type, keyframeTrack, this.track, cursorTicks, totalTicks);
}
}
// Try to find first eligible enabled track
for (KeyframeTrack keyframeTrack : editorState.keyframeTracks) {
for (KeyframeTrack keyframeTrack : editorScene.keyframeTracks) {
if (keyframeTrack.enabled && keyframeTrack.keyframeType == this.type) {
return this.applyTo(editorState, this.type, keyframeTrack, this.track, cursorTicks, totalTicks);
return this.applyTo(editorScene, this.type, keyframeTrack, this.track, cursorTicks, totalTicks);
}
}
// Create new track
return this.applyTo(editorState, this.type, null, this.track, cursorTicks, totalTicks);
return this.applyTo(editorScene, this.type, null, this.track, cursorTicks, totalTicks);
}
private int applyTo(EditorState editorState, KeyframeType type, @Nullable KeyframeTrack existing, int trackIndex, int cursorTicks, int totalTicks) {
List<EditorStateHistoryAction> undo = new ArrayList<>();
List<EditorStateHistoryAction> redo = new ArrayList<>();
private int applyTo(EditorScene editorScene, KeyframeType<?> type, @Nullable KeyframeTrack existing, int trackIndex, int cursorTicks, int totalTicks) {
List<EditorSceneHistoryAction> undo = new ArrayList<>();
List<EditorSceneHistoryAction> redo = new ArrayList<>();
if (existing == null) {
undo.add(new EditorStateHistoryAction.RemoveTrack(type, trackIndex));
redo.add(new EditorStateHistoryAction.AddTrack(type, trackIndex));
undo.add(new EditorSceneHistoryAction.RemoveTrack(type, trackIndex));
redo.add(new EditorSceneHistoryAction.AddTrack(type, trackIndex));
}
int count = 0;
@@ -56,18 +57,18 @@ public record SavedTrack(KeyframeType<?> type, int track, boolean copiedFromDisa
Keyframe old = existing.keyframesByTick.get(newTick);
if (old != null) {
undo.add(new EditorStateHistoryAction.SetKeyframe(type, trackIndex, newTick, old.copy()));
undo.add(new EditorSceneHistoryAction.SetKeyframe(type, trackIndex, newTick, old.copy()));
} else {
undo.add(new EditorStateHistoryAction.RemoveKeyframe(type, trackIndex, newTick));
undo.add(new EditorSceneHistoryAction.RemoveKeyframe(type, trackIndex, newTick));
}
}
redo.add(new EditorStateHistoryAction.SetKeyframe(type, trackIndex, newTick, entry.getValue().copy()));
redo.add(new EditorSceneHistoryAction.SetKeyframe(type, trackIndex, newTick, entry.getValue().copy()));
count += 1;
}
}
editorState.push(new EditorStateHistoryEntry(undo, redo, "Pasted " + count + " keyframe(s)"));
editorScene.push(new EditorSceneHistoryEntry(undo, redo, "Pasted " + count + " keyframe(s)"));
return count;
}