mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
MidiKeyboardComponent: Ensure note is not highlighted after mouse leaves component
This commit is contained in:
parent
fe4515adb6
commit
eeeeb117a1
2 changed files with 22 additions and 29 deletions
|
|
@ -279,14 +279,13 @@ float MidiKeyboardComponent::getTotalKeyboardWidth() const noexcept
|
|||
|
||||
int MidiKeyboardComponent::getNoteAtPosition (Point<float> p)
|
||||
{
|
||||
float v;
|
||||
return xyToNote (p, v);
|
||||
return xyToNote (p).note;
|
||||
}
|
||||
|
||||
int MidiKeyboardComponent::xyToNote (Point<float> pos, float& mousePositionVelocity)
|
||||
MidiKeyboardComponent::NoteAndVelocity MidiKeyboardComponent::xyToNote (Point<float> 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<float> pos, float& mousePositionVeloc
|
|||
p = { (float) getHeight() - p.x, p.y };
|
||||
}
|
||||
|
||||
return remappedXYToNote (p + Point<float> (xOffset, 0), mousePositionVelocity);
|
||||
return remappedXYToNote (p + Point<float> (xOffset, 0));
|
||||
}
|
||||
|
||||
int MidiKeyboardComponent::remappedXYToNote (Point<float> pos, float& mousePositionVelocity) const
|
||||
MidiKeyboardComponent::NoteAndVelocity MidiKeyboardComponent::remappedXYToNote (Point<float> pos) const
|
||||
{
|
||||
auto blackNoteLength = getBlackNoteLength();
|
||||
|
||||
|
|
@ -315,12 +314,11 @@ int MidiKeyboardComponent::remappedXYToNote (Point<float> 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<float> 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<float> 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);
|
||||
|
|
|
|||
|
|
@ -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<float> getKeyPos (int midiNoteNumber) const;
|
||||
int xyToNote (Point<float>, float& mousePositionVelocity);
|
||||
int remappedXYToNote (Point<float>, float& mousePositionVelocity) const;
|
||||
NoteAndVelocity xyToNote (Point<float>);
|
||||
NoteAndVelocity remappedXYToNote (Point<float>) const;
|
||||
void resetAnyKeysInUse();
|
||||
void updateNoteUnderMouse (Point<float>, bool isDown, int fingerNum);
|
||||
void updateNoteUnderMouse (const MouseEvent&, bool isDown);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue