From 46e7455d09767775a1c033f980069c7118d85175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Thu, 21 May 2026 06:42:07 +0200 Subject: [PATCH] Don't have widget hold pointer to state -> Copy what we need and subscribe to changes. Took 12 minutes Took 5 seconds --- .../src/game/board/abstract_counter.cpp | 25 ++++++++++--------- cockatrice/src/game/board/abstract_counter.h | 24 ++++++++++++------ cockatrice/src/game/board/counter_general.cpp | 10 ++++---- cockatrice/src/game/player/player_target.cpp | 2 +- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/cockatrice/src/game/board/abstract_counter.cpp b/cockatrice/src/game/board/abstract_counter.cpp index c7dc20814..88573cccf 100644 --- a/cockatrice/src/game/board/abstract_counter.cpp +++ b/cockatrice/src/game/board/abstract_counter.cpp @@ -17,12 +17,13 @@ #include #include -AbstractCounter::AbstractCounter(CounterState *_state, +AbstractCounter::AbstractCounter(CounterState *state, PlayerLogic *_player, bool _shownInCounterArea, bool _useNameForShortcut, QGraphicsItem *parent) - : QGraphicsItem(parent), state(_state), player(_player), useNameForShortcut(_useNameForShortcut), + : QGraphicsItem(parent), player(_player), id(state->getId()), name(state->getName()), value(state->getValue()), + color(state->getColor()), radius(state->getRadius()), useNameForShortcut(_useNameForShortcut), shownInCounterArea(_shownInCounterArea) { setAcceptHoverEvents(true); @@ -89,14 +90,14 @@ void AbstractCounter::setShortcutsActive() } ShortcutsSettings &sc = SettingsCache::instance().shortcuts(); shortcutActive = true; - if (state->getName() == "life") { + if (name == "life") { aSet->setShortcuts(sc.getShortcut("Player/aSet")); aDec->setShortcuts(sc.getShortcut("Player/aDec")); aInc->setShortcuts(sc.getShortcut("Player/aInc")); } else if (useNameForShortcut) { - aSet->setShortcuts(sc.getShortcut("Player/aSetCounter_" + state->getName())); - aDec->setShortcuts(sc.getShortcut("Player/aDecCounter_" + state->getName())); - aInc->setShortcuts(sc.getShortcut("Player/aIncCounter_" + state->getName())); + aSet->setShortcuts(sc.getShortcut("Player/aSetCounter_" + name)); + aDec->setShortcuts(sc.getShortcut("Player/aDecCounter_" + name)); + aInc->setShortcuts(sc.getShortcut("Player/aIncCounter_" + name)); } } @@ -107,7 +108,7 @@ void AbstractCounter::setShortcutsInactive() } shortcutActive = false; - if (state->getName() == "life" || useNameForShortcut) { + if (name == "life" || useNameForShortcut) { aSet->setShortcut(QKeySequence()); aDec->setShortcut(QKeySequence()); aInc->setShortcut(QKeySequence()); @@ -134,7 +135,7 @@ void AbstractCounter::mousePressEvent(QGraphicsSceneMouseEvent *event) } } else { Command_IncCounter cmd; - cmd.set_counter_id(state->getId()); + cmd.set_counter_id(id); cmd.set_delta(event->button() == Qt::LeftButton ? 1 : -1); player->getPlayerActions()->sendGameCommand(cmd); } @@ -155,7 +156,7 @@ void AbstractCounter::hoverLeaveEvent(QGraphicsSceneHoverEvent *) void AbstractCounter::incrementCounter() { Command_IncCounter cmd; - cmd.set_counter_id(state->getId()); + cmd.set_counter_id(id); cmd.set_delta(static_cast(sender())->data().toInt()); player->getPlayerActions()->sendGameCommand(cmd); } @@ -168,7 +169,7 @@ void AbstractCounter::setCounter() } dialogSemaphore = true; - AbstractCounterDialog dlg(state->getName(), QString::number(state->getValue()), parent); + AbstractCounterDialog dlg(name, QString::number(value), parent); const int ok = dlg.exec(); dialogSemaphore = false; @@ -180,9 +181,9 @@ void AbstractCounter::setCounter() return; } - Expression exp(state->getValue()); + Expression exp(value); Command_SetCounter cmd; - cmd.set_counter_id(state->getId()); + cmd.set_counter_id(id); cmd.set_value(static_cast(exp.parse(dlg.textValue()))); player->getPlayerActions()->sendGameCommand(cmd); } diff --git a/cockatrice/src/game/board/abstract_counter.h b/cockatrice/src/game/board/abstract_counter.h index ae7321cbb..57bc43856 100644 --- a/cockatrice/src/game/board/abstract_counter.h +++ b/cockatrice/src/game/board/abstract_counter.h @@ -26,8 +26,12 @@ class AbstractCounter : public QObject, public QGraphicsItem, public AbstractPla Q_INTERFACES(QGraphicsItem) protected: - CounterState *state; PlayerLogic *player; + int id; + QString name; + int value; + QColor color; + int radius; bool hovered = false; bool useNameForShortcut; @@ -61,25 +65,29 @@ public: void setShortcutsInactive() override; void delCounter(); - CounterState *getState() const - { - return state; - } QMenu *getMenu() const { return menu; } int getId() const { - return state->getId(); + return id; } QString getName() const { - return state->getName(); + return name; + } + QColor getColor() const + { + return color; + } + int getRadius() const + { + return radius; } int getValue() const { - return state->getValue(); + return value; } bool getShownInCounterArea() const { diff --git a/cockatrice/src/game/board/counter_general.cpp b/cockatrice/src/game/board/counter_general.cpp index ee08d9f2e..5147ede6b 100644 --- a/cockatrice/src/game/board/counter_general.cpp +++ b/cockatrice/src/game/board/counter_general.cpp @@ -13,7 +13,7 @@ GeneralCounter::GeneralCounter(CounterState *state, PlayerLogic *player, bool us QRectF GeneralCounter::boundingRect() const { - return QRectF(0, 0, state->getRadius() * 2, state->getRadius() * 2); + return QRectF(0, 0, radius * 2, radius * 2); } void GeneralCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/) @@ -21,19 +21,19 @@ void GeneralCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * / QRectF mapRect = painter->combinedTransform().mapRect(boundingRect()); int translatedHeight = mapRect.size().height(); qreal scaleFactor = translatedHeight / boundingRect().height(); - QPixmap pixmap = CounterPixmapGenerator::generatePixmap(translatedHeight, state->getName(), hovered); + QPixmap pixmap = CounterPixmapGenerator::generatePixmap(translatedHeight, name, hovered); painter->save(); resetPainterTransform(painter); painter->drawPixmap(QPoint(0, 0), pixmap); - if (state->getValue()) { + if (value) { QFont f("Serif"); - f.setPixelSize(qMax((int)(state->getRadius() * scaleFactor), 10)); + f.setPixelSize(qMax((int)(radius * scaleFactor), 10)); f.setWeight(QFont::Bold); painter->setPen(Qt::black); painter->setFont(f); - painter->drawText(mapRect, Qt::AlignCenter, QString::number(state->getValue())); + painter->drawText(mapRect, Qt::AlignCenter, QString::number(value)); } painter->restore(); } diff --git a/cockatrice/src/game/player/player_target.cpp b/cockatrice/src/game/player/player_target.cpp index 632a6aed0..97fd51998 100644 --- a/cockatrice/src/game/player/player_target.cpp +++ b/cockatrice/src/game/player/player_target.cpp @@ -44,7 +44,7 @@ void PlayerCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * /* font.setPixelSize(qMax(qRound(translatedSize.height() / 1.3), 9)); painter->setFont(font); painter->setPen(Qt::white); - painter->drawText(translatedRect, Qt::AlignCenter, QString::number(state->getValue())); + painter->drawText(translatedRect, Qt::AlignCenter, QString::number(value)); } PlayerTarget::PlayerTarget(PlayerLogic *_owner, QGraphicsItem *parentItem)