improve input handling

This commit is contained in:
Peter Hartmann 2021-11-08 16:22:22 +01:00
parent 0067cdfd8f
commit d2b6847d0b
3 changed files with 21 additions and 14 deletions

View File

@ -102,9 +102,11 @@ void LightDisplay::mouseMoveEvent( QMouseEvent* event )
return; return;
} }
const qreal ratioX = ( mousePos.x() - rect.x() ) / rect.width(); const QskArcMetrics metrics = arcMetricsHint( ColdAndWarmArc );
const qreal angle = angleFromPoint( rect, mousePos );
setValueAsRatio( ratioX ); qDebug() << "angle:" << angle;
const qreal ratio = ( metrics.spanAngle() - angle * 16 ) / metrics.spanAngle();
setValueAsRatio( ratio );
} }
void LightDisplay::mouseReleaseEvent( QMouseEvent* /*event*/ ) void LightDisplay::mouseReleaseEvent( QMouseEvent* /*event*/ )
@ -112,24 +114,31 @@ void LightDisplay::mouseReleaseEvent( QMouseEvent* /*event*/ )
setSkinStateFlag( Pressed, false ); setSkinStateFlag( Pressed, false );
} }
qreal LightDisplay::angleFromPoint( const QRectF& rect, const QPointF& point ) const
{
QPointF circlePos( point.x() - rect.center().x(),
rect.center().y() - point.y() );
const qreal atan = qAtan2( circlePos.y(), circlePos.x() );
const qreal angle = qRadiansToDegrees( atan );
return angle;
}
bool LightDisplay::arcContainsPoint( const QRectF& rect, const QPointF& point ) const bool LightDisplay::arcContainsPoint( const QRectF& rect, const QPointF& point ) const
{ {
// putting this in an own function just because it might be useful // putting this in an own function just because it might be useful
// at other places in the future // at other places in the future
const QskArcMetrics metrics = arcMetricsHint( ColdAndWarmArc ); const QskArcMetrics metrics = arcMetricsHint( ColdAndWarmArc );
const int tolerance = 10; const int tolerance = 20;
// 1. check angle // 1. check angle
QPointF circlePos( point.x() - rect.center().x(), QPointF circlePos( point.x() - rect.center().x(),
rect.center().y() - point.y() ); rect.center().y() - point.y() );
const qreal atan = qAtan2( circlePos.y(), circlePos.x() ); const qreal angle = angleFromPoint( rect, point );
const qreal angle = qRadiansToDegrees( atan ); const bool angleWithinRange = ( angle + tolerance ) > metrics.startAngle()
// the qAbs() actually only works for the 180 degrees case, && angle < ( metrics.startAngle() + metrics.spanAngle() - tolerance );
// we might want to generalize this later:
const bool angleWithinRange = ( qAbs( angle ) + tolerance ) > metrics.startAngle()
&& qAbs( angle ) < ( metrics.startAngle() + metrics.spanAngle() - tolerance );
// 2. check whether point is on arc // 2. check whether point is on arc
const qreal radiusMax = rect.width() / 2; const qreal radiusMax = rect.width() / 2;

View File

@ -37,6 +37,7 @@ class LightDisplay : public QskBoundedValueInput
void mouseReleaseEvent( QMouseEvent* e ) override; void mouseReleaseEvent( QMouseEvent* e ) override;
private: private:
qreal angleFromPoint( const QRectF&, const QPointF& ) const;
bool arcContainsPoint( const QRectF&, const QPointF& ) const; bool arcContainsPoint( const QRectF&, const QPointF& ) const;
QskShadowMetrics m_shadow; QskShadowMetrics m_shadow;

View File

@ -140,10 +140,7 @@ QSGNode* LightDisplaySkinlet::updateSubNode(
} }
case ColdAndWarmArcRole: case ColdAndWarmArcRole:
{ {
const qreal startAngle = 0; return updateArcNode( skinnable, node, LightDisplay::ColdAndWarmArc );
const qreal spanAngle = 180 * 16;
return updateArcNode( skinnable, node, startAngle, spanAngle,
LightDisplay::ColdAndWarmArc );
} }
case ValueTextRole: case ValueTextRole:
{ {