1 /*
2     This file is part of Knights, a chess board for KDE SC 4.
3     Copyright 2009,2010,2011  Miha Čančula <miha@noughmad.eu>
4 
5     This program is free software; you can redistribute it and/or
6     modify it under the terms of the GNU General Public License as
7     published by the Free Software Foundation; either version 2 of
8     the License or (at your option) version 3 or any later version
9     accepted by the membership of KDE e.V. (or its successor approved
10     by the membership of KDE e.V.), which shall act as a proxy
11     defined in Section 14 of version 3 of the license.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "item.h"
23 #include "board.h"
24 #include "settings.h"
25 
26 #include <QPropertyAnimation>
27 #include <QParallelAnimationGroup>
28 #include <QtMath>
29 
30 using namespace Knights;
31 
32 static const int fastAnimationDuration = 150;
33 static const int normalAnimationDuration = 250;
34 static const int slowAnimationDuration = 400;
35 
Item(KGameRenderer * renderer,const QString & key,QGraphicsScene * scene,Pos boardPos,QGraphicsItem * parentItem)36 Item::Item ( KGameRenderer* renderer, const QString &key, QGraphicsScene* scene, Pos boardPos, QGraphicsItem* parentItem ) : KGameRenderedObjectItem ( renderer, key, parentItem ) {
37 	setBoardPos ( boardPos );
38 	if ( scene )
39 		scene->addItem ( this );
40 }
41 
~Item()42 Item::~Item() {
43 	if ( scene() )
44 		scene()->removeItem ( this );
45 }
46 
boardPos() const47 Pos Item::boardPos() const {
48 	return m_pos;
49 }
50 
setBoardPos(const Pos & pos)51 void Item::setBoardPos ( const Pos& pos ) {
52 	m_pos = pos;
53 }
54 
move(const QPointF & pos,qreal tileSize,bool animated)55 void Item::move ( const QPointF& pos, qreal tileSize, bool animated ) {
56 	if ( !animated || Settings::animationSpeed() == Settings::EnumAnimationSpeed::Instant )
57 		setPos ( pos );
58 	else {
59 		int duration = 0;
60 		switch ( Settings::animationSpeed() ) {
61 		case Settings::EnumAnimationSpeed::Fast:
62 			duration = fastAnimationDuration;
63 			break;
64 		case Settings::EnumAnimationSpeed::Normal:
65 			duration = normalAnimationDuration;
66 			break;
67 		case Settings::EnumAnimationSpeed::Slow:
68 			duration = slowAnimationDuration;
69 			break;
70 		default:
71 			break;
72 		}
73 		duration *= qSqrt ( QPointF ( this->pos() - pos ).manhattanLength() / tileSize );
74 		QPropertyAnimation* anim = new QPropertyAnimation ( this, "pos" );
75 		anim->setDuration ( duration );
76 		anim->setEasingCurve ( QEasingCurve::InOutCubic );
77 		anim->setEndValue ( pos );
78 		anim->start ( QAbstractAnimation::DeleteWhenStopped );
79 	}
80 }
81 
resize(const QSize & size,bool animated)82 void Item::resize ( const QSize& size, bool animated ) {
83 	if ( !animated || Settings::animationSpeed() == Settings::EnumAnimationSpeed::Instant )
84 		setRenderSize ( size );
85 	else {
86 		int duration = 0;
87 		switch ( Settings::animationSpeed() ) {
88 		case Settings::EnumAnimationSpeed::Fast:
89 			duration = fastAnimationDuration;
90 			break;
91 		case Settings::EnumAnimationSpeed::Normal:
92 			duration = normalAnimationDuration;
93 			break;
94 		case Settings::EnumAnimationSpeed::Slow:
95 			duration = slowAnimationDuration;
96 			break;
97 		default:
98 			break;
99 		}
100 		QPropertyAnimation* anim = new QPropertyAnimation ( this, "renderSize" );
101 		anim->setDuration ( duration );
102 		anim->setEasingCurve ( QEasingCurve::InOutCubic );
103 		anim->setEndValue ( size );
104 		anim->start ( QAbstractAnimation::DeleteWhenStopped );
105 	}
106 }
107 
moveAndResize(const QPointF & pos,qreal tileSize,const QSize & size,bool animated)108 void Item::moveAndResize ( const QPointF& pos, qreal tileSize, const QSize& size, bool animated ) {
109 	if ( !animated || Settings::animationSpeed() == Settings::EnumAnimationSpeed::Instant ) {
110 		setPos ( pos );
111 		setRenderSize ( size );
112 	} else {
113 		int duration = 0;
114 		switch ( Settings::animationSpeed() ) {
115 		case Settings::EnumAnimationSpeed::Fast:
116 			duration = fastAnimationDuration;
117 			break;
118 		case Settings::EnumAnimationSpeed::Normal:
119 			duration = normalAnimationDuration;
120 			break;
121 		case Settings::EnumAnimationSpeed::Slow:
122 			duration = slowAnimationDuration;
123 			break;
124 		default:
125 			break;
126 		}
127 		duration *= qSqrt ( QPointF ( this->pos() - pos ).manhattanLength() / tileSize );
128 		QParallelAnimationGroup* group = new QParallelAnimationGroup;
129 		QPropertyAnimation* posAnimation = new QPropertyAnimation ( this, "pos" );
130 		posAnimation->setDuration ( duration );
131 		posAnimation->setEasingCurve ( QEasingCurve::InOutCubic );
132 		posAnimation->setEndValue ( pos );
133 		group->addAnimation ( posAnimation );
134 		QPropertyAnimation* sizeAnimation = new QPropertyAnimation ( this, "renderSize" );
135 		sizeAnimation->setDuration ( duration );
136 		sizeAnimation->setEasingCurve ( QEasingCurve::InOutCubic );
137 		sizeAnimation->setEndValue ( size );
138 		group->addAnimation ( sizeAnimation );
139 		group->start ( QAbstractAnimation::DeleteWhenStopped );
140 	}
141 }
142