Don't have widget hold pointer to state -> Copy what we need and subscribe to changes.

Took 12 minutes

Took 5 seconds
This commit is contained in:
Lukas Brübach 2026-05-21 06:42:07 +02:00
parent 2063be6fa0
commit 46e7455d09
4 changed files with 35 additions and 26 deletions

View file

@ -17,12 +17,13 @@
#include <libcockatrice/protocol/pb/command_set_counter.pb.h> #include <libcockatrice/protocol/pb/command_set_counter.pb.h>
#include <libcockatrice/utility/expression.h> #include <libcockatrice/utility/expression.h>
AbstractCounter::AbstractCounter(CounterState *_state, AbstractCounter::AbstractCounter(CounterState *state,
PlayerLogic *_player, PlayerLogic *_player,
bool _shownInCounterArea, bool _shownInCounterArea,
bool _useNameForShortcut, bool _useNameForShortcut,
QGraphicsItem *parent) 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) shownInCounterArea(_shownInCounterArea)
{ {
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
@ -89,14 +90,14 @@ void AbstractCounter::setShortcutsActive()
} }
ShortcutsSettings &sc = SettingsCache::instance().shortcuts(); ShortcutsSettings &sc = SettingsCache::instance().shortcuts();
shortcutActive = true; shortcutActive = true;
if (state->getName() == "life") { if (name == "life") {
aSet->setShortcuts(sc.getShortcut("Player/aSet")); aSet->setShortcuts(sc.getShortcut("Player/aSet"));
aDec->setShortcuts(sc.getShortcut("Player/aDec")); aDec->setShortcuts(sc.getShortcut("Player/aDec"));
aInc->setShortcuts(sc.getShortcut("Player/aInc")); aInc->setShortcuts(sc.getShortcut("Player/aInc"));
} else if (useNameForShortcut) { } else if (useNameForShortcut) {
aSet->setShortcuts(sc.getShortcut("Player/aSetCounter_" + state->getName())); aSet->setShortcuts(sc.getShortcut("Player/aSetCounter_" + name));
aDec->setShortcuts(sc.getShortcut("Player/aDecCounter_" + state->getName())); aDec->setShortcuts(sc.getShortcut("Player/aDecCounter_" + name));
aInc->setShortcuts(sc.getShortcut("Player/aIncCounter_" + state->getName())); aInc->setShortcuts(sc.getShortcut("Player/aIncCounter_" + name));
} }
} }
@ -107,7 +108,7 @@ void AbstractCounter::setShortcutsInactive()
} }
shortcutActive = false; shortcutActive = false;
if (state->getName() == "life" || useNameForShortcut) { if (name == "life" || useNameForShortcut) {
aSet->setShortcut(QKeySequence()); aSet->setShortcut(QKeySequence());
aDec->setShortcut(QKeySequence()); aDec->setShortcut(QKeySequence());
aInc->setShortcut(QKeySequence()); aInc->setShortcut(QKeySequence());
@ -134,7 +135,7 @@ void AbstractCounter::mousePressEvent(QGraphicsSceneMouseEvent *event)
} }
} else { } else {
Command_IncCounter cmd; Command_IncCounter cmd;
cmd.set_counter_id(state->getId()); cmd.set_counter_id(id);
cmd.set_delta(event->button() == Qt::LeftButton ? 1 : -1); cmd.set_delta(event->button() == Qt::LeftButton ? 1 : -1);
player->getPlayerActions()->sendGameCommand(cmd); player->getPlayerActions()->sendGameCommand(cmd);
} }
@ -155,7 +156,7 @@ void AbstractCounter::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
void AbstractCounter::incrementCounter() void AbstractCounter::incrementCounter()
{ {
Command_IncCounter cmd; Command_IncCounter cmd;
cmd.set_counter_id(state->getId()); cmd.set_counter_id(id);
cmd.set_delta(static_cast<QAction *>(sender())->data().toInt()); cmd.set_delta(static_cast<QAction *>(sender())->data().toInt());
player->getPlayerActions()->sendGameCommand(cmd); player->getPlayerActions()->sendGameCommand(cmd);
} }
@ -168,7 +169,7 @@ void AbstractCounter::setCounter()
} }
dialogSemaphore = true; dialogSemaphore = true;
AbstractCounterDialog dlg(state->getName(), QString::number(state->getValue()), parent); AbstractCounterDialog dlg(name, QString::number(value), parent);
const int ok = dlg.exec(); const int ok = dlg.exec();
dialogSemaphore = false; dialogSemaphore = false;
@ -180,9 +181,9 @@ void AbstractCounter::setCounter()
return; return;
} }
Expression exp(state->getValue()); Expression exp(value);
Command_SetCounter cmd; Command_SetCounter cmd;
cmd.set_counter_id(state->getId()); cmd.set_counter_id(id);
cmd.set_value(static_cast<int>(exp.parse(dlg.textValue()))); cmd.set_value(static_cast<int>(exp.parse(dlg.textValue())));
player->getPlayerActions()->sendGameCommand(cmd); player->getPlayerActions()->sendGameCommand(cmd);
} }

