GameScene
This commit is contained in:
parent
0d774b5d48
commit
e75c8370eb
2 changed files with 92 additions and 0 deletions
64
cockatrice/src/gamescene.cpp
Normal file
64
cockatrice/src/gamescene.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include "gamescene.h"
|
||||
#include "player.h"
|
||||
#include "zoneviewlayout.h"
|
||||
|
||||
GameScene::GameScene(ZoneViewLayout *_zvLayout, QObject *parent)
|
||||
: QGraphicsScene(parent), zvLayout(_zvLayout)
|
||||
{
|
||||
connect(zvLayout, SIGNAL(sizeChanged()), this, SLOT(updateSceneSize()));
|
||||
addItem(zvLayout);
|
||||
}
|
||||
|
||||
void GameScene::updateSceneSize()
|
||||
{
|
||||
int sceneWidth = 0;
|
||||
int sceneHeight = 0;
|
||||
for (int i = 0; i < players.size(); ++i) {
|
||||
const QRectF br = players[i]->boundingRect();
|
||||
if (i)
|
||||
sceneHeight += playerAreaSpacing;
|
||||
sceneHeight += br.height();
|
||||
if (br.width() > sceneWidth)
|
||||
sceneWidth = br.width();
|
||||
}
|
||||
sceneWidth += zvLayout->size().width();
|
||||
qDebug(QString("updateSceneSize: w=%1 h=%2").arg(sceneWidth).arg(sceneHeight).toLatin1());
|
||||
setSceneRect(sceneRect().x(), sceneRect().y(), sceneWidth, sceneHeight);
|
||||
}
|
||||
|
||||
void GameScene::addPlayer(Player *player)
|
||||
{
|
||||
players << player;
|
||||
updateSceneSize();
|
||||
addItem(player);
|
||||
rearrangePlayers();
|
||||
}
|
||||
|
||||
void GameScene::removePlayer(Player *player)
|
||||
{
|
||||
players.removeAt(players.indexOf(player));
|
||||
removeItem(player);
|
||||
updateSceneSize();
|
||||
rearrangePlayers();
|
||||
}
|
||||
|
||||
void GameScene::rearrangePlayers()
|
||||
{
|
||||
QPointF base;
|
||||
qreal maxWidth = 0;
|
||||
Player *localPlayer = 0;
|
||||
for (int i = 0; i < players.size(); ++i) {
|
||||
QRectF br = players[i]->boundingRect();
|
||||
if (br.width() > maxWidth)
|
||||
maxWidth = br.width();
|
||||
if (!players[i]->getLocal()) {
|
||||
players[i]->setPos(base);
|
||||
// Change this for better multiplayer support.
|
||||
base += QPointF(0, br.height() + playerAreaSpacing);
|
||||
} else
|
||||
localPlayer = players[i];
|
||||
}
|
||||
if (localPlayer)
|
||||
localPlayer->setPos(base);
|
||||
zvLayout->setPos(QPointF(maxWidth, 0));
|
||||
}
|
28
cockatrice/src/gamescene.h
Normal file
28
cockatrice/src/gamescene.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef GAMESCENE_H
|
||||
#define GAMESCENE_H
|
||||
|
||||
#include <QGraphicsScene>
|
||||
#include <QList>
|
||||
|
||||
class Player;
|
||||
class ZoneViewLayout;
|
||||
|
||||
class GameScene : public QGraphicsScene {
|
||||
Q_OBJECT
|
||||
private:
|
||||
static const int playerAreaSpacing = 5;
|
||||
|
||||
QList<Player *> players;
|
||||
ZoneViewLayout *zvLayout;
|
||||
|
||||
void rearrangePlayers();
|
||||
public:
|
||||
GameScene(ZoneViewLayout *_zvLayout, QObject *parent = 0);
|
||||
public slots:
|
||||
void addPlayer(Player *player);
|
||||
void removePlayer(Player *player);
|
||||
private slots:
|
||||
void updateSceneSize();
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue