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/utility/expression.h>
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<QAction *>(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<int>(exp.parse(dlg.textValue())));
player->getPlayerActions()->sendGameCommand(cmd);
}

View file

@ -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
{

View file

@ -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();
}

View file

@ -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)