From 7b606e2e3ff9f942d1a12972ad2024967929d900 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 12 Oct 2015 18:16:07 +0100 Subject: [PATCH] Refactored some internal code in MidiKeyboardComponent --- .../gui/juce_MidiKeyboardComponent.cpp | 55 ++++++++++--------- .../gui/juce_MidiKeyboardComponent.h | 7 ++- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp index 2422c625ce..b895984399 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp @@ -230,21 +230,35 @@ void MidiKeyboardComponent::getKeyPos (int midiNoteNumber, int& x, int& w) const x -= xOffset + rx; } -Rectangle MidiKeyboardComponent::getWhiteNotePos (int noteNum) const +Rectangle MidiKeyboardComponent::getRectangleForKey (const int note) const { - int x, w; - getKeyPos (noteNum, x, w); - Rectangle pos; + jassert (note >= rangeStart && note <= rangeEnd); - switch (orientation) + int x, w; + getKeyPos (note, x, w); + + if (MidiMessage::isMidiNoteBlack (note)) { - case horizontalKeyboard: pos.setBounds (x, 0, w, getHeight()); break; - case verticalKeyboardFacingLeft: pos.setBounds (0, x, getWidth(), w); break; - case verticalKeyboardFacingRight: pos.setBounds (0, getHeight() - x - w, getWidth(), w); break; - default: break; + switch (orientation) + { + case horizontalKeyboard: return Rectangle (x, 0, w, blackNoteLength); + case verticalKeyboardFacingLeft: return Rectangle (getWidth() - blackNoteLength, x, blackNoteLength, w); + case verticalKeyboardFacingRight: return Rectangle (0, getHeight() - x - w, blackNoteLength, w); + default: jassertfalse; break; + } + } + else + { + switch (orientation) + { + case horizontalKeyboard: return Rectangle (x, 0, w, getHeight()); + case verticalKeyboardFacingLeft: return Rectangle (0, x, getWidth(), w); + case verticalKeyboardFacingRight: return Rectangle (0, getHeight() - x - w, getWidth(), w); + default: jassertfalse; break; + } } - return pos; + return Rectangle(); } int MidiKeyboardComponent::getKeyStartPosition (const int midiNoteNumber) const @@ -339,7 +353,7 @@ int MidiKeyboardComponent::remappedXYToNote (Point pos, float& mousePositio void MidiKeyboardComponent::repaintNote (const int noteNum) { if (noteNum >= rangeStart && noteNum <= rangeEnd) - repaint (getWhiteNotePos (noteNum)); + repaint (getRectangleForKey (noteNum)); } void MidiKeyboardComponent::paint (Graphics& g) @@ -349,9 +363,7 @@ void MidiKeyboardComponent::paint (Graphics& g) const Colour lineColour (findColour (keySeparatorLineColourId)); const Colour textColour (findColour (textLabelColourId)); - int octave; - - for (octave = 0; octave < 128; octave += 12) + for (int octave = 0; octave < 128; octave += 12) { for (int white = 0; white < 7; ++white) { @@ -359,7 +371,7 @@ void MidiKeyboardComponent::paint (Graphics& g) if (noteNum >= rangeStart && noteNum <= rangeEnd) { - const Rectangle pos (getWhiteNotePos (noteNum)); + Rectangle pos = getRectangleForKey (noteNum); drawWhiteNote (noteNum, g, pos.getX(), pos.getY(), pos.getWidth(), pos.getHeight(), state.isNoteOnForChannels (midiInChannelMask, noteNum), @@ -416,7 +428,7 @@ void MidiKeyboardComponent::paint (Graphics& g) const Colour blackNoteColour (findColour (blackNoteColourId)); - for (octave = 0; octave < 128; octave += 12) + for (int octave = 0; octave < 128; octave += 12) { for (int black = 0; black < 5; ++black) { @@ -424,16 +436,7 @@ void MidiKeyboardComponent::paint (Graphics& g) if (noteNum >= rangeStart && noteNum <= rangeEnd) { - getKeyPos (noteNum, x, w); - Rectangle pos; - - switch (orientation) - { - case horizontalKeyboard: pos.setBounds (x, 0, w, blackNoteLength); break; - case verticalKeyboardFacingLeft: pos.setBounds (width - blackNoteLength, x, blackNoteLength, w); break; - case verticalKeyboardFacingRight: pos.setBounds (0, height - x - w, blackNoteLength, w); break; - default: break; - } + Rectangle pos = getRectangleForKey (noteNum); drawBlackNote (noteNum, g, pos.getX(), pos.getY(), pos.getWidth(), pos.getHeight(), state.isNoteOnForChannels (midiInChannelMask, noteNum), diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h index caa8483ba9..621a38d95d 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h @@ -366,6 +366,10 @@ protected: virtual void getKeyPosition (int midiNoteNumber, float keyWidth, int& x, int& w) const; + /** Returns the rectangle for a given key if within the displayable range */ + Rectangle getRectangleForKey (int midiNoteNumber) const; + + private: //============================================================================== friend class MidiKeyboardUpDownButton; @@ -400,9 +404,8 @@ private: void resetAnyKeysInUse(); void updateNoteUnderMouse (Point, bool isDown, int fingerNum); void updateNoteUnderMouse (const MouseEvent&, bool isDown); - void repaintNote (const int midiNoteNumber); + void repaintNote (int midiNoteNumber); void setLowestVisibleKeyFloat (float noteNumber); - Rectangle getWhiteNotePos (int noteNumber) const; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiKeyboardComponent) };