mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-10 16:24:45 -07:00
[Game] Populate playerLists for menus in their aboutToShow … (#6214)
* Populate playerLists for menus in their aboutToShow so they are always current and do not rely on playerMenu manually tracking them. Also add playerActions for previous playerListActions. Took 1 hour 35 minutes --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
015570c833
commit
30e6b52783
10 changed files with 313 additions and 159 deletions
|
|
@ -12,29 +12,13 @@
|
|||
|
||||
PlayerMenu::PlayerMenu(Player *_player) : player(_player)
|
||||
{
|
||||
|
||||
if (player->getPlayerInfo()->getLocalOrJudge()) {
|
||||
connect(player->getGame()->getPlayerManager(), &PlayerManager::playerAdded, this, &PlayerMenu::addPlayer);
|
||||
connect(player->getGame()->getPlayerManager(), &PlayerManager::playerRemoved, this, &PlayerMenu::removePlayer);
|
||||
}
|
||||
|
||||
const QList<Player *> &players = player->getGame()->getPlayerManager()->getPlayers().values();
|
||||
for (const auto playerToAdd : players) {
|
||||
addPlayer(playerToAdd);
|
||||
}
|
||||
|
||||
playerMenu = new TearOffMenu();
|
||||
|
||||
if (player->getPlayerInfo()->getLocalOrJudge()) {
|
||||
handMenu = new HandMenu(player, player->getPlayerActions(), playerMenu);
|
||||
playerMenu->addMenu(handMenu);
|
||||
playerLists.append(handMenu->revealHandMenu());
|
||||
playerLists.append(handMenu->revealRandomHandCardMenu());
|
||||
|
||||
libraryMenu = new LibraryMenu(player, playerMenu);
|
||||
playerLists.append(libraryMenu->revealLibrary());
|
||||
playerLists.append(libraryMenu->lendLibraryMenu());
|
||||
playerLists.append(libraryMenu->revealTopCardMenu());
|
||||
playerMenu->addMenu(libraryMenu);
|
||||
} else {
|
||||
handMenu = nullptr;
|
||||
|
|
@ -42,7 +26,6 @@ PlayerMenu::PlayerMenu(Player *_player) : player(_player)
|
|||
}
|
||||
|
||||
graveMenu = new GraveyardMenu(player, playerMenu);
|
||||
connect(graveMenu, &GraveyardMenu::newPlayerActionCreated, this, &PlayerMenu::onNewPlayerListActionCreated);
|
||||
playerMenu->addMenu(graveMenu);
|
||||
|
||||
rfgMenu = new RfgMenu(player, playerMenu);
|
||||
|
|
@ -73,17 +56,6 @@ PlayerMenu::PlayerMenu(Player *_player) : player(_player)
|
|||
sayMenu = nullptr;
|
||||
}
|
||||
|
||||
if (player->getPlayerInfo()->getLocalOrJudge()) {
|
||||
|
||||
for (auto &playerList : playerLists) {
|
||||
QAction *newAction = playerList->addAction(QString());
|
||||
newAction->setData(-1);
|
||||
connect(newAction, &QAction::triggered, this, &PlayerMenu::playerListActionTriggered);
|
||||
allPlayersActions.append(newAction);
|
||||
playerList->addSeparator();
|
||||
}
|
||||
}
|
||||
|
||||
connect(&SettingsCache::instance().shortcuts(), &ShortcutsSettings::shortCutChanged, this,
|
||||
&PlayerMenu::refreshShortcuts);
|
||||
refreshShortcuts();
|
||||
|
|
@ -103,89 +75,6 @@ void PlayerMenu::setMenusForGraphicItems()
|
|||
}
|
||||
}
|
||||
|
||||
void PlayerMenu::addPlayer(Player *playerToAdd)
|
||||
{
|
||||
if (playerToAdd == nullptr || playerToAdd == player) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &playerList : playerLists) {
|
||||
addPlayerToList(playerList, playerToAdd);
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerMenu::addPlayerToList(QMenu *playerList, Player *playerToAdd)
|
||||
{
|
||||
QAction *newAction = playerList->addAction(playerToAdd->getPlayerInfo()->getName());
|
||||
newAction->setData(playerToAdd->getPlayerInfo()->getId());
|
||||
connect(newAction, &QAction::triggered, this, &PlayerMenu::playerListActionTriggered);
|
||||
}
|
||||
|
||||
void PlayerMenu::removePlayer(Player *playerToRemove)
|
||||
{
|
||||
if (playerToRemove == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &playerList : playerLists) {
|
||||
removePlayerFromList(playerList, playerToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerMenu::removePlayerFromList(QMenu *playerList, Player *player)
|
||||
{
|
||||
QList<QAction *> actionList = playerList->actions();
|
||||
for (auto &j : actionList)
|
||||
if (j->data().toInt() == player->getPlayerInfo()->getId()) {
|
||||
playerList->removeAction(j);
|
||||
j->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerMenu::playerListActionTriggered()
|
||||
{
|
||||
auto *action = static_cast<QAction *>(sender());
|
||||
auto *menu = static_cast<QMenu *>(action->parent());
|
||||
|
||||
Command_RevealCards cmd;
|
||||
const int otherPlayerId = action->data().toInt();
|
||||
if (otherPlayerId != -1) {
|
||||
cmd.set_player_id(otherPlayerId);
|
||||
}
|
||||
|
||||
if (menu == libraryMenu->revealLibrary() || menu == libraryMenu->lendLibraryMenu()) {
|
||||
cmd.set_zone_name("deck");
|
||||
cmd.set_grant_write_access(menu == libraryMenu->lendLibraryMenu());
|
||||
} else if (menu == libraryMenu->revealTopCardMenu()) {
|
||||
int deckSize = player->getDeckZone()->getCards().size();
|
||||
bool ok;
|
||||
int number = QInputDialog::getInt(player->getGame()->getTab(), tr("Reveal top cards of library"),
|
||||
tr("Number of cards: (max. %1)").arg(deckSize), /* defaultNumberTopCards */ 1,
|
||||
1, deckSize, 1, &ok);
|
||||
if (ok) {
|
||||
cmd.set_zone_name("deck");
|
||||
cmd.set_top_cards(number);
|
||||
// backward compatibility: servers before #1051 only permits to reveal the first card
|
||||
cmd.add_card_id(0);
|
||||
// defaultNumberTopCards = number;
|
||||
}
|
||||
} else if (menu == handMenu->revealHandMenu()) {
|
||||
cmd.set_zone_name("hand");
|
||||
} else if (menu == handMenu->revealRandomHandCardMenu()) {
|
||||
cmd.set_zone_name("hand");
|
||||
cmd.add_card_id(PlayerActions::RANDOM_CARD_FROM_ZONE);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
player->getPlayerActions()->sendGameCommand(cmd);
|
||||
}
|
||||
|
||||
void PlayerMenu::onNewPlayerListActionCreated(QAction *action)
|
||||
{
|
||||
allPlayersActions.append(action);
|
||||
}
|
||||
|
||||
QMenu *PlayerMenu::updateCardMenu(const CardItem *card)
|
||||
{
|
||||
if (!card) {
|
||||
|
|
@ -241,10 +130,6 @@ void PlayerMenu::retranslateUi()
|
|||
utilityMenu->retranslateUi();
|
||||
}
|
||||
|
||||
for (auto &allPlayersAction : allPlayersActions) {
|
||||
allPlayersAction->setText(tr("&All players"));
|
||||
}
|
||||
|
||||
if (sayMenu) {
|
||||
sayMenu->setTitle(tr("S&ay"));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue