Multiple bg images zone (#4005)

This commit is contained in:
fdipilla 2020-08-23 16:55:53 -03:00 committed by GitHub
parent 964207d04f
commit b0c7b9078d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 9 deletions

View file

@ -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) {

View file

@ -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<QString, CardZone *> &getZones() const
{
return zones;

View file

@ -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;
}

View file

@ -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);

View file

@ -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;
}

View file

@ -9,6 +9,7 @@
#include <QString>
typedef QMap<QString, QString> QStringMap;
typedef QMap<int, QBrush> 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: