From c536d10be9a2b9509709f1a4fa5a2bf7dc87fa09 Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Tue, 5 Jan 2016 16:39:39 +0000 Subject: [PATCH] MidiKeyboardComponent: added possibility to change the length of the black keys as a proportion of the white keys to something else than the default of 0.7, and to query that proportion. --- .../gui/juce_MidiKeyboardComponent.cpp | 25 ++++++++++++++++--- .../gui/juce_MidiKeyboardComponent.h | 13 +++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp index b895984399..cac00f3de5 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp @@ -59,8 +59,8 @@ private: //============================================================================== MidiKeyboardComponent::MidiKeyboardComponent (MidiKeyboardState& s, Orientation o) : state (s), + blackNoteLengthRatio (0.7f), xOffset (0), - blackNoteLength (1), keyWidth (16.0f), orientation (o), midiChannel (1), @@ -239,6 +239,8 @@ Rectangle MidiKeyboardComponent::getRectangleForKey (const int note) const if (MidiMessage::isMidiNoteBlack (note)) { + const int blackNoteLength = getBlackNoteLength(); + switch (orientation) { case horizontalKeyboard: return Rectangle (x, 0, w, blackNoteLength); @@ -299,6 +301,8 @@ int MidiKeyboardComponent::xyToNote (Point pos, float& mousePositionVelocit int MidiKeyboardComponent::remappedXYToNote (Point pos, float& mousePositionVelocity) const { + const int blackNoteLength = getBlackNoteLength(); + if (pos.getY() < blackNoteLength) { for (int octaveStart = 12 * (rangeStart / 12); octaveStart <= rangeEnd; octaveStart += 12) @@ -577,6 +581,23 @@ void MidiKeyboardComponent::drawUpDownButton (Graphics& g, int w, int h, g.fillPath (path, path.getTransformToScaleToFit (1.0f, 1.0f, w - 2.0f, h - 2.0f, true)); } +void MidiKeyboardComponent::setBlackNoteLengthProportion (float ratio) noexcept +{ + jassert (ratio >= 0.0f && ratio <= 1.0f); + if (blackNoteLengthRatio != ratio) + { + blackNoteLengthRatio = ratio; + resized(); + } +} + +int MidiKeyboardComponent::getBlackNoteLength() const noexcept +{ + const int whiteNoteLength = orientation == horizontalKeyboard ? getHeight() : getWidth(); + + return roundToInt (whiteNoteLength * blackNoteLengthRatio); +} + void MidiKeyboardComponent::resized() { int w = getWidth(); @@ -587,8 +608,6 @@ void MidiKeyboardComponent::resized() if (orientation != horizontalKeyboard) std::swap (w, h); - blackNoteLength = roundToInt (h * 0.7f); - int kx2, kw2; getKeyPos (rangeEnd, kx2, kw2); diff --git a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h index 621a38d95d..22e1c7517c 100644 --- a/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h +++ b/modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h @@ -165,10 +165,16 @@ public: */ int getLowestVisibleKey() const noexcept { return (int) firstKey; } - /** Returns the length of the black notes. + /** Sets the length of the black notes as a proportion of the white note length. */ + void setBlackNoteLengthProportion (float ratio) noexcept; + + /** Returns the length of the black notes as a proportion of the white note length. */ + float getBlackNoteLengthProportion() const noexcept { return blackNoteLengthRatio; } + + /** Returns the absolute length of the black notes. This will be their vertical or horizontal length, depending on the keyboard's orientation. */ - int getBlackNoteLength() const noexcept { return blackNoteLength; } + int getBlackNoteLength() const noexcept; /** If set to true, then scroll buttons will appear at either end of the keyboard if there are too many notes to fit them all in the component at once. @@ -375,7 +381,8 @@ private: friend class MidiKeyboardUpDownButton; MidiKeyboardState& state; - int xOffset, blackNoteLength; + float blackNoteLengthRatio; + int xOffset; float keyWidth; Orientation orientation;