From 56bbd8a172340e3feb75ebce20e88f2154b4bbf4 Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Wed, 9 Apr 2025 08:26:14 -0700 Subject: [PATCH] Refactor: move last token info into struct (#5808) * add override * refactor token info into struct * correct default destroy value --- cockatrice/src/dialogs/dlg_create_token.cpp | 28 +++--------- cockatrice/src/dialogs/dlg_create_token.h | 17 ++++--- cockatrice/src/game/player/player.cpp | 50 ++++++++++----------- cockatrice/src/game/player/player.h | 6 ++- 4 files changed, 45 insertions(+), 56 deletions(-) diff --git a/cockatrice/src/dialogs/dlg_create_token.cpp b/cockatrice/src/dialogs/dlg_create_token.cpp index 25d549868..cf0614d66 100644 --- a/cockatrice/src/dialogs/dlg_create_token.cpp +++ b/cockatrice/src/dialogs/dlg_create_token.cpp @@ -224,27 +224,11 @@ void DlgCreateToken::actReject() reject(); } -QString DlgCreateToken::getName() const +TokenInfo DlgCreateToken::getTokenInfo() const { - return nameEdit->text(); -} - -QString DlgCreateToken::getColor() const -{ - return QString(colorEdit->itemData(colorEdit->currentIndex()).toChar()); -} - -QString DlgCreateToken::getPT() const -{ - return ptEdit->text(); -} - -QString DlgCreateToken::getAnnotation() const -{ - return annotationEdit->text(); -} - -bool DlgCreateToken::getDestroy() const -{ - return destroyCheckBox->isChecked(); + return {.name = nameEdit->text(), + .color = colorEdit->itemData(colorEdit->currentIndex()).toString(), + .pt = ptEdit->text(), + .annotation = annotationEdit->text(), + .destroy = destroyCheckBox->isChecked()}; } diff --git a/cockatrice/src/dialogs/dlg_create_token.h b/cockatrice/src/dialogs/dlg_create_token.h index a226faaca..373e094f1 100644 --- a/cockatrice/src/dialogs/dlg_create_token.h +++ b/cockatrice/src/dialogs/dlg_create_token.h @@ -17,19 +17,24 @@ class CardDatabaseModel; class TokenDisplayModel; class CardInfoPictureWidget; +struct TokenInfo +{ + QString name; + QString color; + QString pt; + QString annotation; + bool destroy = true; +}; + class DlgCreateToken : public QDialog { Q_OBJECT public: explicit DlgCreateToken(const QStringList &_predefinedTokens, QWidget *parent = nullptr); - QString getName() const; - QString getColor() const; - QString getPT() const; - QString getAnnotation() const; - bool getDestroy() const; + TokenInfo getTokenInfo() const; protected: - void closeEvent(QCloseEvent *event); + void closeEvent(QCloseEvent *event) override; private slots: void tokenSelectionChanged(const QModelIndex ¤t, const QModelIndex &previous); void updateSearch(const QString &search); diff --git a/cockatrice/src/game/player/player.cpp b/cockatrice/src/game/player/player.cpp index d82a6ee6e..26f34e977 100644 --- a/cockatrice/src/game/player/player.cpp +++ b/cockatrice/src/game/player/player.cpp @@ -4,7 +4,6 @@ #include "../../client/tabs/tab_game.h" #include "../../client/ui/theme_manager.h" #include "../../deck/deck_loader.h" -#include "../../dialogs/dlg_create_token.h" #include "../../dialogs/dlg_move_top_cards_until.h" #include "../../dialogs/dlg_roll_dice.h" #include "../../main.h" @@ -109,9 +108,9 @@ void PlayerArea::setPlayerZoneId(int _playerZoneId) } Player::Player(const ServerInfo_User &info, int _id, bool _local, bool _judge, TabGame *_parent) - : QObject(_parent), game(_parent), movingCardsUntil(false), shortcutsActive(false), lastTokenDestroy(true), - lastTokenTableRow(0), id(_id), active(false), local(_local), judge(_judge), mirrored(false), handVisible(false), - conceded(false), zoneId(0), dialogSemaphore(false), deck(nullptr) + : QObject(_parent), game(_parent), movingCardsUntil(false), shortcutsActive(false), lastTokenTableRow(0), id(_id), + active(false), local(_local), judge(_judge), mirrored(false), handVisible(false), conceded(false), zoneId(0), + dialogSemaphore(false), deck(nullptr) { userInfo = new ServerInfo_User; userInfo->CopyFrom(info); @@ -1826,37 +1825,35 @@ void Player::actCreateToken() return; } - lastTokenName = dlg.getName(); - lastTokenPT = dlg.getPT(); - CardInfoPtr correctedCard = CardDatabaseManager::getInstance()->guessCard(lastTokenName); + lastTokenInfo = dlg.getTokenInfo(); + + CardInfoPtr correctedCard = CardDatabaseManager::getInstance()->guessCard(lastTokenInfo.name); if (correctedCard) { - lastTokenName = correctedCard->getName(); + lastTokenInfo.name = correctedCard->getName(); lastTokenTableRow = TableZone::clampValidTableRow(2 - correctedCard->getTableRow()); - if (lastTokenPT.isEmpty()) { - lastTokenPT = correctedCard->getPowTough(); + if (lastTokenInfo.pt.isEmpty()) { + lastTokenInfo.pt = correctedCard->getPowTough(); } } - lastTokenColor = dlg.getColor(); - lastTokenAnnotation = dlg.getAnnotation(); - lastTokenDestroy = dlg.getDestroy(); + aCreateAnotherToken->setEnabled(true); - aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenName)); + aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenInfo.name)); actCreateAnotherToken(); } void Player::actCreateAnotherToken() { - if (lastTokenName.isEmpty()) { + if (lastTokenInfo.name.isEmpty()) { return; } Command_CreateToken cmd; cmd.set_zone("table"); - cmd.set_card_name(lastTokenName.toStdString()); - cmd.set_color(lastTokenColor.toStdString()); - cmd.set_pt(lastTokenPT.toStdString()); - cmd.set_annotation(lastTokenAnnotation.toStdString()); - cmd.set_destroy_on_zone_change(lastTokenDestroy); + cmd.set_card_name(lastTokenInfo.name.toStdString()); + cmd.set_color(lastTokenInfo.color.toStdString()); + cmd.set_pt(lastTokenInfo.pt.toStdString()); + cmd.set_annotation(lastTokenInfo.annotation.toStdString()); + cmd.set_destroy_on_zone_change(lastTokenInfo.destroy); cmd.set_x(-1); cmd.set_y(lastTokenTableRow); @@ -4180,12 +4177,13 @@ void Player::setLastToken(CardInfoPtr cardInfo) return; } - lastTokenName = cardInfo->getName(); - lastTokenColor = cardInfo->getColors().isEmpty() ? QString() : cardInfo->getColors().left(1).toLower(); - lastTokenPT = cardInfo->getPowTough(); - lastTokenAnnotation = SettingsCache::instance().getAnnotateTokens() ? cardInfo->getText() : ""; + lastTokenInfo = {.name = cardInfo->getName(), + .color = cardInfo->getColors().isEmpty() ? QString() : cardInfo->getColors().left(1).toLower(), + .pt = cardInfo->getPowTough(), + .annotation = SettingsCache::instance().getAnnotateTokens() ? cardInfo->getText() : "", + .destroy = true}; + lastTokenTableRow = TableZone::clampValidTableRow(2 - cardInfo->getTableRow()); - lastTokenDestroy = true; - aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenName)); + aCreateAnotherToken->setText(tr("C&reate another %1 token").arg(lastTokenInfo.name)); aCreateAnotherToken->setEnabled(true); } diff --git a/cockatrice/src/game/player/player.h b/cockatrice/src/game/player/player.h index 26df3ae50..e32798481 100644 --- a/cockatrice/src/game/player/player.h +++ b/cockatrice/src/game/player/player.h @@ -2,6 +2,7 @@ #define PLAYER_H #include "../../client/tearoff_menu.h" +#include "../../dialogs/dlg_create_token.h" #include "../board/abstract_graphics_item.h" #include "../cards/card_database.h" #include "../filters/filter_string.h" @@ -291,9 +292,10 @@ private: int defaultNumberTopCardsToPlaceBelow = 1; int defaultNumberBottomCards = 1; int defaultNumberDieRoll = 20; - QString lastTokenName, lastTokenColor, lastTokenPT, lastTokenAnnotation; - bool lastTokenDestroy; + + TokenInfo lastTokenInfo; int lastTokenTableRow; + ServerInfo_User *userInfo; int id; bool active;