doc/src/examples/stickman.qdoc
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /*!
       
    43     \example animation/stickman
       
    44     \title Stickman Example
       
    45 
       
    46     The Stickman example shows how to animate transitions in a state machine to implement key frame 
       
    47     animations.
       
    48 
       
    49     \image stickman-example.png
       
    50     
       
    51     In this example, we will write a small application which animates the joints in a skeleton and
       
    52     projects a stickman figure on top. The stickman can be either "alive" or "dead", and when in the
       
    53     "alive" state, he can be performing different actions defined by key frame animations. 
       
    54     
       
    55     Animations are implemented as composite states. Each child state of the animation state 
       
    56     represents a frame in the animation by setting the position of each joint in the stickman's 
       
    57     skeleton to the positions defined for the particular frame. The frames are then bound together 
       
    58     with animated transitions that trigger on the source state's polished() signal. Thus, the 
       
    59     machine will enter the state representing the next frame in the animation immediately after it
       
    60     has finished animating into the previous frame.
       
    61     
       
    62     \image stickman-example1.png
       
    63         
       
    64     The states for an animation is constructed by reading a custom animation file format and 
       
    65     creating states that assign values to the the "position" properties of each of the nodes in the 
       
    66     skeleton graph. 
       
    67     
       
    68     \snippet examples/animation/stickman/lifecycle.cpp 1
       
    69     
       
    70     The states are then bound together with signal transitions that listen to the polished() signal.
       
    71     
       
    72     \snippet examples/animation/stickman/lifecycle.cpp 2
       
    73     
       
    74     The last frame state is given a transition to the first one, so that the animation will loop
       
    75     until it is interrupted when a transition out from the animation state is taken. To get smooth 
       
    76     animations between the different key frames, we set a default animation on the state machine. 
       
    77     This is a parallel animation group which contains animations for all the "position" properties 
       
    78     and will be selected by default when taking any transition that leads into a state that assigns 
       
    79     values to these properties.
       
    80     
       
    81     \snippet examples/animation/stickman/lifecycle.cpp 3
       
    82     
       
    83     Several such animation states are constructed, and are placed together as children of a top 
       
    84     level "alive" state which represents the stickman life cycle. Transitions go from the parent
       
    85     state to the child state to ensure that each of the child states inherit them. 
       
    86     
       
    87     \image stickman-example2.png
       
    88     
       
    89     This saves us the effort of connect every state to every state with identical transitions. The 
       
    90     state machine  makes sure that transitions between the key frame animations are also smooth by 
       
    91     applying the default animation when interrupting one and starting another.
       
    92         
       
    93     Finally, there is a transition out from the "alive" state and into the "dead" state. This is 
       
    94     a custom transition type called LightningSrikesTransition which samples every second and 
       
    95     triggers at random (one out of fifty times on average.) 
       
    96     
       
    97     \snippet examples/animation/stickman/lifecycle.cpp 4
       
    98     
       
    99     When it triggers, the machine will first enter a "lightningBlink" state which uses a timer to 
       
   100     pause for a brief period of time while the background color of the scene is white. This gives us 
       
   101     a flash effect when the lightning strikes.
       
   102     
       
   103     \snippet examples/animation/stickman/lifecycle.cpp 5
       
   104     
       
   105     We start and stop a QTimer object when entering and exiting the state. Then we transition into
       
   106     the "dead" state when the timer times out.
       
   107     
       
   108     \snippet examples/animation/stickman/lifecycle.cpp 0
       
   109     
       
   110     When the machine is in the "dead" state, it will be unresponsive. This is because the "dead" 
       
   111     state has no transitions leading out.
       
   112     
       
   113     \image stickman-example3.png
       
   114     
       
   115 */