From b0c7b9078d8b0d8993177a4e0c046d883652b131 Mon Sep 17 00:00:00 2001 From: fdipilla Date: Sun, 23 Aug 2020 16:55:53 -0300 Subject: [PATCH] Multiple bg images zone (#4005) --- cockatrice/src/player.cpp | 9 +++++++-- cockatrice/src/player.h | 6 ++++++ cockatrice/src/tab_game.cpp | 19 +++++++++++++++++++ cockatrice/src/tablezone.cpp | 7 +++---- cockatrice/src/thememanager.cpp | 28 ++++++++++++++++++++++++++-- cockatrice/src/thememanager.h | 8 +++++++- 6 files changed, 68 insertions(+), 9 deletions(-) diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 63b9d4f7..3c5c0b3c 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -95,8 +95,8 @@ void PlayerArea::setSize(qreal width, qreal height) Player::Player(const ServerInfo_User &info, int _id, bool _local, bool _judge, TabGame *_parent) : QObject(_parent), game(_parent), shortcutsActive(false), defaultNumberTopCards(1), defaultNumberTopCardsToPlaceBelow(1), lastTokenDestroy(true), lastTokenTableRow(0), id(_id), active(false), - local(_local), judge(_judge), mirrored(false), handVisible(false), conceded(false), dialogSemaphore(false), - deck(nullptr) + local(_local), judge(_judge), mirrored(false), handVisible(false), conceded(false), zoneId(0), + dialogSemaphore(false), deck(nullptr) { userInfo = new ServerInfo_User; userInfo->CopyFrom(info); @@ -3263,6 +3263,11 @@ void Player::setConceded(bool _conceded) emit playerCountChanged(); } +void Player::setZoneId(int _zoneId) +{ + zoneId = _zoneId; +} + void Player::setMirrored(bool _mirrored) { if (mirrored != _mirrored) { diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 085c4640..4dbdf594 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -240,6 +240,7 @@ private: bool mirrored; bool handVisible; bool conceded; + int zoneId; bool dialogSemaphore; bool clearCardsToDelete(); @@ -389,6 +390,11 @@ public: { return mirrored; } + int getZoneId() const + { + return zoneId; + } + void setZoneId(int _zoneId); const QMap &getZones() const { return zones; diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index 2ba40b96..009b77c4 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -804,6 +804,25 @@ Player *TabGame::addPlayer(int playerId, const ServerInfo_User &info) gameMenu->insertMenu(playersSeparator, newPlayer->getPlayerMenu()); players.insert(playerId, newPlayer); + + if (!spectators.contains(playerId)) { + + // Loop for each player, the idea is to have one assigned zone for each non-spectator player + for (int i = 1; i <= players.count(); ++i) { + bool aPlayerHasThisZone = false; + for (auto &player : players) { + if (player->getZoneId() == i) { + aPlayerHasThisZone = true; + break; + } + } + if (!aPlayerHasThisZone) { + newPlayer->setZoneId(i); + break; + } + } + } + emit playerAdded(newPlayer); return newPlayer; } diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index aad1f2e2..c38f5ba6 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -57,10 +57,9 @@ void TableZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*opti { QBrush brush = themeManager->getTableBgBrush(); - // If the player is other than Player 1 - if (player->getId() > 0) { - // The player's id starts with 0 so in order to get the correct image we need to add 1 - brush = themeManager->getExtraTableBgBrush(QString::number(player->getId() + 1)); + if (player->getZoneId() > 0) { + // If the extra image is not found, load the default one + brush = themeManager->getExtraTableBgBrush(QString::number(player->getZoneId()), brush); } painter->fillRect(boundingRect(), brush); diff --git a/cockatrice/src/thememanager.cpp b/cockatrice/src/thememanager.cpp index b81e3bd7..8b928d2a 100644 --- a/cockatrice/src/thememanager.cpp +++ b/cockatrice/src/thememanager.cpp @@ -78,6 +78,20 @@ QBrush ThemeManager::loadBrush(QString fileName, QColor fallbackColor) return brush; } +QBrush ThemeManager::loadExtraBrush(QString fileName, QBrush &fallbackBrush) +{ + QBrush brush; + QPixmap tmp = QPixmap("theme:zones/" + fileName); + + if (tmp.isNull()) { + brush = fallbackBrush; + } else { + brush.setTexture(tmp); + } + + return brush; +} + void ThemeManager::themeChangedSlot() { QString themeName = SettingsCache::instance().getThemeName(); @@ -103,13 +117,23 @@ void ThemeManager::themeChangedSlot() tableBgBrush = loadBrush(TABLEZONE_BG_NAME, QColor(70, 50, 100)); playerBgBrush = loadBrush(PLAYERZONE_BG_NAME, QColor(200, 200, 200)); stackBgBrush = loadBrush(STACKZONE_BG_NAME, QColor(113, 43, 43)); + tableBgBrushesCache.clear(); QPixmapCache::clear(); emit themeChanged(); } -QBrush ThemeManager::getExtraTableBgBrush(QString extraNumber) +QBrush ThemeManager::getExtraTableBgBrush(QString extraNumber, QBrush &fallbackBrush) { - return loadBrush(TABLEZONE_BG_NAME + extraNumber, QColor(70, 50, 100)); + QBrush returnBrush; + + if (!tableBgBrushesCache.contains(extraNumber.toInt())) { + returnBrush = loadExtraBrush(TABLEZONE_BG_NAME + extraNumber, fallbackBrush); + tableBgBrushesCache.insert(extraNumber.toInt(), returnBrush); + } else { + returnBrush = tableBgBrushesCache.value(extraNumber.toInt()); + } + + return returnBrush; } diff --git a/cockatrice/src/thememanager.h b/cockatrice/src/thememanager.h index 7eb9457c..23bad895 100644 --- a/cockatrice/src/thememanager.h +++ b/cockatrice/src/thememanager.h @@ -9,6 +9,7 @@ #include typedef QMap QStringMap; +typedef QMap QBrushMap; class QApplication; @@ -21,10 +22,15 @@ public: private: QBrush handBgBrush, stackBgBrush, tableBgBrush, playerBgBrush; QStringMap availableThemes; + /* + Internal cache for table backgrounds + */ + QBrushMap tableBgBrushesCache; protected: void ensureThemeDirectoryExists(); QBrush loadBrush(QString fileName, QColor fallbackColor); + QBrush loadExtraBrush(QString fileName, QBrush &fallbackBrush); public: QBrush &getHandBgBrush() @@ -44,7 +50,7 @@ public: return playerBgBrush; } QStringMap &getAvailableThemes(); - QBrush getExtraTableBgBrush(QString extraNumber); + QBrush getExtraTableBgBrush(QString extraNumber, QBrush &fallbackBrush); protected slots: void themeChangedSlot(); signals: