From 83a9d835b506b21d59949e0a7f7cbce26ee8ff07 Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Tue, 3 Jan 2023 18:08:39 +0100 Subject: [PATCH] IOT dashboard: fix swiping over 2 hops --- examples/iotdashboard/MainItem.cpp | 37 ++++++++++++++++++++---------- examples/iotdashboard/MainItem.h | 2 ++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/iotdashboard/MainItem.cpp b/examples/iotdashboard/MainItem.cpp index cb7a7104..8d01b21d 100644 --- a/examples/iotdashboard/MainItem.cpp +++ b/examples/iotdashboard/MainItem.cpp @@ -63,6 +63,7 @@ Cube::Cube( QQuickItem* parent ) : QskStackBox( false, parent ) , m_destination( FrontPos ) , m_previousPosition( FrontPos ) + , m_isIntermediateHop( false ) { // The code below covers the case where we need 2 cube movements to get // to the desired position. @@ -73,15 +74,11 @@ Cube::Cube( QQuickItem* parent ) { const bool animationIsFinished = ( position == qFloor( position ) ); - if( animationIsFinished) - { - qDebug() << "finished." << position << m_destination << ( position == m_destination ); - } - if( animationIsFinished && position != m_destination ) { QTimer::singleShot( 0, this, [this]() { + m_isIntermediateHop = true; switchToPosition( m_destination ); } ); } @@ -90,8 +87,6 @@ Cube::Cube( QQuickItem* parent ) void Cube::doSwitch( Qsk::Direction direction, Position position ) { - m_previousPosition = m_destination; - using Animator = QskStackBoxAnimator4; auto animator = qobject_cast< Animator* >( this->animator() ); @@ -99,12 +94,26 @@ void Cube::doSwitch( Qsk::Direction direction, Position position ) if ( animator == nullptr ) { animator = new Animator( this ); - animator->setEasingCurve( QEasingCurve::InOutQuad ); animator->setDuration( 1000 ); - setAnimator( animator ); } + if( position == m_destination && !m_isIntermediateHop ) // 1 hop + { + animator->setEasingCurve( QEasingCurve::InOutQuad ); + } + else if( !m_isIntermediateHop ) // 1st of 2 hops + { + animator->setEasingCurve( QEasingCurve::InQuad ); + } + else // 2nd of 2 hops + { + animator->setEasingCurve( QEasingCurve::OutQuad ); + m_isIntermediateHop = false; + } + + m_previousPosition = m_destination; + const auto orientation = ( direction == Qsk::LeftToRight || direction == Qsk::RightToLeft ) ? Qt::Horizontal : Qt::Vertical; animator->setOrientation( orientation ); @@ -140,19 +149,23 @@ void Cube::switchPosition( const Qsk::Direction direction ) void Cube::switchToPosition( const Position position ) { - if( currentIndex() == position ) + if( currentPosition() == position ) return; m_destination = position; const auto from = static_cast< Position >( currentIndex() ); const auto direction = this->direction( from, position ); - const auto currentPosition = static_cast< Position >( currentIndex() ); - const auto intermediatePosition = neighbor( currentPosition, direction ); + const auto intermediatePosition = neighbor( currentPosition(), direction ); doSwitch( direction, intermediatePosition ); } +Cube::Position Cube::currentPosition() const +{ + return static_cast< Position >( currentIndex() ); +} + Cube::Position Cube::neighbor( const Position position, const Qsk::Direction direction ) const { const auto n = s_neighbors[ position ][ direction ]; diff --git a/examples/iotdashboard/MainItem.h b/examples/iotdashboard/MainItem.h index 617568e4..8a47a51c 100644 --- a/examples/iotdashboard/MainItem.h +++ b/examples/iotdashboard/MainItem.h @@ -37,12 +37,14 @@ class Cube : public QskStackBox void cubeIndexChanged( const int index ); private: + Position currentPosition() const; Position neighbor( const Position position, const Qsk::Direction direction ) const; Qsk::Direction direction( const Position from, const Position to ) const; void doSwitch( Qsk::Direction direction, Position position ); Position m_destination; Position m_previousPosition; + bool m_isIntermediateHop; static Position s_neighbors[ NumPositions ][ 4 ]; };