Fix alpha setting in Designer's "Edit Palette" window
[qt-netbsd.git] / demos / sub-attaq / submarine.cpp
blob3d8490fa1aa4ebf3d9f334cd815f2d8142dc01bd
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 //Own
43 #include "submarine.h"
44 #include "submarine_p.h"
45 #include "torpedo.h"
46 #include "pixmapitem.h"
47 #include "graphicsscene.h"
48 #include "animationmanager.h"
49 #include "custompropertyanimation.h"
50 #include "qanimationstate.h"
52 #include <QtCore/QPropertyAnimation>
53 #include <QtCore/QStateMachine>
54 #include <QtCore/QFinalState>
55 #include <QtCore/QSequentialAnimationGroup>
57 static QAbstractAnimation *setupDestroyAnimation(SubMarine *sub)
59 QSequentialAnimationGroup *group = new QSequentialAnimationGroup(sub);
60 #if QT_VERSION >=0x040500
61 PixmapItem *step1 = new PixmapItem(QString("explosion/submarine/step1"),GraphicsScene::Big, sub);
62 step1->setZValue(6);
63 PixmapItem *step2 = new PixmapItem(QString("explosion/submarine/step2"),GraphicsScene::Big, sub);
64 step2->setZValue(6);
65 PixmapItem *step3 = new PixmapItem(QString("explosion/submarine/step3"),GraphicsScene::Big, sub);
66 step3->setZValue(6);
67 PixmapItem *step4 = new PixmapItem(QString("explosion/submarine/step4"),GraphicsScene::Big, sub);
68 step4->setZValue(6);
69 step1->setOpacity(0);
70 step2->setOpacity(0);
71 step3->setOpacity(0);
72 step4->setOpacity(0);
73 CustomPropertyAnimation *anim1 = new CustomPropertyAnimation(sub);
74 anim1->setMemberFunctions((QGraphicsItem*)step1, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
75 anim1->setDuration(100);
76 anim1->setEndValue(1);
77 CustomPropertyAnimation *anim2 = new CustomPropertyAnimation(sub);
78 anim2->setMemberFunctions((QGraphicsItem*)step2, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
79 anim2->setDuration(100);
80 anim2->setEndValue(1);
81 CustomPropertyAnimation *anim3 = new CustomPropertyAnimation(sub);
82 anim3->setMemberFunctions((QGraphicsItem*)step3, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
83 anim3->setDuration(100);
84 anim3->setEndValue(1);
85 CustomPropertyAnimation *anim4 = new CustomPropertyAnimation(sub);
86 anim4->setMemberFunctions((QGraphicsItem*)step4, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
87 anim4->setDuration(100);
88 anim4->setEndValue(1);
89 group->addAnimation(anim1);
90 group->addAnimation(anim2);
91 group->addAnimation(anim3);
92 group->addAnimation(anim4);
93 #else
94 // work around for a bug where we don't transition if the duration is zero.
95 QtPauseAnimation *anim = new QtPauseAnimation(group);
96 anim->setDuration(1);
97 group->addAnimation(anim);
98 #endif
99 AnimationManager::self()->registerAnimation(group);
100 return group;
104 SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem * parent, Qt::WindowFlags wFlags)
105 : QGraphicsWidget(parent,wFlags), subType(type), subName(name), subPoints(points), speed(0), direction(SubMarine::None)
107 pixmapItem = new PixmapItem(QString("submarine"),GraphicsScene::Big, this);
108 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
109 setZValue(5);
110 setFlags(QGraphicsItem::ItemIsMovable);
111 resize(pixmapItem->boundingRect().width(),pixmapItem->boundingRect().height());
112 setTransformOriginPoint(boundingRect().center());
114 graphicsRotation = new QGraphicsRotation(this);
115 graphicsRotation->setAxis(QVector3D(0, 1, 0));
116 graphicsRotation->setOrigin(QVector3D(size().width()/2, size().height()/2, 0));
117 QList<QGraphicsTransform *> r;
118 r.append(graphicsRotation);
119 setTransformations(r);
121 //We setup the state machine of the submarine
122 QStateMachine *machine = new QStateMachine(this);
124 //This state is when the boat is moving/rotating
125 QState *moving = new QState(machine);
127 //This state is when the boat is moving from left to right
128 MovementState *movement = new MovementState(this, moving);
130 //This state is when the boat is moving from left to right
131 ReturnState *rotation = new ReturnState(this, moving);
133 //This is the initial state of the moving root state
134 moving->setInitialState(movement);
136 movement->addTransition(this, SIGNAL(subMarineStateChanged()), moving);
138 //This is the initial state of the machine
139 machine->setInitialState(moving);
141 //End
142 QFinalState *final = new QFinalState(machine);
144 //If the moving animation is finished we move to the return state
145 movement->addTransition(movement, SIGNAL(animationFinished()), rotation);
147 //If the return animation is finished we move to the moving state
148 rotation->addTransition(rotation, SIGNAL(animationFinished()), movement);
150 //This state play the destroyed animation
151 QAnimationState *destroyedState = new QAnimationState(machine);
152 destroyedState->setAnimation(setupDestroyAnimation(this));
154 //Play a nice animation when the submarine is destroyed
155 moving->addTransition(this, SIGNAL(subMarineDestroyed()), destroyedState);
157 //Transition to final state when the destroyed animation is finished
158 destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final);
160 //The machine has finished to be executed, then the submarine is dead
161 connect(machine,SIGNAL(finished()),this, SIGNAL(subMarineExecutionFinished()));
163 machine->start();
166 int SubMarine::points()
168 return subPoints;
171 void SubMarine::setCurrentDirection(SubMarine::Movement direction)
173 if (this->direction == direction)
174 return;
175 if (direction == SubMarine::Right && this->direction == SubMarine::None) {
176 graphicsRotation->setAngle(180);
178 this->direction = direction;
181 enum SubMarine::Movement SubMarine::currentDirection() const
183 return direction;
186 void SubMarine::setCurrentSpeed(int speed)
188 if (speed < 0 || speed > 3) {
189 qWarning("SubMarine::setCurrentSpeed : The speed is invalid");
191 this->speed = speed;
192 emit subMarineStateChanged();
195 int SubMarine::currentSpeed() const
197 return speed;
200 void SubMarine::launchTorpedo(int speed)
202 Torpedo * torp = new Torpedo();
203 GraphicsScene *scene = static_cast<GraphicsScene *>(this->scene());
204 scene->addItem(torp);
205 torp->setPos(x(), y());
206 torp->setCurrentSpeed(speed);
207 torp->launch();
210 void SubMarine::destroy()
212 emit subMarineDestroyed();
215 int SubMarine::type() const
217 return Type;