summaryrefslogtreecommitdiffstats
path: root/examples/animation/stickman
diff options
context:
space:
mode:
authorEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-14 14:50:23 (GMT)
committerEskil Abrahamsen Blomfeldt <eblomfel@trolltech.com>2009-05-14 14:52:33 (GMT)
commit9400e522d7b89025157657ec87fb1631fc6bf4de (patch)
treef7497987af5a261e740febd9b51e943eac2919d9 /examples/animation/stickman
parent889316a3f57b57160e2a08fc41def774d56e288c (diff)
downloadQt-9400e522d7b89025157657ec87fb1631fc6bf4de.zip
Qt-9400e522d7b89025157657ec87fb1631fc6bf4de.tar.gz
Qt-9400e522d7b89025157657ec87fb1631fc6bf4de.tar.bz2
Remove the connectByAnimation() function and add some documentation for the Stickman example
The connectByAnimation() function is no longer needed since we have default animations. The docs are unfinished.
Diffstat (limited to 'examples/animation/stickman')
-rw-r--r--examples/animation/stickman/lifecycle.cpp48
-rw-r--r--examples/animation/stickman/lifecycle.h2
2 files changed, 22 insertions, 28 deletions
diff --git a/examples/animation/stickman/lifecycle.cpp b/examples/animation/stickman/lifecycle.cpp
index 6b69a26..1feb31d 100644
--- a/examples/animation/stickman/lifecycle.cpp
+++ b/examples/animation/stickman/lifecycle.cpp
@@ -82,6 +82,7 @@ private:
Qt::Key m_key;
};
+//! [4]
class LightningStrikesTransition: public QEventTransition
{
public:
@@ -97,6 +98,7 @@ public:
return QEventTransition::eventTest(e) && ((qrand() % 50) == 0);
}
};
+//! [4]
LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
: m_stickMan(stickMan), m_keyReceiver(keyReceiver)
@@ -110,7 +112,10 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
}
// Set up intial state graph
+//! [3]
m_machine = new QStateMachine();
+ m_machine->addDefaultAnimation(m_animationGroup);
+//! [3]
m_alive = new QState(m_machine->rootState());
m_alive->setObjectName("alive");
@@ -122,11 +127,13 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
lightningBlink->assignProperty(m_stickMan, "fillColor", Qt::white);
lightningBlink->assignProperty(m_stickMan, "isDead", true);
+//! [5]
QTimer *timer = new QTimer(lightningBlink);
timer->setSingleShot(true);
timer->setInterval(100);
QObject::connect(lightningBlink, SIGNAL(entered()), timer, SLOT(start()));
QObject::connect(lightningBlink, SIGNAL(exited()), timer, SLOT(stop()));
+//! [5]
m_dead = new QState(m_machine->rootState());
m_dead->assignProperty(m_stickMan->scene(), "backgroundBrush", Qt::black);
@@ -141,9 +148,9 @@ LifeCycle::LifeCycle(StickMan *stickMan, GraphicsView *keyReceiver)
m_alive->setInitialState(m_idle);
// Lightning strikes at random
-//! [0]
m_alive->addTransition(new LightningStrikesTransition(lightningBlink));
- connectByAnimation(lightningBlink, m_dead, new QSignalTransition(timer, SIGNAL(timeout())));
+//! [0]
+ lightningBlink->addTransition(timer, SIGNAL(timeout()), m_dead);
//! [0]
m_machine->setInitialState(m_alive);
@@ -160,23 +167,10 @@ void LifeCycle::start()
m_machine->start();
}
-//! [1]
-void LifeCycle::connectByAnimation(QState *s1, QAbstractState *s2, QAbstractTransition *transition)
-{
- if (transition == 0) {
- transition = s1->addTransition(s2);
- } else {
- transition->setTargetState(s2);
- s1->addTransition(transition);
- }
- transition->addAnimation(m_animationGroup);
-}
-//! [1]
-
void LifeCycle::addActivity(const QString &fileName, Qt::Key key)
{
QState *state = makeState(m_alive, fileName);
- connectByAnimation(m_alive, state, new KeyPressTransition(m_keyReceiver, key));
+ m_alive->addTransition(new KeyPressTransition(m_keyReceiver, key, state));
}
QState *LifeCycle::makeState(QState *parentState, const QString &animationFileName)
@@ -193,26 +187,28 @@ QState *LifeCycle::makeState(QState *parentState, const QString &animationFileNa
const int frameCount = animation.totalFrames();
QState *previousState = 0;
for (int i=0; i<frameCount; ++i) {
- QState *frameState = new QState(topLevel);
- frameState->setObjectName(QString::fromLatin1("frame %0").arg(i));
-
animation.setCurrentFrame(i);
+
+//! [1]
+ QState *frameState = new QState(topLevel);
const int nodeCount = animation.nodeCount();
for (int j=0; j<nodeCount; ++j)
frameState->assignProperty(m_stickMan->node(j), "position", animation.nodePos(j));
+//! [1]
- if (previousState == 0) {
+ frameState->setObjectName(QString::fromLatin1("frame %0").arg(i));
+ if (previousState == 0)
topLevel->setInitialState(frameState);
- } else {
- connectByAnimation(previousState, frameState,
- new QSignalTransition(previousState, SIGNAL(polished())));
- }
+ else
+//! [2]
+ previousState->addTransition(previousState, SIGNAL(polished()), frameState);
+//! [2]
+
previousState = frameState;
}
// Loop
- connectByAnimation(previousState, topLevel->initialState(),
- new QSignalTransition(previousState, SIGNAL(polished())));
+ previousState->addTransition(previousState, SIGNAL(polished()), topLevel->initialState());
return topLevel;
diff --git a/examples/animation/stickman/lifecycle.h b/examples/animation/stickman/lifecycle.h
index e520402..8fd0fb2 100644
--- a/examples/animation/stickman/lifecycle.h
+++ b/examples/animation/stickman/lifecycle.h
@@ -63,8 +63,6 @@ public:
void start();
private:
- void connectByAnimation(QState *s1, QAbstractState *s2,
- QAbstractTransition *transition = 0);
QState *makeState(QState *parentState, const QString &animationFileName);
StickMan *m_stickMan;