mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-19 00:42:14 -07:00
Use enum for ThemeManager brushes
This patch introduces an enum to distinguish the different brushes that can be set by the theme (hand, stack, etc.) and generic functions taking the enum rather than having one copy of each function for each brush. This is preliminary work before merging StackZone and HandZone to simplify #4974.
This commit is contained in:
parent
c99afe7956
commit
1fbcb69166
7 changed files with 57 additions and 99 deletions
|
|
@ -120,10 +120,10 @@ void ThemeManager::themeChangedSlot()
|
||||||
if (dirPath.isEmpty()) {
|
if (dirPath.isEmpty()) {
|
||||||
// set default values
|
// set default values
|
||||||
QDir::setSearchPaths("theme", DEFAULT_RESOURCE_PATHS);
|
QDir::setSearchPaths("theme", DEFAULT_RESOURCE_PATHS);
|
||||||
handBgBrush = HANDZONE_BG_DEFAULT;
|
brushes[Role::Hand] = HANDZONE_BG_DEFAULT;
|
||||||
tableBgBrush = TABLEZONE_BG_DEFAULT;
|
brushes[Role::Table] = TABLEZONE_BG_DEFAULT;
|
||||||
playerBgBrush = PLAYERZONE_BG_DEFAULT;
|
brushes[Role::Player] = PLAYERZONE_BG_DEFAULT;
|
||||||
stackBgBrush = STACKZONE_BG_DEFAULT;
|
brushes[Role::Stack] = STACKZONE_BG_DEFAULT;
|
||||||
} else {
|
} else {
|
||||||
// resources
|
// resources
|
||||||
QStringList resources;
|
QStringList resources;
|
||||||
|
|
@ -132,73 +132,58 @@ void ThemeManager::themeChangedSlot()
|
||||||
|
|
||||||
// zones bg
|
// zones bg
|
||||||
dir.cd("zones");
|
dir.cd("zones");
|
||||||
handBgBrush = loadBrush(HANDZONE_BG_NAME, HANDZONE_BG_DEFAULT);
|
brushes[Role::Hand] = loadBrush(HANDZONE_BG_NAME, HANDZONE_BG_DEFAULT);
|
||||||
tableBgBrush = loadBrush(TABLEZONE_BG_NAME, TABLEZONE_BG_DEFAULT);
|
brushes[Role::Table] = loadBrush(TABLEZONE_BG_NAME, TABLEZONE_BG_DEFAULT);
|
||||||
playerBgBrush = loadBrush(PLAYERZONE_BG_NAME, PLAYERZONE_BG_DEFAULT);
|
brushes[Role::Player] = loadBrush(PLAYERZONE_BG_NAME, PLAYERZONE_BG_DEFAULT);
|
||||||
stackBgBrush = loadBrush(STACKZONE_BG_NAME, STACKZONE_BG_DEFAULT);
|
brushes[Role::Stack] = loadBrush(STACKZONE_BG_NAME, STACKZONE_BG_DEFAULT);
|
||||||
|
}
|
||||||
|
for (auto &brushCache : brushesCache) {
|
||||||
|
brushCache.clear();
|
||||||
}
|
}
|
||||||
tableBgBrushesCache.clear();
|
|
||||||
stackBgBrushesCache.clear();
|
|
||||||
playerBgBrushesCache.clear();
|
|
||||||
handBgBrushesCache.clear();
|
|
||||||
|
|
||||||
QPixmapCache::clear();
|
QPixmapCache::clear();
|
||||||
|
|
||||||
emit themeChanged();
|
emit themeChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
QBrush ThemeManager::getExtraTableBgBrush(QString extraNumber, QBrush &fallbackBrush)
|
static QString roleBgName(ThemeManager::Role role)
|
||||||
{
|
{
|
||||||
QBrush returnBrush;
|
switch (role) {
|
||||||
|
case ThemeManager::Hand:
|
||||||
|
return HANDZONE_BG_NAME;
|
||||||
|
|
||||||
if (!tableBgBrushesCache.contains(extraNumber.toInt())) {
|
case ThemeManager::Player:
|
||||||
returnBrush = loadExtraBrush(TABLEZONE_BG_NAME + extraNumber, fallbackBrush);
|
return PLAYERZONE_BG_NAME;
|
||||||
tableBgBrushesCache.insert(extraNumber.toInt(), returnBrush);
|
|
||||||
} else {
|
case ThemeManager::Stack:
|
||||||
returnBrush = tableBgBrushesCache.value(extraNumber.toInt());
|
return STACKZONE_BG_NAME;
|
||||||
|
|
||||||
|
case ThemeManager::Table:
|
||||||
|
return TABLEZONE_BG_NAME;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Q_ASSERT(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnBrush;
|
QBrush &ThemeManager::getBgBrush(Role role)
|
||||||
}
|
|
||||||
|
|
||||||
QBrush ThemeManager::getExtraStackBgBrush(QString extraNumber, QBrush &fallbackBrush)
|
|
||||||
{
|
{
|
||||||
QBrush returnBrush;
|
return brushes[role];
|
||||||
|
|
||||||
if (!stackBgBrushesCache.contains(extraNumber.toInt())) {
|
|
||||||
returnBrush = loadExtraBrush(STACKZONE_BG_NAME + extraNumber, fallbackBrush);
|
|
||||||
stackBgBrushesCache.insert(extraNumber.toInt(), returnBrush);
|
|
||||||
} else {
|
|
||||||
returnBrush = stackBgBrushesCache.value(extraNumber.toInt());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnBrush;
|
QBrush ThemeManager::getExtraBgBrush(Role role, int zoneId)
|
||||||
}
|
|
||||||
|
|
||||||
QBrush ThemeManager::getExtraPlayerBgBrush(QString extraNumber, QBrush &fallbackBrush)
|
|
||||||
{
|
{
|
||||||
QBrush returnBrush;
|
if (zoneId <= 0) {
|
||||||
|
return getBgBrush(role);
|
||||||
if (!playerBgBrushesCache.contains(extraNumber.toInt())) {
|
|
||||||
returnBrush = loadExtraBrush(PLAYERZONE_BG_NAME + extraNumber, fallbackBrush);
|
|
||||||
playerBgBrushesCache.insert(extraNumber.toInt(), returnBrush);
|
|
||||||
} else {
|
|
||||||
returnBrush = playerBgBrushesCache.value(extraNumber.toInt());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnBrush;
|
QBrushMap &brushCache = brushesCache[role];
|
||||||
|
|
||||||
|
if (!brushCache.contains(zoneId)) {
|
||||||
|
QBrush brush = loadExtraBrush(roleBgName(role) + QString::number(zoneId), getBgBrush(role));
|
||||||
|
brushCache.insert(zoneId, brush);
|
||||||
|
return brush;
|
||||||
}
|
}
|
||||||
|
|
||||||
QBrush ThemeManager::getExtraHandBgBrush(QString extraNumber, QBrush &fallbackBrush)
|
return brushCache.value(zoneId);
|
||||||
{
|
|
||||||
QBrush returnBrush;
|
|
||||||
|
|
||||||
if (!handBgBrushesCache.contains(extraNumber.toInt())) {
|
|
||||||
returnBrush = loadExtraBrush(HANDZONE_BG_NAME + extraNumber, fallbackBrush);
|
|
||||||
handBgBrushesCache.insert(extraNumber.toInt(), returnBrush);
|
|
||||||
} else {
|
|
||||||
returnBrush = handBgBrushesCache.value(extraNumber.toInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
return returnBrush;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,13 +22,23 @@ class ThemeManager : public QObject
|
||||||
public:
|
public:
|
||||||
ThemeManager(QObject *parent = nullptr);
|
ThemeManager(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
enum Role
|
||||||
|
{
|
||||||
|
MinRole = 0,
|
||||||
|
Hand = MinRole,
|
||||||
|
Stack,
|
||||||
|
Table,
|
||||||
|
Player,
|
||||||
|
MaxRole = Player,
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QBrush handBgBrush, stackBgBrush, tableBgBrush, playerBgBrush;
|
std::array<QBrush, Role::MaxRole + 1> brushes;
|
||||||
QStringMap availableThemes;
|
QStringMap availableThemes;
|
||||||
/*
|
/*
|
||||||
Internal cache for multiple backgrounds
|
Internal cache for multiple backgrounds
|
||||||
*/
|
*/
|
||||||
QBrushMap tableBgBrushesCache, stackBgBrushesCache, playerBgBrushesCache, handBgBrushesCache;
|
std::array<QBrushMap, Role::MaxRole + 1> brushesCache;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void ensureThemeDirectoryExists();
|
void ensureThemeDirectoryExists();
|
||||||
|
|
@ -36,27 +46,10 @@ protected:
|
||||||
QBrush loadExtraBrush(QString fileName, QBrush &fallbackBrush);
|
QBrush loadExtraBrush(QString fileName, QBrush &fallbackBrush);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
QBrush &getHandBgBrush()
|
|
||||||
{
|
|
||||||
return handBgBrush;
|
|
||||||
}
|
|
||||||
QBrush &getStackBgBrush()
|
|
||||||
{
|
|
||||||
return stackBgBrush;
|
|
||||||
}
|
|
||||||
QBrush &getTableBgBrush()
|
|
||||||
{
|
|
||||||
return tableBgBrush;
|
|
||||||
}
|
|
||||||
QBrush &getPlayerBgBrush()
|
|
||||||
{
|
|
||||||
return playerBgBrush;
|
|
||||||
}
|
|
||||||
QStringMap &getAvailableThemes();
|
QStringMap &getAvailableThemes();
|
||||||
QBrush getExtraTableBgBrush(QString extraNumber, QBrush &fallbackBrush);
|
|
||||||
QBrush getExtraStackBgBrush(QString extraNumber, QBrush &fallbackBrush);
|
QBrush &getBgBrush(Role zone);
|
||||||
QBrush getExtraPlayerBgBrush(QString extraNumber, QBrush &fallbackBrush);
|
QBrush getExtraBgBrush(Role zone, int zoneId = 0);
|
||||||
QBrush getExtraHandBgBrush(QString extraNumber, QBrush &fallbackBrush);
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void themeChangedSlot();
|
void themeChangedSlot();
|
||||||
signals:
|
signals:
|
||||||
|
|
|
||||||
|
|
@ -172,7 +172,7 @@ void DeckViewCardContainer::paint(QPainter *painter, const QStyleOptionGraphicsI
|
||||||
{
|
{
|
||||||
qreal totalTextWidth = getCardTypeTextWidth();
|
qreal totalTextWidth = getCardTypeTextWidth();
|
||||||
|
|
||||||
painter->fillRect(boundingRect(), themeManager->getTableBgBrush());
|
painter->fillRect(boundingRect(), themeManager->getBgBrush(ThemeManager::Table));
|
||||||
painter->setPen(QColor(255, 255, 255, 100));
|
painter->setPen(QColor(255, 255, 255, 100));
|
||||||
painter->drawLine(QPointF(0, separatorY), QPointF(width, separatorY));
|
painter->drawLine(QPointF(0, separatorY), QPointF(width, separatorY));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -93,12 +93,7 @@ void PlayerArea::updateBg()
|
||||||
|
|
||||||
void PlayerArea::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
void PlayerArea::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
||||||
{
|
{
|
||||||
QBrush brush = themeManager->getPlayerBgBrush();
|
QBrush brush = themeManager->getExtraBgBrush(ThemeManager::Player, playerZoneId);
|
||||||
|
|
||||||
if (playerZoneId > 0) {
|
|
||||||
// If the extra image is not found, load the default one
|
|
||||||
brush = themeManager->getExtraPlayerBgBrush(QString::number(playerZoneId), brush);
|
|
||||||
}
|
|
||||||
painter->fillRect(boundingRect(), brush);
|
painter->fillRect(boundingRect(), brush);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -78,12 +78,7 @@ QRectF HandZone::boundingRect() const
|
||||||
|
|
||||||
void HandZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
void HandZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
||||||
{
|
{
|
||||||
QBrush brush = themeManager->getHandBgBrush();
|
QBrush brush = themeManager->getExtraBgBrush(ThemeManager::Hand, player->getZoneId());
|
||||||
|
|
||||||
if (player->getZoneId() > 0) {
|
|
||||||
// If the extra image is not found, load the default one
|
|
||||||
brush = themeManager->getExtraHandBgBrush(QString::number(player->getZoneId()), brush);
|
|
||||||
}
|
|
||||||
painter->fillRect(boundingRect(), brush);
|
painter->fillRect(boundingRect(), brush);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,12 +49,7 @@ QRectF StackZone::boundingRect() const
|
||||||
|
|
||||||
void StackZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
void StackZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
||||||
{
|
{
|
||||||
QBrush brush = themeManager->getStackBgBrush();
|
QBrush brush = themeManager->getExtraBgBrush(ThemeManager::Stack, player->getZoneId());
|
||||||
|
|
||||||
if (player->getZoneId() > 0) {
|
|
||||||
// If the extra image is not found, load the default one
|
|
||||||
brush = themeManager->getExtraStackBgBrush(QString::number(player->getZoneId()), brush);
|
|
||||||
}
|
|
||||||
painter->fillRect(boundingRect(), brush);
|
painter->fillRect(boundingRect(), brush);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,12 +54,7 @@ bool TableZone::isInverted() const
|
||||||
|
|
||||||
void TableZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
void TableZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
|
||||||
{
|
{
|
||||||
QBrush brush = themeManager->getTableBgBrush();
|
QBrush brush = themeManager->getExtraBgBrush(ThemeManager::Table, player->getZoneId());
|
||||||
|
|
||||||
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);
|
painter->fillRect(boundingRect(), brush);
|
||||||
|
|
||||||
if (active) {
|
if (active) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue