mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-01 11:03:54 -07:00
[Game] Refactor: explicitly pass params from card counter actions
This commit is contained in:
parent
dbed6890da
commit
09f10c3c0a
3 changed files with 78 additions and 77 deletions
|
|
@ -75,17 +75,17 @@ CardMenu::CardMenu(Player *_player, const CardItem *_card, bool _shortcutsActive
|
||||||
|
|
||||||
for (int i = 0; i < 6; ++i) {
|
for (int i = 0; i < 6; ++i) {
|
||||||
auto *tempAddCounter = new QAction(this);
|
auto *tempAddCounter = new QAction(this);
|
||||||
tempAddCounter->setData(9 + i * 1000);
|
|
||||||
auto *tempRemoveCounter = new QAction(this);
|
auto *tempRemoveCounter = new QAction(this);
|
||||||
tempRemoveCounter->setData(10 + i * 1000);
|
|
||||||
auto *tempSetCounter = new QAction(this);
|
auto *tempSetCounter = new QAction(this);
|
||||||
tempSetCounter->setData(11 + i * 1000);
|
|
||||||
aAddCounter.append(tempAddCounter);
|
aAddCounter.append(tempAddCounter);
|
||||||
aRemoveCounter.append(tempRemoveCounter);
|
aRemoveCounter.append(tempRemoveCounter);
|
||||||
aSetCounter.append(tempSetCounter);
|
aSetCounter.append(tempSetCounter);
|
||||||
connect(tempAddCounter, &QAction::triggered, playerActions, &PlayerActions::actCardCounterTrigger);
|
connect(tempAddCounter, &QAction::triggered, playerActions,
|
||||||
connect(tempRemoveCounter, &QAction::triggered, playerActions, &PlayerActions::actCardCounterTrigger);
|
[playerActions, i] { playerActions->actAddCardCounter(i); });
|
||||||
connect(tempSetCounter, &QAction::triggered, playerActions, &PlayerActions::actCardCounterTrigger);
|
connect(tempRemoveCounter, &QAction::triggered, playerActions,
|
||||||
|
[playerActions, i] { playerActions->actRemoveCardCounter(i); });
|
||||||
|
connect(tempSetCounter, &QAction::triggered, playerActions,
|
||||||
|
[playerActions, i] { playerActions->actSetCardCounter(i); });
|
||||||
}
|
}
|
||||||
|
|
||||||
setShortcutsActive();
|
setShortcutsActive();
|
||||||
|
|
|
||||||
|
|
@ -1539,80 +1539,77 @@ void PlayerActions::actUnattach()
|
||||||
sendGameCommand(prepareGameCommand(commandList));
|
sendGameCommand(prepareGameCommand(commandList));
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerActions::actCardCounterTrigger()
|
void PlayerActions::actAddCardCounter(int counterId)
|
||||||
|
{
|
||||||
|
offsetCardCounter(counterId, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerActions::actRemoveCardCounter(int counterId)
|
||||||
|
{
|
||||||
|
offsetCardCounter(counterId, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerActions::offsetCardCounter(int counterId, int offset)
|
||||||
{
|
{
|
||||||
auto *action = static_cast<QAction *>(sender());
|
|
||||||
int counterId = action->data().toInt() / 1000;
|
|
||||||
QList<const ::google::protobuf::Message *> commandList;
|
QList<const ::google::protobuf::Message *> commandList;
|
||||||
switch (action->data().toInt() % 1000) {
|
for (const auto &item : player->getGameScene()->selectedItems()) {
|
||||||
case 9: { // increment counter
|
auto *card = static_cast<CardItem *>(item);
|
||||||
for (const auto &item : player->getGameScene()->selectedItems()) {
|
|
||||||
auto *card = static_cast<CardItem *>(item);
|
int oldValue = card->getCounters().value(counterId, 0);
|
||||||
if (card->getCounters().value(counterId, 0) < MAX_COUNTERS_ON_CARD) {
|
int newValue = oldValue + offset;
|
||||||
auto *cmd = new Command_SetCardCounter;
|
|
||||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
if (newValue >= 0 && newValue <= MAX_COUNTERS_ON_CARD) {
|
||||||
cmd->set_card_id(card->getId());
|
auto *cmd = new Command_SetCardCounter;
|
||||||
cmd->set_counter_id(counterId);
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||||
cmd->set_counter_value(card->getCounters().value(counterId, 0) + 1);
|
cmd->set_card_id(card->getId());
|
||||||
commandList.append(cmd);
|
cmd->set_counter_id(counterId);
|
||||||
}
|
cmd->set_counter_value(newValue);
|
||||||
}
|
commandList.append(cmd);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case 10: { // decrement counter
|
|
||||||
for (const auto &item : player->getGameScene()->selectedItems()) {
|
|
||||||
auto *card = static_cast<CardItem *>(item);
|
|
||||||
if (card->getCounters().value(counterId, 0)) {
|
|
||||||
auto *cmd = new Command_SetCardCounter;
|
|
||||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
||||||
cmd->set_card_id(card->getId());
|
|
||||||
cmd->set_counter_id(counterId);
|
|
||||||
cmd->set_counter_value(card->getCounters().value(counterId, 0) - 1);
|
|
||||||
commandList.append(cmd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 11: { // set counter with dialog
|
|
||||||
player->setDialogSemaphore(true);
|
|
||||||
|
|
||||||
// If a single card is selected, we show the old value in the dialog. Otherwise, we show "x"
|
|
||||||
QList<QGraphicsItem *> sel = player->getGameScene()->selectedItems();
|
|
||||||
QString oldValueForDlg = "x";
|
|
||||||
if (sel.size() == 1) {
|
|
||||||
auto *card = dynamic_cast<CardItem *>(sel.first());
|
|
||||||
oldValueForDlg = QString::number(card->getCounters().value(counterId, 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto &cardCounterSettings = SettingsCache::instance().cardCounters();
|
|
||||||
QString counterName = cardCounterSettings.displayName(counterId);
|
|
||||||
|
|
||||||
AbstractCounterDialog dialog(counterName, oldValueForDlg, player->getGame()->getTab());
|
|
||||||
int ok = dialog.exec();
|
|
||||||
|
|
||||||
player->setDialogSemaphore(false);
|
|
||||||
if (player->clearCardsToDelete() || !ok) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto &item : sel) {
|
|
||||||
auto *card = dynamic_cast<CardItem *>(item);
|
|
||||||
|
|
||||||
int oldValue = card->getCounters().value(counterId, 0);
|
|
||||||
Expression exp(oldValue);
|
|
||||||
int number = static_cast<int>(exp.parse(dialog.textValue()));
|
|
||||||
|
|
||||||
auto *cmd = new Command_SetCardCounter;
|
|
||||||
cmd->set_zone(card->getZone()->getName().toStdString());
|
|
||||||
cmd->set_card_id(card->getId());
|
|
||||||
cmd->set_counter_id(counterId);
|
|
||||||
cmd->set_counter_value(number);
|
|
||||||
commandList.append(cmd);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendGameCommand(prepareGameCommand(commandList));
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerActions::actSetCardCounter(int counterId)
|
||||||
|
{
|
||||||
|
player->setDialogSemaphore(true);
|
||||||
|
|
||||||
|
// If a single card is selected, we show the old value in the dialog. Otherwise, we show "x"
|
||||||
|
QList<QGraphicsItem *> sel = player->getGameScene()->selectedItems();
|
||||||
|
QString oldValueForDlg = "x";
|
||||||
|
if (sel.size() == 1) {
|
||||||
|
auto *card = dynamic_cast<CardItem *>(sel.first());
|
||||||
|
oldValueForDlg = QString::number(card->getCounters().value(counterId, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto &cardCounterSettings = SettingsCache::instance().cardCounters();
|
||||||
|
QString counterName = cardCounterSettings.displayName(counterId);
|
||||||
|
|
||||||
|
AbstractCounterDialog dialog(counterName, oldValueForDlg, player->getGame()->getTab());
|
||||||
|
int ok = dialog.exec();
|
||||||
|
|
||||||
|
player->setDialogSemaphore(false);
|
||||||
|
if (player->clearCardsToDelete() || !ok) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<const ::google::protobuf::Message *> commandList;
|
||||||
|
for (const auto &item : sel) {
|
||||||
|
auto *card = dynamic_cast<CardItem *>(item);
|
||||||
|
|
||||||
|
int oldValue = card->getCounters().value(counterId, 0);
|
||||||
|
Expression exp(oldValue);
|
||||||
|
int number = static_cast<int>(exp.parse(dialog.textValue()));
|
||||||
|
|
||||||
|
auto *cmd = new Command_SetCardCounter;
|
||||||
|
cmd->set_zone(card->getZone()->getName().toStdString());
|
||||||
|
cmd->set_card_id(card->getId());
|
||||||
|
cmd->set_counter_id(counterId);
|
||||||
|
cmd->set_counter_value(number);
|
||||||
|
commandList.append(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
sendGameCommand(prepareGameCommand(commandList));
|
sendGameCommand(prepareGameCommand(commandList));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,9 @@ public slots:
|
||||||
void actCreateAllRelatedCards();
|
void actCreateAllRelatedCards();
|
||||||
|
|
||||||
void actMoveCardXCardsFromTop();
|
void actMoveCardXCardsFromTop();
|
||||||
void actCardCounterTrigger();
|
void actRemoveCardCounter(int counterId);
|
||||||
|
void actAddCardCounter(int counterId);
|
||||||
|
void actSetCardCounter(int counterId);
|
||||||
void actAttach();
|
void actAttach();
|
||||||
void actUnattach();
|
void actUnattach();
|
||||||
void actDrawArrow();
|
void actDrawArrow();
|
||||||
|
|
@ -197,6 +199,8 @@ private:
|
||||||
void cmdSetTopCard(Command_MoveCard &cmd);
|
void cmdSetTopCard(Command_MoveCard &cmd);
|
||||||
void cmdSetBottomCard(Command_MoveCard &cmd);
|
void cmdSetBottomCard(Command_MoveCard &cmd);
|
||||||
|
|
||||||
|
void offsetCardCounter(int counterId, int offset);
|
||||||
|
|
||||||
QVariantList parsePT(const QString &pt);
|
QVariantList parsePT(const QString &pt);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue