Merge pull request #77 from LMMS/smooth-knob

UI: smoother knobs
This commit is contained in:
Tobias Doerffel
2014-01-21 15:19:40 -08:00
3 changed files with 13 additions and 38 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
/build
.*.sw?
*~
/CMakeLists.txt.user

View File

@@ -148,12 +148,11 @@ private:
QPixmap * m_knobPixmap;
BoolModel m_volumeKnob;
float m_mouseOffset;
QPoint m_mouseOffset;
QPoint m_origMousePos;
float m_origValue;
bool m_buttonPressed;
int m_magneticDecay;
float m_totalAngle;
int m_angle;
QImage m_cache;

View File

@@ -61,9 +61,7 @@ knob::knob( int _knob_num, QWidget * _parent, const QString & _name ) :
m_label( "" ),
m_knobPixmap( NULL ),
m_volumeKnob( false ),
m_mouseOffset( 0.0f ),
m_buttonPressed( false ),
m_magneticDecay( 0 ),
m_angle( -10 ),
m_outerColor( NULL )
{
@@ -369,16 +367,15 @@ void knob::drawKnob( QPainter * _p )
_p->drawImage( 0, 0, m_cache );
}
float knob::getValue( const QPoint & _p )
{
const float SMOOTH_FACTOR = 0.125f;
int yDist = (_p.y() - m_origMousePos.y()) * SMOOTH_FACTOR;
if( engine::mainWindow()->isShiftPressed() )
{
return( ( _p.y() - m_origMousePos.y() ) * model()->step<float>() );
return m_origValue - (yDist * model()->step<float>());
}
return( ( _p.y() - m_origMousePos.y() ) * pageSize() );
return m_origValue - (yDist * pageSize());
}
@@ -447,6 +444,8 @@ void knob::mousePressEvent( QMouseEvent * _me )
const QPoint & p = _me->pos();
m_origMousePos = p;
m_mouseOffset = QPoint(0, 0);
m_origValue = model()->value();
emit sliderPressed();
@@ -475,9 +474,10 @@ void knob::mousePressEvent( QMouseEvent * _me )
void knob::mouseMoveEvent( QMouseEvent * _me )
{
if( m_buttonPressed == true && _me->pos() != m_origMousePos )
if( m_buttonPressed && _me->pos() != m_origMousePos )
{
setPosition( _me->pos() );
m_mouseOffset += _me->pos() - m_origMousePos;
setPosition( m_mouseOffset );
emit sliderMoved( model()->value() );
QCursor::setPos( mapToGlobal( m_origMousePos ) );
}
@@ -494,7 +494,6 @@ void knob::mouseReleaseEvent( QMouseEvent * /* _me*/ )
m_buttonPressed = false;
m_mouseOffset = 0;
emit sliderReleased();
QApplication::restoreOverrideCursor();
@@ -564,31 +563,7 @@ void knob::wheelEvent( QWheelEvent * _we )
void knob::setPosition( const QPoint & _p )
{
const float current = model()->value();
const float next = current - getValue( _p );
if( model()->initValue() == current )
{
if( ++m_magneticDecay > 20 )
{
m_magneticDecay = 0;
model()->setValue( next );
}
return;
}
const bool current_sign = model()->initValue() - current < 0;
const bool next_sign = model()->initValue() - next < 0;
if( current_sign != next_sign )
{
model()->setValue( model()->initValue() );
}
else
{
model()->setValue( next );
}
model()->setValue( getValue(_p) );
}