From 6a10ad2398fe55bf5b356f454bf71934fb98a223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sun, 19 Mar 2023 17:59:20 +0100 Subject: [PATCH] reworked the octave css layout to get rid of the Zindex added support for showNoteNames policy and added an onhover policy --- front/components/VirtualPiano/Octave.tsx | 184 ++++++++++-------- .../components/VirtualPiano/VirtualPiano.tsx | 9 +- front/models/Piano.ts | 2 +- 3 files changed, 114 insertions(+), 81 deletions(-) diff --git a/front/components/VirtualPiano/Octave.tsx b/front/components/VirtualPiano/Octave.tsx index 3fa0d62..961e949 100644 --- a/front/components/VirtualPiano/Octave.tsx +++ b/front/components/VirtualPiano/Octave.tsx @@ -19,21 +19,37 @@ const getKeyIndex = (n: Note, keys: PianoKey[]) => { return -1; }; -type OctaveProps = { +const isNoteVisible = ( + showNoteNamesPolicy: NoteNameBehavior, + isPressed: boolean, + isHovered: boolean, + isHighlighted: boolean +) => { + if (showNoteNamesPolicy === "always") return true; + if (showNoteNamesPolicy === "never") return false; + + if (showNoteNamesPolicy === "onpress") { + return isPressed; + } else if (showNoteNamesPolicy === "onhover") { + return isHovered; + } else if (showNoteNamesPolicy === "onhighlight") { + return isHighlighted; + } + return false; +}; + +type OctaveProps = Parameters[0] & { number: number; startNote: Note; endNote: Note; + showNoteNames: NoteNameBehavior; onNoteDown: (note: PianoKey) => void; onNoteUp: (note: PianoKey) => void; }; -const Octave = ({ - number, - startNote, - endNote, - onNoteDown, - onNoteUp, -}: OctaveProps) => { +const Octave = (props: OctaveProps) => { + const { number, startNote, endNote, showNoteNames, onNoteDown, onNoteUp } = + props; const oK: PianoKey[] = octaveKeys.map((k) => { return new PianoKey(k.note, k.accidental, number); }); @@ -45,38 +61,41 @@ const Octave = ({ const whiteKeys = keys.filter((k) => k?.accidental === undefined); const blackKeys = keys.filter((k) => k?.accidental !== undefined); - const whiteKeyWidthExpr = "50px"; - const whiteKeyHeightExpr = "200px"; - const blackKeyWidthExpr = "25px"; - const blackKeyHeightExpr = "100px"; + const whiteKeyWidthExpr = "calc(100% / 7)"; + const whiteKeyHeightExpr = "100%"; + const blackKeyWidthExpr = "calc(100% / 13)"; + const blackKeyHeightExpr = "calc(100% / 1.5)"; return ( - - - - {whiteKeys.map((key, i) => { - return ( - onNoteDown(key)} - onPressOut={() => onNoteUp(key)} - > - {({ isHovered, isPressed }) => ( - + + + {whiteKeys.map((key, i) => { + return ( + onNoteDown(key)} + onPressOut={() => onNoteUp(key)} + > + {({ isHovered, isPressed }) => ( + + {isNoteVisible( + showNoteNames, + isPressed, + isHovered, + false + ) && ( {key.note} - - )} - - ); - })} - - - {blackKeys.map((key, i) => { - return ( - onNoteDown(key)} - onPressOut={() => onNoteUp(key)} - > - {({ isHovered, isPressed }) => ( - 1) as unknown as number)) * 50 + (50 - 25 / 2) - }px`, - top: "0px", - justifyContent: "flex-end", - alignItems: "center", - }} - > + )} + + )} + + ); + })} + {blackKeys.map((key, i) => { + return ( + onNoteDown(key)} + onPressOut={() => onNoteUp(key)} + width={blackKeyWidthExpr} + height={blackKeyHeightExpr} + style={{ + position: "absolute", + left: `calc(calc(${whiteKeyWidthExpr} * ${ + i + ((i > 1) as unknown as number) + 1 + }) - calc(${blackKeyWidthExpr} / 2))`, + top: "0px", + }} + > + {({ isHovered, isPressed }) => ( + + {isNoteVisible( + showNoteNames, + isPressed, + isHovered, + false + ) && ( {key.note + key.accidental} - - )} - - ); - })} - - + )} + + )} + + ); + })} + ); }; diff --git a/front/components/VirtualPiano/VirtualPiano.tsx b/front/components/VirtualPiano/VirtualPiano.tsx index 47f8e36..0e64eeb 100644 --- a/front/components/VirtualPiano/VirtualPiano.tsx +++ b/front/components/VirtualPiano/VirtualPiano.tsx @@ -48,8 +48,11 @@ const VirtualPiano = ({ {octaveList.map((octaveNum) => { return (