~kubuntu-members/killbots/4.11

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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 *  Copyright 2007-2009  Parker Coates <coates@kde.org>
 *
 *  This file is part of Killbots.
 *
 *  Killbots is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Killbots is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Killbots. If not, see <http://www.gnu.org/licenses/>.
 */

#include "scene.h"

#include "numericdisplayitem.h"
#include "renderer.h"
#include "settings.h"

#include <KGamePopupItem>

#include <KDE/KDebug>

#include <QtGui/QPainter>
#include <QtGui/QGraphicsSceneMouseEvent>
#include <QtGui/QGraphicsView>

#include <cmath>


Killbots::Scene::Scene( QObject * parent )
  : QGraphicsScene( parent ),
    m_hero( 0 ),
    m_rows( 0 ),
    m_columns( 0 )
{
	setItemIndexMethod( QGraphicsScene::NoIndex );
}


Killbots::Scene::~Scene()
{
}


void Killbots::Scene::addNumericDisplay( NumericDisplayItem * displayItem )
{
	addItem( displayItem );
	displayItem->setPos( -1000000, 0 );
	m_numericDisplays << displayItem;
}


void Killbots::Scene::setGridSize(int rows, int columns)
{
	if ( m_rows != rows || m_columns != columns )
	{
		m_rows = rows;
		m_columns = columns;
	}
}


void Killbots::Scene::forgetHero()
{
	m_hero = 0;
}


Killbots::Sprite * Killbots::Scene::createSprite( SpriteType type, QPoint position )
{
	Sprite * sprite = new Sprite();
	sprite->setSpriteType( type );
	sprite->setRenderSize( m_cellSize );
	sprite->enqueueGridPos( position );
	updateSpritePos( sprite, position );
	sprite->scale( 0, 0 );
	// A bit of a hack, but we use the sprite type for stacking order.
	sprite->setZValue( type );


	addItem( sprite );

	if ( type == Hero )
		m_hero = sprite;

	return sprite;
}


void Killbots::Scene::animateSprites( const QList<Sprite *> & newSprites,
                                      const QList<Sprite *> & slidingSprites,
                                      const QList<Sprite *> & teleportingSprites,
                                      const QList<Sprite *> & destroyedSprites,
                                      qreal value
                                    ) const
{
	static bool halfDone = false;

	if ( value == 0.0 )
	{
		halfDone = false;
	}
	else if ( value < 1.0 )
	{
		foreach ( Sprite * sprite, newSprites )
		{
			sprite->resetTransform();
			sprite->scale( value, value );
		}

		foreach ( Sprite * sprite, slidingSprites )
		{
			QPointF posInGridCoordinates = value * QPointF( sprite->nextGridPos() - sprite->currentGridPos() ) + sprite->currentGridPos();
			sprite->setPos( QPointF( posInGridCoordinates.x() * m_cellSize.width(), posInGridCoordinates.y() * m_cellSize.height() ) );
		}

		qreal scaleFactor = value < 0.5
		                  ? 1.0 - 2 * value
		                  : 2 * value - 1.0;

		if ( !halfDone && value >= 0.5 )
		{
			halfDone = true;
			foreach ( Sprite * sprite, teleportingSprites )
				updateSpritePos( sprite, sprite->nextGridPos() );
		}

		foreach ( Sprite * sprite, teleportingSprites )
		{
			sprite->resetTransform();
			sprite->scale( scaleFactor, scaleFactor );
		}

		foreach ( Sprite * sprite, destroyedSprites )
		{
			sprite->resetTransform();
			sprite->scale( 1 - value, 1 - value );
			sprite->rotate( value * 180 );
		}
	}
	else
	{
		foreach ( Sprite * sprite, newSprites + slidingSprites + teleportingSprites )
		{
			sprite->resetTransform();
			sprite->advanceGridPosQueue();
			updateSpritePos( sprite, sprite->currentGridPos() );
		}

		qDeleteAll( destroyedSprites );
	}
}


