Skip to content

Commit

Permalink
NoteのインスタンスではなくNoteIdを渡すようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
sigprogramming committed Dec 17, 2024
1 parent df96722 commit 1574677
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/sing/stateMachine/sequencerStateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,6 @@ const executeNotesSelectionProcess = (
}
};

const getSelectedNotes = (context: Context) => {
return context.notesInSelectedTrack.value.filter((value) =>
context.selectedNoteIds.value.has(value.id),
);
};

class IdleState implements IState<State, Input, Context> {
readonly id = "idle";

Expand Down Expand Up @@ -194,11 +188,10 @@ class IdleState implements IState<State, Input, Context> {
}
if (mouseButton === "LEFT_BUTTON") {
executeNotesSelectionProcess(context, input.mouseEvent, input.note);
const selectedNotes = getSelectedNotes(context);
const moveNoteState = new MoveNoteState(
input.cursorPos,
selectedTrackId,
selectedNotes,
context.selectedNoteIds.value,
input.note.id,
);
setNextState(moveNoteState);
Expand Down Expand Up @@ -347,13 +340,14 @@ class MoveNoteState implements IState<State, Input, Context> {

private readonly cursorPosAtStart: PositionOnSequencer;
private readonly targetTrackId: TrackId;
private readonly targetNotesAtStart: Map<NoteId, Note>;
private readonly targetNoteIds: Set<NoteId>;
private readonly mouseDownNoteId: NoteId;

private currentCursorPos: PositionOnSequencer;

private innerContext:
| {
targetNotesAtStart: Map<NoteId, Note>;
previewRequestId: number;
executePreviewProcess: boolean;
edited: boolean;
Expand All @@ -364,18 +358,15 @@ class MoveNoteState implements IState<State, Input, Context> {
constructor(
cursorPosAtStart: PositionOnSequencer,
targetTrackId: TrackId,
targetNotes: Note[],
targetNoteIds: Set<NoteId>,
mouseDownNoteId: NoteId,
) {
if (!targetNotes.some((value) => value.id === mouseDownNoteId)) {
throw new Error("mouseDownNote is not included in targetNotes.");
if (!targetNoteIds.has(mouseDownNoteId)) {
throw new Error("mouseDownNoteId is not included in targetNoteIds.");
}
this.cursorPosAtStart = cursorPosAtStart;
this.targetTrackId = targetTrackId;
this.targetNotesAtStart = new Map();
for (const targetNote of targetNotes) {
this.targetNotesAtStart.set(targetNote.id, targetNote);
}
this.targetNoteIds = targetNoteIds;
this.mouseDownNoteId = mouseDownNoteId;

this.currentCursorPos = cursorPosAtStart;
Expand All @@ -387,7 +378,7 @@ class MoveNoteState implements IState<State, Input, Context> {
}
const snapTicks = context.snapTicks.value;
const previewNotes = context.previewNotes.value;
const targetNotesAtStart = this.targetNotesAtStart;
const targetNotesAtStart = this.innerContext.targetNotesAtStart;
const mouseDownNote = getOrThrow(targetNotesAtStart, this.mouseDownNoteId);
const dragTicks = this.currentCursorPos.ticks - this.cursorPosAtStart.ticks;
const notePos = mouseDownNote.position;
Expand Down Expand Up @@ -434,8 +425,15 @@ class MoveNoteState implements IState<State, Input, Context> {

onEnter(context: Context) {
const guideLineTicks = getGuideLineTicks(this.cursorPosAtStart, context);
const targetNotesArray = context.notesInSelectedTrack.value.filter(
(value) => this.targetNoteIds.has(value.id),
);
const targetNotesMap = new Map<NoteId, Note>();
for (const targetNote of targetNotesArray) {
targetNotesMap.set(targetNote.id, targetNote);
}

context.previewNotes.value = [...this.targetNotesAtStart.values()];
context.previewNotes.value = [...targetNotesArray];
context.nowPreviewing.value = true;

const previewIfNeeded = () => {
Expand All @@ -452,6 +450,7 @@ class MoveNoteState implements IState<State, Input, Context> {
const previewRequestId = requestAnimationFrame(previewIfNeeded);

this.innerContext = {
targetNotesAtStart: targetNotesMap,
executePreviewProcess: false,
previewRequestId,
edited: false,
Expand Down

0 comments on commit 1574677

Please sign in to comment.