submarine.cpp Example File
demos/sub-attaq/submarine.cpp
#include "submarine.h"
#include "submarine_p.h"
#include "torpedo.h"
#include "pixmapitem.h"
#include "graphicsscene.h"
#include "animationmanager.h"
#include "custompropertyanimation.h"
#include "qanimationstate.h"
#include <QtCore/QPropertyAnimation>
#include <QtCore/QStateMachine>
#include <QtCore/QFinalState>
#include <QtCore/QSequentialAnimationGroup>
static QAbstractAnimation *setupDestroyAnimation(SubMarine *sub)
{
QSequentialAnimationGroup *group = new QSequentialAnimationGroup(sub);
#if QT_VERSION >=0x040500
PixmapItem *step1 = new PixmapItem(QString("explosion/submarine/step1"),GraphicsScene::Big, sub);
step1->setZValue(6);
PixmapItem *step2 = new PixmapItem(QString("explosion/submarine/step2"),GraphicsScene::Big, sub);
step2->setZValue(6);
PixmapItem *step3 = new PixmapItem(QString("explosion/submarine/step3"),GraphicsScene::Big, sub);
step3->setZValue(6);
PixmapItem *step4 = new PixmapItem(QString("explosion/submarine/step4"),GraphicsScene::Big, sub);
step4->setZValue(6);
step1->setOpacity(0);
step2->setOpacity(0);
step3->setOpacity(0);
step4->setOpacity(0);
CustomPropertyAnimation *anim1 = new CustomPropertyAnimation(sub);
anim1->setMemberFunctions((QGraphicsItem*)step1, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim1->setDuration(100);
anim1->setEndValue(1);
CustomPropertyAnimation *anim2 = new CustomPropertyAnimation(sub);
anim2->setMemberFunctions((QGraphicsItem*)step2, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim2->setDuration(100);
anim2->setEndValue(1);
CustomPropertyAnimation *anim3 = new CustomPropertyAnimation(sub);
anim3->setMemberFunctions((QGraphicsItem*)step3, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim3->setDuration(100);
anim3->setEndValue(1);
CustomPropertyAnimation *anim4 = new CustomPropertyAnimation(sub);
anim4->setMemberFunctions((QGraphicsItem*)step4, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity);
anim4->setDuration(100);
anim4->setEndValue(1);
group->addAnimation(anim1);
group->addAnimation(anim2);
group->addAnimation(anim3);
group->addAnimation(anim4);
#else
QtPauseAnimation *anim = new QtPauseAnimation(group);
anim->setDuration(1);
group->addAnimation(anim);
#endif
AnimationManager::self()->registerAnimation(group);
return group;
}
SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem * parent, Qt::WindowFlags wFlags)
: QGraphicsWidget(parent,wFlags), subType(type), subName(name), subPoints(points), speed(0), direction(SubMarine::None)
{
pixmapItem = new PixmapItem(QString("submarine"),GraphicsScene::Big, this);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setZValue(5);
setFlags(QGraphicsItem::ItemIsMovable);
resize(pixmapItem->boundingRect().width(),pixmapItem->boundingRect().height());
setTransformOriginPoint(boundingRect().center());
graphicsRotation = new QGraphicsRotation(this);
graphicsRotation->setAxis(QVector3D(0, 1, 0));
graphicsRotation->setOrigin(QVector3D(size().width()/2, size().height()/2, 0));
QList<QGraphicsTransform *> r;
r.append(graphicsRotation);
setTransformations(r);
QStateMachine *machine = new QStateMachine(this);
QState *moving = new QState(machine);
MovementState *movement = new MovementState(this, moving);
ReturnState *rotation = new ReturnState(this, moving);
moving->setInitialState(movement);
movement->addTransition(this, SIGNAL(subMarineStateChanged()), moving);
machine->setInitialState(moving);
QFinalState *final = new QFinalState(machine);
movement->addTransition(movement, SIGNAL(animationFinished()), rotation);
rotation->addTransition(rotation, SIGNAL(animationFinished()), movement);
QAnimationState *destroyedState = new QAnimationState(machine);
destroyedState->setAnimation(setupDestroyAnimation(this));
moving->addTransition(this, SIGNAL(subMarineDestroyed()), destroyedState);
destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final);
connect(machine,SIGNAL(finished()),this, SIGNAL(subMarineExecutionFinished()));
machine->start();
}
int SubMarine::points()
{
return subPoints;
}
void SubMarine::setCurrentDirection(SubMarine::Movement direction)
{
if (this->direction == direction)
return;
if (direction == SubMarine::Right && this->direction == SubMarine::None) {
graphicsRotation->setAngle(180);
}
this->direction = direction;
}
enum SubMarine::Movement SubMarine::currentDirection() const
{
return direction;
}
void SubMarine::setCurrentSpeed(int speed)
{
if (speed < 0 || speed > 3) {
qWarning("SubMarine::setCurrentSpeed : The speed is invalid");
}
this->speed = speed;
emit subMarineStateChanged();
}
int SubMarine::currentSpeed() const
{
return speed;
}
void SubMarine::launchTorpedo(int speed)
{
Torpedo * torp = new Torpedo();
GraphicsScene *scene = static_cast<GraphicsScene *>(this->scene());
scene->addItem(torp);
torp->setPos(x(), y());
torp->setCurrentSpeed(speed);
torp->launch();
}
void SubMarine::destroy()
{
emit subMarineDestroyed();
}
int SubMarine::type() const
{
return Type;
}
| Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) |
Trademarks |
Qt 4.6.0-tp1 |