1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00
vcmi/mapeditor/playersettings.cpp

97 lines
2.3 KiB
C++
Raw Normal View History

2022-10-12 23:51:55 +02:00
/*
* playersettings.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
*
*/
2022-09-18 01:23:17 +02:00
#include "StdInc.h"
#include "playersettings.h"
#include "ui_playersettings.h"
#include "playerparams.h"
#include "mainwindow.h"
PlayerSettings::PlayerSettings(MapController & ctrl, QWidget *parent) :
QDialog(parent),
ui(new Ui::PlayerSettings),
controller(ctrl)
{
ui->setupUi(this);
show();
int players = 0;
const int minAllowedPlayers = 1;
2023-04-21 01:17:13 +02:00
for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
2022-09-18 01:23:17 +02:00
{
2023-04-21 01:17:13 +02:00
if(controller.map()->players[i].canAnyonePlay())
2022-09-18 01:23:17 +02:00
{
2023-04-21 01:17:13 +02:00
paramWidgets.push_back(new PlayerParams(controller, i));
2022-09-18 01:23:17 +02:00
ui->playersLayout->addWidget(paramWidgets.back());
++players;
}
}
2022-10-12 23:40:52 +02:00
if(players < minAllowedPlayers)
2022-09-18 01:23:17 +02:00
ui->playersCount->setCurrentText("");
else
2022-10-12 23:40:52 +02:00
ui->playersCount->setCurrentIndex(players - minAllowedPlayers);
2022-09-18 01:23:17 +02:00
setAttribute(Qt::WA_DeleteOnClose);
}
PlayerSettings::~PlayerSettings()
{
delete ui;
}
void PlayerSettings::on_playersCount_currentIndexChanged(int index)
{
const auto selectedPlayerCount = index + 1;
2022-10-12 23:40:52 +02:00
assert(selectedPlayerCount <= controller.map()->players.size());
2023-04-21 01:17:13 +02:00
std::set<int> availableColors{0, 1, 2, 3, 4, 5, 6, 7};
2022-09-18 01:23:17 +02:00
2022-10-12 23:40:52 +02:00
for(int i = 0; i < selectedPlayerCount; ++i)
2022-09-18 01:23:17 +02:00
{
if(i < paramWidgets.size())
2023-04-21 01:17:13 +02:00
{
availableColors.erase(paramWidgets[i]->playerColor);
2022-09-18 01:23:17 +02:00
continue;
2023-04-21 01:17:13 +02:00
}
2022-09-18 01:23:17 +02:00
2023-04-21 01:17:13 +02:00
assert(!availableColors.empty());
auto plColor = *availableColors.begin();
auto & p = controller.map()->players[plColor];
2022-09-18 01:23:17 +02:00
p.canComputerPlay = true;
2023-04-21 01:17:13 +02:00
paramWidgets.push_back(new PlayerParams(controller, plColor));
availableColors.erase(plColor);
2022-09-18 01:23:17 +02:00
ui->playersLayout->addWidget(paramWidgets.back());
}
assert(!paramWidgets.empty());
2022-10-12 23:40:52 +02:00
for(int i = paramWidgets.size() - 1; i >= selectedPlayerCount; --i)
2022-09-18 01:23:17 +02:00
{
2023-04-21 01:17:13 +02:00
auto & p = controller.map()->players[paramWidgets[i]->playerColor];
2022-09-18 01:23:17 +02:00
p.canComputerPlay = false;
p.canHumanPlay = false;
ui->playersLayout->removeWidget(paramWidgets[i]);
delete paramWidgets[i];
paramWidgets.pop_back();
}
}
void PlayerSettings::on_pushButton_clicked()
{
for(auto * w : paramWidgets)
{
controller.map()->players[w->playerColor] = w->playerInfo;
}
controller.commitChangeWithoutRedraw();
close();
}