From cc364cfe7ae679127708f5f981ae7237768da8ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sat, 18 Mar 2023 00:36:36 +0100 Subject: [PATCH] revert PianoKey to Note for start and endNote and now start for blackkeys support --- front/components/VirtualPiano/Octave.tsx | 84 ++++++++++++------- .../components/VirtualPiano/VirtualPiano.tsx | 13 +-- 2 files changed, 62 insertions(+), 35 deletions(-) diff --git a/front/components/VirtualPiano/Octave.tsx b/front/components/VirtualPiano/Octave.tsx index 43c22a6..d158d36 100644 --- a/front/components/VirtualPiano/Octave.tsx +++ b/front/components/VirtualPiano/Octave.tsx @@ -5,18 +5,14 @@ import { octaveKeys, Accidental, } from "../../models/Piano"; -import { Box, Row, Pressable } from "native-base"; +import { Box, Row, Pressable, ZStack } from "native-base"; const notesList: Array = ["C", "D", "E", "F", "G", "A", "B"]; const accidentalsList: Array = ["#", "b", "##", "bb"]; -const getKeyIndex = (k: PianoKey, keys: PianoKey[]) => { +const getKeyIndex = (n: Note, keys: PianoKey[]) => { for (let i = 0; i < keys.length; i++) { - if ( - keys[i]?.note === k.note && - ((keys[i]?.accidental && keys[i]?.accidental === k.accidental) || - (!keys[i]?.accidental && !k.accidental)) - ) { + if (keys[i]?.note === n) { return i; } } @@ -25,8 +21,8 @@ const getKeyIndex = (k: PianoKey, keys: PianoKey[]) => { type OctaveProps = { number: number; - startNote: PianoKey; - endNote: PianoKey; + startNote: Note; + endNote: Note; onNoteDown: (note: PianoKey) => void; onNoteUp: (note: PianoKey) => void; }; @@ -46,28 +42,58 @@ const Octave = ({ const endNoteIndex = getKeyIndex(endNote, oK); const keys = oK.slice(startNoteIndex, endNoteIndex + 1); + const whiteKeys = octaveKeys.filter((k) => k?.accidental === undefined); + const blackKeys = octaveKeys.filter((k) => k?.accidental !== undefined); + return ( - - {keys.map((key, i) => { - return ( - onNoteDown(key)} - onPressOut={() => onNoteUp(key)} - > - + + + {whiteKeys.map((key, i) => { + return ( + onNoteDown(key)} + onPressOut={() => onNoteUp(key)} > - {key.toString()} - - - ); - })} - + + {key.toString()} + + + ); + })} + + + {blackKeys.map((key, i) => { + return ( + onNoteDown(key)} + onPressOut={() => onNoteUp(key)} + > + + {key.toString()} + + + ); + })} + ; + + + ); }; diff --git a/front/components/VirtualPiano/VirtualPiano.tsx b/front/components/VirtualPiano/VirtualPiano.tsx index 93922f2..47f8e36 100644 --- a/front/components/VirtualPiano/VirtualPiano.tsx +++ b/front/components/VirtualPiano/VirtualPiano.tsx @@ -7,9 +7,9 @@ type VirtualPianoProps = Parameters[0] & { onNoteDown: (note: PianoKey) => void; onNoteUp: (note: PianoKey) => void; startOctave: number; - startNote: PianoKey; + startNote: Note; endOctave: number; - endNote: PianoKey; + endNote: Note; showNoteNames: NoteNameBehavior; // default "onpress" highlightedNotes: Array; highlightColor: string; @@ -36,6 +36,7 @@ const VirtualPiano = ({ keyPressStyle, vividKeyPressColor, }: VirtualPianoProps) => { + const notesList: Array = ["C", "D", "E", "F", "G", "A", "B"]; const octaveList = []; for (let octaveNum = startOctave; octaveNum <= endOctave; octaveNum++) { @@ -49,8 +50,8 @@ const VirtualPiano = ({ @@ -68,9 +69,9 @@ VirtualPiano.defaultProps = { console.log("Note up: " + n); }, startOctave: 2, - startNote: new PianoKey("C"), + startNote: "C", endOctave: 6, - endNote: new PianoKey("C"), + endNote: "C", showNoteNames: "onpress", highlightedNotes: [], highlightColor: "red",