diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-04-29 12:23:54 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-04-29 12:23:54 (GMT) |
commit | eea1c9f34b9b98c55c48f9ed1dbef9a9883c6f2a (patch) | |
tree | 2710760fe8a79cdeda9555debd6d520d6cee9a96 /examples/animation | |
parent | 20702b4a5992a87e8edc40949ea80fa625a3fc8c (diff) | |
parent | 36aa9e0468bfa874679dd94b2d50850a1dd7a1d1 (diff) | |
download | Qt-eea1c9f34b9b98c55c48f9ed1dbef9a9883c6f2a.zip Qt-eea1c9f34b9b98c55c48f9ed1dbef9a9883c6f2a.tar.gz Qt-eea1c9f34b9b98c55c48f9ed1dbef9a9883c6f2a.tar.bz2 |
Merge branch 'kinetic-animations' into kinetic-statemachine
Conflicts:
examples/animation/sub-attaq/states.cpp
Diffstat (limited to 'examples/animation')
21 files changed, 370 insertions, 117 deletions
diff --git a/examples/animation/sub-attaq/animationmanager.cpp b/examples/animation/sub-attaq/animationmanager.cpp index 9f99426..5b9282a 100644 --- a/examples/animation/sub-attaq/animationmanager.cpp +++ b/examples/animation/sub-attaq/animationmanager.cpp @@ -48,7 +48,7 @@ #else # include <QtCore/QAbstractAnimation> #endif -#include <QDebug> +#include <QtCore/QDebug> // the universe's only animation manager AnimationManager *AnimationManager::instance = 0; diff --git a/examples/animation/sub-attaq/animationmanager.h b/examples/animation/sub-attaq/animationmanager.h index fe92680..69ec3d7 100644 --- a/examples/animation/sub-attaq/animationmanager.h +++ b/examples/animation/sub-attaq/animationmanager.h @@ -42,7 +42,7 @@ #ifndef ANIMATIONMANAGER_H #define ANIMATIONMANAGER_H -#include <QObject> +#include <QtCore/QObject> class QAbstractAnimation; diff --git a/examples/animation/sub-attaq/boat.cpp b/examples/animation/sub-attaq/boat.cpp index f01f1bb..143cf94 100644 --- a/examples/animation/sub-attaq/boat.cpp +++ b/examples/animation/sub-attaq/boat.cpp @@ -59,12 +59,12 @@ # include "qpauseanimation.h" #include "qsequentialanimationgroup.h" #else -#include <QPropertyAnimation> -#include <QStateMachine> -#include <QHistoryState> -#include <QFinalState> -#include <QState> -#include <QSequentialAnimationGroup> +#include <QtCore/QPropertyAnimation> +#include <QtCore/QStateMachine> +#include <QtCore/QHistoryState> +#include <QtCore/QFinalState> +#include <QtCore/QState> +#include <QtCore/QSequentialAnimationGroup> #endif static QAbstractAnimation *setupDestroyAnimation(Boat *boat) @@ -99,10 +99,30 @@ static QAbstractAnimation *setupDestroyAnimation(Boat *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); @@ -126,37 +146,39 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags) //The movement animation used to animate the boat movementAnimation = new QPropertyAnimation(this, "pos"); - AnimationManager::self()->registerAnimation(movementAnimation); + + //The movement animation used to animate the boat + destroyAnimation = setupDestroyAnimation(this); //We setup the state machien of the boat - QStateMachine *machine = new QStateMachine(this); + machine = new QStateMachine(this); QState *moving = new QState(machine->rootState()); - StopState *stopState = new StopState(this,moving); + StopState *stopState = new StopState(this, moving); machine->setInitialState(moving); moving->setInitialState(stopState); - MoveStateRight *moveStateRight = new MoveStateRight(this,moving); - MoveStateLeft *moveStateLeft = new MoveStateLeft(this,moving); - LaunchStateRight *launchStateRight = new LaunchStateRight(this,machine->rootState()); - LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this,machine->rootState()); + MoveStateRight *moveStateRight = new MoveStateRight(this, moving); + MoveStateLeft *moveStateLeft = new MoveStateLeft(this, moving); + LaunchStateRight *launchStateRight = new LaunchStateRight(this, machine->rootState()); + LaunchStateLeft *launchStateLeft = new LaunchStateLeft(this, machine->rootState()); //then setup the transitions for the rightMove state - KeyStopTransition *leftStopRight = new KeyStopTransition(this,QEvent::KeyPress,Qt::Key_Left); + KeyStopTransition *leftStopRight = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Left); leftStopRight->setTargetState(stopState); - KeyMoveTransition *leftMoveRight = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Left); + KeyMoveTransition *leftMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Left); leftMoveRight->setTargetState(moveStateRight); - KeyMoveTransition *rightMoveRight = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Right); + KeyMoveTransition *rightMoveRight = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right); rightMoveRight->setTargetState(moveStateRight); - KeyMoveTransition *rightMoveStop = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Right); + KeyMoveTransition *rightMoveStop = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right); rightMoveStop->setTargetState(moveStateRight); //then setup the transitions for the leftMove state - KeyStopTransition *rightStopLeft = new KeyStopTransition(this,QEvent::KeyPress,Qt::Key_Right); + KeyStopTransition *rightStopLeft = new KeyStopTransition(this, QEvent::KeyPress, Qt::Key_Right); rightStopLeft->setTargetState(stopState); - KeyMoveTransition *rightMoveLeft = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Right); + KeyMoveTransition *rightMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress, Qt::Key_Right); rightMoveLeft->setTargetState(moveStateLeft); - KeyMoveTransition *leftMoveLeft = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Left); + KeyMoveTransition *leftMoveLeft = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left); leftMoveLeft->setTargetState(moveStateLeft); - KeyMoveTransition *leftMoveStop = new KeyMoveTransition(this,QEvent::KeyPress,Qt::Key_Left); + KeyMoveTransition *leftMoveStop = new KeyMoveTransition(this, QEvent::KeyPress,Qt::Key_Left); leftMoveStop->setTargetState(moveStateLeft); //We set up the right move state @@ -176,17 +198,17 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags) moveStateRight->addTransition(movementAnimation, SIGNAL(finished()), stopState); //We set up the keys for dropping bombs - KeyLaunchTransition *upFireLeft = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Up); + KeyLaunchTransition *upFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up); upFireLeft->setTargetState(launchStateRight); - KeyLaunchTransition *upFireRight = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Up); + KeyLaunchTransition *upFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up); upFireRight->setTargetState(launchStateRight); - KeyLaunchTransition *upFireStop = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Up); + KeyLaunchTransition *upFireStop = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Up); upFireStop->setTargetState(launchStateRight); - KeyLaunchTransition *downFireLeft = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Down); + KeyLaunchTransition *downFireLeft = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down); downFireLeft->setTargetState(launchStateLeft); - KeyLaunchTransition *downFireRight = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Down); + KeyLaunchTransition *downFireRight = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down); downFireRight->setTargetState(launchStateLeft); - KeyLaunchTransition *downFireMove = new KeyLaunchTransition(this,QEvent::KeyPress,Qt::Key_Down); + KeyLaunchTransition *downFireMove = new KeyLaunchTransition(this, QEvent::KeyPress, Qt::Key_Down); downFireMove->setTargetState(launchStateLeft); //We set up transitions for fire up @@ -208,7 +230,7 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags) //This state play the destroyed animation QAnimationState *destroyedState = new QAnimationState(machine->rootState()); - destroyedState->setAnimation(setupDestroyAnimation(this)); + destroyedState->setAnimation(destroyAnimation); //Play a nice animation when the boat is destroyed moving->addTransition(this, SIGNAL(boatDestroyed()),destroyedState); @@ -219,9 +241,22 @@ Boat::Boat(QGraphicsItem * parent, Qt::WindowFlags wFlags) //The machine has finished to be executed, then the boat is dead connect(machine,SIGNAL(finished()),this, SIGNAL(boatExecutionFinished())); +} + +void Boat::run() +{ + //We register animations + AnimationManager::self()->registerAnimation(movementAnimation); + AnimationManager::self()->registerAnimation(destroyAnimation); machine->start(); } +void Boat::stop() +{ + movementAnimation->stop(); + machine->stop(); +} + void Boat::updateBoatMovement() { if (speed == 0 || direction == Boat::None) { diff --git a/examples/animation/sub-attaq/boat.h b/examples/animation/sub-attaq/boat.h index b8d5772..b28cf20 100644 --- a/examples/animation/sub-attaq/boat.h +++ b/examples/animation/sub-attaq/boat.h @@ -43,10 +43,8 @@ #define __BOAT__H__ //Qt -#include <QObject> -#include <QKeyEvent> - -#include <QDebug> +#include <QtCore/QObject> +#include <QtGui/QKeyEvent> #if defined(QT_EXPERIMENTAL_SOLUTION) # include "qtgraphicswidget.h" @@ -57,6 +55,8 @@ class PixmapItem; class Bomb; class QVariantAnimation; +class QAbstractAnimation; +class QStateMachine; class Boat : public QGraphicsWidget { @@ -71,6 +71,8 @@ public: enum { Type = UserType + 2 }; Boat(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); void destroy(); + void run(); + void stop(); int bombsLaunched() const; void setBombsLaunched(int number); @@ -94,6 +96,8 @@ private: int bombsAlreadyLaunched; Movement direction; QVariantAnimation *movementAnimation; + QAbstractAnimation *destroyAnimation; + QStateMachine *machine; PixmapItem *pixmapItem; }; diff --git a/examples/animation/sub-attaq/boat_p.h b/examples/animation/sub-attaq/boat_p.h index ff69335..855b10f 100644 --- a/examples/animation/sub-attaq/boat_p.h +++ b/examples/animation/sub-attaq/boat_p.h @@ -61,7 +61,7 @@ class KeyStopTransition : public QKeyEventTransition { public: KeyStopTransition(Boat *boat, QEvent::Type type, int key) - : QKeyEventTransition(boat,type, key) + : QKeyEventTransition(boat, type, key) { this->boat = boat; this->key = key; @@ -87,7 +87,7 @@ private: { public: KeyMoveTransition(Boat *boat, QEvent::Type type, int key) - : QKeyEventTransition(boat,type, key) + : QKeyEventTransition(boat, type, key) { this->boat = boat; this->key = key; @@ -125,7 +125,7 @@ private: { public: KeyLaunchTransition(Boat *boat, QEvent::Type type, int key) - : QKeyEventTransition(boat,type, key) + : QKeyEventTransition(boat, type, key) { this->boat = boat; this->key = key; diff --git a/examples/animation/sub-attaq/graphicsscene.cpp b/examples/animation/sub-attaq/graphicsscene.cpp index 5dcc034..2a6f83c 100644 --- a/examples/animation/sub-attaq/graphicsscene.cpp +++ b/examples/animation/sub-attaq/graphicsscene.cpp @@ -50,6 +50,7 @@ #include "custompropertyanimation.h" #include "animationmanager.h" #include "qanimationstate.h" +#include "progressitem.h" //Qt #if defined(QT_EXPERIMENTAL_SOLUTION) @@ -60,20 +61,20 @@ #include "qfinalstate.h" #include "qpauseanimation.h" #else -#include <QPropertyAnimation> -#include <QSequentialAnimationGroup> -#include <QParallelAnimationGroup> -#include <QStateMachine> -#include <QFinalState> -#include <QPauseAnimation> +#include <QtCore/QPropertyAnimation> +#include <QtCore/QSequentialAnimationGroup> +#include <QtCore/QParallelAnimationGroup> +#include <QtCore/QStateMachine> +#include <QtCore/QFinalState> +#include <QtCore/QPauseAnimation> #endif -#include <QAction> -#include <QDir> -#include <QApplication> -#include <QMessageBox> -#include <QGraphicsView> -#include <QGraphicsSceneMouseEvent> -#include <QXmlStreamReader> +#include <QtGui/QAction> +#include <QtCore/QDir> +#include <QtGui/QApplication> +#include <QtGui/QMessageBox> +#include <QtGui/QGraphicsView> +#include <QtGui/QGraphicsSceneMouseEvent> +#include <QtCore/QXmlStreamReader> //helper function that creates an animation for position and inserts it into group static CustomPropertyAnimation *addGraphicsItemPosAnimation(QSequentialAnimationGroup *group, @@ -117,6 +118,15 @@ GraphicsScene::GraphicsScene(int x, int y, int width, int height, Mode mode) surfaceItem->setPos(0,sealLevel() - surfaceItem->boundingRect().height()/2); addItem(surfaceItem); + //The item that display score and level + progressItem = new ProgressItem(backgroundItem); + + //We create the boat + boat = new Boat(); + addItem(boat); + boat->setPos(this->width()/2, sealLevel() - boat->size().height()); + boat->hide(); + //parse the xml that contain all data of the game QXmlStreamReader reader; QFile file(QDir::currentPath() + "/data.xml"); @@ -335,17 +345,17 @@ void GraphicsScene::clearScene() { foreach (SubMarine *sub,submarines) { sub->destroy(); - delete sub; + sub->deleteLater(); } foreach (Torpedo *torpedo,torpedos) { torpedo->destroy(); - delete torpedo; + torpedo->deleteLater(); } foreach (Bomb *bomb,bombs) { bomb->destroy(); - delete bomb; + bomb->deleteLater(); } submarines.clear(); @@ -354,10 +364,8 @@ void GraphicsScene::clearScene() AnimationManager::self()->unregisterAllAnimations(); - if (boat) { - delete boat; - boat = 0; - } + boat->stop(); + boat->hide(); } QGraphicsPixmapItem *GraphicsScene::addWelcomeItem(const QPixmap &pm) diff --git a/examples/animation/sub-attaq/graphicsscene.h b/examples/animation/sub-attaq/graphicsscene.h index 875f59f..0840564 100644 --- a/examples/animation/sub-attaq/graphicsscene.h +++ b/examples/animation/sub-attaq/graphicsscene.h @@ -43,13 +43,13 @@ #define __GRAPHICSSCENE__H__ //Qt -#include <QGraphicsScene> -#include <QSet> +#include <QtGui/QGraphicsScene> +#include <QtCore/QSet> #if defined(QT_EXPERIMENTAL_SOLUTION) # include "qstate.h" #else -# include <QState> +# include <QtCore/QState> #endif @@ -58,6 +58,7 @@ class SubMarine; class Torpedo; class Bomb; class PixmapItem; +class ProgressItem; class QAction; class GraphicsScene : public QGraphicsScene @@ -109,6 +110,7 @@ private slots: private: Mode mode; PixmapItem *backgroundItem; + ProgressItem *progressItem; QAction * newAction; QAction * quitAction; Boat *boat; @@ -121,8 +123,11 @@ private: friend class PauseState; friend class PlayState; + friend class LevelState; friend class LostState; friend class WinState; + friend class WinTransition; + friend class UpdateScoreTransition; }; #endif //__GRAPHICSSCENE__H__ diff --git a/examples/animation/sub-attaq/mainwindow.cpp b/examples/animation/sub-attaq/mainwindow.cpp index c25b9ef..a166241 100644 --- a/examples/animation/sub-attaq/mainwindow.cpp +++ b/examples/animation/sub-attaq/mainwindow.cpp @@ -47,7 +47,7 @@ #include <QtOpenGL/QtOpenGL> #endif //Qt -#include <QGraphicsView> +#include <QtGui/QGraphicsView> MainWindow::MainWindow() : QMainWindow(0) { diff --git a/examples/animation/sub-attaq/mainwindow.h b/examples/animation/sub-attaq/mainwindow.h index 6289b3f..72d1324 100644 --- a/examples/animation/sub-attaq/mainwindow.h +++ b/examples/animation/sub-attaq/mainwindow.h @@ -43,7 +43,7 @@ #define __MAINWINDOW__H__ //Qt -#include <QMainWindow> +#include <QtGui/QMainWindow> class GraphicsScene; class QGraphicsView; diff --git a/examples/animation/sub-attaq/pixmapitem.cpp b/examples/animation/sub-attaq/pixmapitem.cpp index aa8b552..22a363f 100644 --- a/examples/animation/sub-attaq/pixmapitem.cpp +++ b/examples/animation/sub-attaq/pixmapitem.cpp @@ -43,7 +43,7 @@ #include "pixmapitem.h" //Qt -#include <QDir> +#include <QtCore/QDir> PixmapItem::PixmapItem(const QString &fileName,GraphicsScene::Mode mode, QGraphicsItem * parent) : QGraphicsPixmapItem(parent),name(fileName) { diff --git a/examples/animation/sub-attaq/pixmapitem.h b/examples/animation/sub-attaq/pixmapitem.h index f3c1a41..31022c1 100644 --- a/examples/animation/sub-attaq/pixmapitem.h +++ b/examples/animation/sub-attaq/pixmapitem.h @@ -46,7 +46,7 @@ #include "graphicsscene.h" //Qt -#include <QGraphicsPixmapItem> +#include <QtGui/QGraphicsPixmapItem> class PixmapItem : public QGraphicsPixmapItem { diff --git a/examples/animation/sub-attaq/progressitem.cpp b/examples/animation/sub-attaq/progressitem.cpp new file mode 100644 index 0000000..59ccb9a --- /dev/null +++ b/examples/animation/sub-attaq/progressitem.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "progressitem.h" +#include "pixmapitem.h" + +ProgressItem::ProgressItem (QGraphicsItem * parent) + : QGraphicsTextItem(parent), currentLevel(1), currentScore(0) +{ + setFont(QFont("Comic Sans MS")); + setPos(parentItem()->boundingRect().topRight() - QPointF(180, -5)); +} + +void ProgressItem::setLevel(int level) +{ + currentLevel = level; + updateProgress(); +} + +void ProgressItem::setScore(int score) +{ + currentScore = score; + updateProgress(); +} + +void ProgressItem::updateProgress() +{ + setHtml(QString("Level : %1 Score : %2").arg(currentLevel).arg(currentScore)); +} diff --git a/examples/animation/sub-attaq/progressitem.h b/examples/animation/sub-attaq/progressitem.h new file mode 100644 index 0000000..7b8db4d --- /dev/null +++ b/examples/animation/sub-attaq/progressitem.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PROGRESSITEM_H +#define PROGRESSITEM_H + +//Qt +#include <QtGui/QGraphicsTextItem> + +class ProgressItem : public QGraphicsTextItem +{ +public: + ProgressItem(QGraphicsItem * parent = 0); + void setLevel(int level); + void setScore(int score); + +private: + void updateProgress(); + int currentLevel; + int currentScore; +}; + +#endif // PROGRESSITEM_H diff --git a/examples/animation/sub-attaq/states.cpp b/examples/animation/sub-attaq/states.cpp index b381db7..0b98016 100644 --- a/examples/animation/sub-attaq/states.cpp +++ b/examples/animation/sub-attaq/states.cpp @@ -46,10 +46,11 @@ #include "submarine.h" #include "torpedo.h" #include "animationmanager.h" +#include "progressitem.h" //Qt -#include <QMessageBox> -#include <QGraphicsView> +#include <QtGui/QMessageBox> +#include <QtGui/QGraphicsView> #if defined(QT_EXPERIMENTAL_SOLUTION) #include "qstatemachine.h" #include "qkeyeventtransition.h" @@ -59,7 +60,7 @@ #include <QtCore/QStateMachine> #include <QtGui/QKeyEventTransition> #include <QtCore/QSignalEvent> -#include <QFinalState> +#include <QtCore/QFinalState> #endif PlayState::PlayState(GraphicsScene *scene, QState *parent) @@ -81,27 +82,31 @@ void PlayState::onEntry() if (machine) { machine->stop(); scene->clearScene(); + currentLevel = 0; + score = 0; delete machine; } machine = new QStateMachine(this); //This state is when player is playing - QState *playState = new QState(machine->rootState()); + LevelState *levelState = new LevelState(scene, this, machine->rootState()); - initializeLevel(); + //This state is when the player is actually playing but the game is not paused + QState *playingState = new QState(levelState); + levelState->setInitialState(playingState); //This state is when the game is paused - PauseState *pauseState = new PauseState(scene, machine->rootState()); + PauseState *pauseState = new PauseState(scene, levelState); //We have one view, it receive the key press event QKeyEventTransition *pressPplay = new QKeyEventTransition(scene->views().at(0), QEvent::KeyPress, Qt::Key_P); pressPplay->setTargetState(pauseState); QKeyEventTransition *pressPpause = new QKeyEventTransition(scene->views().at(0), QEvent::KeyPress, Qt::Key_P); - pressPpause->setTargetState(playState); + pressPpause->setTargetState(playingState); //Pause "P" is triggered, the player pause the game - playState->addTransition(pressPplay); + playingState->addTransition(pressPplay); //To get back playing when the game has been paused pauseState->addTransition(pressPpause); @@ -113,34 +118,37 @@ void PlayState::onEntry() WinState *winState = new WinState(scene, this, machine->rootState()); //The boat has been destroyed then the game is finished - playState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState); + levelState->addTransition(scene->boat, SIGNAL(boatExecutionFinished()),lostState); //This transition check if we won or not WinTransition *winTransition = new WinTransition(scene, this, winState); //The boat has been destroyed then the game is finished - playState->addTransition(winTransition); + levelState->addTransition(winTransition); //This state is an animation when the score changed - UpdateScoreState *scoreState = new UpdateScoreState(this, machine->rootState()); + UpdateScoreState *scoreState = new UpdateScoreState(this, levelState); //This transition update the score when a submarine die - UpdateScoreTransition *scoreTransition = new UpdateScoreTransition(scene, this, scoreState); + UpdateScoreTransition *scoreTransition = new UpdateScoreTransition(scene, this, levelState); + scoreTransition->setTargetState(scoreState); //The boat has been destroyed then the game is finished - playState->addTransition(scoreTransition); + playingState->addTransition(scoreTransition); //We go back to play state - scoreState->addTransition(playState); + scoreState->addTransition(playingState); //We start playing!!! - machine->setInitialState(playState); + machine->setInitialState(levelState); //Final state QFinalState *final = new QFinalState(machine->rootState()); - //We win we should reach the final state - winState->addTransition(winState, SIGNAL(finished()), final); + //This transition is triggered when the player press space after completing a level + CustomSpaceTransition *spaceTransition = new CustomSpaceTransition(scene->views().at(0), this, QEvent::KeyPress, Qt::Key_Space); + spaceTransition->setTargetState(levelState); + winState->addTransition(spaceTransition); //We lost we should reach the final state lostState->addTransition(lostState, SIGNAL(finished()), final); @@ -148,14 +156,29 @@ void PlayState::onEntry() machine->start(); } -void PlayState::initializeLevel() +LevelState::LevelState(GraphicsScene *scene, PlayState *game, QState *parent) : QState(parent), scene(scene), game(game) { - scene->boat = new Boat(); - scene->addItem(scene->boat); - scene->setFocusItem(scene->boat,Qt::OtherFocusReason); +} +void LevelState::onEntry() +{ + initializeLevel(); +} + +void LevelState::initializeLevel() +{ + //we re-init the boat scene->boat->setPos(scene->width()/2, scene->sealLevel() - scene->boat->size().height()); + scene->boat->setCurrentSpeed(0); + scene->boat->setCurrentDirection(Boat::None); + scene->boat->setBombsLaunched(0); + scene->boat->show(); + scene->setFocusItem(scene->boat,Qt::OtherFocusReason); + scene->boat->run(); + + scene->progressItem->setScore(game->score); + scene->progressItem->setLevel(game->currentLevel + 1); - GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(currentLevel); + GraphicsScene::LevelDescription currentLevelDescription = scene->levelsData.value(game->currentLevel); for (int i = 0; i < currentLevelDescription.submarines.size(); ++i ) { @@ -226,7 +249,7 @@ void WinState::onEntry() QString message; if (scene->levelsData.size() - 1 != game->currentLevel) { - message = QString("You win the level %1. Your score is %2.").arg(game->currentLevel+1).arg(game->score); + message = QString("You win the level %1. Your score is %2.\nPress Space to continue after closing this dialog.").arg(game->currentLevel+1).arg(game->score); //We increment the level number game->currentLevel++; } else { @@ -248,14 +271,13 @@ UpdateScoreState::UpdateScoreState(PlayState *game, QState *parent) : QState(par } void UpdateScoreState::onEntry() { - //### Make a nice anim to update the score in the scene QState::onEntry(); } /** Win transition */ UpdateScoreTransition::UpdateScoreTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target) : QSignalTransition(scene,SIGNAL(subMarineDestroyed(int)), QList<QAbstractState*>() << target), - game(game) + game(game), scene(scene) { } @@ -266,6 +288,7 @@ bool UpdateScoreTransition::eventTest(QEvent *event) const else { QSignalEvent *se = static_cast<QSignalEvent*>(event); game->score += se->arguments().at(0).toInt(); + scene->progressItem->setScore(game->score); return true; } } @@ -273,7 +296,7 @@ bool UpdateScoreTransition::eventTest(QEvent *event) const /** Win transition */ WinTransition::WinTransition(GraphicsScene *scene, PlayState *game, QAbstractState *target) : QSignalTransition(scene,SIGNAL(allSubMarineDestroyed(int)), QList<QAbstractState*>() << target), - game(game) + game(game), scene(scene) { } @@ -284,6 +307,26 @@ bool WinTransition::eventTest(QEvent *event) const else { QSignalEvent *se = static_cast<QSignalEvent*>(event); game->score += se->arguments().at(0).toInt(); + scene->progressItem->setScore(game->score); return true; } } + +/** Space transition */ +CustomSpaceTransition::CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key) + : QKeyEventTransition(widget, type, key), + game(game) +{ +} + +bool CustomSpaceTransition::eventTest(QEvent *event) const +{ + Q_UNUSED(event); + if (!QKeyEventTransition::eventTest(event)) + return false; + if (game->currentLevel != 0) + return true; + else + return false; + +} diff --git a/examples/animation/sub-attaq/states.h b/examples/animation/sub-attaq/states.h index 52d4ffa..ec69ae7 100644 --- a/examples/animation/sub-attaq/states.h +++ b/examples/animation/sub-attaq/states.h @@ -47,12 +47,14 @@ #include "qstate.h" #include "qsignaltransition.h" #include "qpropertyanimation.h" +#include "qkeyeventtransition.h" #else -#include <QState> -#include <QSignalTransition> -#include <QPropertyAnimation> +#include <QtCore/QState> +#include <QtCore/QSignalTransition> +#include <QtCore/QPropertyAnimation> +# include <QtGui/QKeyEventTransition> #endif -#include <QSet> +#include <QtCore/QSet> class GraphicsScene; class Boat; @@ -64,7 +66,6 @@ class PlayState : public QState public: PlayState(GraphicsScene *scene, QState *parent = 0); ~PlayState(); - void initializeLevel(); protected: void onEntry(); @@ -79,8 +80,22 @@ private : friend class UpdateScoreState; friend class UpdateScoreTransition; friend class WinTransition; + friend class CustomSpaceTransition; friend class WinState; friend class LostState; + friend class LevelState; +}; + +class LevelState : public QState +{ +public: + LevelState(GraphicsScene *scene, PlayState *game, QState *parent = 0); +protected: + void onEntry(); +private : + void initializeLevel(); + GraphicsScene *scene; + PlayState *game; }; class PauseState : public QState @@ -140,6 +155,7 @@ protected: virtual bool eventTest(QEvent *event) const; private: PlayState * game; + GraphicsScene *scene; }; //These transtion test if we have won the game @@ -151,6 +167,19 @@ protected: virtual bool eventTest(QEvent *event) const; private: PlayState * game; + GraphicsScene *scene; +}; + +//These transtion is true if one level has been completed and the player want to continue + class CustomSpaceTransition : public QKeyEventTransition +{ +public: + CustomSpaceTransition(QWidget *widget, PlayState *game, QEvent::Type type, int key); +protected: + virtual bool eventTest(QEvent *event) const; +private: + PlayState *game; + int key; }; #endif // STATES_H diff --git a/examples/animation/sub-attaq/sub-attaq.pro b/examples/animation/sub-attaq/sub-attaq.pro index 1456d0e..961a9b5 100644 --- a/examples/animation/sub-attaq/sub-attaq.pro +++ b/examples/animation/sub-attaq/sub-attaq.pro @@ -2,10 +2,10 @@ # Automatically generated by qmake (2.01a) Thu Oct 9 10:53:30 2008 # ##################################################################### TEMPLATE = app -TARGET = +TARGET = DEPENDPATH += . INCLUDEPATH += . -QT+= xml +QT += xml contains(QT_CONFIG, opengl):QT += opengl # Input @@ -21,7 +21,8 @@ HEADERS += boat.h \ boat_p.h \ submarine_p.h \ custompropertyanimation.h \ - qanimationstate.h + qanimationstate.h \ + progressitem.h SOURCES += boat.cpp \ bomb.cpp \ main.cpp \ @@ -33,6 +34,6 @@ SOURCES += boat.cpp \ animationmanager.cpp \ states.cpp \ custompropertyanimation.cpp \ - qanimationstate.cpp - + qanimationstate.cpp \ + progressitem.cpp RESOURCES += subattaq.qrc diff --git a/examples/animation/sub-attaq/submarine.cpp b/examples/animation/sub-attaq/submarine.cpp index 555617c..0f03efc 100644 --- a/examples/animation/sub-attaq/submarine.cpp +++ b/examples/animation/sub-attaq/submarine.cpp @@ -56,10 +56,10 @@ # include "qsequentialanimationgroup.h" # include "qpauseanimation.h" #else -#include <QPropertyAnimation> -#include <QStateMachine> -#include <QFinalState> -#include <QSequentialAnimationGroup> +#include <QtCore/QPropertyAnimation> +#include <QtCore/QStateMachine> +#include <QtCore/QFinalState> +#include <QtCore/QSequentialAnimationGroup> #endif static QAbstractAnimation *setupDestroyAnimation(SubMarine *sub) @@ -126,15 +126,15 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem * QState *moving = new QState(machine->rootState()); //This state is when the boat is moving from left to right - MovementState *movement = new MovementState(this,moving); + MovementState *movement = new MovementState(this, moving); //This state is when the boat is moving from left to right - ReturnState *rotation = new ReturnState(this,moving); + ReturnState *rotation = new ReturnState(this, moving); //This is the initial state of the moving root state moving->setInitialState(movement); - movement->addTransition(this, SIGNAL(subMarineStateChanged()),moving); + movement->addTransition(this, SIGNAL(subMarineStateChanged()), moving); //This is the initial state of the machine machine->setInitialState(moving); @@ -153,7 +153,7 @@ SubMarine::SubMarine(int type, const QString &name, int points, QGraphicsItem * destroyedState->setAnimation(setupDestroyAnimation(this)); //Play a nice animation when the submarine is destroyed - moving->addTransition(this, SIGNAL(subMarineDestroyed()),destroyedState); + moving->addTransition(this, SIGNAL(subMarineDestroyed()), destroyedState); //Transition to final state when the destroyed animation is finished destroyedState->addTransition(destroyedState, SIGNAL(animationFinished()), final); diff --git a/examples/animation/sub-attaq/submarine.h b/examples/animation/sub-attaq/submarine.h index 562b4cf..7ee587d 100644 --- a/examples/animation/sub-attaq/submarine.h +++ b/examples/animation/sub-attaq/submarine.h @@ -47,8 +47,8 @@ #include "qvariantanimation.h" #include "qgraphicswidget.h" #else -#include <QVariantAnimation> -#include <QGraphicsWidget> +#include <QtCore/QVariantAnimation> +#include <QtGui/QGraphicsWidget> #endif class PixmapItem; diff --git a/examples/animation/sub-attaq/submarine_p.h b/examples/animation/sub-attaq/submarine_p.h index 354a406..918e7f5 100644 --- a/examples/animation/sub-attaq/submarine_p.h +++ b/examples/animation/sub-attaq/submarine_p.h @@ -51,9 +51,9 @@ #if defined(QT_EXPERIMENTAL_SOLUTION) #include "qpropertyanimation.h" #else -#include <QPropertyAnimation> +#include <QtCore/QPropertyAnimation> #endif -#include <QGraphicsScene> +#include <QtGui/QGraphicsScene> //This state is describing when the boat is moving right class MovementState : public QAnimationState diff --git a/examples/animation/sub-attaq/torpedo.cpp b/examples/animation/sub-attaq/torpedo.cpp index b24948d..88f1112 100644 --- a/examples/animation/sub-attaq/torpedo.cpp +++ b/examples/animation/sub-attaq/torpedo.cpp @@ -52,9 +52,9 @@ #include "qstatemachine.h" #include "qfinalstate.h" #else -#include <QPropertyAnimation> -#include <QStateMachine> -#include <QFinalState> +#include <QtCore/QPropertyAnimation> +#include <QtCore/QStateMachine> +#include <QtCore/QFinalState> #endif Torpedo::Torpedo(QGraphicsItem * parent, Qt::WindowFlags wFlags) diff --git a/examples/animation/sub-attaq/torpedo.h b/examples/animation/sub-attaq/torpedo.h index 33bbc04..2e44e41 100644 --- a/examples/animation/sub-attaq/torpedo.h +++ b/examples/animation/sub-attaq/torpedo.h @@ -43,13 +43,13 @@ #define __TORPEDO__H__ //Qt -#include <QObject> +#include <QtCore/QObject> #if defined(QT_EXPERIMENTAL_SOLUTION) # include "qvariantanimation.h" # include "qgraphicswidget.h" #else -# include <QVariantAnimation> +# include <QtCore/QVariantAnimation> # include <QtGui/QGraphicsWidget> #endif |