This commit is contained in:
Daniel Ledda
2023-05-07 21:14:48 +02:00
parent 90a39ee2fe
commit 4268cec832
17 changed files with 800 additions and 2292 deletions

View File

@@ -1,5 +1,13 @@
import { type Beat, createBeat, deserialise as deserialiseBeat, type BeatManager } from "@/Beat";
import { inject, computed, type InjectionKey, ref, shallowRef, triggerRef, watch, onScopeDispose, nextTick } from "vue";
import { Beat, deserialise as deserialiseBeat, type BeatManager } from "@/Beat";
import { inject, computed, type InjectionKey, onScopeDispose, ref, shallowRef, triggerRef, watch, nextTick } from "vue";
import { z } from "zod";
export const DrumSlayerSaveSchema = z.object({
orientation: z.union([z.literal("horizontal"), z.literal("vertical")]),
activeBeatIndex: z.number(),
beats: z.array(z.unknown()),
});
export type DrumSlayerSave = z.infer<typeof DrumSlayerSaveSchema>;
function defaultMainBeatGroup(manager: BeatManager): Beat {
const defaultSettings = {
@@ -8,7 +16,7 @@ function defaultMainBeatGroup(manager: BeatManager): Beat {
isLooping: false,
timeSigUp: 8,
};
const mainBeatGroup = createBeat(defaultSettings);
const mainBeatGroup = Beat.asScoped(defaultSettings);
mainBeatGroup.addTrack({ name: "LF" });
mainBeatGroup.addTrack({ name: "LH" });
mainBeatGroup.addTrack({ name: "RH" });
@@ -57,26 +65,25 @@ export function createBeatStore() {
beats: serials,
activeBeatIndex: activeBeatIndex.value,
orientation: orientation.value ?? "horizontal",
}));
} satisfies DrumSlayerSave));
saveDirty.value = false;
}
}
function loadFromSave(source: any): void {
function loadFromSave(source: unknown): void {
beats.value.length = 0;
if (Array.isArray(source.beats)
&& (typeof source.activeBeatIndex === "number" || typeof source.activeBeatIndex === "undefined")
&& typeof source.orientation === "string") {
try {
source.beats.forEach((beat: any) => beats.value.push(deserialiseBeat(beat, manager)));
if (typeof source.activeBeatIndex === "number") {
activeBeatIndex.value = source.activeBeatIndex;
const parse = DrumSlayerSaveSchema.safeParse(source);
if (parse.success) {
parse.data.beats.forEach((beat: unknown) => {
const deserialisedBeat = deserialiseBeat(beat, manager);
if (deserialisedBeat) {
beats.value.push(deserialisedBeat);
}
orientation.value = source.orientation;
} catch (err) {
console.error(err);
}
} else {
});
activeBeatIndex.value = parse.data.activeBeatIndex;
orientation.value = parse.data.orientation;
}
if (beats.value.length === 0) {
resetActiveBeat();
}
nextTick(() => saveDirty.value = false);