Add the color identity to the Deck Preview Widget.

This commit is contained in:
Lukas Brübach 2025-01-11 23:32:47 +01:00
parent f8d0778170
commit 42fda4b01c
5 changed files with 224 additions and 0 deletions

View file

@ -162,6 +162,7 @@ set(cockatrice_SOURCES
src/game/zones/view_zone.cpp
src/client/tabs/visual_deck_storage/tab_deck_storage_visual.cpp
src/client/ui/widgets/cards/deck_preview_card_picture_widget.cpp
src/client/ui/widgets/visual_deck_storage/deck_preview_color_identity_widget.cpp
src/client/ui/widgets/visual_deck_storage/deck_preview_widget.cpp
src/client/ui/widgets/visual_deck_storage/visual_deck_storage_widget.cpp
src/client/ui/widgets/visual_deck_storage/visual_deck_storage_search_widget.cpp

View file

@ -0,0 +1,144 @@
#include "deck_preview_color_identity_widget.h"
#include <QPainter>
#include <QResizeEvent>
DeckPreviewColorCircleWidget::DeckPreviewColorCircleWidget(QChar color, QWidget *parent)
: QWidget(parent), colorChar(color), circleDiameter(0), isActive(false)
{
}
void DeckPreviewColorCircleWidget::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
// Get the parent of the DeckPreviewColorIdentityWidget
QWidget *identityParent = parentWidget() ? parentWidget()->parentWidget() : nullptr;
if (identityParent) {
// Calculate the circle diameter as 10% of the parent's height
int maxSize = identityParent->height() * 0.1;
circleDiameter = maxSize;
// Update the widget size based on the diameter
updateGeometry(); // Request a resize based on sizeHint()
}
update(); // Trigger a repaint
}
QSize DeckPreviewColorCircleWidget::sizeHint() const
{
// Return the size we calculated based on the parent widget
return QSize(circleDiameter, circleDiameter);
}
QSize DeckPreviewColorCircleWidget::minimumSizeHint() const
{
// Return the same value as sizeHint() for minimum size
return QSize(circleDiameter, circleDiameter);
}
void DeckPreviewColorCircleWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// Calculate the circle's bounding rectangle
int x = (width() - circleDiameter) / 2;
int y = (height() - circleDiameter) / 2;
QRect circleRect(x, y, circleDiameter, circleDiameter);
// Map color characters to their respective colors
QColor circleColor;
if (isActive) {
switch (colorChar.unicode()) {
case 'W':
circleColor = Qt::white;
break;
case 'U':
circleColor = QColor(0, 115, 230);
break; // Blue
case 'B':
circleColor = QColor(50, 50, 50);
break; // Black
case 'R':
circleColor = QColor(230, 30, 30);
break; // Red
case 'G':
circleColor = QColor(30, 180, 30);
break; // Green
default:
circleColor = Qt::transparent;
break; // Fallback
}
} else {
circleColor = Qt::gray; // Grey out unused colors
}
// Draw the circle
painter.setBrush(circleColor);
painter.setPen(Qt::black);
painter.drawEllipse(circleRect);
// Draw the color character only if the circle is active
if (isActive) {
QFont font = painter.font();
font.setBold(true);
font.setPointSize(circleDiameter * 0.4); // Adjust font size relative to circle diameter
painter.setFont(font);
painter.setPen(Qt::black);
// Center the text within the circle
painter.drawText(circleRect, Qt::AlignCenter, colorChar);
}
}
void DeckPreviewColorCircleWidget::setColorActive(bool active)
{
isActive = active;
update(); // Redraw the circle with the new active state
}
QChar DeckPreviewColorCircleWidget::getColorChar() const
{
return colorChar;
}
DeckPreviewColorIdentityWidget::DeckPreviewColorIdentityWidget(const QString &colorIdentity, QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setSpacing(5);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
// Define the full WUBRG set (White, Blue, Black, Red, Green)
QString fullColorIdentity = "WUBRG";
// Create and add a DeckPreviewColorCircleWidget for each color in WUBRG
for (const QChar &color : fullColorIdentity) {
auto *circle = new DeckPreviewColorCircleWidget(color, this);
layout->addWidget(circle);
}
// Set any active colors from the input colorIdentity
for (const QChar &color : colorIdentity) {
for (DeckPreviewColorCircleWidget *circle : findChildren<DeckPreviewColorCircleWidget *>()) {
if (circle->getColorChar() == color) {
circle->setColorActive(true); // Mark the color as active
}
}
}
}
void DeckPreviewColorIdentityWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
// Notify child widgets to update their sizes based on the new parent size
for (auto *circle : findChildren<DeckPreviewColorCircleWidget *>()) {
circle->updateGeometry(); // Request each circle to resize
}
}

