2025-03-25 15:39:42 +01:00
|
|
|
/*
|
|
|
|
* PlayerSelectionDialog.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PlayerSelectionDialog.h"
|
|
|
|
#include "../lib/mapping/CMap.h"
|
2025-04-13 17:18:13 +02:00
|
|
|
#include "mainwindow.h"
|
|
|
|
|
2025-04-18 16:06:26 +02:00
|
|
|
#include <QRadioButton>
|
|
|
|
#include <QButtonGroup>
|
2025-04-13 17:18:13 +02:00
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QAction>
|
|
|
|
#include <QLabel>
|
|
|
|
|
2025-03-25 15:39:42 +01:00
|
|
|
|
2025-04-05 18:13:24 +02:00
|
|
|
PlayerSelectionDialog::PlayerSelectionDialog(MainWindow * mainWindow)
|
|
|
|
: QDialog(mainWindow), selectedPlayer(PlayerColor::NEUTRAL)
|
2025-03-25 15:39:42 +01:00
|
|
|
{
|
|
|
|
setupDialogComponents();
|
|
|
|
|
|
|
|
int maxPlayers = 0;
|
2025-04-05 18:13:24 +02:00
|
|
|
if(mainWindow && mainWindow->controller.map())
|
|
|
|
maxPlayers = mainWindow->controller.map()->players.size();
|
2025-03-25 15:39:42 +01:00
|
|
|
|
|
|
|
for(int i = 0; i < maxPlayers; ++i)
|
|
|
|
{
|
|
|
|
PlayerColor player(i);
|
2025-04-18 16:06:26 +02:00
|
|
|
addRadioButton(mainWindow->getActionPlayer(player), player);
|
2025-03-25 15:39:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor PlayerSelectionDialog::getSelectedPlayer() const
|
|
|
|
{
|
|
|
|
return selectedPlayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayerSelectionDialog::setupDialogComponents()
|
|
|
|
{
|
|
|
|
setWindowTitle(tr("Select Player"));
|
2025-04-16 23:44:17 +02:00
|
|
|
setFixedWidth(dialogWidth);
|
2025-03-25 15:39:42 +01:00
|
|
|
setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
|
|
|
|
font.setPointSize(10);
|
|
|
|
setFont(font);
|
|
|
|
|
2025-04-18 16:06:26 +02:00
|
|
|
buttonGroup = new QButtonGroup(this);
|
|
|
|
buttonGroup->setExclusive(true);
|
|
|
|
|
2025-03-25 15:39:42 +01:00
|
|
|
QLabel * errorLabel = new QLabel(tr("Hero cannot be created as NEUTRAL"), this);
|
|
|
|
font.setBold(true);
|
|
|
|
errorLabel->setFont(font);
|
2025-04-16 23:44:17 +02:00
|
|
|
errorLabel->setWordWrap(true);
|
2025-03-25 15:39:42 +01:00
|
|
|
mainLayout.addWidget(errorLabel);
|
|
|
|
|
|
|
|
QLabel * instructionLabel = new QLabel(tr("Switch to one of the available players:"), this);
|
|
|
|
font.setBold(false);
|
|
|
|
instructionLabel->setFont(font);
|
2025-04-16 23:44:17 +02:00
|
|
|
instructionLabel->setWordWrap(true);
|
2025-03-25 15:39:42 +01:00
|
|
|
mainLayout.addWidget(instructionLabel);
|
|
|
|
|
2025-04-18 16:06:26 +02:00
|
|
|
QWidget * radioContainer = new QWidget(this);
|
|
|
|
radioContainer->setLayout(& radioButtonsLayout);
|
|
|
|
mainLayout.addWidget(radioContainer);
|
2025-03-25 15:39:42 +01:00
|
|
|
|
|
|
|
QDialogButtonBox * box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
|
|
|
|
connect(box, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
|
|
|
connect(box, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
|
|
|
mainLayout.addWidget(box);
|
|
|
|
|
|
|
|
setLayout(& mainLayout);
|
|
|
|
}
|
|
|
|
|
2025-04-18 16:06:26 +02:00
|
|
|
void PlayerSelectionDialog::addRadioButton(QAction * action, PlayerColor player)
|
2025-03-25 15:39:42 +01:00
|
|
|
{
|
2025-04-18 16:06:26 +02:00
|
|
|
auto * radioButton = new QRadioButton(action->text(), this);
|
|
|
|
radioButton->setEnabled(action->isEnabled());
|
|
|
|
// Select first available player by default
|
|
|
|
if(buttonGroup->buttons().isEmpty() && radioButton->isEnabled())
|
|
|
|
{
|
|
|
|
radioButton->setChecked(true);
|
|
|
|
selectedPlayer = player;
|
|
|
|
}
|
|
|
|
|
2025-05-13 12:51:31 +02:00
|
|
|
radioButton->setToolTip(tr("Shortcut: %1").arg(action->shortcut().toString()));
|
2025-04-18 16:06:26 +02:00
|
|
|
buttonGroup->addButton(radioButton, player.getNum());
|
2025-05-13 12:51:31 +02:00
|
|
|
radioButtonsLayout.addWidget(radioButton);
|
2025-03-25 15:39:42 +01:00
|
|
|
|
2025-04-18 16:06:26 +02:00
|
|
|
connect(radioButton, &QRadioButton::clicked, this, [this, player]()
|
2025-03-25 15:39:42 +01:00
|
|
|
{
|
|
|
|
selectedPlayer = player;
|
|
|
|
});
|
|
|
|
|
2025-04-18 16:06:26 +02:00
|
|
|
addAction(action);
|
|
|
|
connect(action, &QAction::triggered, this, [radioButton]()
|
2025-03-25 15:39:42 +01:00
|
|
|
{
|
2025-04-18 16:06:26 +02:00
|
|
|
if(radioButton->isEnabled())
|
|
|
|
radioButton->click();
|
2025-03-25 15:39:42 +01:00
|
|
|
});
|
|
|
|
}
|