This commit is contained in:
2024-04-01 23:13:39 +02:00
parent 3c9065ee8c
commit 3cda9c3e96
14 changed files with 382 additions and 214 deletions

View File

@@ -1,7 +1,6 @@
import { deserialise as deserialiseTrack, type TrackInitOptions, isValidTimeSigRange, Track } from "@/Track";
import { greatestCommonDivisor, isPosInt } from "@/utils";
import { watch, computed, ref, shallowRef, triggerRef, type WritableComputedRef, type Ref, type ShallowRef } from "vue";
import EffectScoped from "./EffectScoped";
import { greatestCommonDivisor, isPosInt, EffectScoped } from "@/utils";
import { watch, computed, ref, shallowRef, triggerRef, type WritableComputedRef, type Ref, type ShallowRef, nextTick } from "vue";
import { z } from "zod";
export interface BeatManager {
@@ -9,7 +8,6 @@ export interface BeatManager {
}
type BeatGroupInitOptions = {
manager: BeatManager,
barCount: number;
isLooping: boolean;
timeSigUp: number;
@@ -20,7 +18,7 @@ type BeatGroupInitOptions = {
};
const BeatSerialSchema = z.object({
tracks: z.array(z.any()),
tracks: z.array(z.unknown()),
barCount: z.number(),
timeSigUp: z.number(),
globalLoopLength: z.number(),
@@ -32,9 +30,11 @@ const BeatSerialSchema = z.object({
export type BeatSerial = z.infer<typeof BeatSerialSchema>;
export class Beat extends EffectScoped {
private static count = 0;
private barCountInternal: Ref<number>;
private globalLoopLengthInternal: Ref<number>;
manager: BeatManager;
readonly id = Beat.count++;
tracks: ShallowRef<Track[]>;
barCount: WritableComputedRef<number>;
timeSigUp: Ref<number>;
@@ -43,10 +43,10 @@ export class Beat extends EffectScoped {
useAutoBeatLength: Ref<boolean>;
barSettingsLocked: Ref<boolean>;
name: Ref<string>;
saveDirty = ref(false);
constructor(opts: BeatGroupInitOptions) {
super();
this.manager = opts.manager;
this.tracks = shallowRef<Track[]>([]);
this.barCountInternal = ref<number>(opts.barCount ?? 4);
this.barCount = computed({
@@ -89,14 +89,17 @@ export class Beat extends EffectScoped {
this.globalIsLooping = ref<boolean>(opts.isLooping ?? false);
this.useAutoBeatLength = ref(opts.useAutoBeatLength ?? false);
this.barSettingsLocked = computed(() => this.useAutoBeatLength.value);
this.name = ref(opts.name ?? "Beat");
this.name = ref(opts.name ?? `Beat-${ this.id }`);
watch([this.barCount, this.timeSigUp], ([newBarCount, newTimeSigUp]) => {
watch([this.barCount, this.timeSigUp, this.name], ([newBarCount, newTimeSigUp]) => {
for (const track of this.tracks.value) {
track.barCount.value = newBarCount;
track.timeSigUp.value = newTimeSigUp;
}
this.saveDirty.value = true;
}, { immediate: true });
nextTick(() => this.saveDirty.value = false);
}
setTimeSigUp(timeSigVal: number): void {
@@ -124,9 +127,13 @@ export class Beat extends EffectScoped {
triggerRef(this.tracks);
}
notifyChange() {
this.saveDirty.value = true;
}
addTrack(options?: Omit<TrackInitOptions, 'manager'>): Track | null {
const optionsResolved = {
manager: this.manager,
manager: this,
barCount: this.barCount.value,
isLooping: this.globalIsLooping.value,
loopLength: this.globalLoopLength.value,
@@ -173,7 +180,7 @@ export class Beat extends EffectScoped {
}
}
export function deserialise(serial: unknown, manager: BeatManager): Beat | null {
export function deserialise(serial: unknown): Beat | null {
const parse = BeatSerialSchema.safeParse(serial);
if (!parse.success) {
console.error('Encountered invalid beat serial:', parse.error, serial);
@@ -181,7 +188,6 @@ export function deserialise(serial: unknown, manager: BeatManager): Beat | null
}
const beat = parse.data;
const newBeat = Beat.asScoped({
manager,
loopLength: beat.globalLoopLength,
barCount: beat.barCount,
isLooping: beat.globalIsLooping,
@@ -190,7 +196,7 @@ export function deserialise(serial: unknown, manager: BeatManager): Beat | null
useAutoBeatLength: beat.useAutoBeatLength,
});
beat.tracks.forEach(trackSerial => {
const track = deserialiseTrack(trackSerial, manager);
const track = deserialiseTrack(trackSerial, newBeat);
if (track) newBeat.tracks.value.push(track);
});
return newBeat;