File indexing completed on 2024-05-12 04:04:14
0001 /* 0002 This file is part of Knights, a chess board for KDE SC 4. 0003 SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "item.h" 0009 #include "board.h" 0010 #include "settings.h" 0011 0012 #include <QPropertyAnimation> 0013 #include <QParallelAnimationGroup> 0014 #include <QtMath> 0015 0016 using namespace Knights; 0017 0018 static const int fastAnimationDuration = 150; 0019 static const int normalAnimationDuration = 250; 0020 static const int slowAnimationDuration = 400; 0021 0022 Item::Item ( KGameGraphicsViewRenderer* renderer, const QString &key, QGraphicsScene* scene, Pos boardPos, QGraphicsItem* parentItem ) : KGameRenderedGraphicsObject ( renderer, key, parentItem ) { 0023 setBoardPos ( boardPos ); 0024 if ( scene ) 0025 scene->addItem ( this ); 0026 } 0027 0028 Item::~Item() { 0029 if ( scene() ) 0030 scene()->removeItem ( this ); 0031 } 0032 0033 Pos Item::boardPos() const { 0034 return m_pos; 0035 } 0036 0037 void Item::setBoardPos ( const Pos& pos ) { 0038 m_pos = pos; 0039 } 0040 0041 void Item::move ( const QPointF& pos, qreal tileSize, bool animated ) { 0042 if ( !animated || Settings::animationSpeed() == Settings::EnumAnimationSpeed::Instant ) 0043 setPos ( pos ); 0044 else { 0045 int duration = 0; 0046 switch ( Settings::animationSpeed() ) { 0047 case Settings::EnumAnimationSpeed::Fast: 0048 duration = fastAnimationDuration; 0049 break; 0050 case Settings::EnumAnimationSpeed::Normal: 0051 duration = normalAnimationDuration; 0052 break; 0053 case Settings::EnumAnimationSpeed::Slow: 0054 duration = slowAnimationDuration; 0055 break; 0056 default: 0057 break; 0058 } 0059 duration *= qSqrt ( QPointF ( this->pos() - pos ).manhattanLength() / tileSize ); 0060 QPropertyAnimation* anim = new QPropertyAnimation ( this, "pos" ); 0061 anim->setDuration ( duration ); 0062 anim->setEasingCurve ( QEasingCurve::InOutCubic ); 0063 anim->setEndValue ( pos ); 0064 anim->start ( QAbstractAnimation::DeleteWhenStopped ); 0065 } 0066 } 0067 0068 void Item::resize ( const QSize& size, bool animated ) { 0069 if ( !animated || Settings::animationSpeed() == Settings::EnumAnimationSpeed::Instant ) 0070 setRenderSize ( size ); 0071 else { 0072 int duration = 0; 0073 switch ( Settings::animationSpeed() ) { 0074 case Settings::EnumAnimationSpeed::Fast: 0075 duration = fastAnimationDuration; 0076 break; 0077 case Settings::EnumAnimationSpeed::Normal: 0078 duration = normalAnimationDuration; 0079 break; 0080 case Settings::EnumAnimationSpeed::Slow: 0081 duration = slowAnimationDuration; 0082 break; 0083 default: 0084 break; 0085 } 0086 QPropertyAnimation* anim = new QPropertyAnimation ( this, "renderSize" ); 0087 anim->setDuration ( duration ); 0088 anim->setEasingCurve ( QEasingCurve::InOutCubic ); 0089 anim->setEndValue ( size ); 0090 anim->start ( QAbstractAnimation::DeleteWhenStopped ); 0091 } 0092 } 0093 0094 void Item::moveAndResize ( const QPointF& pos, qreal tileSize, const QSize& size, bool animated ) { 0095 if ( !animated || Settings::animationSpeed() == Settings::EnumAnimationSpeed::Instant ) { 0096 setPos ( pos ); 0097 setRenderSize ( size ); 0098 } else { 0099 int duration = 0; 0100 switch ( Settings::animationSpeed() ) { 0101 case Settings::EnumAnimationSpeed::Fast: 0102 duration = fastAnimationDuration; 0103 break; 0104 case Settings::EnumAnimationSpeed::Normal: 0105 duration = normalAnimationDuration; 0106 break; 0107 case Settings::EnumAnimationSpeed::Slow: 0108 duration = slowAnimationDuration; 0109 break; 0110 default: 0111 break; 0112 } 0113 duration *= qSqrt ( QPointF ( this->pos() - pos ).manhattanLength() / tileSize ); 0114 QParallelAnimationGroup* group = new QParallelAnimationGroup; 0115 QPropertyAnimation* posAnimation = new QPropertyAnimation ( this, "pos" ); 0116 posAnimation->setDuration ( duration ); 0117 posAnimation->setEasingCurve ( QEasingCurve::InOutCubic ); 0118 posAnimation->setEndValue ( pos ); 0119 group->addAnimation ( posAnimation ); 0120 QPropertyAnimation* sizeAnimation = new QPropertyAnimation ( this, "renderSize" ); 0121 sizeAnimation->setDuration ( duration ); 0122 sizeAnimation->setEasingCurve ( QEasingCurve::InOutCubic ); 0123 sizeAnimation->setEndValue ( size ); 0124 group->addAnimation ( sizeAnimation ); 0125 group->start ( QAbstractAnimation::DeleteWhenStopped ); 0126 } 0127 } 0128 0129 #include "moc_item.cpp"