void Killbots::Scene::doLayout()
{
	QSize size = views().first()->size();

	// If no game has been started
	if ( m_rows == 0 || m_columns == 0 )
	{
		setSceneRect( QRectF( QPointF( 0, 0 ), size ) );
		return;
	}

	kDebug() << "Laying out scene at" << size;

	// Make certain layout properties proportional to the scene height,
	// but clamp them between reasonable values. There's probably room for more
	// tweaking here.
	const int baseDimension = qMin( size.width(), size.height() ) / 35;
	const int spacing = qBound( 5, baseDimension, 15 );
	const int newFontPixelSize = qBound( QFontInfo( QFont() ).pixelSize(), baseDimension, 25 );
	const qreal aspectRatio = Renderer::self()->aspectRatio();

	QSize displaySize;
	// If the font size has changed, resize all the displays (visible or not).
	if ( m_numericDisplays.first()->font().pixelSize() != newFontPixelSize )
	{
		QFont font;
		font.setPixelSize( newFontPixelSize  );

		foreach ( NumericDisplayItem * display, m_numericDisplays )
		{
			display->setFont( font );
			displaySize = displaySize.expandedTo( display->preferredSize() );
		}
		foreach ( NumericDisplayItem * display, m_numericDisplays )
			display->setRenderSize( displaySize );
	}
	else
	{
		displaySize = m_numericDisplays.first()->boundingRect().size().toSize();
	}

	// The rest of the function deals only with a list of visible displays.
	QList<NumericDisplayItem *> visibleDisplays;
	foreach ( NumericDisplayItem * display, m_numericDisplays )
	{
		if ( display->isVisible() )
			visibleDisplays << display;
	}

	// Determine the total width required to arrange the displays horizontally.
	const int widthOfDisplaysOnTop = visibleDisplays.size() * displaySize.width()
	                           + ( visibleDisplays.size() - 1 ) * spacing;

	// The displays can either be placed centred, across the top of the
	// scene or top-aligned, down the side of the scene. We first calculate
	// what the cell size would be for both options.
	int availableWidth = size.width() - 3 * spacing - displaySize.width();
	int availableHeight = size.height() - 2 * spacing;
	const qreal cellWidthSide = ( availableWidth / m_columns < availableHeight / m_rows * aspectRatio )
	                          ? availableWidth / m_columns
	                          : availableHeight / m_rows * aspectRatio;

	availableWidth = size.width() - 2 * spacing;
	availableHeight = size.height() - 3 * spacing - displaySize.height();
	const qreal cellWidthTop = ( availableWidth / m_columns < availableHeight / m_rows * aspectRatio )
	                         ? availableWidth / m_columns
	                         : availableHeight / m_rows * aspectRatio;

	// If placing the displays on top would result in larger cells, we take
	// that option, but only if the displays would actually fit.
	const bool displaysOnTop = ( cellWidthTop > cellWidthSide && size.width() > widthOfDisplaysOnTop );
	const qreal newCellWidth = displaysOnTop ? cellWidthTop : cellWidthSide;
	m_cellSize = QSize( qRound( newCellWidth ), qRound( newCellWidth / aspectRatio ) );

	foreach ( QGraphicsItem * item, items() )
	{
		Sprite * sprite = qgraphicsitem_cast<Sprite *>( item );
		if ( sprite )
		{
			sprite->setRenderSize( m_cellSize );
			updateSpritePos( sprite, sprite->currentGridPos() );
		}
	}

	if ( displaysOnTop )
	{
		// Set the sceneRect to centre the grid if possible, but ensure the display items are visible
		const qreal sceneRectXPos = -( size.width() - m_cellSize.width() * ( m_columns - 1 ) ) / 2.0;
		const qreal centeredYPos = - ( size.height() - m_cellSize.height() * ( m_rows - 1 ) ) / 2.0;
		const qreal indentedYPos = - ( m_cellSize.height() / 2.0 + 2 * spacing + displaySize.height() );
		const qreal sceneRectYPos = qMin( centeredYPos, indentedYPos );

		// Position the display items centered at the top of the scene
		const qreal displayYPos = ( sceneRectYPos - ( displaySize.height() + m_cellSize.height() / 2.0 ) ) / 2;

		int xPos = sceneRectXPos + ( size.width() - widthOfDisplaysOnTop ) / 2.0;
		foreach ( NumericDisplayItem * display, visibleDisplays )
		{
			display->setPos( xPos, displayYPos );
			xPos += displaySize.width() + spacing;
		}

		setSceneRect( QRectF( sceneRectXPos, sceneRectYPos, size.width(), size.height() ) );
	}
	else
	{
		qreal sceneRectXPos;
		const qreal centeredXPos = - ( size.width() - m_cellSize.width() * ( m_columns - 1 ) ) / 2.0;
		const qreal sceneRectYPos = -( size.height() - m_cellSize.height() * ( m_rows - 1 ) ) / 2.0;
		qreal displayXPos;

		// If the application layout is LTR, place the displays on left,
		// otherwise, place them on the right.
		if ( views().first()->layoutDirection() == Qt::LeftToRight )
		{
			// Set the sceneRect to centre the grid if possible, but ensure the display items are visible
			const qreal indentedXPos = - ( m_cellSize.width() / 2.0 + 2 * spacing + displaySize.width() );
			sceneRectXPos = qMin( centeredXPos, indentedXPos );

			// Position the display items to the left of the grid
			displayXPos = - ( spacing + displaySize.width() + m_cellSize.width() / 2 );
		}
		else
		{
			// Set the sceneRect to centre the grid if possible, but ensure the display items are visible
			const qreal indentedXPos = ( m_cellSize.width() * m_columns + 1 * spacing + displaySize.width() ) - size.width();
			sceneRectXPos = qMax( centeredXPos, indentedXPos );

			// Position the display items to the right of the grid
			displayXPos = m_cellSize.width() * ( m_columns - 0.5 ) + spacing;
		}

		int yPos = -m_cellSize.height() / 2;
		foreach ( NumericDisplayItem * display, visibleDisplays )
		{
			display->setPos( displayXPos, yPos );
			yPos += displaySize.height() + spacing;
		}

		setSceneRect( QRectF( sceneRectXPos, sceneRectYPos, size.width(), size.height() ) );
	}

	// Update the scene background
	QPainter p;
	QRect gridRect( -sceneRect().x() - m_cellSize.width() / 2,
	                -sceneRect().y() - m_cellSize.height() / 2,
	                m_columns * m_cellSize.width(),
	                m_rows * m_cellSize.height()
	              );

	QPixmap unrotated = Renderer::self()->spritePixmap( "background", size );
	p.begin( &unrotated );
	p.drawTiledPixmap( gridRect, Renderer::self()->spritePixmap( "cell", m_cellSize ) );
	p.end();

	// The background brush begins painting at 0,0 but our sceneRect doesn't
	// start at 0,0 so we have to "rotate" the pixmap so that it looks right
	// when tiled.
	QPixmap background( size );
	background.fill( Qt::transparent );
	p.begin( &background );
	p.drawTiledPixmap( background.rect(), unrotated, -sceneRect().topLeft().toPoint() );
	p.end();

	setBackgroundBrush( background );

	update();
}


