PlayerSettingsDialog.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * playersettings.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "PlayerSettingsDialog.h"
  12. #include "ui_PlayerSettingsDialog.h"
  13. #include "playerparams.h"
  14. #include "mainwindow.h"
  15. #include "../lib/mapping/CMap.h"
  16. PlayerSettingsDialog::PlayerSettingsDialog(MapController & ctrl, QWidget *parent) :
  17. QDialog(parent),
  18. ui(new Ui::PlayerSettingsDialog),
  19. controller(ctrl)
  20. {
  21. ui->setupUi(this);
  22. controller.settingsDialog = this;
  23. setWindowFlags(Qt::Dialog | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
  24. show();
  25. int players = 0;
  26. const int minAllowedPlayers = 1;
  27. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
  28. {
  29. if(controller.map()->players[i].canAnyonePlay())
  30. {
  31. paramWidgets.push_back(new PlayerParams(controller, i));
  32. ui->playersLayout->addWidget(paramWidgets.back());
  33. ++players;
  34. }
  35. }
  36. if(players < minAllowedPlayers)
  37. ui->playersCount->setCurrentText("");
  38. else
  39. ui->playersCount->setCurrentIndex(players - minAllowedPlayers);
  40. setAttribute(Qt::WA_DeleteOnClose);
  41. }
  42. PlayerSettingsDialog::~PlayerSettingsDialog()
  43. {
  44. controller.settingsDialog = nullptr;
  45. delete ui;
  46. }
  47. void PlayerSettingsDialog::on_playersCount_currentIndexChanged(int index)
  48. {
  49. const auto selectedPlayerCount = index + 1;
  50. assert(selectedPlayerCount <= controller.map()->players.size());
  51. std::set<int> availableColors{0, 1, 2, 3, 4, 5, 6, 7};
  52. for(int i = 0; i < selectedPlayerCount; ++i)
  53. {
  54. if(i < paramWidgets.size())
  55. {
  56. availableColors.erase(paramWidgets[i]->playerColor);
  57. continue;
  58. }
  59. assert(!availableColors.empty());
  60. auto plColor = *availableColors.begin();
  61. auto & p = controller.map()->players[plColor];
  62. p.canComputerPlay = true;
  63. paramWidgets.push_back(new PlayerParams(controller, plColor));
  64. availableColors.erase(plColor);
  65. ui->playersLayout->addWidget(paramWidgets.back());
  66. }
  67. assert(!paramWidgets.empty());
  68. for(int i = paramWidgets.size() - 1; i >= selectedPlayerCount; --i)
  69. {
  70. auto & p = controller.map()->players[paramWidgets[i]->playerColor];
  71. p.canComputerPlay = false;
  72. p.canHumanPlay = false;
  73. ui->playersLayout->removeWidget(paramWidgets[i]);
  74. delete paramWidgets[i];
  75. paramWidgets.pop_back();
  76. }
  77. }
  78. void PlayerSettingsDialog::on_pushButton_clicked()
  79. {
  80. for(auto * w : paramWidgets)
  81. {
  82. controller.map()->players[w->playerColor] = w->playerInfo;
  83. }
  84. controller.commitChangeWithoutRedraw();
  85. close();
  86. }