11 #include "kanimation.h"
16 KFrame::KFrame( double nx
, double ny
, int nf
)
33 current_frame_number
= 0;
34 setAutoDelete( TRUE
);
35 following
= new QStrList( TRUE
); // deep copy
36 following
->setAutoDelete( TRUE
);
44 KFrame
*KScene::nextFrame()
46 current_frame_number
++;
47 if ( current_frame_number
< count() )
48 return at( current_frame_number
);
53 KFrame
*KScene::currentFrame()
55 if ( current_frame_number
< count() )
56 return at( current_frame_number
);
61 void KScene::addFollowingScene( const QString scene_name
)
63 following
->append( scene_name
.latin1() );
66 void KScene::followingSceneName( QString
& scene_name
)
68 if ( following
->count() == 0 )
71 scene_name
= following
->at( rand() % following
->count() );
74 void KScene::setToStart()
76 current_frame_number
= 0;
83 KStoryBoard::KStoryBoard( const QString filename
) : QDict
<KScene
>()
90 if ( f
.open( IO_ReadOnly
) ) // file opened successfully
92 QTextStream
t( &f
); // use a text stream
94 kdDebug() << "loading the story board..." << endl
;
96 while ( !t
.eof() ) // until end of file...
98 QString s
= t
.readLine(); // line of text excluding '\n'
99 QString dbgString
= s
;
100 int pos
= s
.find ( "#");
105 dbgString
= s
.mid( pos
+1 );
106 kdDebug() << "Comment: " << dbgString
<< endl
;
111 QString command_word
= s
.section( " ", 0, 0 );
112 // new scene beginning
113 if ( command_word
== "name" )
115 QString scene_name
= s
.section( " ", 1, 1 );
116 c_scene
= new KScene();
117 insert( scene_name
, c_scene
);
118 kdDebug() << "scene found: " << scene_name
<< endl
;
121 if ( c_scene
&& command_word
== "move" )
123 QString parameters
= s
.section( " ", 1, 1 );
124 c_scene
->append( new KFrame(
125 parameters
.section( ",", 0, 0 ).toDouble(),
126 parameters
.section( ",", 1, 1 ).toDouble(),
127 parameters
.section( ",", 2, 2 ).toInt() ) );
128 kdDebug() << parameters
<< endl
;
130 // new following scene
131 if ( c_scene
&& command_word
== "following" )
133 kdDebug() << "following st..." << endl
;
134 c_scene
->addFollowingScene( s
.section( " ", 1, 1 ) );
140 // TODO check, if all scenes called by "following" exist
144 KStoryBoard::~KStoryBoard()
148 KFrame
*KStoryBoard::currentFrame()
151 return current_scene
->currentFrame();
156 KFrame
*KStoryBoard::nextFrame()
160 if ( current_scene
->nextFrame() )
161 return current_scene
->currentFrame();
165 current_scene
->followingSceneName( scene_name
);
166 current_scene
= find( scene_name
);
169 current_scene
->setToStart();
170 return current_scene
->currentFrame();
182 void KStoryBoard::setToStart()
184 current_scene
= find( "init" );
186 current_scene
->setToStart();
192 KAnimation::KAnimation( const QString story_filename
,
193 QCanvasPixmapArray
* a
, QCanvas
* canvas
) :
194 QCanvasSprite( a
, canvas
)
196 story
= new KStoryBoard( story_filename
);
199 KAnimation::~KAnimation()
204 void KAnimation::advance( int phase
)
208 KFrame
* f_
= story
->nextFrame();
210 move( f_
->x
, f_
->y
, f_
->f
);
214 void KAnimation::setToStart()
217 KFrame
*f_
= story
->currentFrame();
219 move( f_
->x
, f_
->y
, f_
->f
);