View file

@ -26,8 +26,12 @@ class AbstractCounter : public QObject, public QGraphicsItem, public AbstractPla
Q_INTERFACES(QGraphicsItem) Q_INTERFACES(QGraphicsItem)
protected: protected:
CounterState *state;
PlayerLogic *player; PlayerLogic *player;
int id;
QString name;
int value;
QColor color;
int radius;
bool hovered = false; bool hovered = false;
bool useNameForShortcut; bool useNameForShortcut;
@ -61,25 +65,29 @@ public:
void setShortcutsInactive() override; void setShortcutsInactive() override;
void delCounter(); void delCounter();
CounterState *getState() const
{
return state;
}
QMenu *getMenu() const QMenu *getMenu() const
{ {
return menu; return menu;
} }
int getId() const int getId() const
{ {
return state->getId(); return id;
} }
QString getName() const QString getName() const
{ {
return state->getName(); return name;
}
QColor getColor() const
{
return color;
}
int getRadius() const
{
return radius;
} }
int getValue() const int getValue() const
{ {
return state->getValue(); return value;
} }
bool getShownInCounterArea() const bool getShownInCounterArea() const
{ {

View file

@ -13,7 +13,7 @@ GeneralCounter::GeneralCounter(CounterState *state, PlayerLogic *player, bool us
QRectF GeneralCounter::boundingRect() const 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*/) 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()); QRectF mapRect = painter->combinedTransform().mapRect(boundingRect());
int translatedHeight = mapRect.size().height(); int translatedHeight = mapRect.size().height();
qreal scaleFactor = translatedHeight / boundingRect().height(); qreal scaleFactor = translatedHeight / boundingRect().height();
QPixmap pixmap = CounterPixmapGenerator::generatePixmap(translatedHeight, state->getName(), hovered); QPixmap pixmap = CounterPixmapGenerator::generatePixmap(translatedHeight, name, hovered);
painter->save(); painter->save();
resetPainterTransform(painter); resetPainterTransform(painter);
painter->drawPixmap(QPoint(0, 0), pixmap); painter->drawPixmap(QPoint(0, 0), pixmap);
if (state->getValue()) { if (value) {
QFont f("Serif"); QFont f("Serif");
f.setPixelSize(qMax((int)(state->getRadius() * scaleFactor), 10)); f.setPixelSize(qMax((int)(radius * scaleFactor), 10));
f.setWeight(QFont::Bold); f.setWeight(QFont::Bold);
painter->setPen(Qt::black); painter->setPen(Qt::black);
painter->setFont(f); painter->setFont(f);
painter->drawText(mapRect, Qt::AlignCenter, QString::number(state->getValue())); painter->drawText(mapRect, Qt::AlignCenter, QString::number(value));
} }
painter->restore(); painter->restore();
} }

View file

@ -44,7 +44,7 @@ void PlayerCounter::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*
font.setPixelSize(qMax(qRound(translatedSize.height() / 1.3), 9)); font.setPixelSize(qMax(qRound(translatedSize.height() / 1.3), 9));
painter->setFont(font); painter->setFont(font);
painter->setPen(Qt::white); 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) PlayerTarget::PlayerTarget(PlayerLogic *_owner, QGraphicsItem *parentItem)