Skip to content

Commit

Permalink
Play correct note when dragging left side of virtual keyboard (LMMS#4265
Browse files Browse the repository at this point in the history
)
  • Loading branch information
PhysSong authored and tresf committed Mar 22, 2018
1 parent bbac23d commit 760abcb
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/gui/PianoView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ void PianoView::modelChanged()
*/
int PianoView::getKeyFromMouse( const QPoint & _p ) const
{
int key_num = (int)( (float) _p.x() / (float) PW_WHITE_KEY_WIDTH );
int offset = _p.x() % PW_WHITE_KEY_WIDTH;
if( offset < 0 ) offset += PW_WHITE_KEY_WIDTH;
int key_num = ( _p.x() - offset) / PW_WHITE_KEY_WIDTH;

for( int i = 0; i <= key_num; ++i )
{
Expand All @@ -336,6 +338,13 @@ int PianoView::getKeyFromMouse( const QPoint & _p ) const
++key_num;
}
}
for( int i = 0; i >= key_num; --i )
{
if ( Piano::isBlackKey( m_startKey+i ) )
{
--key_num;
}
}

key_num += m_startKey;

Expand All @@ -345,16 +354,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 && Piano::isBlackKey( key_num-1 ) &&
_p.x() % PW_WHITE_KEY_WIDTH <=
( PW_WHITE_KEY_WIDTH / 2 ) -
( PW_BLACK_KEY_WIDTH / 2 ) )
offset <= ( PW_WHITE_KEY_WIDTH / 2 ) -
( PW_BLACK_KEY_WIDTH / 2 ) )
{
--key_num;
}
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 ) )
offset >= ( PW_WHITE_KEY_WIDTH -
PW_BLACK_KEY_WIDTH / 2 ) )
{
++key_num;
}
Expand Down

0 comments on commit 760abcb

Please sign in to comment.