Skip to content

Commit

Permalink
Stop/start
Browse files Browse the repository at this point in the history
  • Loading branch information
leegee committed Oct 16, 2024
1 parent e803cf1 commit c6b071f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/components/StepInput.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* components/StepInput.css */

.step-input-button {
.active {
border-style: solid;
background-color: red;
color: white;
}
24 changes: 14 additions & 10 deletions src/components/StepInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { useRef, useState } from 'react';

import './StepInput.css';
import { dispatchCurrentBeatEvent } from '../events/dispatchCurrentBeatEvent';
Expand All @@ -15,6 +15,7 @@ const StepInput: React.FC<StepInputProps> = ({ gridIndex }) => {
const { grids, selectedInput, setOrUpdateNoteInGrid, inputChannels } = useMusicStore();
const grid = grids[gridIndex];
const currentBeatRef = useRef(0);
const [stepInputMode, setStepInputMode] = useState(false); // State for step input mode

const handleMIDIMessage = (event: WebMidi.MIDIMessageEvent) => {
const [status, note, velocity] = event.data;
Expand All @@ -28,13 +29,15 @@ const StepInput: React.FC<StepInputProps> = ({ gridIndex }) => {
}
};

const toggleStepInputMode = () => {
if (currentBeatRef.current > 0) {
const toggleStepInputMode = (forceStop: boolean = false) => {
if (stepInputMode || forceStop) {
// Reset current beat when exiting step input mode
currentBeatRef.current = 0;
stopListening();
setStepInputMode(false); // Update state
} else {
startListening();
setStepInputMode(true); // Update state
}
};

Expand Down Expand Up @@ -68,22 +71,23 @@ const StepInput: React.FC<StepInputProps> = ({ gridIndex }) => {
const nextBeat = currentBeatRef.current + 1;

// Check if the next beat exceeds the number of columns
console.log(nextBeat, grid.numColumns)
if (nextBeat >= grid.numColumns) {
toggleStepInputMode(); // Automatically stop if out of bounds
toggleStepInputMode(true); // Automatically stop if out of bounds
currentBeatRef.current = 0; // Reset current beat for the next activation
}
else {
} else {
currentBeatRef.current = nextBeat; // Update the current beat
dispatchCurrentBeatEvent(currentBeatRef.current);
}
};

return (
<button className='step-input-button' onClick={toggleStepInputMode}
title={currentBeatRef.current > 0 ? 'Exit step input mode' : 'Enter step input mode'}
<button className={`step-input-button ${stepInputMode ? 'active' : ''}`}
onClick={() => toggleStepInputMode()}
title={stepInputMode ? 'Exit step input mode' : 'Enter step input mode'}
>
{currentBeatRef.current > 0 ? 'Exit' : 'Step'}
</button >
{stepInputMode ? 'Exit' : 'Step'}
</button>
);
};

Expand Down

0 comments on commit c6b071f

Please sign in to comment.