From eeeeb117a13f80dc05702caf25be265d6858315a Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 25 Oct 2021 18:44:25 +0100 Subject: [PATCH] MidiKeyboardComponent: Ensure note is not highlighted after mouse leaves component --- .../gui/juce_MidiKeyboardComponent.cpp | 46 ++++++++----------- .../gui/juce_MidiKeyboardComponent.h | 5 +- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp index 110403c47c..6f0f939894 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp @@ -279,14 +279,13 @@ float MidiKeyboardComponent::getTotalKeyboardWidth() const noexcept int MidiKeyboardComponent::getNoteAtPosition (Point p) { - float v; - return xyToNote (p, v); + return xyToNote (p).note; } -int MidiKeyboardComponent::xyToNote (Point pos, float& mousePositionVelocity) +MidiKeyboardComponent::NoteAndVelocity MidiKeyboardComponent::xyToNote (Point pos) { - if (! reallyContains (pos.toInt(), false)) - return -1; + if (! reallyContains (pos, false)) + return { -1, 0.0f }; auto p = pos; @@ -300,10 +299,10 @@ int MidiKeyboardComponent::xyToNote (Point pos, float& mousePositionVeloc p = { (float) getHeight() - p.x, p.y }; } - return remappedXYToNote (p + Point (xOffset, 0), mousePositionVelocity); + return remappedXYToNote (p + Point (xOffset, 0)); } -int MidiKeyboardComponent::remappedXYToNote (Point pos, float& mousePositionVelocity) const +MidiKeyboardComponent::NoteAndVelocity MidiKeyboardComponent::remappedXYToNote (Point pos) const { auto blackNoteLength = getBlackNoteLength(); @@ -315,12 +314,11 @@ int MidiKeyboardComponent::remappedXYToNote (Point pos, float& mousePosit { auto note = octaveStart + blackNotes[i]; - if (note >= rangeStart && note <= rangeEnd) + if (rangeStart <= note && note <= rangeEnd) { if (getKeyPos (note).contains (pos.x - xOffset)) { - mousePositionVelocity = jmax (0.0f, pos.y / blackNoteLength); - return note; + return { note, jmax (0.0f, pos.y / blackNoteLength) }; } } } @@ -338,15 +336,13 @@ int MidiKeyboardComponent::remappedXYToNote (Point pos, float& mousePosit if (getKeyPos (note).contains (pos.x - xOffset)) { auto whiteNoteLength = (orientation == horizontalKeyboard) ? getHeight() : getWidth(); - mousePositionVelocity = jmax (0.0f, pos.y / (float) whiteNoteLength); - return note; + return { note, jmax (0.0f, pos.y / (float) whiteNoteLength) }; } } } } - mousePositionVelocity = 0; - return -1; + return { -1, 0 }; } //============================================================================== @@ -643,9 +639,8 @@ void MidiKeyboardComponent::resized() auto endOfLastKey = getKeyPos (rangeEnd).getEnd(); - float mousePositionVelocity; auto spaceAvailable = w; - auto lastStartKey = remappedXYToNote ({ endOfLastKey - (float) spaceAvailable, 0 }, mousePositionVelocity) + 1; + auto lastStartKey = remappedXYToNote ({ endOfLastKey - (float) spaceAvailable, 0 }).note + 1; if (lastStartKey >= 0 && ((int) firstKey) > lastStartKey) { @@ -709,11 +704,11 @@ void MidiKeyboardComponent::updateNoteUnderMouse (const MouseEvent& e, bool isDo void MidiKeyboardComponent::updateNoteUnderMouse (Point pos, bool isDown, int fingerNum) { - float mousePositionVelocity = 0.0f; - auto newNote = xyToNote (pos, mousePositionVelocity); - auto oldNote = mouseOverNotes.getUnchecked (fingerNum); - auto oldNoteDown = mouseDownNotes.getUnchecked (fingerNum); - auto eventVelocity = useMousePositionForVelocity ? mousePositionVelocity * velocity : velocity; + const auto noteInfo = xyToNote (pos); + const auto newNote = noteInfo.note; + const auto oldNote = mouseOverNotes.getUnchecked (fingerNum); + const auto oldNoteDown = mouseDownNotes.getUnchecked (fingerNum); + const auto eventVelocity = useMousePositionForVelocity ? noteInfo.velocity * velocity : velocity; if (oldNote != newNote) { @@ -757,8 +752,7 @@ void MidiKeyboardComponent::mouseMove (const MouseEvent& e) void MidiKeyboardComponent::mouseDrag (const MouseEvent& e) { - float mousePositionVelocity; - auto newNote = xyToNote (e.position, mousePositionVelocity); + auto newNote = xyToNote (e.position).note; if (newNote >= 0 && mouseDraggedToKey (newNote, e)) updateNoteUnderMouse (e, true); @@ -770,8 +764,7 @@ void MidiKeyboardComponent::mouseUpOnKey (int, const MouseEvent&) {} void MidiKeyboardComponent::mouseDown (const MouseEvent& e) { - float mousePositionVelocity; - auto newNote = xyToNote (e.position, mousePositionVelocity); + auto newNote = xyToNote (e.position).note; if (newNote >= 0 && mouseDownOnKey (newNote, e)) updateNoteUnderMouse (e, true); @@ -781,8 +774,7 @@ void MidiKeyboardComponent::mouseUp (const MouseEvent& e) { updateNoteUnderMouse (e, false); - float mousePositionVelocity; - auto note = xyToNote (e.position, mousePositionVelocity); + auto note = xyToNote (e.position).note; if (note >= 0) mouseUpOnKey (note, e); diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h index c76f489d22..da502d95aa 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h @@ -399,6 +399,7 @@ protected: private: //============================================================================== struct UpDownButton; + struct NoteAndVelocity { int note; float velocity; }; MidiKeyboardState& state; float blackNoteLengthRatio = 0.7f; @@ -425,8 +426,8 @@ private: int keyMappingOctave = 6, octaveNumForMiddleC = 3; Range getKeyPos (int midiNoteNumber) const; - int xyToNote (Point, float& mousePositionVelocity); - int remappedXYToNote (Point, float& mousePositionVelocity) const; + NoteAndVelocity xyToNote (Point); + NoteAndVelocity remappedXYToNote (Point) const; void resetAnyKeysInUse(); void updateNoteUnderMouse (Point, bool isDown, int fingerNum); void updateNoteUnderMouse (const MouseEvent&, bool isDown);