Browse Source

Added some methods to MidiKeyboardComponent to set/get the width of black notes

tags/2021-05-28
ed 8 years ago
parent
commit
822a1e31b8
2 changed files with 29 additions and 8 deletions
  1. +17
    -8
      modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp
  2. +12
    -0
      modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h

+ 17
- 8
modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.cpp View File

@@ -194,21 +194,19 @@ Range<float> MidiKeyboardComponent::getKeyPosition (int midiNoteNumber, float ta
{
jassert (midiNoteNumber >= 0 && midiNoteNumber < 128);
static const float blackNoteWidth = 0.7f;
static const float notePos[] = { 0.0f, 1 - blackNoteWidth * 0.6f,
1.0f, 2 - blackNoteWidth * 0.4f,
static const float notePos[] = { 0.0f, 1 - blackNoteWidthRatio * 0.6f,
1.0f, 2 - blackNoteWidthRatio * 0.4f,
2.0f,
3.0f, 4 - blackNoteWidth * 0.7f,
4.0f, 5 - blackNoteWidth * 0.5f,
5.0f, 6 - blackNoteWidth * 0.3f,
3.0f, 4 - blackNoteWidthRatio * 0.7f,
4.0f, 5 - blackNoteWidthRatio * 0.5f,
5.0f, 6 - blackNoteWidthRatio * 0.3f,
6.0f };
auto octave = midiNoteNumber / 12;
auto note = midiNoteNumber % 12;
auto start = octave * 7.0f * targetKeyWidth + notePos[note] * targetKeyWidth;
auto width = MidiMessage::isMidiNoteBlack (note) ? blackNoteWidth * targetKeyWidth : targetKeyWidth;
auto width = MidiMessage::isMidiNoteBlack (note) ? blackNoteWidthRatio * targetKeyWidth : targetKeyWidth;
return { start, start + width };
}
@@ -568,6 +566,17 @@ float MidiKeyboardComponent::getBlackNoteLength() const noexcept
return whiteNoteLength * blackNoteLengthRatio;
}
void MidiKeyboardComponent::setBlackNoteWidthProportion (float ratio) noexcept
{
jassert (ratio >= 0.0f && ratio <= 1.0f);
if (blackNoteWidthRatio != ratio)
{
blackNoteWidthRatio = ratio;
resized();
}
}
void MidiKeyboardComponent::resized()
{
auto w = getWidth();


+ 12
- 0
modules/juce_audio_utils/gui/juce_MidiKeyboardComponent.h View File

@@ -177,6 +177,17 @@ public:
*/
float getBlackNoteLength() const noexcept;
/** Sets the width of the black notes as a proportion of the white note width. */
void setBlackNoteWidthProportion (float ratio) noexcept;
/** Returns the width of the black notes as a proportion of the white note width. */
float getBlackNoteWidthProportion() const noexcept { return blackNoteWidthRatio; }
/** Returns the absolute width of the black notes.
This will be their vertical or horizontal width, depending on the keyboard's orientation.
*/
float getBlackNoteWidth() const noexcept { return keyWidth * blackNoteWidthRatio; }
/** 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.
*/
@@ -380,6 +391,7 @@ private:
MidiKeyboardState& state;
float blackNoteLengthRatio = 0.7f;
float blackNoteWidthRatio = 0.7f;
float xOffset = 0;
float keyWidth = 16.0f;
Orientation orientation;


Loading…
Cancel
Save