implement a maximum amount of cardmenus generated for views (#4262)

This commit is contained in:
ebbit1q 2021-03-13 20:44:57 +01:00 committed by GitHub
parent 8e1d7d12e0
commit 00ed5c370c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View file

@ -18,9 +18,15 @@
#include <QMenu>
#include <QPainter>
CardItem::CardItem(Player *_owner, const QString &_name, int _cardid, bool _revealedCard, QGraphicsItem *parent)
: AbstractCardItem(_name, _owner, _cardid, parent), zone(0), revealedCard(_revealedCard), attacking(false),
destroyOnZoneChange(false), doesntUntap(false), dragItem(0), attachedTo(0)
CardItem::CardItem(Player *_owner,
const QString &_name,
int _cardid,
bool _revealedCard,
QGraphicsItem *parent,
CardZone *_zone,
bool updateMenu)
: AbstractCardItem(_name, _owner, _cardid, parent), zone(_zone), revealedCard(_revealedCard), attacking(false),
destroyOnZoneChange(false), doesntUntap(false), dragItem(nullptr), attachedTo(nullptr)
{
owner->addCard(this);
@ -29,7 +35,9 @@ CardItem::CardItem(Player *_owner, const QString &_name, int _cardid, bool _reve
moveMenu = new QMenu;
retranslateUi();
emit updateCardMenu(this);
if (updateMenu) { // avoid updating card menu too often
emit updateCardMenu(this);
}
}
CardItem::~CardItem()
@ -382,7 +390,7 @@ void CardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
}
}
if (owner != nullptr){ // cards without owner will be deleted
if (owner != nullptr) { // cards without owner will be deleted
setCursor(Qt::OpenHandCursor);
}
AbstractCardItem::mouseReleaseEvent(event);

View file

@ -52,7 +52,9 @@ public:
const QString &_name = QString(),
int _cardid = -1,
bool revealedCard = false,
QGraphicsItem *parent = nullptr);
QGraphicsItem *parent = nullptr,
CardZone *_zone = nullptr,
bool updateMenu = false);
~CardItem();
void retranslateUi();
CardZone *getZone() const

View file

@ -83,15 +83,19 @@ void ZoneViewZone::initializeCards(const QList<const ServerInfo_Card *> &cardLis
void ZoneViewZone::zoneDumpReceived(const Response &r)
{
static constexpr int MAX_VIEW_MENU = 300;
const Response_DumpZone &resp = r.GetExtension(Response_DumpZone::ext);
const int respCardListSize = resp.zone_info().card_list_size();
for (int i = 0; i < respCardListSize; ++i) {
const ServerInfo_Card &cardInfo = resp.zone_info().card_list(i);
CardItem *card = new CardItem(player, QString::fromStdString(cardInfo.name()), cardInfo.id(), revealZone, this);
addCard(card, false, i);
auto cardName = QString::fromStdString(cardInfo.name());
// stop updating card menus after MAX_VIEW_MENU cards
// this means only the first cards in the menu have actions but others can still be dragged
auto *card = new CardItem(player, cardName, cardInfo.id(), revealZone, this, this, i < MAX_VIEW_MENU);
cards.insert(i, card);
}
reorganizeCards();
emit cardCountChanged();
}
// Because of boundingRect(), this function must not be called before the zone was added to a scene.