Skip to content

Commit

Permalink
feat: [project-sequencer-statemachine] transitionToでonExitが呼ばれた場合でもステ…
Browse files Browse the repository at this point in the history
…ートが正しく動作するようにする (#2519)
  • Loading branch information
sigprogramming authored Feb 4, 2025
1 parent 4205802 commit 036029a
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 99 deletions.
38 changes: 24 additions & 14 deletions src/sing/sequencerStateMachine/states/addNoteState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class AddNoteState
private readonly returnStateId: IdleStateId;

private currentCursorPos: PositionOnSequencer;
private applyPreview: boolean;

private innerContext:
| {
noteToAdd: Note;
Expand All @@ -44,6 +46,7 @@ export class AddNoteState
this.returnStateId = args.returnStateId;

this.currentCursorPos = args.cursorPosAtStart;
this.applyPreview = false;
}

private previewAdd(context: Context) {
Expand Down Expand Up @@ -81,8 +84,10 @@ export class AddNoteState
noteNumber: clamp(this.cursorPosAtStart.noteNumber, 0, 127),
lyric: getDoremiFromNoteNumber(this.cursorPosAtStart.noteNumber),
};
const noteEndPos = noteToAdd.position + noteToAdd.duration;

context.previewNotes.value = [noteToAdd];
context.guideLineTicks.value = noteEndPos;
context.nowPreviewing.value = true;

const previewIfNeeded = () => {
Expand Down Expand Up @@ -121,10 +126,12 @@ export class AddNoteState
if (input.mouseEvent.type === "mousemove") {
this.currentCursorPos = input.cursorPos;
this.innerContext.executePreviewProcess = true;
} else if (input.mouseEvent.type === "mouseup") {
if (mouseButton === "LEFT_BUTTON") {
setNextState(this.returnStateId, undefined);
}
} else if (
input.mouseEvent.type === "mouseup" &&
mouseButton === "LEFT_BUTTON"
) {
this.applyPreview = true;
setNextState(this.returnStateId, undefined);
}
}
}
Expand All @@ -138,18 +145,21 @@ export class AddNoteState

cancelAnimationFrame(this.innerContext.previewRequestId);

void context.store.actions.COMMAND_ADD_NOTES({
notes: context.previewNotes.value,
trackId: this.targetTrackId,
});
void context.store.actions.SELECT_NOTES({ noteIds: previewNoteIds });