View file

@ -0,0 +1,42 @@
#ifndef DECK_PREVIEW_COLOR_IDENTITY_WIDGET_H
#define DECK_PREVIEW_COLOR_IDENTITY_WIDGET_H
#include <QChar>
#include <QHBoxLayout>
#include <QSize>
#include <QWidget>
class DeckPreviewColorCircleWidget : public QWidget
{
Q_OBJECT
public:
explicit DeckPreviewColorCircleWidget(QChar color, QWidget *parent = nullptr);
void setColorActive(bool active);
QChar getColorChar() const;
protected:
void resizeEvent(QResizeEvent *event) override;
void paintEvent(QPaintEvent *event) override;
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
private:
QChar colorChar;
int circleDiameter;
bool isActive;
};
class DeckPreviewColorIdentityWidget : public QWidget
{
Q_OBJECT
public:
explicit DeckPreviewColorIdentityWidget(const QString &colorIdentity, QWidget *parent = nullptr);
protected:
void resizeEvent(QResizeEvent *event) override;
};
#endif // DECK_PREVIEW_COLOR_IDENTITY_WIDGET_H

View file

@ -32,7 +32,41 @@ DeckPreviewWidget::DeckPreviewWidget(QWidget *parent, const QString &_filePath)
bannerCardDisplayWidget->setFontSize(24);
setFilePath(deckLoader->getLastFileName());
colorIdentityWidget = new DeckPreviewColorIdentityWidget(getColorIdentity());
layout->addWidget(bannerCardDisplayWidget);
layout->addWidget(colorIdentityWidget);
}
QString DeckPreviewWidget::getColorIdentity()
{
QStringList cardList = deckLoader->getCardList();
if (cardList.isEmpty()) {
return "";
}
QSet<QChar> colorSet; // A set to collect unique color symbols (e.g., W, U, B, R, G)
for (const QString &cardName : cardList) {
CardInfoPtr currentCard = CardDatabaseManager::getInstance()->getCard(cardName);
if (currentCard) {
QString colors = currentCard->getColors(); // Assuming this returns something like "WUB"
for (const QChar &color : colors) {
colorSet.insert(color);
}
}
}
// Ensure the color identity is in WUBRG order
QString colorIdentity;
const QString wubrgOrder = "WUBRG";
for (const QChar &color : wubrgOrder) {
if (colorSet.contains(color)) {
colorIdentity.append(color);
}
}
return colorIdentity;
}
void DeckPreviewWidget::setFilePath(const QString &_filePath)

View file

@ -3,6 +3,7 @@
#include "../../../../deck/deck_loader.h"
#include "../cards/deck_preview_card_picture_widget.h"
#include "deck_preview_color_identity_widget.h"
#include <QVBoxLayout>
#include <QWidget>
@ -12,11 +13,13 @@ class DeckPreviewWidget final : public QWidget
Q_OBJECT
public:
explicit DeckPreviewWidget(QWidget *parent, const QString &_filePath);
QString getColorIdentity();
QVBoxLayout *layout;
QString filePath;
DeckLoader *deckLoader;
DeckPreviewCardPictureWidget *bannerCardDisplayWidget;
DeckPreviewColorIdentityWidget *colorIdentityWidget;
signals:
void deckPreviewClicked(QMouseEvent *event, DeckPreviewWidget *instance);