mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-04 20:43:54 -07:00
some cleanups and fixes
This commit is contained in:
parent
6ba2e98315
commit
564ae4db84
27 changed files with 371 additions and 206 deletions
|
|
@ -37,7 +37,7 @@
|
|||
CardDatabase *db;
|
||||
QTranslator *translator;
|
||||
|
||||
void myMessageOutput(QtMsgType type, const char *msg)
|
||||
void myMessageOutput(QtMsgType /*type*/, const char *msg)
|
||||
{
|
||||
static FILE *f = NULL;
|
||||
if (!f)
|
||||
|
|
|
|||
21
cockatrice/src/pingpixmapgenerator.cpp
Normal file
21
cockatrice/src/pingpixmapgenerator.cpp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#include "pingpixmapgenerator.h"
|
||||
#include <QPainter>
|
||||
|
||||
QPixmap PingPixmapGenerator::generatePixmap(int size, int value, int max)
|
||||
{
|
||||
QPixmap pixmap(size, size);
|
||||
pixmap.fill(Qt::transparent);
|
||||
QPainter painter(&pixmap);
|
||||
QColor color;
|
||||
if (max == -1)
|
||||
color = Qt::black;
|
||||
else
|
||||
color.setHsv(120 * (1.0 - ((double) value / max)), 255, 255);
|
||||
|
||||
QRadialGradient g(QPointF((double) pixmap.width() / 2, (double) pixmap.height() / 2), qMin(pixmap.width(), pixmap.height()) / 2.0);
|
||||
g.setColorAt(0, color);
|
||||
g.setColorAt(1, Qt::transparent);
|
||||
painter.fillRect(0, 0, pixmap.width(), pixmap.height(), QBrush(g));
|
||||
|
||||
return pixmap;
|
||||
}
|
||||
11
cockatrice/src/pingpixmapgenerator.h
Normal file
11
cockatrice/src/pingpixmapgenerator.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef PINGPIXMAPGENERATOR_H
|
||||
#define PINGPIXMAPGENERATOR_H
|
||||
|
||||
#include <QPixmap>
|
||||
|
||||
class PingPixmapGenerator {
|
||||
public:
|
||||
static QPixmap generatePixmap(int size, int value, int max);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -363,22 +363,6 @@ void Player::initSayMenu()
|
|||
}
|
||||
}
|
||||
|
||||
void Player::prepareForGame()
|
||||
{
|
||||
if (local) {
|
||||
sendGameCommand(new Command_CreateCounter(-1, "life", Qt::white, 25, 20));
|
||||
sendGameCommand(new Command_CreateCounter(-1, "w", QColor(255, 255, 150), 20, 0));
|
||||
sendGameCommand(new Command_CreateCounter(-1, "u", QColor(150, 150, 255), 20, 0));
|
||||
sendGameCommand(new Command_CreateCounter(-1, "b", QColor(150, 150, 150), 20, 0));
|
||||
sendGameCommand(new Command_CreateCounter(-1, "r", QColor(250, 150, 150), 20, 0));
|
||||
sendGameCommand(new Command_CreateCounter(-1, "g", QColor(150, 255, 150), 20, 0));
|
||||
sendGameCommand(new Command_CreateCounter(-1, "x", QColor(255, 255, 255), 20, 0));
|
||||
sendGameCommand(new Command_CreateCounter(-1, "storm", QColor(255, 255, 255), 20, 0));
|
||||
|
||||
mulliganCards = 7;
|
||||
}
|
||||
}
|
||||
|
||||
void Player::actViewLibrary()
|
||||
{
|
||||
emit toggleZoneView(this, "deck", -1);
|
||||
|
|
@ -421,14 +405,7 @@ void Player::actDrawCard()
|
|||
|
||||
void Player::actMulligan()
|
||||
{
|
||||
if (mulliganCards <= 0)
|
||||
return;
|
||||
|
||||
const CardList &handCards = hand->getCards();
|
||||
for (int i = 0; i < handCards.size(); i++)
|
||||
sendGameCommand(new Command_MoveCard(-1, "hand", handCards.at(i)->getId(), "deck", 0, -1, false));
|
||||
sendGameCommand(new Command_Shuffle);
|
||||
sendGameCommand(new Command_DrawCards(-1, mulliganCards--));
|
||||
sendGameCommand(new Command_Mulligan);
|
||||
}
|
||||
|
||||
void Player::actDrawCards()
|
||||
|
|
|
|||
|
|
@ -114,7 +114,6 @@ private:
|
|||
void actMoveToExile(CardItem *card);
|
||||
|
||||
int defaultNumberTopCards;
|
||||
int mulliganCards;
|
||||
QString name;
|
||||
int id;
|
||||
bool active;
|
||||
|
|
@ -188,7 +187,6 @@ public:
|
|||
bool getActive() const { return active; }
|
||||
void setActive(bool _active);
|
||||
|
||||
void prepareForGame();
|
||||
void processPlayerInfo(ServerInfo_Player *info);
|
||||
void processGameEvent(GameEvent *event);
|
||||
void sendGameCommand(GameCommand *command);
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
#include "playerlistwidget.h"
|
||||
#include "protocol_datastructures.h"
|
||||
#include "pingpixmapgenerator.h"
|
||||
|
||||
PlayerListWidget::PlayerListWidget(QWidget *parent)
|
||||
: QTreeWidget(parent)
|
||||
{
|
||||
setColumnCount(1);
|
||||
setColumnCount(2);
|
||||
setRootIsDecorated(false);
|
||||
retranslateUi();
|
||||
}
|
||||
|
|
@ -12,12 +13,14 @@ PlayerListWidget::PlayerListWidget(QWidget *parent)
|
|||
void PlayerListWidget::retranslateUi()
|
||||
{
|
||||
headerItem()->setText(0, tr("Player name"));
|
||||
headerItem()->setText(1, tr("Role"));
|
||||
}
|
||||
|
||||
void PlayerListWidget::addPlayer(ServerInfo_Player *player)
|
||||
{
|
||||
QTreeWidgetItem *newPlayer = new QTreeWidgetItem;
|
||||
newPlayer->setText(0, player->getName());
|
||||
newPlayer->setText(1, player->getSpectator() ? tr("Spectator") : tr("Player"));
|
||||
addTopLevelItem(newPlayer);
|
||||
players.insert(player->getPlayerId(), newPlayer);
|
||||
}
|
||||
|
|
@ -37,6 +40,16 @@ void PlayerListWidget::setActivePlayer(int playerId)
|
|||
while (i.hasNext()) {
|
||||
i.next();
|
||||
QTreeWidgetItem *twi = i.value();
|
||||
twi->setBackground(0, i.key() == playerId ? QColor(150, 255, 150) : Qt::white);
|
||||
QColor c = i.key() == playerId ? QColor(150, 255, 150) : Qt::white;
|
||||
twi->setBackground(0, c);
|
||||
twi->setBackground(1, c);
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerListWidget::updatePing(int playerId, int pingTime)
|
||||
{
|
||||
QTreeWidgetItem *twi = players.value(playerId, 0);
|
||||
if (!twi)
|
||||
return;
|
||||
twi->setIcon(0, QIcon(PingPixmapGenerator::generatePixmap(10, pingTime, 10)));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ public:
|
|||
void addPlayer(ServerInfo_Player *player);
|
||||
void removePlayer(int playerId);
|
||||
void setActivePlayer(int playerId);
|
||||
void updatePing(int playerId, int pingTime);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -102,7 +102,10 @@ void TabDeckStorage::actUpload()
|
|||
if (!deck->loadFromFile(filePath, DeckList::CockatriceFormat))
|
||||
return;
|
||||
if (deck->getName().isEmpty()) {
|
||||
QString deckName = QInputDialog::getText(this, tr("Enter deck name"), tr("This decklist does not have a name.\nPlease enter a name:"), QLineEdit::Normal, tr("Unnamed deck"));
|
||||
bool ok;
|
||||
QString deckName = QInputDialog::getText(this, tr("Enter deck name"), tr("This decklist does not have a name.\nPlease enter a name:"), QLineEdit::Normal, tr("Unnamed deck"), &ok);
|
||||
if (!ok)
|
||||
return;
|
||||
if (deckName.isEmpty())
|
||||
deckName = tr("Unnamed deck");
|
||||
deck->setName(deckName);
|
||||
|
|
|
|||
|
|
@ -20,8 +20,8 @@
|
|||
#include "arrowitem.h"
|
||||
#include "main.h"
|
||||
|
||||
TabGame::TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectator)
|
||||
: Tab(), client(_client), gameId(_gameId), localPlayerId(_localPlayerId), spectator(_spectator), started(false), currentPhase(-1)
|
||||
TabGame::TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectator, bool _resuming)
|
||||
: Tab(), client(_client), gameId(_gameId), localPlayerId(_localPlayerId), spectator(_spectator), started(false), resuming(_resuming), currentPhase(-1)
|
||||
{
|
||||
zoneLayout = new ZoneViewLayout;
|
||||
scene = new GameScene(zoneLayout, this);
|
||||
|
|
@ -31,6 +31,7 @@ TabGame::TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectat
|
|||
loadLocalButton = new QPushButton;
|
||||
loadRemoteButton = new QPushButton;
|
||||
readyStartButton = new QPushButton;
|
||||
readyStartButton->setEnabled(false);
|
||||
|
||||
QHBoxLayout *buttonHBox = new QHBoxLayout;
|
||||
buttonHBox->addWidget(loadLocalButton);
|
||||
|
|
@ -50,7 +51,7 @@ TabGame::TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectat
|
|||
sayLabel = new QLabel;
|
||||
sayEdit = new QLineEdit;
|
||||
sayLabel->setBuddy(sayEdit);
|
||||
|
||||
|
||||
QHBoxLayout *hLayout = new QHBoxLayout;
|
||||
hLayout->addWidget(sayLabel);
|
||||
hLayout->addWidget(sayEdit);
|
||||
|
|
@ -71,6 +72,14 @@ TabGame::TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectat
|
|||
mainLayout->addWidget(deckViewContainer, 10);
|
||||
mainLayout->addLayout(verticalLayout);
|
||||
|
||||
if (spectator) {
|
||||
sayLabel->hide();
|
||||
sayEdit->hide();
|
||||
loadLocalButton->hide();
|
||||
loadRemoteButton->hide();
|
||||
readyStartButton->hide();
|
||||
}
|
||||
|
||||
aCloseMostRecentZoneView = new QAction(this);
|
||||
connect(aCloseMostRecentZoneView, SIGNAL(triggered()), zoneLayout, SLOT(closeMostRecentZoneView()));
|
||||
addAction(aCloseMostRecentZoneView);
|
||||
|
|
@ -214,13 +223,14 @@ Player *TabGame::addPlayer(int playerId, const QString &playerName)
|
|||
void TabGame::processGameEvent(GameEvent *event)
|
||||
{
|
||||
switch (event->getItemId()) {
|
||||
case ItemId_Event_GameStart: eventGameStart(qobject_cast<Event_GameStart *>(event)); break;
|
||||
case ItemId_Event_GameStateChanged: eventGameStateChanged(qobject_cast<Event_GameStateChanged *>(event)); break;
|
||||
case ItemId_Event_Join: eventJoin(qobject_cast<Event_Join *>(event)); break;
|
||||
case ItemId_Event_Leave: eventLeave(qobject_cast<Event_Leave *>(event)); break;
|
||||
case ItemId_Event_GameClosed: eventGameClosed(qobject_cast<Event_GameClosed *>(event)); break;
|
||||
case ItemId_Event_SetActivePlayer: eventSetActivePlayer(qobject_cast<Event_SetActivePlayer *>(event)); break;
|
||||
case ItemId_Event_SetActivePhase: eventSetActivePhase(qobject_cast<Event_SetActivePhase *>(event)); break;
|
||||
case ItemId_Event_Ping: eventPing(qobject_cast<Event_Ping *>(event)); break;
|
||||
|
||||
default: {
|
||||
Player *player = players.value(event->getPlayerId(), 0);
|
||||
if (!player) {
|
||||
|
|
@ -257,34 +267,33 @@ void TabGame::stopGame()
|
|||
deckViewContainer->show();
|
||||
}
|
||||
|
||||
void TabGame::eventGameStart(Event_GameStart * /*event*/)
|
||||
{
|
||||
startGame();
|
||||
messageLog->logGameStart();
|
||||
|
||||
QMapIterator<int, Player *> i(players);
|
||||
while (i.hasNext())
|
||||
i.next().value()->prepareForGame();
|
||||
}
|
||||
|
||||
void TabGame::eventGameStateChanged(Event_GameStateChanged *event)
|
||||
{
|
||||
const QList<ServerInfo_Player *> &plList = event->getPlayerList();
|
||||
for (int i = 0; i < plList.size(); ++i) {
|
||||
ServerInfo_Player *pl = plList[i];
|
||||
Player *player = players.value(pl->getPlayerId(), 0);
|
||||
if (!player) {
|
||||
player = addPlayer(pl->getPlayerId(), pl->getName());
|
||||
playerListWidget->addPlayer(pl);
|
||||
}
|
||||
player->processPlayerInfo(pl);
|
||||
if (player->getLocal() && pl->getDeck()) {
|
||||
Deck_PictureCacher::cachePictures(pl->getDeck(), this);
|
||||
deckView->setDeck(new DeckList(pl->getDeck()));
|
||||
if (pl->getSpectator()) {
|
||||
if (!spectators.contains(pl->getPlayerId())) {
|
||||
spectators.insert(pl->getPlayerId(), pl->getName());
|
||||
playerListWidget->addPlayer(pl);
|
||||
}
|
||||
} else {
|
||||
Player *player = players.value(pl->getPlayerId(), 0);
|
||||
if (!player) {
|
||||
player = addPlayer(pl->getPlayerId(), pl->getName());
|
||||
playerListWidget->addPlayer(pl);
|
||||
}
|
||||
player->processPlayerInfo(pl);
|
||||
if (player->getLocal() && pl->getDeck()) {
|
||||
Deck_PictureCacher::cachePictures(pl->getDeck(), this);
|
||||
deckView->setDeck(new DeckList(pl->getDeck()));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (event->getGameStarted() && !started) {
|
||||
startGame();
|
||||
if (!resuming)
|
||||
messageLog->logGameStart();
|
||||
setActivePlayer(event->getActivePlayer());
|
||||
setActivePhase(event->getActivePhase());
|
||||
} else if (!event->getGameStarted() && started) {
|
||||
|
|
@ -297,8 +306,9 @@ void TabGame::eventJoin(Event_Join *event)
|
|||
{
|
||||
ServerInfo_Player *playerInfo = event->getPlayer();
|
||||
if (playerInfo->getSpectator()) {
|
||||
spectatorList.append(playerInfo->getName());
|
||||
spectators.insert(playerInfo->getPlayerId(), playerInfo->getName());
|
||||
messageLog->logJoinSpectator(playerInfo->getName());
|
||||
playerListWidget->addPlayer(playerInfo);
|
||||
} else {
|
||||
Player *newPlayer = addPlayer(playerInfo->getPlayerId(), playerInfo->getName());
|
||||
messageLog->logJoin(newPlayer);
|
||||
|
|
@ -309,12 +319,18 @@ void TabGame::eventJoin(Event_Join *event)
|
|||
void TabGame::eventLeave(Event_Leave *event)
|
||||
{
|
||||
int playerId = event->getPlayerId();
|
||||
|
||||
Player *player = players.value(playerId, 0);
|
||||
if (!player)
|
||||
return;
|
||||
|
||||
messageLog->logLeave(player);
|
||||
playerListWidget->removePlayer(playerId);
|
||||
if (player) {
|
||||
messageLog->logLeave(player);
|
||||
playerListWidget->removePlayer(playerId);
|
||||
players.remove(playerId);
|
||||
delete player;
|
||||
} else if (spectators.contains(playerId)) {
|
||||
messageLog->logLeaveSpectator(spectators.value(playerId));
|
||||
playerListWidget->removePlayer(playerId);
|
||||
spectators.remove(playerId);
|
||||
}
|
||||
}
|
||||
|
||||
void TabGame::eventGameClosed(Event_GameClosed * /*event*/)
|
||||
|
|
@ -362,6 +378,13 @@ void TabGame::eventSetActivePhase(Event_SetActivePhase *event)
|
|||
setActivePhase(phase);
|
||||
}
|
||||
|
||||
void TabGame::eventPing(Event_Ping *event)
|
||||
{
|
||||
const QList<ServerInfo_PlayerPing *> &pingList = event->getPingList();
|
||||
for (int i = 0; i < pingList.size(); ++i)
|
||||
playerListWidget->updatePing(pingList[i]->getPlayerId(), pingList[i]->getPingTime());
|
||||
}
|
||||
|
||||
void TabGame::loadLocalDeck()
|
||||
{
|
||||
QFileDialog dialog(this, tr("Load deck"));
|
||||
|
|
@ -403,6 +426,7 @@ void TabGame::deckSelectFinished(ProtocolResponse *r)
|
|||
|
||||
Deck_PictureCacher::cachePictures(resp->getDeck(), this);
|
||||
deckView->setDeck(new DeckList(resp->getDeck()));
|
||||
readyStartButton->setEnabled(true);
|
||||
}
|
||||
|
||||
void TabGame::readyStart()
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ class PlayerListWidget;
|
|||
class ProtocolResponse;
|
||||
class GameEvent;
|
||||
class GameCommand;
|
||||
class Event_GameStart;
|
||||
class Event_GameStateChanged;
|
||||
class Event_Join;
|
||||
class Event_Leave;
|
||||
|
|
@ -30,6 +29,7 @@ class Event_GameClosed;
|
|||
class Event_GameStart;
|
||||
class Event_SetActivePlayer;
|
||||
class Event_SetActivePhase;
|
||||
class Event_Ping;
|
||||
class Player;
|
||||
class CardZone;
|
||||
class CardItem;
|
||||
|
|
@ -41,9 +41,10 @@ private:
|
|||
int gameId;
|
||||
int localPlayerId;
|
||||
bool spectator;
|
||||
QStringList spectatorList;
|
||||
QMap<int, Player *> players;
|
||||
QMap<int, QString> spectators;
|
||||
bool started;
|
||||
bool resuming;
|
||||
int currentPhase;
|
||||
|
||||
QPushButton *loadLocalButton, *loadRemoteButton, *readyStartButton;
|
||||
|
|
@ -68,7 +69,6 @@ private:
|
|||
void startGame();
|
||||
void stopGame();
|
||||
|
||||
void eventGameStart(Event_GameStart *event);
|
||||
void eventGameStateChanged(Event_GameStateChanged *event);
|
||||
void eventJoin(Event_Join *event);
|
||||
void eventLeave(Event_Leave *event);
|
||||
|
|
@ -77,6 +77,7 @@ private:
|
|||
void eventSetActivePlayer(Event_SetActivePlayer *event);
|
||||
void setActivePhase(int phase);
|
||||
void eventSetActivePhase(Event_SetActivePhase *event);
|
||||
void eventPing(Event_Ping *event);
|
||||
signals:
|
||||
void gameClosing(TabGame *tab);
|
||||
private slots:
|
||||
|
|
@ -93,7 +94,7 @@ private slots:
|
|||
void actNextPhase();
|
||||
void actNextTurn();
|
||||
public:
|
||||
TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectator);
|
||||
TabGame(Client *_client, int _gameId, int _localPlayerId, bool _spectator, bool _resuming);
|
||||
~TabGame();
|
||||
void retranslateUi();
|
||||
const QMap<int, Player *> &getPlayers() const { return players; }
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include "tab_game.h"
|
||||
#include "tab_deck_storage.h"
|
||||
#include "protocol_items.h"
|
||||
#include <QPainter>
|
||||
#include "pingpixmapgenerator.h"
|
||||
|
||||
TabSupervisor:: TabSupervisor(QWidget *parent)
|
||||
: QTabWidget(parent), client(0), tabServer(0), tabDeckStorage(0)
|
||||
|
|
@ -83,26 +83,13 @@ void TabSupervisor::updatePingTime(int value, int max)
|
|||
{
|
||||
if (!tabServer)
|
||||
return;
|
||||
QPixmap pixmap(15, 15);
|
||||
pixmap.fill(Qt::transparent);
|
||||
QPainter painter(&pixmap);
|
||||
QColor color;
|
||||
if (max == -1)
|
||||
color = Qt::black;
|
||||
else
|
||||
color.setHsv(120 * (1.0 - ((double) value / max)), 255, 255);
|
||||
|
||||
QRadialGradient g(QPointF((double) pixmap.width() / 2, (double) pixmap.height() / 2), qMin(pixmap.width(), pixmap.height()) / 2.0);
|
||||
g.setColorAt(0, color);
|
||||
g.setColorAt(1, Qt::transparent);
|
||||
painter.fillRect(0, 0, pixmap.width(), pixmap.height(), QBrush(g));
|
||||
|
||||
setTabIcon(0, QIcon(pixmap));
|
||||
setTabIcon(0, QIcon(PingPixmapGenerator::generatePixmap(15, value, max)));
|
||||
}
|
||||
|
||||
void TabSupervisor::gameJoined(Event_GameJoined *event)
|
||||
{
|
||||
TabGame *tab = new TabGame(client, event->getGameId(), event->getPlayerId(), event->getSpectator());
|
||||
TabGame *tab = new TabGame(client, event->getGameId(), event->getPlayerId(), event->getSpectator(), event->getResuming());
|
||||
connect(tab, SIGNAL(gameClosing(TabGame *)), this, SLOT(gameLeft(TabGame *)));
|
||||
addTab(tab, tr("Game %1").arg(event->getGameId()));
|
||||
gameTabs.insert(event->getGameId(), tab);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue