summaryrefslogtreecommitdiffstats
path: root/kpercentage/kpercentage/kanimation.cpp
blob: 93974af35e1e69c7263c2a2eec8d4f941c8c69fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// C/C++ includes
#include <stdlib.h>

// Qt includes
#include <tqfile.h>

// KDE includes
#include <kdebug.h>

// local includes
#include "kanimation.h"

///////////
// KFrame

KFrame::KFrame( double nx, double ny, int nf )
{
	x = nx;
	y = ny;
	f = nf;
}

KFrame::~KFrame()
{
}


////////////
// KScene

KScene::KScene()
{
	current_frame_number = 0;
	setAutoDelete( TRUE );
	following = new TQStrList( TRUE ); // deep copy
	following->setAutoDelete( TRUE );
}

KScene::~KScene()
{
	delete following;
}

KFrame *KScene::nextFrame()
{
	current_frame_number++;
	if ( current_frame_number < count() )
		return at( current_frame_number );
	else
		return 0;
}

KFrame *KScene::currentFrame()
{
	if ( current_frame_number < count() )
		return at( current_frame_number );
	else
		return 0;
}

void KScene::addFollowingScene( const TQString scene_name )
{
	following->append( scene_name.latin1() );
}

void KScene::followingSceneName( TQString& scene_name )
{
	if ( following->count() == 0 )
		scene_name = "";
	else
		scene_name = following->at( rand() % following->count() );
}

void KScene::setToStart()
{
	current_frame_number = 0;
}


/////////////
// KStoryBoard

KStoryBoard::KStoryBoard( const TQString filename ) : TQDict<KScene>()
{
	current_scene = 0;

	TQFile f( filename );
	KScene *c_scene = 0;

	if ( f.open( IO_ReadOnly ) )           // file opened successfully
	{
		TQTextStream t( &f );        // use a text stream

		kdDebug() << "loading the story board..." << endl;

		while ( !t.eof() )               // until end of file...
		{
			TQString s = t.readLine();       // line of text excluding '\n'
			TQString dbgString = s;
			int pos = s.find ( "#");
			if (pos==-1)
				pos = s.find ( ";");
			if ( pos > -1 )
			{
				dbgString = s.mid( pos +1 );
				kdDebug() << "Comment: " << dbgString << endl;
				s = s.left( pos );
			}
			if ( !s.isEmpty() )
			{
					TQString command_word = s.section( " ", 0, 0 );
					// new scene beginning
					if ( command_word == "name" )
					{
						TQString scene_name = s.section( " ", 1, 1 );
						c_scene = new KScene();
						insert( scene_name, c_scene );
						kdDebug() << "scene found: " << scene_name << endl;
					}
					// new frame
					if ( c_scene && command_word == "move" )
					{
						TQString parameters = s.section( " ", 1, 1 );
						c_scene->append( new KFrame(
									parameters.section( ",", 0, 0 ).toDouble(),
									parameters.section( ",", 1, 1 ).toDouble(),
									parameters.section( ",", 2, 2 ).toInt() ) );
						kdDebug() << parameters << endl;
					}
					// new following scene
					if ( c_scene && command_word == "following" )
					{
						kdDebug() << "following st..." << endl;
						c_scene->addFollowingScene( s.section( " ", 1, 1 ) );
					}
				}
			}
		}
		f.close();
		// TODO check, if all scenes called by "following" exist
		setToStart();
	}

	KStoryBoard::~KStoryBoard()
	{
	}

	KFrame *KStoryBoard::currentFrame()
	{
		if ( current_scene )
			return current_scene->currentFrame();
		else
			return 0;
	}

	KFrame *KStoryBoard::nextFrame()
	{
		if ( current_scene )
		{
			if ( current_scene->nextFrame() )
				return current_scene->currentFrame();
			else
			{
				TQString scene_name;
				current_scene->followingSceneName( scene_name );
				current_scene = find( scene_name );
				if ( current_scene )
				{
					current_scene->setToStart();
					return current_scene->currentFrame();
				}
				else
					return 0;
			}
		}
		else
		{
			return 0;
		}
	}

	void KStoryBoard::setToStart()
	{
		current_scene = find( "init" );
		if ( current_scene )
			current_scene->setToStart();
	}

	//////////
	// KAnimation

	KAnimation::KAnimation( const TQString story_filename,
	                        TQCanvasPixmapArray * a, TQCanvas * canvas ) :
			TQCanvasSprite( a, canvas )
	{
		story = new KStoryBoard( story_filename );
	}

	KAnimation::~KAnimation()
	{
		delete story;
	}

	void KAnimation::advance( int phase )
	{
		if ( phase == 1 )
		{
			KFrame * f_ = story->nextFrame();
			if ( f_ )
				move( f_->x, f_->y, f_->f );
		}
	}

	void KAnimation::setToStart()
	{
		story->setToStart();
		KFrame *f_ = story->currentFrame();
		if ( f_ )
			move( f_->x, f_->y, f_->f );
	}