if (previewNotes.length === 1) {
void context.store.actions.PLAY_PREVIEW_SOUND({
noteNumber: previewNotes[0].noteNumber,
duration: PREVIEW_SOUND_DURATION,
if (this.applyPreview) {
void context.store.actions.COMMAND_ADD_NOTES({
notes: context.previewNotes.value,
trackId: this.targetTrackId,
});
void context.store.actions.SELECT_NOTES({ noteIds: previewNoteIds });

if (previewNotes.length === 1) {
void context.store.actions.PLAY_PREVIEW_SOUND({
noteNumber: previewNotes[0].noteNumber,
duration: PREVIEW_SOUND_DURATION,
});
}
}

context.previewNotes.value = [];
context.nowPreviewing.value = false;
}
Expand Down
27 changes: 20 additions & 7 deletions src/sing/sequencerStateMachine/states/drawPitchState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class DrawPitchState
private readonly returnStateId: IdleStateId;

private currentCursorPos: PositionOnSequencer;
private applyPreview: boolean;

private innerContext:
| {
Expand All @@ -43,6 +44,7 @@ export class DrawPitchState
this.returnStateId = args.returnStateId;

this.currentCursorPos = args.cursorPosAtStart;
this.applyPreview = false;
}

private previewDrawPitch(context: Context) {
Expand Down Expand Up @@ -146,6 +148,7 @@ export class DrawPitchState

process({
input,
context,
setNextState,
}: {
input: Input;
Expand All @@ -155,15 +158,27 @@ export class DrawPitchState
if (this.innerContext == undefined) {
throw new Error("innerContext is undefined.");
}
if (context.previewPitchEdit.value == undefined) {
throw new Error("previewPitchEdit is undefined.");
}
if (context.previewPitchEdit.value.type !== "draw") {
throw new Error("previewPitchEdit.type is not draw.");
}
const mouseButton = getButton(input.mouseEvent);
if (input.targetArea === "SequencerBody") {
if (input.mouseEvent.type === "mousemove") {
this.currentCursorPos = input.cursorPos;
this.innerContext.executePreviewProcess = true;
} else if (input.mouseEvent.type === "mouseup") {
if (mouseButton === "LEFT_BUTTON") {
setNextState(this.returnStateId, undefined);
}
} else if (
input.mouseEvent.type === "mouseup" &&
mouseButton === "LEFT_BUTTON"
) {
// カーソルを動かさずにマウスのボタンを離したときに1フレームのみの変更になり、
// 1フレームの変更はピッチ編集ラインとして表示されないので、無視する
const previewPitchEditDataLength =
context.previewPitchEdit.value.data.length;
this.applyPreview = previewPitchEditDataLength >= 2;
setNextState(this.returnStateId, undefined);
}
}
}
Expand All @@ -181,9 +196,7 @@ export class DrawPitchState

cancelAnimationFrame(this.innerContext.previewRequestId);

// カーソルを動かさずにマウスのボタンを離したときに1フレームのみの変更になり、
// 1フレームの変更はピッチ編集ラインとして表示されないので、無視する
if (context.previewPitchEdit.value.data.length >= 2) {
if (this.applyPreview) {
// 平滑化を行う
let data = context.previewPitchEdit.value.data;
data = data.map((value) => Math.log(value));
Expand Down
24 changes: 15 additions & 9 deletions src/sing/sequencerStateMachine/states/erasePitchState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class ErasePitchState
private readonly returnStateId: IdleStateId;

private currentCursorPos: PositionOnSequencer;
private applyPreview: boolean;

private innerContext:
| {
Expand All @@ -37,6 +38,7 @@ export class ErasePitchState
this.returnStateId = args.returnStateId;

this.currentCursorPos = args.cursorPosAtStart;
this.applyPreview = false;
}

private previewErasePitch(context: Context) {
Expand Down Expand Up @@ -108,10 +110,12 @@ export class ErasePitchState
if (input.mouseEvent.type === "mousemove") {
this.currentCursorPos = input.cursorPos;
this.innerContext.executePreviewProcess = true;
} else if (input.mouseEvent.type === "mouseup") {
if (mouseButton === "LEFT_BUTTON") {
setNextState(this.returnStateId, undefined);
}
} else if (
input.mouseEvent.type === "mouseup" &&
mouseButton === "LEFT_BUTTON"
) {
this.applyPreview = true;
setNextState(this.returnStateId, undefined);
}
}
}
Expand All @@ -129,11 +133,13 @@ export class ErasePitchState

cancelAnimationFrame(this.innerContext.previewRequestId);

void context.store.actions.COMMAND_ERASE_PITCH_EDIT_DATA({
startFrame: context.previewPitchEdit.value.startFrame,
frameLength: context.previewPitchEdit.value.frameLength,
trackId: this.targetTrackId,
});
if (this.applyPreview) {
void context.store.actions.COMMAND_ERASE_PITCH_EDIT_DATA({
startFrame: context.previewPitchEdit.value.startFrame,
frameLength: context.previewPitchEdit.value.frameLength,
trackId: this.targetTrackId,
});
}

context.previewPitchEdit.value = undefined;
context.nowPreviewing.value = false;
Expand Down
40 changes: 24 additions & 16 deletions src/sing/sequencerStateMachine/states/moveNoteState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class MoveNoteState
private readonly returnStateId: IdleStateId;

private currentCursorPos: PositionOnSequencer;
private applyPreview: boolean;

private innerContext:
| {
Expand Down Expand Up @@ -53,6 +54,7 @@ export class MoveNoteState
this.returnStateId = args.returnStateId;

this.currentCursorPos = args.cursorPosAtStart;
this.applyPreview = false;
}

private previewMove(context: Context) {
Expand Down Expand Up @@ -108,6 +110,7 @@ export class MoveNoteState
}

context.previewNotes.value = [...targetNotesArray];
context.guideLineTicks.value = guideLineTicks;
context.nowPreviewing.value = true;

const previewIfNeeded = () => {
Expand Down Expand Up @@ -148,10 +151,12 @@ export class MoveNoteState
if (input.mouseEvent.type === "mousemove") {
this.currentCursorPos = input.cursorPos;
this.innerContext.executePreviewProcess = true;
} else if (input.mouseEvent.type === "mouseup") {
if (mouseButton === "LEFT_BUTTON") {
setNextState(this.returnStateId, undefined);
}
} else if (
input.mouseEvent.type === "mouseup" &&
mouseButton === "LEFT_BUTTON"
) {
this.applyPreview = this.innerContext.edited;
setNextState(this.returnStateId, undefined);
}
}
}
Expand All @@ -165,20 +170,23 @@ export class MoveNoteState

cancelAnimationFrame(this.innerContext.previewRequestId);

void context.store.actions.COMMAND_UPDATE_NOTES({
notes: previewNotes,
trackId: this.targetTrackId,
});
void context.store.actions.SELECT_NOTES({
noteIds: previewNoteIds,
});

if (previewNotes.length === 1) {
void context.store.actions.PLAY_PREVIEW_SOUND({
noteNumber: previewNotes[0].noteNumber,
duration: PREVIEW_SOUND_DURATION,
if (this.applyPreview) {
void context.store.actions.COMMAND_UPDATE_NOTES({
notes: previewNotes,
trackId: this.targetTrackId,
});
void context.store.actions.SELECT_NOTES({
noteIds: previewNoteIds,
});

if (previewNotes.length === 1) {
void context.store.actions.PLAY_PREVIEW_SOUND({
noteNumber: previewNotes[0].noteNumber,
duration: PREVIEW_SOUND_DURATION,
});
}
}

context.previewNotes.value = [];
context.nowPreviewing.value = false;
}
Expand Down
28 changes: 18 additions & 10 deletions src/sing/sequencerStateMachine/states/resizeNoteLeftState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class ResizeNoteLeftState
private readonly returnStateId: IdleStateId;

private currentCursorPos: PositionOnSequencer;
private applyPreview: boolean;

private innerContext:
| {
Expand Down Expand Up @@ -53,6 +54,7 @@ export class ResizeNoteLeftState
this.returnStateId = args.returnStateId;

this.currentCursorPos = args.cursorPosAtStart;
this.applyPreview = false;
}

private previewResizeLeft(context: Context) {
Expand Down Expand Up @@ -101,8 +103,10 @@ export class ResizeNoteLeftState
for (const targetNote of targetNotesArray) {
targetNotesMap.set(targetNote.id, targetNote);
}
const mouseDownNote = getOrThrow(targetNotesMap, this.mouseDownNoteId);

context.previewNotes.value = [...targetNotesArray];
context.guideLineTicks.value = mouseDownNote.position;
context.nowPreviewing.value = true;

const previewIfNeeded = () => {
Expand Down Expand Up @@ -147,6 +151,7 @@ export class ResizeNoteLeftState
input.mouseEvent.type === "mouseup" &&
mouseButton === "LEFT_BUTTON"
) {
this.applyPreview = this.innerContext.edited;
setNextState(this.returnStateId, undefined);
}
}
Expand All @@ -161,18 +166,21 @@ export class ResizeNoteLeftState

cancelAnimationFrame(this.innerContext.previewRequestId);

void context.store.actions.COMMAND_UPDATE_NOTES({
notes: previewNotes,
trackId: this.targetTrackId,
});
void context.store.actions.SELECT_NOTES({ noteIds: previewNoteIds });

if (previewNotes.length === 1) {
void context.store.actions.PLAY_PREVIEW_SOUND({
noteNumber: previewNotes[0].noteNumber,
duration: PREVIEW_SOUND_DURATION,
if (this.applyPreview) {
void context.store.actions.COMMAND_UPDATE_NOTES({
notes: previewNotes,
trackId: this.targetTrackId,
});
void context.store.actions.SELECT_NOTES({ noteIds: previewNoteIds });

if (previewNotes.length === 1) {
void context.store.actions.PLAY_PREVIEW_SOUND({
noteNumber: previewNotes[0].noteNumber,
duration: PREVIEW_SOUND_DURATION,
});
}
}

context.previewNotes.value = [];
context.nowPreviewing.value = false;
}
Expand Down
Loading

0 comments on commit 036029a

Please sign in to comment.