Implement a little "raise on enter" animation for deck preview widgets. (#5844)

* Implement a little "raise on enter" animation for deck preview widgets.

* Why does the linter need to be run twice?

* Fix build.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-04-18 05:31:47 +02:00 committed by GitHub
parent 2dc1b875d2
commit 8af1f2b6d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 127 additions and 9 deletions

View File

@ -30,8 +30,9 @@
*
* Initializes the widget with a minimum height and sets the pixmap to a dirty state for initial loading.
*/
CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool hoverToZoomEnabled)
: QWidget(parent), info(nullptr), pixmapDirty(true), hoverToZoomEnabled(hoverToZoomEnabled)
CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool _hoverToZoomEnabled, const bool _raiseOnEnter)
: QWidget(parent), info(nullptr), pixmapDirty(true), hoverToZoomEnabled(_hoverToZoomEnabled),
raiseOnEnter(_raiseOnEnter)
{
setMinimumHeight(baseHeight);
if (hoverToZoomEnabled) {
@ -45,6 +46,17 @@ CardInfoPictureWidget::CardInfoPictureWidget(QWidget *parent, const bool hoverTo
hoverTimer->setSingleShot(true);
connect(hoverTimer, &QTimer::timeout, this, &CardInfoPictureWidget::showEnlargedPixmap);
// Store the widget's original position
originalPos = this->pos();
// Create the animation
animation = new QPropertyAnimation(this, "pos");
animation->setDuration(200); // 200ms animation duration
animation->setEasingCurve(QEasingCurve::OutQuad);
animation->setStartValue(originalPos);
animation->setEndValue(originalPos - QPoint(0, animationOffset));
connect(&SettingsCache::instance(), &SettingsCache::roundCardCornersChanged, this, [this](bool _roundCardCorners) {
Q_UNUSED(_roundCardCorners);
@ -84,6 +96,11 @@ void CardInfoPictureWidget::setHoverToZoomEnabled(const bool enabled)
setMouseTracking(enabled);
}
void CardInfoPictureWidget::setRaiseOnEnterEnabled(const bool enabled)
{
raiseOnEnter = enabled;
}
/**
* @brief Handles widget resizing by updating the pixmap size.
* @param event The resize event (unused).
@ -93,6 +110,7 @@ void CardInfoPictureWidget::setHoverToZoomEnabled(const bool enabled)
void CardInfoPictureWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
originalPos = pos(); // Update the baseline position
updatePixmap();
}
@ -234,6 +252,18 @@ void CardInfoPictureWidget::enterEvent(QEvent *event)
// Emit signal indicating a card is being hovered on
emit hoveredOnCard(info);
if (raiseOnEnter) {
if (animation->state() == QAbstractAnimation::Running) {
animation->pause(); // Pause current animation
} else {
originalPos = this->pos(); // Update the baseline position
animation->setStartValue(originalPos);
animation->setEndValue(originalPos - QPoint(0, animationOffset));
}
animation->setDirection(QAbstractAnimation::Forward);
animation->start();
}
}
/**
@ -243,10 +273,32 @@ void CardInfoPictureWidget::enterEvent(QEvent *event)
void CardInfoPictureWidget::leaveEvent(QEvent *event)
{
QWidget::leaveEvent(event);
if (hoverToZoomEnabled) {
hoverTimer->stop();
enlargedPixmapWidget->hide();
}
if (raiseOnEnter) {
if (animation->state() == QAbstractAnimation::Running) {
animation->pause(); // Pause current animation
}
animation->setDirection(QAbstractAnimation::Backward);
animation->start();
}
}
void CardInfoPictureWidget::moveEvent(QMoveEvent *event)
{
QWidget::moveEvent(event);
hoverTimer->stop();
enlargedPixmapWidget->hide();
if (animation->state() == QAbstractAnimation::Running) {
return;
}
originalPos = this->pos(); // Update the baseline position
}
/**

View File

@ -4,6 +4,7 @@
#include "../../../../game/cards/card_info.h"
#include "card_info_picture_enlarged_widget.h"
#include <QPropertyAnimation>
#include <QTimer>
#include <QWidget>
@ -17,17 +18,20 @@ class CardInfoPictureWidget : public QWidget
Q_OBJECT
public:
explicit CardInfoPictureWidget(QWidget *parent = nullptr, bool hoverToZoomEnabled = false);
explicit CardInfoPictureWidget(QWidget *parent = nullptr,
bool hoverToZoomEnabled = false,
bool raiseOnEnter = false);
CardInfoPtr getInfo()
{
return info;
}
[[nodiscard]] QSize sizeHint() const override;
void setHoverToZoomEnabled(bool enabled);
public slots:
void setCard(CardInfoPtr card);
void setScaleFactor(int scale); // New slot for scaling
void setHoverToZoomEnabled(bool enabled);
void setRaiseOnEnterEnabled(bool enabled);
void updatePixmap();
signals:
@ -45,6 +49,7 @@ protected:
void enterEvent(QEvent *event) override; // Qt5 signature
#endif
void leaveEvent(QEvent *event) override;
void moveEvent(QMoveEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void loadPixmap();
@ -65,10 +70,14 @@ private:
QPixmap resizedPixmap;
bool pixmapDirty;
bool hoverToZoomEnabled;
bool raiseOnEnter;
int hoverActivateThresholdInMs = 500;
CardInfoPictureEnlargedWidget *enlargedPixmapWidget;
int enlargedPixmapOffset = 10;
QTimer *hoverTimer;
QPropertyAnimation *animation;
QPoint originalPos; // Store the original position
const int animationOffset = 10; // Adjust this for how much the widget moves up
QMenu *createRightClickMenu();
QMenu *createViewRelatedCardsMenu();

View File

@ -9,6 +9,7 @@
* @brief Constructs a CardPictureWithTextOverlay widget.
* @param parent The parent widget.
* @param hoverToZoomEnabled If this widget will spawn a larger widget when hovered over.
* @param raiseOnEnter If this widget will raise slightly when entered.
* @param textColor The color of the overlay text.
* @param outlineColor The color of the outline around the text.
* @param fontSize The font size of the overlay text.
@ -18,11 +19,12 @@
*/
CardInfoPictureWithTextOverlayWidget::CardInfoPictureWithTextOverlayWidget(QWidget *parent,
const bool hoverToZoomEnabled,
const bool raiseOnEnter,
const QColor &textColor,
const QColor &outlineColor,
const int fontSize,
const Qt::Alignment alignment)
: CardInfoPictureWidget(parent, hoverToZoomEnabled), textColor(textColor), outlineColor(outlineColor),
: CardInfoPictureWidget(parent, hoverToZoomEnabled, raiseOnEnter), textColor(textColor), outlineColor(outlineColor),
fontSize(fontSize), textAlignment(alignment)
{
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

View File

@ -14,6 +14,7 @@ class CardInfoPictureWithTextOverlayWidget : public CardInfoPictureWidget
public:
explicit CardInfoPictureWithTextOverlayWidget(QWidget *parent = nullptr,
bool hoverToZoomEnabled = false,
bool raiseOnEnter = false,
const QColor &textColor = Qt::white,
const QColor &outlineColor = Qt::black,
int fontSize = 12,

View File

@ -1,5 +1,7 @@
#include "deck_preview_card_picture_widget.h"
#include "../../../../settings/cache_settings.h"
#include <QApplication>
#include <QFileInfo>
#include <QFontMetrics>
@ -22,15 +24,24 @@
*/
DeckPreviewCardPictureWidget::DeckPreviewCardPictureWidget(QWidget *parent,
const bool hoverToZoomEnabled,
const bool raiseOnEnter,
const QColor &textColor,
const QColor &outlineColor,
const int fontSize,
const Qt::Alignment alignment)
: CardInfoPictureWithTextOverlayWidget(parent, hoverToZoomEnabled, textColor, outlineColor, fontSize, alignment)
: CardInfoPictureWithTextOverlayWidget(parent,
hoverToZoomEnabled,
raiseOnEnter,
textColor,
outlineColor,
fontSize,
alignment)
{
singleClickTimer = new QTimer(this);
singleClickTimer->setSingleShot(true);
connect(singleClickTimer, &QTimer::timeout, this, [this]() { emit imageClicked(lastMouseEvent, this); });
connect(&SettingsCache::instance(), &SettingsCache::visualDeckStorageSelectionAnimationChanged, this,
&CardInfoPictureWidget::setRaiseOnEnterEnabled);
}
void DeckPreviewCardPictureWidget::mousePressEvent(QMouseEvent *event)

View File

@ -14,6 +14,7 @@ class DeckPreviewCardPictureWidget final : public CardInfoPictureWithTextOverlay
public:
explicit DeckPreviewCardPictureWidget(QWidget *parent,
bool hoverToZoomEnabled = false,
bool raiseOnEnter = false,
const QColor &textColor = Qt::white,
const QColor &outlineColor = Qt::black,
int fontSize = 12,

View File

@ -31,7 +31,8 @@ DeckPreviewWidget::DeckPreviewWidget(QWidget *_parent,
&VisualDeckStorageTagFilterWidget::refreshTags);
deckLoader->loadFromFileAsync(filePath, DeckLoader::getFormatFromName(filePath), false);
bannerCardDisplayWidget = new DeckPreviewCardPictureWidget(this);
bannerCardDisplayWidget =
new DeckPreviewCardPictureWidget(this, false, visualDeckStorageWidget->deckPreviewSelectionAnimationEnabled);
connect(bannerCardDisplayWidget, &DeckPreviewCardPictureWidget::imageClicked, this,
&DeckPreviewWidget::imageClickedEvent);

View File

@ -125,6 +125,10 @@ VisualDeckStorageWidget::VisualDeckStorageWidget(QWidget *parent) : QWidget(pare
tagFilterWidget = new VisualDeckStorageTagFilterWidget(this);
updateTagsVisibility(SettingsCache::instance().getVisualDeckStorageShowTagFilter());
deckPreviewSelectionAnimationEnabled = SettingsCache::instance().getVisualDeckStorageSelectionAnimation();
connect(&SettingsCache::instance(), &SettingsCache::visualDeckStorageSelectionAnimationChanged, this,
&VisualDeckStorageWidget::updateSelectionAnimationEnabled);
// deck area
scrollArea = new QScrollArea(this);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
@ -270,4 +274,9 @@ void VisualDeckStorageWidget::updateTagsVisibility(const bool visible)
} else {
tagFilterWidget->setHidden(true);
}
}
void VisualDeckStorageWidget::updateSelectionAnimationEnabled(const bool enabled)
{
deckPreviewSelectionAnimationEnabled = enabled;
}

View File

@ -31,6 +31,7 @@ public:
CardSizeWidget *cardSizeWidget;
VisualDeckStorageTagFilterWidget *tagFilterWidget;
bool deckPreviewSelectionAnimationEnabled;
public slots:
void createRootFolderWidget(); // Refresh the display of cards based on the current sorting option
@ -39,6 +40,7 @@ public slots:
void updateColorFilter();
void updateSearchFilter();
void updateTagsVisibility(bool visible);
void updateSelectionAnimationEnabled(bool enabled);
void updateSortOrder();
void resizeEvent(QResizeEvent *event) override;
void showEvent(QShowEvent *event) override;

View File

@ -676,6 +676,11 @@ UserInterfaceSettingsPage::UserInterfaceSettingsPage()
connect(&visualDeckStorageInGameCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance(),
&SettingsCache::setVisualDeckStorageInGame);
visualDeckStorageSelectionAnimationCheckBox.setChecked(
SettingsCache::instance().getVisualDeckStorageSelectionAnimation());
connect(&visualDeckStorageSelectionAnimationCheckBox, &QCheckBox::QT_STATE_CHANGED, &SettingsCache::instance(),
&SettingsCache::setVisualDeckStorageSelectionAnimation);
visualDeckStoragePromptForConversionSelector.addItem(""); // these will be set in retranslateUI
visualDeckStoragePromptForConversionSelector.addItem("");
visualDeckStoragePromptForConversionSelector.addItem("");
@ -697,8 +702,9 @@ UserInterfaceSettingsPage::UserInterfaceSettingsPage()
auto *deckEditorGrid = new QGridLayout;
deckEditorGrid->addWidget(&openDeckInNewTabCheckBox, 0, 0);
deckEditorGrid->addWidget(&visualDeckStorageInGameCheckBox, 1, 0);
deckEditorGrid->addWidget(&visualDeckStoragePromptForConversionLabel, 2, 0);
deckEditorGrid->addWidget(&visualDeckStoragePromptForConversionSelector, 2, 1);
deckEditorGrid->addWidget(&visualDeckStorageSelectionAnimationCheckBox, 2, 0);
deckEditorGrid->addWidget(&visualDeckStoragePromptForConversionLabel, 3, 0);
deckEditorGrid->addWidget(&visualDeckStoragePromptForConversionSelector, 3, 1);
deckEditorGroupBox = new QGroupBox;
deckEditorGroupBox->setLayout(deckEditorGrid);
@ -759,6 +765,7 @@ void UserInterfaceSettingsPage::retranslateUi()
deckEditorGroupBox->setTitle(tr("Deck editor/storage settings"));
openDeckInNewTabCheckBox.setText(tr("Open deck in new tab by default"));
visualDeckStorageInGameCheckBox.setText(tr("Use visual deck storage in game lobby"));
visualDeckStorageSelectionAnimationCheckBox.setText(tr("Use selection animation for Visual Deck Storage"));
visualDeckStoragePromptForConversionLabel.setText(
tr("When adding a tag in the visual deck storage to a .txt deck:"));
visualDeckStoragePromptForConversionSelector.setItemText(visualDeckStoragePromptForConversionIndexNone,

View File

@ -154,6 +154,7 @@ private:
QLabel visualDeckStoragePromptForConversionLabel;
QComboBox visualDeckStoragePromptForConversionSelector;
QCheckBox visualDeckStorageInGameCheckBox;
QCheckBox visualDeckStorageSelectionAnimationCheckBox;
QLabel rewindBufferingMsLabel;
QSpinBox rewindBufferingMsBox;
QGroupBox *generalGroupBox;

View File

@ -280,6 +280,8 @@ SettingsCache::SettingsCache()
settings->value("interface/visualdeckstoragepromptforconversion", true).toBool();
visualDeckStorageAlwaysConvert = settings->value("interface/visualdeckstoragealwaysconvert", false).toBool();
visualDeckStorageInGame = settings->value("interface/visualdeckstorageingame", true).toBool();
visualDeckStorageSelectionAnimation =
settings->value("interface/visualdeckstorageselectionanimation", true).toBool();
horizontalHand = settings->value("hand/horizontal", true).toBool();
invertVerticalCoordinate = settings->value("table/invert_vertical", false).toBool();
minPlayersForMultiColumnLayout = settings->value("interface/min_players_multicolumn", 4).toInt();
@ -761,6 +763,13 @@ void SettingsCache::setVisualDeckStorageInGame(QT_STATE_CHANGED_T value)
emit visualDeckStorageInGameChanged(visualDeckStorageInGame);
}
void SettingsCache::setVisualDeckStorageSelectionAnimation(QT_STATE_CHANGED_T value)
{
visualDeckStorageSelectionAnimation = value;
settings->setValue("interface/visualdeckstorageselectionanimation", visualDeckStorageSelectionAnimation);
emit visualDeckStorageSelectionAnimationChanged(visualDeckStorageSelectionAnimation);
}
void SettingsCache::setHorizontalHand(QT_STATE_CHANGED_T _horizontalHand)
{
horizontalHand = static_cast<bool>(_horizontalHand);

View File

@ -68,6 +68,7 @@ signals:
void visualDeckStorageDrawUnusedColorIdentitiesChanged(bool _visible);
void visualDeckStorageUnusedColorIdentitiesOpacityChanged(bool value);
void visualDeckStorageInGameChanged(bool enabled);
void visualDeckStorageSelectionAnimationChanged(bool enabled);
void horizontalHandChanged();
void handJustificationChanged();
void invertVerticalCoordinateChanged();
@ -145,6 +146,7 @@ private:
bool visualDeckStoragePromptForConversion;
bool visualDeckStorageAlwaysConvert;
bool visualDeckStorageInGame;
bool visualDeckStorageSelectionAnimation;
bool horizontalHand;
bool invertVerticalCoordinate;
int minPlayersForMultiColumnLayout;
@ -466,6 +468,10 @@ public:
{
return visualDeckStorageInGame;
}
bool getVisualDeckStorageSelectionAnimation() const
{
return visualDeckStorageSelectionAnimation;
}
bool getHorizontalHand() const
{
return horizontalHand;
@ -806,6 +812,7 @@ public slots:
void setVisualDeckStoragePromptForConversion(bool _visualDeckStoragePromptForConversion);
void setVisualDeckStorageAlwaysConvert(bool _visualDeckStorageAlwaysConvert);
void setVisualDeckStorageInGame(QT_STATE_CHANGED_T value);
void setVisualDeckStorageSelectionAnimation(QT_STATE_CHANGED_T value);
void setHorizontalHand(QT_STATE_CHANGED_T _horizontalHand);
void setInvertVerticalCoordinate(QT_STATE_CHANGED_T _invertVerticalCoordinate);
void setMinPlayersForMultiColumnLayout(int _minPlayersForMultiColumnLayout);

View File

@ -246,6 +246,9 @@ void SettingsCache::setVisualDeckStorageAlwaysConvert(bool /* _visualDeckStorage
void SettingsCache::setVisualDeckStorageInGame(QT_STATE_CHANGED_T /* value */)
{
}
void SettingsCache::setVisualDeckStorageSelectionAnimation(QT_STATE_CHANGED_T /* value */)
{
}
void SettingsCache::setHorizontalHand(QT_STATE_CHANGED_T /* _horizontalHand */)
{
}

View File

@ -250,6 +250,9 @@ void SettingsCache::setVisualDeckStorageAlwaysConvert(bool /* _visualDeckStorage
void SettingsCache::setVisualDeckStorageInGame(QT_STATE_CHANGED_T /* value */)
{
}
void SettingsCache::setVisualDeckStorageSelectionAnimation(QT_STATE_CHANGED_T /* value */)
{
}
void SettingsCache::setHorizontalHand(QT_STATE_CHANGED_T /* _horizontalHand */)
{
}