Cleaned up CrazyhouseMove.
[tagua/yd.git] / src / piecegroup.cpp
blob95a35024a8a0bde4468ad7d6258872dfaa699abe
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
12 #include <cmath>
13 #include <QTimer>
14 #include "global.h"
15 #include "piecegroup.h"
16 #include "pointconverter.h"
17 #include "animation.h"
18 #include "sprite.h"
20 using namespace boost;
22 //BEGIN PieceGroup
24 PieceGroup::PieceGroup(KGameCanvasAbstract* parent)
25 : ClickableCanvas(parent)
26 , m_flipped(false)
27 , m_square_size(0) {
28 m_main_animation = new MainAnimation( 1.0 );
29 settingsChanged();
32 PieceGroup::~PieceGroup()
34 delete m_main_animation;
37 void PieceGroup::settingsChanged() {
38 Settings s_anim = settings.group("animations");
39 if (s_anim.flag("enabled", true)) {
40 m_anim_fade = s_anim["fading"].flag("enabled", true);
41 m_anim_movement = s_anim["movement"].flag("enabled", true);
43 else {
44 m_anim_fade = false;
45 m_anim_movement = false;
48 int speed = (s_anim["speed"] | 16);
49 int smoothness = (s_anim["smoothness"] | 16);
50 m_main_animation->setSpeed( 0.4*pow(10.0, speed/32.0) );
51 m_main_animation->setDelay( int(70.0*pow(10.0, -smoothness/32.0)) );
54 void PieceGroup::onResize(int new_size, bool force_reload) {
55 if(m_square_size == new_size && !force_reload)
56 return;
58 m_square_size = new_size;
59 m_loader.setSize(m_square_size);
62 void PieceGroup::animatePiece(const SpritePtr& piece, const Point& to, double speed) {
63 enqueue(
64 m_anim_movement
65 ? AnimationPtr(new MovementAnimation(piece, converter()->toReal(to), speed))
66 : AnimationPtr(new InstantAnimation(piece, converter()->toReal(to)))
70 void PieceGroup::enqueue(const shared_ptr<Animation>& anim) {
71 m_main_animation->addAnimation(anim);
74 void PieceGroup::stopAnimations() {
75 m_main_animation->stop();
78 void PieceGroup::finalizeAnimation(AnimationGroup* group) {
79 delete group;
82 void PieceGroup::adjustSprite(const Point& p, bool smooth) {
83 shared_ptr<Sprite> sprite = spriteAt(p);
84 if (sprite) {
85 if (smooth) {
86 animatePiece(sprite, p, 1.0);
88 else {
89 enqueue(shared_ptr<Animation>(new InstantAnimation(sprite, converter()->toReal(p))));
94 void PieceGroup::fadeIn(const Point& p) {
95 shared_ptr<Sprite> sprite = spriteAt(p);
96 if (sprite) {
97 if(m_anim_fade)
98 enqueue(shared_ptr<Animation>(new FadeAnimation(sprite, converter()->toReal(p), 0, 255)));
99 else
100 enqueue(shared_ptr<Animation>(new DropAnimation(sprite)));
104 SpritePtr PieceGroup::createSprite(const QPixmap& pix, const Point& pos) {
105 return SpritePtr(new Sprite(pix, piecesGroup(), converter()->toReal(pos)));
108 //END PieceGroup