summaryrefslogtreecommitdiffstats
path: root/kdirstat/kpacman.cpp
blob: c63a5846481c723ebbaaa9475a4b4cf157a737bd (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

/*
 *   File name:	kpacman.cpp
 *   Summary:	PacMan animation
 *   License:	LGPL - See file COPYING.LIB for details.
 *   Author:	Stefan Hundhammer <sh@suse.de>
 *
 *   Updated:	2004-03-29
 */


#include <unistd.h>
#include <stdlib.h>
#include <tqtimer.h>
#include <tqpixmap.h>
#include <kdebug.h>

#include "kpacman.h"


KPacManAnimation::KPacManAnimation( TQWidget *	widget,
				    int		size,
				    bool	randomStart )
{
    _widget		= widget;
    _size		= size;
    _randomStart	= randomStart;
    _brush		= TQBrush( TQt::yellow );
    _pos		= 0;
    _speed		= 4;
    _interval		= 100;

    _minMouth		= 10;
    _maxMouth		= 70;
    _mouthInc		= ( _maxMouth - _minMouth ) / 3;
    _mouth		= _minMouth;
    _pacManRect		= TQRect( 0, 0, 0, 0 );

    restart();
}


KPacManAnimation::~KPacManAnimation()
{
}


void
KPacManAnimation::restart()
{
    _justStarted = true;

    if ( _randomStart )
    {
	_goingRight = ( rand() > ( RAND_MAX / 2 ) );

	// Initial _pos is set in animate() since the width (upon which it
	// depends) is still unknown here.
    }
    else
    {
	_goingRight	= true;
	_pos		= 0;
    }

    // Care for initial display
    _time = _time.addMSecs( _interval + 1 );
}


void
KPacManAnimation::animate( TQPainter *	painter,
			   TQRect 	rect )
{
    if ( _time.elapsed() < _interval )
	return;

    _time.restart();


    // Make PacMan fit into height

    int size = _size <= rect.height() ? _size : rect.height();

    if ( rect.width() < size )		// No space to animate in?
	return;				// -> forget it!


    if ( _justStarted )
    {
	_justStarted = false;

	if ( _pacManRect.width() > 0 )
	    painter->eraseRect( _pacManRect );

	if ( _randomStart )
	{
	    // Set random initial position
	    // - this depends on the width which is unknown in the constructor.

	    _pos = (int) ( (rect.width() - size ) * ( (double) rand() / RAND_MAX) );
	}
    }
    else	// ! _justStarted
    {
	// Erase last PacMan

	if ( ! _goingRight )
	    _pacManRect.setX( _pacManRect.x() + _pacManRect.width() - _speed );

	_pacManRect.setWidth( _speed );
	painter->eraseRect( _pacManRect );
    }


    if ( _pos + size > rect.width() )	// Right edge reached?
    {
	// Notice: This can also happen when the rectangle is resized - i.e. it
	// really makes sense to do that right here rather than at the end of
	// this function!

	// Turn left

	_pos 		= rect.width() - size;
	_goingRight	= false;
	_mouth		= _minMouth;
    }
    else if ( _pos < 0 )	// Left edge reached?
    {
	// Turn right

	_pos 		= 0;
	_goingRight	= true;
	_mouth		= _minMouth;
    }


    // Draw PacMan (double-buffered)

    _pacManRect = TQRect( 0, 0, size, size );
    TQPixmap pixmap( size, size );
    pixmap.fill( painter->backgroundColor() );
    TQPainter p( &pixmap, _widget );
    p.setBrush( _brush );

    if ( _goingRight )
    {
	p.drawPie( _pacManRect,
		   _mouth * 16,				// arc (1/16 degrees)
		   ( 360 - 2 * _mouth ) * 16 );		// arc length (1/16 degrees)
    }
    else
    {
	p.drawPie( _pacManRect,
		   ( 180 + _mouth ) * 16,		// arc (1/16 degrees)
		   ( 360 - 2 * _mouth ) * 16 );		// arc length (1/16 degrees)
    }

    _pacManRect = TQRect( rect.x() + _pos,		// x
			 ( rect.height() - size ) / 2,	// y
			 size, size );			// width, height

    // Transfer pixmap into widget

#if 0
    TQPoint offset = painter->worldMatrix().map( _pacManRect.topLeft() );
    // kdDebug() << "bitBlt() to " << offset.x() << ", " << offset.y() << endl;
    bitBlt( _widget, offset, &pixmap );
#endif

    painter->drawPixmap( _pacManRect.topLeft(), pixmap );


    // Animate mouth for next turn

    _mouth += _mouthInc;

    if ( _mouth >= _maxMouth )		// max open reached
    {
	_mouth    = _maxMouth;
	_mouthInc = -_mouthInc;		// reverse direction
    }
    else if ( _mouth <= _minMouth )	// min open reached
    {
	_mouth    = _minMouth;
	_mouthInc = -_mouthInc;		// reverse direction
    }


    // Advance position for next turn

    if ( _goingRight )
	_pos += _speed;
    else
	_pos -= _speed;
}






KPacMan::KPacMan( TQWidget * 	parent,
		  int 		pacManSize,
		  bool		randomStart,
		  const char *	widgetName )
    : TQWidget( parent, widgetName )
{
    _pacManSize	= pacManSize;
    _pacMan 	= new KPacManAnimation( this, _pacManSize, randomStart );
    _timer	= 0;
    _interval	= 100;	// millisec
    _active	= false;
    _painter	= new TQPainter( this );
    _margin	= 1;
}


KPacMan::~KPacMan()
{
    if ( _painter )
	delete _painter;

    if ( _pacMan )
	delete _pacMan;
}


void
KPacMan::start()
{
    if ( ! _timer )
    {
	_timer = new TQTimer( this );
    }

    _pacMan->restart();

    if ( _timer )
    {
	_active = true;
	_timer->start( _interval );
	connect( _timer, TQT_SIGNAL( timeout() ),
		 this,   TQT_SLOT  ( animate() ) );
    }
}


void
KPacMan::stop()
{
    _active = false;

    if ( _timer )
	_timer->stop();

    repaint();
}


void
KPacMan::animate()
{
    repaint( false );
}


void
KPacMan::setInterval( int intervalMilliSec )
{
    _interval = intervalMilliSec;
    _pacMan->setInterval( _interval );

    if ( _timer )
	_timer->changeInterval( _interval );
}


void
KPacMan::paintEvent( TQPaintEvent *ev )
{
    TQWidget::paintEvent( ev );
    
    if ( _active )
    {
	_pacMan->animate( _painter, TQRect( _margin, 0, width() - _margin, height() ) );
    }
}


void
KPacMan::mouseReleaseEvent ( TQMouseEvent *ev )
{
    if ( _active )
    {
	if ( _pacMan->lastPacMan().contains( ev->pos() ) )
	    stop();
    }
}


TQSize
KPacMan::sizeHint() const
{
    return TQSize( 16 * _pacManSize,		// width - admittedly somewhat random
		  _pacManSize + 2 * _margin );	// height
}



// EOF