Move isWhiteKey, isBlackKey into Piano.h to make use of these functions in a wider scope

This commit is contained in:
Colin Wallace
2016-02-03 18:46:59 -08:00
parent 1228de750f
commit aa0f7a08d6
4 changed files with 39 additions and 51 deletions

View File

@@ -63,6 +63,9 @@ public:
return m_midiEvProc;
}
static bool isWhiteKey(int key);
static bool isBlackKey(int key);
private:
static bool isValidKey( int key )

View File

@@ -37,9 +37,11 @@
*/
#include "Piano.h"
#include "InstrumentTrack.h"
#include "MidiEvent.h"
#include "MidiEventProcessor.h"
#include "Note.h"
/*! \brief Create a new keyboard display
@@ -125,6 +127,26 @@ void Piano::handleKeyRelease( int key )
bool Piano::isBlackKey( int key )
{
int keyCode = key % KeysPerOctave;
switch (keyCode)
{
case 1:
case 3:
case 6:
case 8:
case 10:
return true;
}
return false;
}
bool Piano::isWhiteKey( int key )
{
return !isBlackKey( key );
}

View File

@@ -45,6 +45,7 @@
#include "PianoView.h"
#include "Piano.h"
#include "CaptionMenu.h"
#include "embed.h"
#include "Engine.h"
@@ -58,17 +59,6 @@
#include "update_event.h"
/*! The black / white order of keys as they appear on the keyboard.
*/
const Piano::KeyTypes KEY_ORDER[] =
{
// C CIS D DIS
Piano::WhiteKey, Piano::BlackKey, Piano::WhiteKey, Piano::BlackKey,
// E F FIS G
Piano::WhiteKey, Piano::WhiteKey, Piano::BlackKey, Piano::WhiteKey,
// GIS A B H
Piano::BlackKey, Piano::WhiteKey, Piano::BlackKey, Piano::WhiteKey
} ;
/*! The scale of C Major - white keys only.
@@ -348,8 +338,7 @@ int PianoView::getKeyFromMouse( const QPoint & _p ) const
for( int i = 0; i <= key_num; ++i )
{
if( KEY_ORDER[( m_startKey+i ) % KeysPerOctave] ==
Piano::BlackKey )
if ( Piano::isBlackKey( m_startKey+i ) )
{
++key_num;
}
@@ -362,17 +351,14 @@ int PianoView::getKeyFromMouse( const QPoint & _p ) const
{
// then do extra checking whether the mouse-cursor is over
// a black key
if( key_num > 0 && KEY_ORDER[(key_num-1 ) % KeysPerOctave] ==
Piano::BlackKey &&
if( key_num > 0 && Piano::isBlackKey( key_num-1 ) &&
_p.x() % PW_WHITE_KEY_WIDTH <=
( PW_WHITE_KEY_WIDTH / 2 ) -
( PW_BLACK_KEY_WIDTH / 2 ) )
{
--key_num;
}
if( key_num < NumKeys - 1 &&
KEY_ORDER[( key_num + 1 ) % KeysPerOctave] ==
Piano::BlackKey &&
if( key_num < NumKeys - 1 && Piano::isBlackKey( key_num+1 ) &&
_p.x() % PW_WHITE_KEY_WIDTH >=
( PW_WHITE_KEY_WIDTH -
PW_BLACK_KEY_WIDTH / 2 ) )
@@ -456,8 +442,7 @@ void PianoView::mousePressEvent( QMouseEvent * _me )
{
int y_diff = _me->pos().y() - PIANO_BASE;
int velocity = (int)( ( float ) y_diff /
( ( KEY_ORDER[key_num % KeysPerOctave] ==
Piano::WhiteKey ) ?
( Piano::isWhiteKey( key_num ) ?
PW_WHITE_KEY_HEIGHT : PW_BLACK_KEY_HEIGHT ) *
(float) m_piano->instrumentTrack()->midiPort()->baseVelocity() );
if( y_diff < 0 )
@@ -465,8 +450,7 @@ void PianoView::mousePressEvent( QMouseEvent * _me )
velocity = 0;
}
else if( y_diff >
( ( KEY_ORDER[key_num % KeysPerOctave] ==
Piano::WhiteKey ) ?
( Piano::isWhiteKey( key_num ) ?
PW_WHITE_KEY_HEIGHT : PW_BLACK_KEY_HEIGHT ) )
{
velocity = m_piano->instrumentTrack()->midiPort()->baseVelocity();
@@ -555,7 +539,7 @@ void PianoView::mouseMoveEvent( QMouseEvent * _me )
int key_num = getKeyFromMouse( _me->pos() );
int y_diff = _me->pos().y() - PIANO_BASE;
int velocity = (int)( (float) y_diff /
( ( KEY_ORDER[key_num % KeysPerOctave] == Piano::WhiteKey ) ?
( Piano::isWhiteKey( key_num ) ?
PW_WHITE_KEY_HEIGHT : PW_BLACK_KEY_HEIGHT ) *
(float) m_piano->instrumentTrack()->midiPort()->baseVelocity() );
// maybe the user moved the mouse-cursor above or under the
@@ -566,7 +550,7 @@ void PianoView::mouseMoveEvent( QMouseEvent * _me )
velocity = 0;
}
else if( y_diff >
( ( KEY_ORDER[key_num % KeysPerOctave] == Piano::WhiteKey ) ?
( Piano::isWhiteKey( key_num ) ?
PW_WHITE_KEY_HEIGHT : PW_BLACK_KEY_HEIGHT ) )
{
velocity = m_piano->instrumentTrack()->midiPort()->baseVelocity();
@@ -751,7 +735,7 @@ int PianoView::getKeyX( int _key_num ) const
while( k <= _key_num )
{
if( KEY_ORDER[k % KeysPerOctave] == Piano::WhiteKey )
if( Piano::isWhiteKey( k ) )
{
++white_cnt;
if( white_cnt > 1 )
@@ -818,7 +802,7 @@ void PianoView::paintEvent( QPaintEvent * )
QPalette::BrightText ) );
g.setColorAt( 1, QApplication::palette().color( QPalette::Active,
QPalette::BrightText ) );
if( KEY_ORDER[base_key % KeysPerOctave] == Piano::WhiteKey )
if( Piano::isWhiteKey( base_key ) )
{
p.fillRect( QRect( getKeyX( base_key ), 1, PW_WHITE_KEY_WIDTH-1,
PIANO_BASE-2 ), g );
@@ -835,7 +819,7 @@ void PianoView::paintEvent( QPaintEvent * )
// draw all white keys...
for( int x = 0; x < width(); )
{
while( KEY_ORDER[cur_key%KeysPerOctave] != Piano::WhiteKey )
while( Piano::isBlackKey( cur_key ) )
{
++cur_key;
}
@@ -870,8 +854,7 @@ void PianoView::paintEvent( QPaintEvent * )
int white_cnt = 0;
int startKey = m_startKey;
if( startKey > 0 &&
KEY_ORDER[(Keys)(--startKey) % KeysPerOctave] == Piano::BlackKey )
if( startKey > 0 && Piano::isBlackKey( (Keys)(--startKey) ) )
{
if( m_piano && m_piano->isKeyPressed( startKey ) )
{
@@ -886,7 +869,7 @@ void PianoView::paintEvent( QPaintEvent * )
// now draw all black keys...
for( int x = 0; x < width(); )
{
if( KEY_ORDER[cur_key%KeysPerOctave] == Piano::BlackKey )
if( Piano::isBlackKey( cur_key ) )
{
// draw pressed or not pressed key, depending on
// state of current key

View File

@@ -139,27 +139,7 @@ static QString getNoteString( int key )
return s_noteStrings[key % 12] + QString::number( static_cast<int>( key / KeysPerOctave ) );
}
static bool isBlackKey( int key )
{
int keyCode = key % KeysPerOctave;
switch (keyCode)
{
case 1:
case 3:
case 6:
case 8:
case 10:
return true;
}
return false;
}
static bool isWhiteKey( int key )
{
return !isBlackKey( key );
}
// used for drawing of piano
PianoRoll::PianoRollKeyTypes PianoRoll::prKeyOrder[] =
@@ -2691,7 +2671,7 @@ void PianoRoll::paintEvent(QPaintEvent * pe )
break;
}
if ( isWhiteKey( key ) )
if ( Piano::isWhiteKey( key ) )
{
// Draw note names if activated in the preferences, C notes are always drawn
if ( key % 12 == 0 || drawNoteNames )