Browse Source

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.

tags/2021-05-28
Timur Doumler 9 years ago
parent
commit
c536d10be9
2 changed files with 32 additions and 6 deletions
  1. +22
    -3
      modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp
  2. +10
    -3
      modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h

+ 22
- 3
modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp View File

@@ -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<int> MidiKeyboardComponent::getRectangleForKey (const int note) const
if (MidiMessage::isMidiNoteBlack (note))
{
const int blackNoteLength = getBlackNoteLength();
switch (orientation)
{
case horizontalKeyboard: return Rectangle<int> (x, 0, w, blackNoteLength);
@@ -299,6 +301,8 @@ int MidiKeyboardComponent::xyToNote (Point<int> pos, float& mousePositionVelocit
int MidiKeyboardComponent::remappedXYToNote (Point<int> 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);


+ 10
- 3
modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h View File

@@ -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;


Loading…
Cancel
Save