moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kpercentage / kpercentage / kanimation.cpp
bloba0e77910fac189180ae28a980df8bab99c50a2b3
1 // C/C++ includes
2 #include <stdlib.h>
4 // Qt includes
5 #include <qfile.h>
7 // KDE includes
8 #include <kdebug.h>
10 // local includes
11 #include "kanimation.h"
13 ///////////
14 // KFrame
16 KFrame::KFrame( double nx, double ny, int nf )
18 x = nx;
19 y = ny;
20 f = nf;
23 KFrame::~KFrame()
28 ////////////
29 // KScene
31 KScene::KScene()
33 current_frame_number = 0;
34 setAutoDelete( TRUE );
35 following = new QStrList( TRUE ); // deep copy
36 following->setAutoDelete( TRUE );
39 KScene::~KScene()
41 delete following;
44 KFrame *KScene::nextFrame()
46 current_frame_number++;
47 if ( current_frame_number < count() )
48 return at( current_frame_number );
49 else
50 return 0;
53 KFrame *KScene::currentFrame()
55 if ( current_frame_number < count() )
56 return at( current_frame_number );
57 else
58 return 0;
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 )
69 scene_name = "";
70 else
71 scene_name = following->at( rand() % following->count() );
74 void KScene::setToStart()
76 current_frame_number = 0;
80 /////////////
81 // KStoryBoard
83 KStoryBoard::KStoryBoard( const QString filename ) : QDict<KScene>()
85 current_scene = 0;
87 QFile f( filename );
88 KScene *c_scene = 0;
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 ( "#");
101 if (pos==-1)
102 pos = s.find ( ";");
103 if ( pos > -1 )
105 dbgString = s.mid( pos +1 );
106 kdDebug() << "Comment: " << dbgString << endl;
107 s = s.left( pos );
109 if ( !s.isEmpty() )
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;
120 // new frame
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 ) );
139 f.close();
140 // TODO check, if all scenes called by "following" exist
141 setToStart();
144 KStoryBoard::~KStoryBoard()
148 KFrame *KStoryBoard::currentFrame()
150 if ( current_scene )
151 return current_scene->currentFrame();
152 else
153 return 0;
156 KFrame *KStoryBoard::nextFrame()
158 if ( current_scene )
160 if ( current_scene->nextFrame() )
161 return current_scene->currentFrame();
162 else
164 QString scene_name;
165 current_scene->followingSceneName( scene_name );
166 current_scene = find( scene_name );
167 if ( current_scene )
169 current_scene->setToStart();
170 return current_scene->currentFrame();
172 else
173 return 0;
176 else
178 return 0;
182 void KStoryBoard::setToStart()
184 current_scene = find( "init" );
185 if ( current_scene )
186 current_scene->setToStart();
189 //////////
190 // KAnimation
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()
201 delete story;
204 void KAnimation::advance( int phase )
206 if ( phase == 1 )
208 KFrame * f_ = story->nextFrame();
209 if ( f_ )
210 move( f_->x, f_->y, f_->f );
214 void KAnimation::setToStart()
216 story->setToStart();
217 KFrame *f_ = story->currentFrame();
218 if ( f_ )
219 move( f_->x, f_->y, f_->f );