Skip to content

Commit

Permalink
Add:ルーラークリックでスナップ位置に再生ヘッドをあわせる (#2372)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
romot-co and Hiroshiba authored Nov 22, 2024
1 parent a69e4b2 commit 7552ace
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/Sing/SequencerRuler/Container.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:sequencerZoomX
:numMeasures
:playheadTicks
:sequencerSnapType
@update:playheadTicks="updatePlayheadTicks"
@deselectAllNotes="deselectAllNotes"
/>
Expand Down Expand Up @@ -35,6 +36,7 @@ const store = useStore();
const tpqn = computed(() => store.state.tpqn);
const timeSignatures = computed(() => store.state.timeSignatures);
const sequencerZoomX = computed(() => store.state.sequencerZoomX);
const sequencerSnapType = computed(() => store.state.sequencerSnapType);
const playheadTicks = computed(() => store.getters.PLAYHEAD_POSITION);
Expand Down
17 changes: 15 additions & 2 deletions src/components/Sing/SequencerRuler/Presentation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@

<script setup lang="ts">
import { computed, ref, onMounted, onUnmounted } from "vue";
import { getMeasureDuration, getTimeSignaturePositions } from "@/sing/domain";
import {
getMeasureDuration,
getTimeSignaturePositions,
getNoteDuration,
snapTicksToGrid,
} from "@/sing/domain";
import { baseXToTick, tickToBaseX } from "@/sing/viewHelper";
import { TimeSignature } from "@/store/type";
Expand All @@ -73,6 +78,7 @@ const props = defineProps<{
tpqn: number;
timeSignatures: TimeSignature[];
sequencerZoomX: number;
sequencerSnapType: number;
}>();
const playheadTicks = defineModel<number>("playheadTicks", { required: true });
const emit = defineEmits<{
Expand Down Expand Up @@ -134,6 +140,10 @@ const playheadX = computed(() => {
return Math.floor(baseX * props.sequencerZoomX);
});
const snapTicks = computed(() => {
return getNoteDuration(props.sequencerSnapType, props.tpqn);
});
const onClick = (event: MouseEvent) => {
emit("deselectAllNotes");
Expand All @@ -142,7 +152,10 @@ const onClick = (event: MouseEvent) => {
throw new Error("sequencerRulerElement is null.");
}
const baseX = (props.offset + event.offsetX) / props.sequencerZoomX;
const ticks = baseXToTick(baseX, props.tpqn);
const ticks = snapTicksToGrid(
baseXToTick(baseX, props.tpqn),
snapTicks.value,
);
playheadTicks.value = ticks;
};
Expand Down
1 change: 1 addition & 0 deletions src/components/Sing/SequencerRuler/index.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const meta: Meta<typeof Presentation> = {
tpqn: 480,
offset: 0,
numMeasures: 32,
sequencerSnapType: 4,
"onUpdate:playheadTicks": fn<(value: number) => void>(),
onDeselectAllNotes: fn(),
},
Expand Down
7 changes: 7 additions & 0 deletions src/sing/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,10 @@ export const shouldPlayTracks = (tracks: Map<TrackId, Track>): Set<TrackId> => {
.map(([trackId]) => trackId),
);
};

/**
* 指定されたティックを直近のグリッドに合わせる
*/
export function snapTicksToGrid(ticks: number, snapTicks: number): number {
return Math.round(ticks / snapTicks) * snapTicks;
}

0 comments on commit 7552ace

Please sign in to comment.