void Killbots::Scene::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
	getMouseDirection( event->scenePos() );
	QGraphicsScene::mouseMoveEvent( event );
}


void Killbots::Scene::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
{
	HeroAction actionFromPosition = getMouseDirection( event->scenePos() );

	if ( actionFromPosition != NoAction )
	{
		Settings::ClickAction userAction = Settings::Nothing;

		if ( event->button() == Qt::LeftButton )
		{
			if ( event->modifiers() & Qt::ControlModifier )
				userAction = Settings::middleClickAction();
			else
				userAction = Settings::Step;
		}
		else if ( event->button() == Qt::RightButton )
			userAction = Settings::rightClickAction();
		else if ( event->button() == Qt::MidButton )
			userAction = Settings::middleClickAction();

		if ( userAction == Settings::Step )
			emit clicked( actionFromPosition );
		else if ( userAction == Settings::RepeatedStep )
			emit clicked( -actionFromPosition - 1 );
		else if ( userAction == Settings::Teleport )
			emit clicked( Teleport );
		else if ( userAction == Settings::TeleportSafely )
			emit clicked( TeleportSafely );
		else if ( userAction == Settings::TeleportSafelyIfPossible )
			emit clicked( TeleportSafelyIfPossible );
		else if ( userAction == Settings::WaitOutRound )
			emit clicked( WaitOutRound );
	}

	QGraphicsScene::mouseReleaseEvent( event );
}


Killbots::HeroAction Killbots::Scene::getMouseDirection( QPointF cursorPosition ) const
{
	HeroAction result;
	const bool heroOnScreen = m_hero && sceneRect().contains( m_hero->sceneBoundingRect() );

	if ( heroOnScreen && !popupAtPosition( cursorPosition ) )
	{
		if ( m_hero->sceneBoundingRect().contains( cursorPosition ) )
			result = Hold;
		else
		{
			const qreal piOver4 = 0.78539816339744830961566L;

			QPointF delta = cursorPosition - m_hero->sceneBoundingRect().center();
			int direction = qRound( atan2( -delta.y(), delta.x() ) / piOver4 );
			if ( direction < 0 )
				direction += 8;

			result = static_cast<HeroAction>( direction );
		}

		views().first()->setCursor( Renderer::self()->cursorFromAction( result ) );
	}
	else
	{
		views().first()->unsetCursor();
		result = NoAction;
	}

	return result;
}


bool Killbots::Scene::popupAtPosition( QPointF position ) const
{
	foreach ( QGraphicsItem * item, items( position ) )
	{
		if ( dynamic_cast<KGamePopupItem *>( item ) != 0 )
			return true;
	}
	return false;
}


void Killbots::Scene::updateSpritePos( Sprite * sprite, QPoint gridPosition ) const
{
	sprite->setPos( gridPosition.x() * m_cellSize.width(), gridPosition.y() * m_cellSize.height() );
}

#include "moc_scene.cpp"