From 32a9237c49fd126bee0cbe02c3c5bff5145a6e21 Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Mon, 5 Oct 2009 11:49:54 +0200 Subject: Simplification and use of QGraphicsObject in sub-attas demo Reviewed-by: alexis --- demos/sub-attaq/animationmanager.cpp | 6 +- demos/sub-attaq/boat.cpp | 92 ++++--------- demos/sub-attaq/boat.h | 12 +- demos/sub-attaq/boat_p.h | 50 ++----- demos/sub-attaq/bomb.cpp | 16 +-- demos/sub-attaq/bomb.h | 16 +-- demos/sub-attaq/custompropertyanimation.cpp | 108 --------------- demos/sub-attaq/custompropertyanimation.h | 114 ---------------- demos/sub-attaq/graphicsscene.cpp | 196 +++++++--------------------- demos/sub-attaq/graphicsscene.h | 13 +- demos/sub-attaq/mainwindow.cpp | 31 ++--- demos/sub-attaq/mainwindow.h | 1 - demos/sub-attaq/pixmapitem.cpp | 33 +++-- demos/sub-attaq/pixmapitem.h | 14 +- demos/sub-attaq/states.cpp | 39 ++---- demos/sub-attaq/states.h | 2 - demos/sub-attaq/sub-attaq.pro | 2 - demos/sub-attaq/submarine.cpp | 62 ++------- demos/sub-attaq/submarine.h | 11 +- demos/sub-attaq/submarine_p.h | 1 - demos/sub-attaq/torpedo.cpp | 20 +-- demos/sub-attaq/torpedo.h | 16 +-- 22 files changed, 181 insertions(+), 674 deletions(-) delete mode 100644 demos/sub-attaq/custompropertyanimation.cpp delete mode 100644 demos/sub-attaq/custompropertyanimation.h diff --git a/demos/sub-attaq/animationmanager.cpp b/demos/sub-attaq/animationmanager.cpp index 916dd21..eb5a125 100644 --- a/demos/sub-attaq/animationmanager.cpp +++ b/demos/sub-attaq/animationmanager.cpp @@ -77,16 +77,14 @@ void AnimationManager::unregisterAllAnimations() void AnimationManager::pauseAll() { - foreach (QAbstractAnimation* animation, animations) - { + foreach (QAbstractAnimation* animation, animations) { if (animation->state() == QAbstractAnimation::Running) animation->pause(); } } void AnimationManager::resumeAll() { - foreach (QAbstractAnimation* animation, animations) - { + foreach (QAbstractAnimation* animation, animations) { if (animation->state() == QAbstractAnimation::Paused) animation->resume(); } diff --git a/demos/sub-attaq/boat.cpp b/demos/sub-attaq/boat.cpp index 864a099..3b1bac7 100644 --- a/demos/sub-attaq/boat.cpp +++ b/demos/sub-attaq/boat.cpp @@ -46,7 +46,6 @@ #include "pixmapitem.h" #include "graphicsscene.h" #include "animationmanager.h" -#include "custompropertyanimation.h" #include "qanimationstate.h" //Qt @@ -60,79 +59,35 @@ static QAbstractAnimation *setupDestroyAnimation(Boat *boat) { QSequentialAnimationGroup *group = new QSequentialAnimationGroup(boat); -#if QT_VERSION >=0x040500 - PixmapItem *step1 = new PixmapItem(QString("explosion/boat/step1"),GraphicsScene::Big, boat); - step1->setZValue(6); - PixmapItem *step2 = new PixmapItem(QString("explosion/boat/step2"),GraphicsScene::Big, boat); - step2->setZValue(6); - PixmapItem *step3 = new PixmapItem(QString("explosion/boat/step3"),GraphicsScene::Big, boat); - step3->setZValue(6); - PixmapItem *step4 = new PixmapItem(QString("explosion/boat/step4"),GraphicsScene::Big, boat); - step4->setZValue(6); - step1->setOpacity(0); - step2->setOpacity(0); - step3->setOpacity(0); - step4->setOpacity(0); - CustomPropertyAnimation *anim1 = new CustomPropertyAnimation(boat); - anim1->setMemberFunctions((QGraphicsItem*)step1, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim1->setDuration(100); - anim1->setEndValue(1); - CustomPropertyAnimation *anim2 = new CustomPropertyAnimation(boat); - anim2->setMemberFunctions((QGraphicsItem*)step2, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim2->setDuration(100); - anim2->setEndValue(1); - CustomPropertyAnimation *anim3 = new CustomPropertyAnimation(boat); - anim3->setMemberFunctions((QGraphicsItem*)step3, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim3->setDuration(100); - anim3->setEndValue(1); - CustomPropertyAnimation *anim4 = new CustomPropertyAnimation(boat); - anim4->setMemberFunctions((QGraphicsItem*)step4, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim4->setDuration(100); - anim4->setEndValue(1); - CustomPropertyAnimation *anim5 = new CustomPropertyAnimation(boat); - anim5->setMemberFunctions((QGraphicsItem*)step1, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim5->setDuration(100); - anim5->setEndValue(0); - CustomPropertyAnimation *anim6 = new CustomPropertyAnimation(boat); - anim6->setMemberFunctions((QGraphicsItem*)step2, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim6->setDuration(100); - anim6->setEndValue(0); - CustomPropertyAnimation *anim7 = new CustomPropertyAnimation(boat); - anim7->setMemberFunctions((QGraphicsItem*)step3, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim7->setDuration(100); - anim7->setEndValue(0); - CustomPropertyAnimation *anim8 = new CustomPropertyAnimation(boat); - anim8->setMemberFunctions((QGraphicsItem*)step4, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim8->setDuration(100); - anim8->setEndValue(0); - group->addAnimation(anim1); - group->addAnimation(anim2); - group->addAnimation(anim3); - group->addAnimation(anim4); - group->addAnimation(anim5); - group->addAnimation(anim6); - group->addAnimation(anim7); - group->addAnimation(anim8); -#else - // work around for a bug where we don't transition if the duration is zero. - QtPauseAnimation *anim = new QtPauseAnimation(group); - anim->setDuration(1); - group->addAnimation(anim); -#endif + for (int i = 1; i <= 4; i++) { + PixmapItem *step = new PixmapItem(QString("explosion/boat/step%1").arg(i),GraphicsScene::Big, boat); + step->setZValue(6); + step->setOpacity(0); + + //fade-in + QPropertyAnimation *anim = new QPropertyAnimation(step, "opacity"); + anim->setEndValue(1); + anim->setDuration(100); + group->insertAnimationAt(i-1, anim); + + //and then fade-out + QPropertyAnimation *anim2 = new QPropertyAnimation(step, "opacity"); + anim2->setEndValue(0); + anim2->setDuration(100); + group->addAnimation(anim2); + } + AnimationManager::self()->registerAnimation(group); return group; } -Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags) - : QGraphicsWidget(parent,wFlags), speed(0), bombsAlreadyLaunched(0), direction(Boat::None), movementAnimation(0) +Boat::Boat() : PixmapItem(QString("boat"), GraphicsScene::Big), + speed(0), bombsAlreadyLaunched(0), direction(Boat::None), movementAnimation(0) { - pixmapItem = new PixmapItem(QString("boat"),GraphicsScene::Big, this); setZValue(4); - setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable); - resize(pixmapItem->boundingRect().size()); + setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsFocusable); //The movement animation used to animate the boat movementAnimation = new QPropertyAnimation(this, "pos"); @@ -223,13 +178,13 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags) destroyedState->setAnimation(destroyAnimation); //Play a nice animation when the boat is destroyed - moving->addTransition(this, SIGNAL(boatDestroyed()),destroyedState); + moving->addTransition(this, SIGNAL(boatDestroyed()), destroyedState); //Transition to final state when the destroyed animation is finished destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final); //The machine has finished to be executed, then the boat is dead - connect(machine,SIGNAL(finished()),this, SIGNAL(boatExecutionFinished())); + connect(machine,SIGNAL(finished()), this, SIGNAL(boatExecutionFinished())); } @@ -255,7 +210,6 @@ void Boat::updateBoatMovement() } movementAnimation->stop(); - movementAnimation->setStartValue(pos()); if (direction == Boat::Left) { movementAnimation->setEndValue(QPointF(0,y())); diff --git a/demos/sub-attaq/boat.h b/demos/sub-attaq/boat.h index 0fe8ce4..0b4de1e 100644 --- a/demos/sub-attaq/boat.h +++ b/demos/sub-attaq/boat.h @@ -42,13 +42,8 @@ #ifndef __BOAT__H__ #define __BOAT__H__ -//Qt -#include -#include +#include "pixmapitem.h" -#include - -class PixmapItem; class Bomb; QT_BEGIN_NAMESPACE class QVariantAnimation; @@ -56,7 +51,7 @@ class QAbstractAnimation; class QStateMachine; QT_END_NAMESPACE -class Boat : public QGraphicsWidget +class Boat : public PixmapItem { Q_OBJECT public: @@ -66,7 +61,7 @@ public: Right }; enum { Type = UserType + 2 }; - Boat(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); + Boat(); void destroy(); void run(); void stop(); @@ -95,7 +90,6 @@ private: QVariantAnimation *movementAnimation; QAbstractAnimation *destroyAnimation; QStateMachine *machine; - PixmapItem *pixmapItem; }; #endif //__BOAT__H__ diff --git a/demos/sub-attaq/boat_p.h b/demos/sub-attaq/boat_p.h index 692702b..6eb52b6 100644 --- a/demos/sub-attaq/boat_p.h +++ b/demos/sub-attaq/boat_p.h @@ -67,22 +67,16 @@ static const int MAX_BOMB = 5; class KeyStopTransition : public QKeyEventTransition { public: - KeyStopTransition(Boat *boat, QEvent::Type type, int key) - : QKeyEventTransition(boat, type, key) + KeyStopTransition(Boat *b, QEvent::Type t, int k) + : QKeyEventTransition(b, t, k), boat(b), key(k) { - this->boat = boat; - this->key = key; } protected: virtual bool eventTest(QEvent *event) { - Q_UNUSED(event); if (!QKeyEventTransition::eventTest(event)) return false; - if (boat->currentSpeed() == 1) - return true; - else - return false; + return (boat->currentSpeed() == 1); } private: Boat * boat; @@ -93,23 +87,16 @@ private: class KeyMoveTransition : public QKeyEventTransition { public: - KeyMoveTransition(Boat *boat, QEvent::Type type, int key) - : QKeyEventTransition(boat, type, key) + KeyMoveTransition(Boat *b, QEvent::Type t, int k) + : QKeyEventTransition(b, t, k), boat(b), key(k) { - this->boat = boat; - this->key = key; } protected: virtual bool eventTest(QEvent *event) { - Q_UNUSED(event); if (!QKeyEventTransition::eventTest(event)) return false; - if (boat->currentSpeed() >= 0) - return true; - else - return false; - + return (boat->currentSpeed() >= 0); } void onTransition(QEvent *) { @@ -132,22 +119,16 @@ private: { public: KeyLaunchTransition(Boat *boat, QEvent::Type type, int key) - : QKeyEventTransition(boat, type, key) + : QKeyEventTransition(boat, type, key), boat(boat), key(key) { - this->boat = boat; - this->key = key; } protected: virtual bool eventTest(QEvent *event) { - Q_UNUSED(event); if (!QKeyEventTransition::eventTest(event)) return false; //We have enough bomb? - if (boat->bombsLaunched() < MAX_BOMB) - return true; - else - return false; + return (boat->bombsLaunched() < MAX_BOMB); } private: Boat * boat; @@ -158,9 +139,8 @@ private: class MoveStateRight : public QState { public: - MoveStateRight(Boat *boat,QState *parent = 0) : QState(parent) + MoveStateRight(Boat *boat,QState *parent = 0) : QState(parent), boat(boat) { - this->boat = boat; } protected: void onEntry(QEvent *) @@ -176,9 +156,8 @@ private: class MoveStateLeft : public QState { public: - MoveStateLeft(Boat *boat,QState *parent = 0) : QState(parent) + MoveStateLeft(Boat *boat,QState *parent = 0) : QState(parent), boat(boat) { - this->boat = boat; } protected: void onEntry(QEvent *) @@ -194,9 +173,8 @@ private: class StopState : public QState { public: - StopState(Boat *boat,QState *parent = 0) : QState(parent) + StopState(Boat *boat,QState *parent = 0) : QState(parent), boat(boat) { - this->boat = boat; } protected: void onEntry(QEvent *) @@ -213,9 +191,8 @@ private: class LaunchStateRight : public QState { public: - LaunchStateRight(Boat *boat,QState *parent = 0) : QState(parent) + LaunchStateRight(Boat *boat,QState *parent = 0) : QState(parent), boat(boat) { - this->boat = boat; } protected: void onEntry(QEvent *) @@ -235,9 +212,8 @@ private: class LaunchStateLeft : public QState { public: - LaunchStateLeft(Boat *boat,QState *parent = 0) : QState(parent) + LaunchStateLeft(Boat *boat,QState *parent = 0) : QState(parent), boat(boat) { - this->boat = boat; } protected: void onEntry(QEvent *) diff --git a/demos/sub-attaq/bomb.cpp b/demos/sub-attaq/bomb.cpp index d17024f..acc3475 100644 --- a/demos/sub-attaq/bomb.cpp +++ b/demos/sub-attaq/bomb.cpp @@ -52,19 +52,14 @@ #include #include -Bomb::Bomb(QGraphicsItem * parent, Qt::WindowFlags wFlags) - : QGraphicsWidget(parent,wFlags), launchAnimation(0) +Bomb::Bomb() : PixmapItem(QString("bomb"), GraphicsScene::Big) { - pixmapItem = new PixmapItem(QString("bomb"),GraphicsScene::Big, this); - setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - setFlags(QGraphicsItem::ItemIsMovable); setZValue(2); - resize(pixmapItem->boundingRect().size()); } void Bomb::launch(Bomb::Direction direction) { - launchAnimation = new QSequentialAnimationGroup(); + QSequentialAnimationGroup *launchAnimation = new QSequentialAnimationGroup; AnimationManager::self()->registerAnimation(launchAnimation); qreal delta = direction == Right ? 20 : - 20; QPropertyAnimation *anim = new QPropertyAnimation(this, "pos"); @@ -80,7 +75,7 @@ void Bomb::launch(Bomb::Direction direction) anim->setDuration(y()/2*60); launchAnimation->addAnimation(anim); connect(anim,SIGNAL(valueChanged(const QVariant &)),this,SLOT(onAnimationLaunchValueChanged(const QVariant &))); - + connect(this, SIGNAL(bombExploded()), launchAnimation, SLOT(stop())); //We setup the state machine of the bomb QStateMachine *machine = new QStateMachine(this); @@ -94,7 +89,7 @@ void Bomb::launch(Bomb::Direction direction) machine->setInitialState(launched); //### Add a nice animation when the bomb is destroyed - launched->addTransition(this, SIGNAL(bombExplosed()),final); + launched->addTransition(this, SIGNAL(bombExploded()),final); //If the animation is finished, then we move to the final state launched->addTransition(launched, SIGNAL(animationFinished()), final); @@ -119,6 +114,5 @@ void Bomb::onAnimationLaunchValueChanged(const QVariant &) void Bomb::destroy() { - launchAnimation->stop(); - emit bombExplosed(); + emit bombExploded(); } diff --git a/demos/sub-attaq/bomb.h b/demos/sub-attaq/bomb.h index f5b221c..ec059b5 100644 --- a/demos/sub-attaq/bomb.h +++ b/demos/sub-attaq/bomb.h @@ -42,13 +42,9 @@ #ifndef __BOMB__H__ #define __BOMB__H__ -//Qt -#include -#include +#include "pixmapitem.h" -class PixmapItem; - -class Bomb : public QGraphicsWidget +class Bomb : public PixmapItem { Q_OBJECT public: @@ -56,20 +52,16 @@ public: Left = 0, Right }; - Bomb(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0); + Bomb(); void launch(Direction direction); void destroy(); signals: - void bombExplosed(); + void bombExploded(); void bombExecutionFinished(); private slots: void onAnimationLaunchValueChanged(const QVariant &); - -private: - QAnimationGroup *launchAnimation; - PixmapItem *pixmapItem; }; #endif //__BOMB__H__ diff --git a/demos/sub-attaq/custompropertyanimation.cpp b/demos/sub-attaq/custompropertyanimation.cpp deleted file mode 100644 index 9b435f0..0000000 --- a/demos/sub-attaq/custompropertyanimation.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "custompropertyanimation.h" - -// Qt -#include - -CustomPropertyAnimation::CustomPropertyAnimation(QObject *parent) : - QVariantAnimation(parent), animProp(0) -{ -} - -CustomPropertyAnimation::~CustomPropertyAnimation() -{ -} - -void CustomPropertyAnimation::setProperty(AbstractProperty *_animProp) -{ - if (animProp == _animProp) - return; - delete animProp; - animProp = _animProp; -} - -/*! - \reimp - */ -void CustomPropertyAnimation::updateCurrentValue(const QVariant &value) -{ - if (!animProp || state() == QAbstractAnimation::Stopped) - return; - - animProp->write(value); -} - - -/*! - \reimp -*/ -void CustomPropertyAnimation::updateState(QAbstractAnimation::State oldState, QAbstractAnimation::State newState) -{ - // Initialize start value - if (oldState == QAbstractAnimation::Stopped) { - if (!animProp) - return; - QVariant def = animProp->read(); - if (def.isValid()) { - const int t = def.userType(); - KeyValues values = keyValues(); - //this ensures that all the keyValues are of type t - for (int i = 0; i < values.count(); ++i) { - QVariantAnimation::KeyValue &pair = values[i]; - if (pair.second.userType() != t) - pair.second.convert(static_cast(t)); - } - //let's now update the key values - setKeyValues(values); - } - - if ((animProp && !startValue().isValid() && currentTime() == 0) - || (currentTime() == duration() && currentLoop() == (loopCount() - 1))) { - setStartValue(def); - } - } - - QVariantAnimation::updateState(oldState, newState); -} - -#include "moc_custompropertyanimation.cpp" diff --git a/demos/sub-attaq/custompropertyanimation.h b/demos/sub-attaq/custompropertyanimation.h deleted file mode 100644 index 0c97bf0..0000000 --- a/demos/sub-attaq/custompropertyanimation.h +++ /dev/null @@ -1,114 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef CUSTOMPROPERTYANIMATION_H -#define CUSTOMPROPERTYANIMATION_H - -#include - -QT_BEGIN_NAMESPACE -class QGraphicsItem; -QT_END_NAMESPACE - -struct AbstractProperty -{ - virtual QVariant read() const = 0; - virtual void write(const QVariant &value) = 0; -}; - - -class CustomPropertyAnimation : public QVariantAnimation -{ - Q_OBJECT - - template - class MemberFunctionProperty : public AbstractProperty - { - public: - typedef T (Target::*Getter)(void) const; - typedef void (Target::*Setter)(T2); - - MemberFunctionProperty(Target* target, Getter getter, Setter setter) - : m_target(target), m_getter(getter), m_setter(setter) {} - - virtual void write(const QVariant &value) - { - if (m_setter) (m_target->*m_setter)(qVariantValue(value)); - } - - virtual QVariant read() const - { - if (m_getter) return qVariantFromValue((m_target->*m_getter)()); - return QVariant(); - } - - private: - Target *m_target; - Getter m_getter; - Setter m_setter; - }; - -public: - CustomPropertyAnimation(QObject *parent = 0); - ~CustomPropertyAnimation(); - - template - void setMemberFunctions(Target* target, T (Target::*getter)() const, void (Target::*setter)(const T& )) - { - setProperty(new MemberFunctionProperty(target, getter, setter)); - } - - template - void setMemberFunctions(Target* target, T (Target::*getter)() const, void (Target::*setter)(T)) - { - setProperty(new MemberFunctionProperty(target, getter, setter)); - } - - void updateCurrentValue(const QVariant &value); - void updateState(QAbstractAnimation::State oldState, QAbstractAnimation::State newState); - void setProperty(AbstractProperty *animProp); - -private: - Q_DISABLE_COPY(CustomPropertyAnimation); - AbstractProperty *animProp; -}; - -#endif // CUSTOMPROPERTYANIMATION_H diff --git a/demos/sub-attaq/graphicsscene.cpp b/demos/sub-attaq/graphicsscene.cpp index e5d7aad..812eadf 100644 --- a/demos/sub-attaq/graphicsscene.cpp +++ b/demos/sub-attaq/graphicsscene.cpp @@ -47,7 +47,6 @@ #include "torpedo.h" #include "bomb.h" #include "pixmapitem.h" -#include "custompropertyanimation.h" #include "animationmanager.h" #include "qanimationstate.h" #include "progressitem.h" @@ -68,39 +67,10 @@ #include #include -//helper function that creates an animation for position and inserts it into group -static CustomPropertyAnimation *addGraphicsItemPosAnimation(QSequentialAnimationGroup *group, - QGraphicsItem *item, const QPointF &endPos) -{ - CustomPropertyAnimation *ret = new CustomPropertyAnimation(group); - ret->setMemberFunctions(item, &QGraphicsItem::pos, &QGraphicsItem::setPos); - ret->setEndValue(endPos); - ret->setDuration(200); - ret->setEasingCurve(QEasingCurve::OutElastic); - group->addPause(50); - return ret; -} - -//helper function that creates an animation for opacity and inserts it into group -static void addGraphicsItemFadeoutAnimation(QAnimationGroup *group, QGraphicsItem *item) -{ -#if QT_VERSION >=0x040500 - CustomPropertyAnimation *anim = new CustomPropertyAnimation(group); - anim->setMemberFunctions(item, &QGraphicsItem::opacity, &QGraphicsItem::setOpacity); - anim->setDuration(800); - anim->setEndValue(0); - anim->setEasingCurve(QEasingCurve::OutQuad); -#else - // work around for a bug where we don't transition if the duration is zero. - QtPauseAnimation *anim = new QtPauseAnimation(group); - anim->setDuration(1); -#endif -} - GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode) - : QGraphicsScene(x,y,width,height), mode(mode), newAction(0), quitAction(0), boat(0) + : QGraphicsScene(x , y, width, height), mode(mode), boat(new Boat) { - backgroundItem = new PixmapItem(QString("background"),mode); + PixmapItem *backgroundItem = new PixmapItem(QString("background"),mode); backgroundItem->setZValue(1); backgroundItem->setPos(0,0); addItem(backgroundItem); @@ -116,7 +86,6 @@ GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode) textInformationItem = new TextInformationItem(backgroundItem); textInformationItem->hide(); //We create the boat - boat = new Boat(); addItem(boat); boat->setPos(this->width()/2, sealLevel() - boat->size().height()); boat->hide(); @@ -130,28 +99,21 @@ GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode) while (!reader.atEnd()) { reader.readNext(); if (reader.tokenType() == QXmlStreamReader::StartElement) { - if (reader.name() == "submarine") - { + if (reader.name() == "submarine") { SubmarineDescription desc; desc.name = reader.attributes().value("name").toString(); desc.points = reader.attributes().value("points").toString().toInt(); desc.type = reader.attributes().value("type").toString().toInt(); submarinesData.append(desc); - } - if (reader.name() == "level") - { + } else if (reader.name() == "level") { currentLevel.id = reader.attributes().value("id").toString().toInt(); currentLevel.name = reader.attributes().value("name").toString(); + } else if (reader.name() == "subinstance") { + currentLevel.submarines.append(qMakePair(reader.attributes().value("type").toString().toInt(), reader.attributes().value("nb").toString().toInt())); } - if (reader.name() == "subinstance") - { - currentLevel.submarines.append(qMakePair(reader.attributes().value("type").toString().toInt(),reader.attributes().value("nb").toString().toInt())); - } - } - if (reader.tokenType() == QXmlStreamReader::EndElement) { - if (reader.name() == "level") - { - levelsData.insert(currentLevel.id,currentLevel); + } else if (reader.tokenType() == QXmlStreamReader::EndElement) { + if (reader.name() == "level") { + levelsData.insert(currentLevel.id, currentLevel); currentLevel.submarines.clear(); } } @@ -160,80 +122,52 @@ GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode) qreal GraphicsScene::sealLevel() const { - if (mode == Big) - return 220; - else - return 160; + return (mode == Big) ? 220 : 160; } -void GraphicsScene::setupScene(const QList &actions) +void GraphicsScene::setupScene(QAction *newAction, QAction *quitAction) { - newAction = actions.at(0); - quitAction = actions.at(1); - - QGraphicsItem *logo_s = addWelcomeItem(QPixmap(":/logo-s")); - QGraphicsItem *logo_u = addWelcomeItem(QPixmap(":/logo-u")); - QGraphicsItem *logo_b = addWelcomeItem(QPixmap(":/logo-b")); - QGraphicsItem *logo_dash = addWelcomeItem(QPixmap(":/logo-dash")); - QGraphicsItem *logo_a = addWelcomeItem(QPixmap(":/logo-a")); - QGraphicsItem *logo_t = addWelcomeItem(QPixmap(":/logo-t")); - QGraphicsItem *logo_t2 = addWelcomeItem(QPixmap(":/logo-t2")); - QGraphicsItem *logo_a2 = addWelcomeItem(QPixmap(":/logo-a2")); - QGraphicsItem *logo_q = addWelcomeItem(QPixmap(":/logo-q")); - QGraphicsItem *logo_excl = addWelcomeItem(QPixmap(":/logo-excl")); - logo_s->setZValue(3); - logo_u->setZValue(4); - logo_b->setZValue(5); - logo_dash->setZValue(6); - logo_a->setZValue(7); - logo_t->setZValue(8); - logo_t2->setZValue(9); - logo_a2->setZValue(10); - logo_q->setZValue(11); - logo_excl->setZValue(12); - logo_s->setPos(QPointF(-1000, -1000)); - logo_u->setPos(QPointF(-800, -1000)); - logo_b->setPos(QPointF(-600, -1000)); - logo_dash->setPos(QPointF(-400, -1000)); - logo_a->setPos(QPointF(1000, 2000)); - logo_t->setPos(QPointF(800, 2000)); - logo_t2->setPos(QPointF(600, 2000)); - logo_a2->setPos(QPointF(400, 2000)); - logo_q->setPos(QPointF(200, 2000)); - logo_excl->setPos(QPointF(0, 2000)); + static const int nLetters = 10; + static struct { + char *pix; + qreal initX, initY; + qreal destX, destY; + } logoData[nLetters] = { + {"s", -1000, -1000, 300, 150 }, + {"u", -800, -1000, 350, 150 }, + {"b", -600, -1000, 400, 120 }, + {"dash", -400, -1000, 460, 150 }, + {"a", 1000, 2000, 350, 250 }, + {"t", 800, 2000, 400, 250 }, + {"t2", 600, 2000, 430, 250 }, + {"a2", 400, 2000, 465, 250 }, + {"q", 200, 2000, 510, 250 }, + {"excl", 0, 2000, 570, 220 } }; QSequentialAnimationGroup * lettersGroupMoving = new QSequentialAnimationGroup(this); QParallelAnimationGroup * lettersGroupFading = new QParallelAnimationGroup(this); - //creation of the animations for moving letters - addGraphicsItemPosAnimation(lettersGroupMoving, logo_s, QPointF(300, 150)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_u, QPointF(350, 150)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_b, QPointF(400, 120)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_dash, QPointF(460, 150)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_a, QPointF(350, 250)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_t, QPointF(400, 250)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_t2, QPointF(430, 250)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_a2, QPointF(465, 250)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_q, QPointF(510, 250)); - addGraphicsItemPosAnimation(lettersGroupMoving, logo_excl, QPointF(570, 220)); - - //creation of the animations for fading out the letters - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_s); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_u); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_b); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_dash); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_a); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_t); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_t2); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_a2); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_q); - addGraphicsItemFadeoutAnimation(lettersGroupFading, logo_excl); - connect(lettersGroupFading, SIGNAL(finished()), this, SLOT(onIntroAnimationFinished())); + for (int i = 0; i < nLetters; ++i) { + PixmapItem *logo = new PixmapItem(QLatin1String(":/logo-") + logoData[i].pix, this); + logo->setPos(logoData[i].initX, logoData[i].initY); + logo->setZValue(i + 3); + //creation of the animations for moving letters + QPropertyAnimation *moveAnim = new QPropertyAnimation(logo, "pos", lettersGroupMoving); + moveAnim->setEndValue(QPointF(logoData[i].destX, logoData[i].destY)); + moveAnim->setDuration(200); + moveAnim->setEasingCurve(QEasingCurve::OutElastic); + lettersGroupMoving->addPause(50); + //creation of the animations for fading out the letters + QPropertyAnimation *fadeAnim = new QPropertyAnimation(logo, "opacity", lettersGroupFading); + fadeAnim->setDuration(800); + fadeAnim->setEndValue(0); + fadeAnim->setEasingCurve(QEasingCurve::OutQuad); + } QStateMachine *machine = new QStateMachine(this); //This state is when the player is playing - PlayState *gameState = new PlayState(this,machine); + PlayState *gameState = new PlayState(this, machine); //Final state QFinalState *final = new QFinalState(machine); @@ -263,7 +197,7 @@ void GraphicsScene::setupScene(const QList &actions) machine->start(); //We reach the final state, then we quit - connect(machine,SIGNAL(finished()),this, SLOT(onQuitGameTriggered())); + connect(machine, SIGNAL(finished()), qApp, SLOT(quit())); } void GraphicsScene::addItem(Bomb *bomb) @@ -292,16 +226,6 @@ void GraphicsScene::addItem(QGraphicsItem *item) QGraphicsScene::addItem(item); } -void GraphicsScene::mousePressEvent (QGraphicsSceneMouseEvent * event) -{ - event->ignore(); -} - -void GraphicsScene::onQuitGameTriggered() -{ - qApp->closeAllWindows(); -} - void GraphicsScene::onBombExecutionFinished() { Bomb *bomb = qobject_cast(sender()); @@ -322,32 +246,26 @@ void GraphicsScene::onSubMarineExecutionFinished() { SubMarine *submarine = qobject_cast(sender()); submarines.remove(submarine); - if (submarines.count() == 0) { + if (submarines.count() == 0) emit allSubMarineDestroyed(submarine->points()); - } else { + else emit subMarineDestroyed(submarine->points()); - } submarine->deleteLater(); } -int GraphicsScene::remainingSubMarines() const -{ - return submarines.count(); -} - void GraphicsScene::clearScene() { - foreach (SubMarine *sub,submarines) { + foreach (SubMarine *sub, submarines) { sub->destroy(); sub->deleteLater(); } - foreach (Torpedo *torpedo,torpedos) { + foreach (Torpedo *torpedo, torpedos) { torpedo->destroy(); torpedo->deleteLater(); } - foreach (Bomb *bomb,bombs) { + foreach (Bomb *bomb, bombs) { bomb->destroy(); bomb->deleteLater(); } @@ -361,17 +279,3 @@ void GraphicsScene::clearScene() boat->stop(); boat->hide(); } - -QGraphicsPixmapItem *GraphicsScene::addWelcomeItem(const QPixmap &pm) -{ - QGraphicsPixmapItem *item = addPixmap(pm); - welcomeItems << item; - return item; -} - -void GraphicsScene::onIntroAnimationFinished() -{ - qDeleteAll(welcomeItems); - welcomeItems.clear(); -} - diff --git a/demos/sub-attaq/graphicsscene.h b/demos/sub-attaq/graphicsscene.h index 7d7252d..ce2c91f 100644 --- a/demos/sub-attaq/graphicsscene.h +++ b/demos/sub-attaq/graphicsscene.h @@ -82,41 +82,30 @@ public: GraphicsScene(int x, int y, int width, int height, Mode mode = Big); qreal sealLevel() const; - void setupScene(const QList &actions); + void setupScene(QAction *newAction, QAction *quitAction); void addItem(Bomb *bomb); void addItem(Torpedo *torpedo); void addItem(SubMarine *submarine); void addItem(QGraphicsItem *item); - int remainingSubMarines() const; void clearScene(); - QGraphicsPixmapItem *addWelcomeItem(const QPixmap &pm); signals: void subMarineDestroyed(int); void allSubMarineDestroyed(int); -protected: - void mousePressEvent (QGraphicsSceneMouseEvent * event); - private slots: - void onQuitGameTriggered(); void onBombExecutionFinished(); void onTorpedoExecutionFinished(); void onSubMarineExecutionFinished(); - void onIntroAnimationFinished(); private: Mode mode; - PixmapItem *backgroundItem; ProgressItem *progressItem; TextInformationItem *textInformationItem; - QAction * newAction; - QAction * quitAction; Boat *boat; QSet submarines; QSet bombs; QSet torpedos; - QVector welcomeItems; QVector submarinesData; QHash levelsData; diff --git a/demos/sub-attaq/mainwindow.cpp b/demos/sub-attaq/mainwindow.cpp index 37129f8..45e5554 100644 --- a/demos/sub-attaq/mainwindow.cpp +++ b/demos/sub-attaq/mainwindow.cpp @@ -56,42 +56,27 @@ MainWindow::MainWindow() : QMainWindow(0) { - QMenuBar *menuBar = new QMenuBar; - QMenu *file = new QMenu(tr("&File"),menuBar); + QMenu *file = menuBar()->addMenu(tr("&File")); - QAction *newAction = new QAction(tr("New Game"),file); + QAction *newAction = file->addAction(tr("New Game")); newAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_N)); - file->addAction(newAction); - QAction *quitAction = new QAction(tr("Quit"),file); + QAction *quitAction = file->addAction(tr("Quit")); quitAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q)); - file->addAction(quitAction); - menuBar->addMenu(file); - setMenuBar(menuBar); - - QStringList list = QApplication::arguments(); - if (list.contains("-fullscreen")) { - scene = new GraphicsScene(0, 0, 750, 400,GraphicsScene::Small); + if (QApplication::arguments().contains("-fullscreen")) { + scene = new GraphicsScene(0, 0, 750, 400, GraphicsScene::Small); setWindowState(Qt::WindowFullScreen); } else { scene = new GraphicsScene(0, 0, 880, 630); layout()->setSizeConstraint(QLayout::SetFixedSize); } - view = new QGraphicsView(scene,this); + view = new QGraphicsView(scene, this); view->setAlignment(Qt::AlignLeft | Qt::AlignTop); - QList actions; - actions << newAction << quitAction; - scene->setupScene(actions); + scene->setupScene(newAction, quitAction); #ifndef QT_NO_OPENGL - view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); + view->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers))); #endif setCentralWidget(view); - -} - -MainWindow::~MainWindow() -{ } - diff --git a/demos/sub-attaq/mainwindow.h b/demos/sub-attaq/mainwindow.h index d626ad7..12a7364 100644 --- a/demos/sub-attaq/mainwindow.h +++ b/demos/sub-attaq/mainwindow.h @@ -54,7 +54,6 @@ class MainWindow : public QMainWindow Q_OBJECT public: MainWindow(); - ~MainWindow(); private: GraphicsScene *scene; diff --git a/demos/sub-attaq/pixmapitem.cpp b/demos/sub-attaq/pixmapitem.cpp index 9abf745..fcc7ce9 100644 --- a/demos/sub-attaq/pixmapitem.cpp +++ b/demos/sub-attaq/pixmapitem.cpp @@ -43,17 +43,34 @@ #include "pixmapitem.h" //Qt -#include +#include -PixmapItem::PixmapItem(const QString &fileName,GraphicsScene::Mode mode, QGraphicsItem * parent) : QGraphicsPixmapItem(parent),name(fileName) +PixmapItem::PixmapItem(const QString &fileName,GraphicsScene::Mode mode, QGraphicsItem * parent) : QGraphicsObject(parent) { - loadPixmap(mode); + if (mode == GraphicsScene::Big) + pix = ":/big/" + fileName; + else + pix = ":/small/" + fileName; } -void PixmapItem::loadPixmap(GraphicsScene::Mode mode) +PixmapItem::PixmapItem(const QString &fileName, QGraphicsScene *scene) : QGraphicsObject(), pix(fileName) { - if (mode == GraphicsScene::Big) - setPixmap(":/big/" + name); - else - setPixmap(":/small/" + name); + scene->addItem(this); } + +QSizeF PixmapItem::size() const +{ + return pix.size(); +} + +QRectF PixmapItem::boundingRect() const +{ + return QRectF(QPointF(0, 0), pix.size()); +} + +void PixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) +{ + painter->drawPixmap(0, 0, pix); +} + + diff --git a/demos/sub-attaq/pixmapitem.h b/demos/sub-attaq/pixmapitem.h index b176215..57f831a 100644 --- a/demos/sub-attaq/pixmapitem.h +++ b/demos/sub-attaq/pixmapitem.h @@ -46,18 +46,18 @@ #include "graphicsscene.h" //Qt -#include +#include -class PixmapItem : public QGraphicsPixmapItem +class PixmapItem : public QGraphicsObject { public: PixmapItem(const QString &fileName, GraphicsScene::Mode mode, QGraphicsItem * parent = 0); - + PixmapItem(const QString &fileName, QGraphicsScene *scene); + QSizeF size() const; + QRectF boundingRect() const; + void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); private: - void loadPixmap(GraphicsScene::Mode mode); - - QString name; - QPixmap pixmap; + QPixmap pix; }; #endif //__PIXMAPITEM__H__ diff --git a/demos/sub-attaq/states.cpp b/demos/sub-attaq/states.cpp index 7443ae7..742095e 100644 --- a/demos/sub-attaq/states.cpp +++ b/demos/sub-attaq/states.cpp @@ -67,8 +67,7 @@ PlayState::PlayState(GraphicsScene *scene, QState *parent) PlayState::~PlayState() { - if (machine) - delete machine; + delete machine; } void PlayState::onEntry(QEvent *) @@ -169,7 +168,7 @@ void LevelState::initializeLevel() scene->boat->setCurrentDirection(Boat::None); scene->boat->setBombsLaunched(0); scene->boat->show(); - scene->setFocusItem(scene->boat,Qt::OtherFocusReason); + scene->setFocusItem(scene->boat, Qt::OtherFocusReason); scene->boat->run(); scene->progressItem->setScore(game->score); @@ -276,13 +275,8 @@ void WinState::onExit(QEvent *) } /** UpdateScore State */ -UpdateScoreState::UpdateScoreState(PlayState *game, QState *parent) : QState(parent) -{ - this->game = game; -} -void UpdateScoreState::onEntry(QEvent *e) +UpdateScoreState::UpdateScoreState(PlayState *g, QState *parent) : QState(parent), game(g) { - QState::onEntry(e); } /** Win transition */ @@ -297,12 +291,10 @@ bool UpdateScoreTransition::eventTest(QEvent *event) { if (!QSignalTransition::eventTest(event)) return false; - else { - QStateMachine::SignalEvent *se = static_cast(event); - game->score += se->arguments().at(0).toInt(); - scene->progressItem->setScore(game->score); - return true; - } + QStateMachine::SignalEvent *se = static_cast(event); + game->score += se->arguments().at(0).toInt(); + scene->progressItem->setScore(game->score); + return true; } /** Win transition */ @@ -317,12 +309,10 @@ bool WinTransition::eventTest(QEvent *event) { if (!QSignalTransition::eventTest(event)) return false; - else { - QStateMachine::SignalEvent *se = static_cast(event); - game->score += se->arguments().at(0).toInt(); - scene->progressItem->setScore(game->score); - return true; - } + QStateMachine::SignalEvent *se = static_cast(event); + game->score += se->arguments().at(0).toInt(); + scene->progressItem->setScore(game->score); + return true; } /** Space transition */ @@ -334,12 +324,7 @@ CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, Q bool CustomSpaceTransition::eventTest(QEvent *event) { - Q_UNUSED(event); if (!QKeyEventTransition::eventTest(event)) return false; - if (game->currentLevel != 0) - return true; - else - return false; - + return (game->currentLevel != 0); } diff --git a/demos/sub-attaq/states.h b/demos/sub-attaq/states.h index 9e78ae4..f588e5d 100644 --- a/demos/sub-attaq/states.h +++ b/demos/sub-attaq/states.h @@ -136,8 +136,6 @@ class UpdateScoreState : public QState { public: UpdateScoreState(PlayState *game, QState *parent); -protected: - void onEntry(QEvent *); private: QPropertyAnimation *scoreAnimation; PlayState *game; diff --git a/demos/sub-attaq/sub-attaq.pro b/demos/sub-attaq/sub-attaq.pro index 8677ff5..b5aa465 100644 --- a/demos/sub-attaq/sub-attaq.pro +++ b/demos/sub-attaq/sub-attaq.pro @@ -10,7 +10,6 @@ HEADERS += boat.h \ states.h \ boat_p.h \ submarine_p.h \ - custompropertyanimation.h \ qanimationstate.h \ progressitem.h \ textinformationitem.h @@ -24,7 +23,6 @@ SOURCES += boat.cpp \ graphicsscene.cpp \ animationmanager.cpp \ states.cpp \ - custompropertyanimation.cpp \ qanimationstate.cpp \ progressitem.cpp \ textinformationitem.cpp diff --git a/demos/sub-attaq/submarine.cpp b/demos/sub-attaq/submarine.cpp index 3d8490f..f71b81c 100644 --- a/demos/sub-attaq/submarine.cpp +++ b/demos/sub-attaq/submarine.cpp @@ -46,7 +46,6 @@ #include "pixmapitem.h" #include "graphicsscene.h" #include "animationmanager.h" -#include "custompropertyanimation.h" #include "qanimationstate.h" #include @@ -57,62 +56,27 @@ 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 - // work around for a bug where we don't transition if the duration is zero. - QtPauseAnimation *anim = new QtPauseAnimation(group); - anim->setDuration(1); - group->addAnimation(anim); -#endif + for (int i = 1; i <= 4; ++i) { + PixmapItem *step = new PixmapItem(QString::fromLatin1("explosion/submarine/step%1").arg(i), GraphicsScene::Big, sub); + step->setZValue(6); + step->setOpacity(0); + QPropertyAnimation *anim = new QPropertyAnimation(step, "opacity", group); + anim->setDuration(100); + anim->setEndValue(1); + } 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) +SubMarine::SubMarine(int type, const QString &name, int points) : PixmapItem(QString("submarine"), GraphicsScene::Big), + 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->setAxis(Qt::YAxis); graphicsRotation->setOrigin(QVector3D(size().width()/2, size().height()/2, 0)); QList r; r.append(graphicsRotation); @@ -163,7 +127,7 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem * machine->start(); } -int SubMarine::points() +int SubMarine::points() const { return subPoints; } @@ -202,7 +166,7 @@ void SubMarine::launchTorpedo(int speed) Torpedo * torp = new Torpedo(); GraphicsScene *scene = static_cast(this->scene()); scene->addItem(torp); - torp->setPos(x(), y()); + torp->setPos(pos()); torp->setCurrentSpeed(speed); torp->launch(); } diff --git a/demos/sub-attaq/submarine.h b/demos/sub-attaq/submarine.h index 1a3d2e5..326a1c8 100644 --- a/demos/sub-attaq/submarine.h +++ b/demos/sub-attaq/submarine.h @@ -43,15 +43,13 @@ #define __SUBMARINE__H__ //Qt -#include -#include #include -class PixmapItem; +#include "pixmapitem.h" class Torpedo; -class SubMarine : public QGraphicsWidget +class SubMarine : public PixmapItem { Q_OBJECT public: @@ -61,9 +59,9 @@ public: Right }; enum { Type = UserType + 1 }; - SubMarine(int type, const QString &name, int points, QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0); + SubMarine(int type, const QString &name, int points); - int points(); + int points() const; void setCurrentDirection(Movement direction); enum Movement currentDirection() const; @@ -89,7 +87,6 @@ private: int subPoints; int speed; Movement direction; - PixmapItem *pixmapItem; QGraphicsRotation *graphicsRotation; }; diff --git a/demos/sub-attaq/submarine_p.h b/demos/sub-attaq/submarine_p.h index fa7430b..64a0cf7 100644 --- a/demos/sub-attaq/submarine_p.h +++ b/demos/sub-attaq/submarine_p.h @@ -94,7 +94,6 @@ protected: movementAnimation->setEndValue(QPointF(submarine->scene()->width()-submarine->size().width(),submarine->y())); movementAnimation->setDuration((submarine->scene()->width()-submarine->size().width()-submarine->x())/submarine->currentSpeed()*12); } - movementAnimation->setStartValue(submarine->pos()); QAnimationState::onEntry(e); } diff --git a/demos/sub-attaq/torpedo.cpp b/demos/sub-attaq/torpedo.cpp index cce430d..95f88e6 100644 --- a/demos/sub-attaq/torpedo.cpp +++ b/demos/sub-attaq/torpedo.cpp @@ -51,24 +51,21 @@ #include #include -Torpedo::Torpedo(QGraphicsItem * parent, Qt::WindowFlags wFlags) - : QGraphicsWidget(parent,wFlags), currentSpeed(0), launchAnimation(0) +Torpedo::Torpedo() : PixmapItem(QString::fromLatin1("torpedo"),GraphicsScene::Big), + currentSpeed(0) { - pixmapItem = new PixmapItem(QString::fromLatin1("torpedo"),GraphicsScene::Big, this); setZValue(2); - setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); - setFlags(QGraphicsItem::ItemIsMovable); - resize(pixmapItem->boundingRect().size()); } void Torpedo::launch() { - launchAnimation = new QPropertyAnimation(this, "pos"); + QPropertyAnimation *launchAnimation = new QPropertyAnimation(this, "pos"); AnimationManager::self()->registerAnimation(launchAnimation); launchAnimation->setEndValue(QPointF(x(),qobject_cast(scene())->sealLevel() - 15)); launchAnimation->setEasingCurve(QEasingCurve::InQuad); launchAnimation->setDuration(y()/currentSpeed*10); connect(launchAnimation,SIGNAL(valueChanged(const QVariant &)),this,SLOT(onAnimationLaunchValueChanged(const QVariant &))); + connect(this,SIGNAL(torpedoExploded()), launchAnimation, SLOT(stop())); //We setup the state machine of the torpedo QStateMachine *machine = new QStateMachine(this); @@ -83,7 +80,7 @@ void Torpedo::launch() machine->setInitialState(launched); //### Add a nice animation when the torpedo is destroyed - launched->addTransition(this, SIGNAL(torpedoExplosed()),final); + launched->addTransition(this, SIGNAL(torpedoExploded()),final); //If the animation is finished, then we move to the final state launched->addTransition(launched, SIGNAL(animationFinished()), final); @@ -106,15 +103,12 @@ void Torpedo::setCurrentSpeed(int speed) void Torpedo::onAnimationLaunchValueChanged(const QVariant &) { foreach (QGraphicsItem *item , collidingItems(Qt::IntersectsItemBoundingRect)) { - if (item->type() == Boat::Type) { - Boat *b = static_cast(item); + if (Boat *b = qgraphicsitem_cast(item)) b->destroy(); - } } } void Torpedo::destroy() { - launchAnimation->stop(); - emit torpedoExplosed(); + emit torpedoExploded(); } diff --git a/demos/sub-attaq/torpedo.h b/demos/sub-attaq/torpedo.h index 2e654f4..03f277d 100644 --- a/demos/sub-attaq/torpedo.h +++ b/demos/sub-attaq/torpedo.h @@ -42,25 +42,19 @@ #ifndef __TORPEDO__H__ #define __TORPEDO__H__ -//Qt -#include +#include "pixmapitem.h" -#include -#include - -class PixmapItem; - -class Torpedo : public QGraphicsWidget +class Torpedo : public PixmapItem { Q_OBJECT public: - Torpedo(QGraphicsItem * parent = 0, Qt::WindowFlags wFlags = 0); + Torpedo(); void launch(); void setCurrentSpeed(int speed); void destroy(); signals: - void torpedoExplosed(); + void torpedoExploded(); void torpedoExecutionFinished(); private slots: @@ -68,8 +62,6 @@ private slots: private: int currentSpeed; - PixmapItem *pixmapItem; - QVariantAnimation *launchAnimation; }; #endif //__TORPEDO__H__ -- cgit v0.12