OptionsTab.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * OptionsTab.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 "OptionsTab.h"
  12. #include "CSelectionBase.h"
  13. #include "../CGameInfo.h"
  14. #include "../CServerHandler.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../render/Graphics.h"
  19. #include "../render/IFont.h"
  20. #include "../media/ISoundPlayer.h"
  21. #include "../widgets/CComponent.h"
  22. #include "../widgets/ComboBox.h"
  23. #include "../widgets/CTextInput.h"
  24. #include "../widgets/Buttons.h"
  25. #include "../widgets/Images.h"
  26. #include "../widgets/MiscWidgets.h"
  27. #include "../widgets/ObjectLists.h"
  28. #include "../widgets/Slider.h"
  29. #include "../widgets/TextControls.h"
  30. #include "../windows/GUIClasses.h"
  31. #include "../windows/InfoWindows.h"
  32. #include "../windows/CHeroOverview.h"
  33. #include "../eventsSDL/InputHandler.h"
  34. #include "../../lib/entities/faction/CFaction.h"
  35. #include "../../lib/entities/faction/CTown.h"
  36. #include "../../lib/entities/faction/CTownHandler.h"
  37. #include "../../lib/filesystem/Filesystem.h"
  38. #include "../../lib/networkPacks/PacksForLobby.h"
  39. #include "../../lib/texts/CGeneralTextHandler.h"
  40. #include "../../lib/CArtHandler.h"
  41. #include "../../lib/CConfigHandler.h"
  42. #include "../../lib/CHeroHandler.h"
  43. #include "../../lib/mapping/CMapInfo.h"
  44. #include "../../lib/mapping/CMapHeader.h"
  45. static JsonPath optionsTabConfigLocation()
  46. {
  47. if(settings["general"]["enableUiEnhancements"].Bool())
  48. return JsonPath::builtin("config/widgets/playerOptionsTab.json");
  49. else
  50. return JsonPath::builtin("config/widgets/advancedOptionsTab.json");
  51. }
  52. OptionsTab::OptionsTab()
  53. : OptionsTabBase(optionsTabConfigLocation())
  54. , humanPlayers(0)
  55. {
  56. }
  57. void OptionsTab::recreate()
  58. {
  59. entries.clear();
  60. humanPlayers = 0;
  61. for (auto selectionWindow : GH.windows().findWindows<SelectionWindow>())
  62. {
  63. selectionWindow->reopen();
  64. }
  65. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  66. for(auto & pInfo : SEL->getStartInfo()->playerInfos)
  67. {
  68. if(pInfo.second.isControlledByHuman())
  69. humanPlayers++;
  70. entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
  71. }
  72. OptionsTabBase::recreate();
  73. }
  74. size_t OptionsTab::CPlayerSettingsHelper::getImageIndex(bool big)
  75. {
  76. enum EBonusSelection //frames of bonuses file
  77. {
  78. WOOD_ORE = 0, CRYSTAL = 1, GEM = 2,
  79. MERCURY = 3, SULFUR = 5, GOLD = 8,
  80. ARTIFACT = 9, RANDOM = 10,
  81. WOOD = 0, ORE = 0, MITHRIL = 10, // resources unavailable in bonuses file
  82. TOWN_RANDOM = 38, TOWN_NONE = 39, // Special frames in ITPA
  83. HERO_RANDOM = 156, HERO_NONE = 157 // Special frames in PortraitsSmall
  84. };
  85. auto factionIndex = playerSettings.getCastleValidated();
  86. switch(selectionType)
  87. {
  88. case TOWN:
  89. {
  90. if (playerSettings.castle == FactionID::NONE)
  91. return TOWN_NONE;
  92. if (playerSettings.castle == FactionID::RANDOM)
  93. return TOWN_RANDOM;
  94. return (*CGI->townh)[factionIndex]->town->clientInfo.icons[true][false] + (big ? 0 : 2);
  95. }
  96. case HERO:
  97. {
  98. if (playerSettings.hero == HeroTypeID::NONE)
  99. return HERO_NONE;
  100. if (playerSettings.hero == HeroTypeID::RANDOM)
  101. return HERO_RANDOM;
  102. if(playerSettings.heroPortrait != HeroTypeID::NONE)
  103. return playerSettings.heroPortrait;
  104. auto index = playerSettings.getHeroValidated();
  105. return (*CGI->heroh)[index]->imageIndex;
  106. }
  107. case BONUS:
  108. {
  109. switch(playerSettings.bonus)
  110. {
  111. case PlayerStartingBonus::RANDOM:
  112. return RANDOM;
  113. case PlayerStartingBonus::ARTIFACT:
  114. return ARTIFACT;
  115. case PlayerStartingBonus::GOLD:
  116. return GOLD;
  117. case PlayerStartingBonus::RESOURCE:
  118. {
  119. switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
  120. {
  121. case EGameResID::WOOD_AND_ORE:
  122. return WOOD_ORE;
  123. case EGameResID::WOOD:
  124. return WOOD;
  125. case EGameResID::MERCURY:
  126. return MERCURY;
  127. case EGameResID::ORE:
  128. return ORE;
  129. case EGameResID::SULFUR:
  130. return SULFUR;
  131. case EGameResID::CRYSTAL:
  132. return CRYSTAL;
  133. case EGameResID::GEMS:
  134. return GEM;
  135. case EGameResID::GOLD:
  136. return GOLD;
  137. case EGameResID::MITHRIL:
  138. return MITHRIL;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. return 0;
  145. }
  146. AnimationPath OptionsTab::CPlayerSettingsHelper::getImageName(bool big)
  147. {
  148. switch(selectionType)
  149. {
  150. case OptionsTab::TOWN:
  151. return AnimationPath::builtin(big ? "ITPt": "ITPA");
  152. case OptionsTab::HERO:
  153. return AnimationPath::builtin(big ? "PortraitsLarge": "PortraitsSmall");
  154. case OptionsTab::BONUS:
  155. return AnimationPath::builtin("SCNRSTAR");
  156. }
  157. return {};
  158. }
  159. std::string OptionsTab::CPlayerSettingsHelper::getName()
  160. {
  161. switch(selectionType)
  162. {
  163. case TOWN:
  164. {
  165. if (playerSettings.castle == FactionID::NONE)
  166. return CGI->generaltexth->allTexts[523];
  167. if (playerSettings.castle == FactionID::RANDOM)
  168. return CGI->generaltexth->allTexts[522];
  169. auto factionIndex = playerSettings.getCastleValidated();
  170. return (*CGI->townh)[factionIndex]->getNameTranslated();
  171. }
  172. case HERO:
  173. {
  174. if (playerSettings.hero == HeroTypeID::NONE)
  175. return CGI->generaltexth->allTexts[523];
  176. if (playerSettings.hero == HeroTypeID::RANDOM)
  177. return CGI->generaltexth->allTexts[522];
  178. if(!playerSettings.heroNameTextId.empty())
  179. return CGI->generaltexth->translate(playerSettings.heroNameTextId);
  180. auto index = playerSettings.getHeroValidated();
  181. return (*CGI->heroh)[index]->getNameTranslated();
  182. }
  183. case BONUS:
  184. {
  185. if (playerSettings.bonus == PlayerStartingBonus::RANDOM)
  186. return CGI->generaltexth->allTexts[522];
  187. return CGI->generaltexth->arraytxt[214 + static_cast<int>(playerSettings.bonus)];
  188. }
  189. }
  190. return "";
  191. }
  192. std::string OptionsTab::CPlayerSettingsHelper::getTitle()
  193. {
  194. switch(selectionType)
  195. {
  196. case OptionsTab::TOWN:
  197. return playerSettings.castle.isValid() ? CGI->generaltexth->allTexts[80] : CGI->generaltexth->allTexts[103];
  198. case OptionsTab::HERO:
  199. return playerSettings.hero.isValid() ? CGI->generaltexth->allTexts[77] : CGI->generaltexth->allTexts[101];
  200. case OptionsTab::BONUS:
  201. {
  202. switch(playerSettings.bonus)
  203. {
  204. case PlayerStartingBonus::RANDOM:
  205. return CGI->generaltexth->allTexts[86]; //{Random Bonus}
  206. case PlayerStartingBonus::ARTIFACT:
  207. return CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  208. case PlayerStartingBonus::GOLD:
  209. return CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  210. case PlayerStartingBonus::RESOURCE:
  211. return CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  212. }
  213. }
  214. }
  215. return "";
  216. }
  217. std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
  218. {
  219. auto factionIndex = playerSettings.getCastleValidated();
  220. auto heroIndex = playerSettings.getHeroValidated();
  221. switch(selectionType)
  222. {
  223. case TOWN:
  224. return getName();
  225. case HERO:
  226. {
  227. if(playerSettings.hero.isValid())
  228. return getName() + " - " + (*CGI->heroh)[heroIndex]->heroClass->getNameTranslated();
  229. return getName();
  230. }
  231. case BONUS:
  232. {
  233. switch(playerSettings.bonus)
  234. {
  235. case PlayerStartingBonus::GOLD:
  236. return CGI->generaltexth->allTexts[87]; //500-1000
  237. case PlayerStartingBonus::RESOURCE:
  238. {
  239. switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
  240. {
  241. case EGameResID::MERCURY:
  242. return CGI->generaltexth->allTexts[694];
  243. case EGameResID::SULFUR:
  244. return CGI->generaltexth->allTexts[695];
  245. case EGameResID::CRYSTAL:
  246. return CGI->generaltexth->allTexts[692];
  247. case EGameResID::GEMS:
  248. return CGI->generaltexth->allTexts[693];
  249. case EGameResID::WOOD_AND_ORE:
  250. return CGI->generaltexth->allTexts[89]; //At the start of the game, 5-10 wood and 5-10 ore are added to your Kingdom's resource pool
  251. }
  252. }
  253. }
  254. }
  255. }
  256. return "";
  257. }
  258. std::string OptionsTab::CPlayerSettingsHelper::getDescription()
  259. {
  260. auto factionIndex = playerSettings.getCastleValidated();
  261. switch(selectionType)
  262. {
  263. case TOWN:
  264. return CGI->generaltexth->allTexts[104];
  265. case HERO:
  266. return CGI->generaltexth->allTexts[102];
  267. case BONUS:
  268. {
  269. switch(playerSettings.bonus)
  270. {
  271. case PlayerStartingBonus::RANDOM:
  272. return CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  273. case PlayerStartingBonus::ARTIFACT:
  274. return CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  275. case PlayerStartingBonus::GOLD:
  276. return CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  277. case PlayerStartingBonus::RESOURCE:
  278. {
  279. switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
  280. {
  281. case EGameResID::MERCURY:
  282. return CGI->generaltexth->allTexts[690];
  283. case EGameResID::SULFUR:
  284. return CGI->generaltexth->allTexts[691];
  285. case EGameResID::CRYSTAL:
  286. return CGI->generaltexth->allTexts[688];
  287. case EGameResID::GEMS:
  288. return CGI->generaltexth->allTexts[689];
  289. case EGameResID::WOOD_AND_ORE:
  290. return CGI->generaltexth->allTexts[93]; //At the start of the game, 5-10 wood and 5-10 ore are added to your Kingdom's resource pool
  291. }
  292. }
  293. }
  294. }
  295. }
  296. return "";
  297. }
  298. OptionsTab::CPlayerOptionTooltipBox::CPlayerOptionTooltipBox(CPlayerSettingsHelper & helper)
  299. : CWindowObject(BORDERED | RCLICK_POPUP), CPlayerSettingsHelper(helper)
  300. {
  301. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  302. switch(selectionType)
  303. {
  304. case TOWN:
  305. genTownWindow();
  306. break;
  307. case HERO:
  308. genHeroWindow();
  309. break;
  310. case BONUS:
  311. genBonusWindow();
  312. break;
  313. }
  314. center();
  315. }
  316. void OptionsTab::CPlayerOptionTooltipBox::genHeader()
  317. {
  318. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
  319. updateShadow();
  320. labelTitle = std::make_shared<CLabel>(pos.w / 2 + 8, 21, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, getTitle());
  321. labelSubTitle = std::make_shared<CLabel>(pos.w / 2, 88, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getSubtitle());
  322. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex(), 0, pos.w / 2 - 24, 45);
  323. }
  324. void OptionsTab::CPlayerOptionTooltipBox::genTownWindow()
  325. {
  326. auto factionIndex = playerSettings.getCastleValidated();
  327. if (playerSettings.castle == FactionID::RANDOM)
  328. return genBonusWindow();
  329. pos = Rect(0, 0, 228, 290);
  330. genHeader();
  331. labelAssociatedCreatures = std::make_shared<CLabel>(pos.w / 2 + 8, 122, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[79]);
  332. std::vector<std::shared_ptr<CComponent>> components;
  333. const CTown * town = (*CGI->townh)[factionIndex]->town;
  334. for(auto & elem : town->creatures)
  335. {
  336. if(!elem.empty())
  337. components.push_back(std::make_shared<CComponent>(ComponentType::CREATURE, elem.front(), std::nullopt, CComponent::tiny));
  338. }
  339. boxAssociatedCreatures = std::make_shared<CComponentBox>(components, Rect(10, 140, pos.w - 20, 140), 20, 10, 22, 4);
  340. }
  341. void OptionsTab::CPlayerOptionTooltipBox::genHeroWindow()
  342. {
  343. auto heroIndex = playerSettings.getHeroValidated();
  344. if (playerSettings.hero == HeroTypeID::RANDOM)
  345. return genBonusWindow();
  346. pos = Rect(0, 0, 292, 226);
  347. genHeader();
  348. labelHeroSpeciality = std::make_shared<CLabel>(pos.w / 2 + 4, 117, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[78]);
  349. imageSpeciality = std::make_shared<CAnimImage>(AnimationPath::builtin("UN44"), (*CGI->heroh)[heroIndex]->imageIndex, 0, pos.w / 2 - 22, 134);
  350. labelSpecialityName = std::make_shared<CLabel>(pos.w / 2, 188, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, (*CGI->heroh)[heroIndex]->getSpecialtyNameTranslated());
  351. }
  352. void OptionsTab::CPlayerOptionTooltipBox::genBonusWindow()
  353. {
  354. pos = Rect(0, 0, 228, 162);
  355. genHeader();
  356. textBonusDescription = std::make_shared<CTextBox>(getDescription(), Rect(10, 100, pos.w - 20, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  357. }
  358. OptionsTab::SelectionWindow::SelectionWindow(const PlayerColor & color, SelType _type, int sliderPos)
  359. : CWindowObject(BORDERED), color(color)
  360. {
  361. addUsedEvents(LCLICK | SHOW_POPUP);
  362. type = _type;
  363. initialFaction = SEL->getStartInfo()->playerInfos.find(color)->second.castle;
  364. initialHero = SEL->getStartInfo()->playerInfos.find(color)->second.hero;
  365. initialBonus = SEL->getStartInfo()->playerInfos.find(color)->second.bonus;
  366. selectedFaction = initialFaction;
  367. selectedHero = initialHero;
  368. selectedBonus = initialBonus;
  369. allowedFactions = SEL->getPlayerInfo(color).allowedFactions;
  370. allowedHeroes = SEL->getMapInfo()->mapHeader->allowedHeroes;
  371. for(auto & player : SEL->getStartInfo()->playerInfos)
  372. {
  373. if(player.first != color && (int)player.second.hero > HeroTypeID::RANDOM)
  374. unusableHeroes.insert(player.second.hero);
  375. }
  376. allowedBonus.push_back(PlayerStartingBonus::RANDOM);
  377. if(initialHero != HeroTypeID::NONE|| SEL->getPlayerInfo(color).heroesNames.size() > 0)
  378. allowedBonus.push_back(PlayerStartingBonus::ARTIFACT);
  379. allowedBonus.push_back(PlayerStartingBonus::GOLD);
  380. if(initialFaction.isValid())
  381. allowedBonus.push_back(PlayerStartingBonus::RESOURCE);
  382. recreate(sliderPos);
  383. }
  384. std::tuple<int, int> OptionsTab::SelectionWindow::calcLines(FactionID faction)
  385. {
  386. int additionalItems = 1; // random
  387. if(!faction.isValid())
  388. return std::make_tuple(
  389. std::ceil(((double)allowedFactions.size() + additionalItems) / MAX_ELEM_PER_LINES),
  390. (allowedFactions.size() + additionalItems) % MAX_ELEM_PER_LINES
  391. );
  392. int count = 0;
  393. for(auto & elemh : allowedHeroes)
  394. {
  395. const CHero * type = elemh.toHeroType();
  396. if(type->heroClass->faction == faction)
  397. count++;
  398. }
  399. return std::make_tuple(
  400. std::ceil(((double)count + additionalItems) / MAX_ELEM_PER_LINES),
  401. (count + additionalItems) % MAX_ELEM_PER_LINES
  402. );
  403. }
  404. void OptionsTab::SelectionWindow::apply()
  405. {
  406. if(GH.windows().isTopWindow(this))
  407. {
  408. GH.input().hapticFeedback();
  409. CCS->soundh->playSound(soundBase::button);
  410. close();
  411. setSelection();
  412. }
  413. }
  414. void OptionsTab::SelectionWindow::setSelection()
  415. {
  416. if(selectedFaction != initialFaction)
  417. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN_ID, selectedFaction, color);
  418. if(selectedHero != initialHero)
  419. CSH->setPlayerOption(LobbyChangePlayerOption::HERO_ID, selectedHero, color);
  420. if(selectedBonus != initialBonus)
  421. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS_ID, static_cast<int>(selectedBonus), color);
  422. }
  423. void OptionsTab::SelectionWindow::reopen()
  424. {
  425. if(type == SelType::HERO && SEL->getStartInfo()->playerInfos.find(color)->second.castle == FactionID::RANDOM)
  426. close();
  427. else{
  428. auto window = std::shared_ptr<SelectionWindow>(new SelectionWindow(color, type, slider ? slider->getValue() : 0));
  429. close();
  430. if(CSH->isMyColor(color) || CSH->isHost())
  431. GH.windows().pushWindow(window);
  432. }
  433. }
  434. void OptionsTab::SelectionWindow::recreate(int sliderPos)
  435. {
  436. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  437. int amountLines = 1;
  438. if(type == SelType::BONUS)
  439. elementsPerLine = allowedBonus.size();
  440. else
  441. {
  442. std::tie(amountLines, elementsPerLine) = calcLines((type > SelType::TOWN) ? selectedFaction : FactionID::RANDOM);
  443. if(amountLines > 1 || elementsPerLine == 0)
  444. elementsPerLine = MAX_ELEM_PER_LINES;
  445. }
  446. int x = (elementsPerLine) * (ICON_BIG_WIDTH-1);
  447. int y = (std::min(amountLines, MAX_LINES)) * (ICON_BIG_HEIGHT-1);
  448. int sliderWidth = ((amountLines > MAX_LINES) ? 16 : 0);
  449. pos = Rect(pos.x, pos.y, x + sliderWidth, y);
  450. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w - sliderWidth, pos.h));
  451. backgroundTexture->setPlayerColor(PlayerColor(1));
  452. updateShadow();
  453. if(type == SelType::TOWN)
  454. genContentFactions();
  455. if(type == SelType::HERO)
  456. genContentHeroes();
  457. if(type == SelType::BONUS)
  458. genContentBonus();
  459. genContentGrid(std::min(amountLines, MAX_LINES));
  460. if(!slider && amountLines > MAX_LINES)
  461. {
  462. slider = std::make_shared<CSlider>(Point(x, 0), y, std::bind(&OptionsTab::SelectionWindow::sliderMove, this, _1), MAX_LINES, amountLines, 0, Orientation::VERTICAL, CSlider::BLUE);
  463. slider->setPanningStep(ICON_BIG_HEIGHT);
  464. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, x + slider->pos.w, y));
  465. slider->scrollTo(sliderPos);
  466. }
  467. center();
  468. }
  469. void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
  470. {
  471. components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  472. components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  473. components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  474. components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  475. components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text, 56));
  476. }
  477. void OptionsTab::SelectionWindow::genContentGrid(int lines)
  478. {
  479. for(int y = 0; y < lines; y++)
  480. {
  481. for(int x = 0; x < elementsPerLine; x++)
  482. {
  483. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  484. }
  485. }
  486. }
  487. void OptionsTab::SelectionWindow::genContentFactions()
  488. {
  489. int i = 1;
  490. // random
  491. PlayerSettings set = PlayerSettings();
  492. set.castle = FactionID::RANDOM;
  493. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  494. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  495. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedFaction == FactionID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  496. if(selectedFaction == FactionID::RANDOM)
  497. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  498. factions.clear();
  499. for(auto & elem : allowedFactions)
  500. {
  501. int x = i % elementsPerLine;
  502. int y = (i / elementsPerLine) - (slider ? slider->getValue() : 0);
  503. PlayerSettings set = PlayerSettings();
  504. set.castle = elem;
  505. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  506. factions.push_back(elem);
  507. i++;
  508. if(y < 0 || y > MAX_LINES - 1)
  509. continue;
  510. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  511. components.push_back(std::make_shared<CPicture>(ImagePath::builtin(selectedFaction == elem ? "lobby/townBorderBigActivated" : "lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  512. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, (selectedFaction == elem) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  513. }
  514. }
  515. void OptionsTab::SelectionWindow::genContentHeroes()
  516. {
  517. int i = 1;
  518. // random
  519. PlayerSettings set = PlayerSettings();
  520. set.hero = HeroTypeID::RANDOM;
  521. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  522. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  523. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedHero == HeroTypeID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  524. if(selectedHero == HeroTypeID::RANDOM)
  525. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  526. heroes.clear();
  527. for(auto & elem : allowedHeroes)
  528. {
  529. const CHero * type = elem.toHeroType();
  530. if(type->heroClass->faction != selectedFaction)
  531. continue;
  532. int x = i % elementsPerLine;
  533. int y = (i / elementsPerLine) - (slider ? slider->getValue() : 0);
  534. PlayerSettings set = PlayerSettings();
  535. set.hero = elem;
  536. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  537. heroes.push_back(elem);
  538. i++;
  539. if(y < 0 || y > MAX_LINES - 1)
  540. continue;
  541. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  542. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, (selectedHero == elem) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  543. ImagePath image = ImagePath::builtin("lobby/townBorderBig");
  544. if(selectedHero == elem)
  545. image = ImagePath::builtin("lobby/townBorderBigActivated");
  546. if(unusableHeroes.count(elem))
  547. image = ImagePath::builtin("lobby/townBorderBigGrayedOut");
  548. components.push_back(std::make_shared<CPicture>(image, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  549. }
  550. }
  551. void OptionsTab::SelectionWindow::genContentBonus()
  552. {
  553. PlayerSettings set = SEL->getStartInfo()->playerInfos.find(color)->second;
  554. int i = 0;
  555. for(auto elem : allowedBonus)
  556. {
  557. int x = i;
  558. int y = 0;
  559. set.bonus = elem;
  560. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  561. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, x * (ICON_BIG_WIDTH-1) + 6, y * (ICON_BIG_HEIGHT-1) + (ICON_SMALL_HEIGHT/2)));
  562. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::WHITE , helper.getName());
  563. if(selectedBonus == elem)
  564. {
  565. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), x * (ICON_BIG_WIDTH-1) + 6, y * (ICON_BIG_HEIGHT-1) + (ICON_SMALL_HEIGHT/2)));
  566. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::YELLOW , helper.getName());
  567. }
  568. i++;
  569. }
  570. }
  571. int OptionsTab::SelectionWindow::getElement(const Point & cursorPosition)
  572. {
  573. int x = (cursorPosition.x - pos.x) / (ICON_BIG_WIDTH-1);
  574. int y = (cursorPosition.y - pos.y) / (ICON_BIG_HEIGHT-1) + (slider ? slider->getValue() : 0);
  575. return x + y * elementsPerLine;
  576. }
  577. void OptionsTab::SelectionWindow::setElement(int elem, bool doApply)
  578. {
  579. PlayerSettings set = PlayerSettings();
  580. if(type == SelType::TOWN)
  581. {
  582. if(elem > 0)
  583. {
  584. elem--;
  585. if(elem >= factions.size())
  586. return;
  587. set.castle = factions[elem];
  588. }
  589. else
  590. {
  591. set.castle = FactionID::RANDOM;
  592. }
  593. if(set.castle != FactionID::NONE)
  594. {
  595. if(!doApply)
  596. {
  597. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  598. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  599. }
  600. else
  601. selectedFaction = set.castle;
  602. }
  603. }
  604. if(type == SelType::HERO)
  605. {
  606. if(elem > 0)
  607. {
  608. elem--;
  609. if(elem >= heroes.size())
  610. return;
  611. set.hero = heroes[elem];
  612. }
  613. else
  614. {
  615. set.hero = HeroTypeID::RANDOM;
  616. }
  617. if(doApply && unusableHeroes.count(heroes[elem]))
  618. return;
  619. if(set.hero != HeroTypeID::NONE)
  620. {
  621. if(!doApply)
  622. {
  623. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  624. if(settings["general"]["enableUiEnhancements"].Bool() && helper.playerSettings.hero.isValid() && helper.playerSettings.heroNameTextId.empty())
  625. GH.windows().createAndPushWindow<CHeroOverview>(helper.playerSettings.hero);
  626. else
  627. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  628. }
  629. else
  630. selectedHero = set.hero;
  631. }
  632. }
  633. if(type == SelType::BONUS)
  634. {
  635. if(elem >= 4)
  636. return;
  637. set.bonus = static_cast<PlayerStartingBonus>(allowedBonus[elem]);
  638. if(!doApply)
  639. {
  640. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  641. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  642. }
  643. else
  644. selectedBonus = set.bonus;
  645. }
  646. if(doApply)
  647. apply();
  648. }
  649. void OptionsTab::SelectionWindow::sliderMove(int slidPos)
  650. {
  651. if(!slider)
  652. return; // ignore spurious call when slider is being created
  653. recreate();
  654. redraw();
  655. }
  656. bool OptionsTab::SelectionWindow::receiveEvent(const Point & position, int eventType) const
  657. {
  658. return true; // capture click also outside of window
  659. }
  660. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
  661. {
  662. if(slider && slider->pos.isInside(cursorPosition))
  663. return;
  664. if(!pos.isInside(cursorPosition))
  665. {
  666. close();
  667. return;
  668. }
  669. int elem = getElement(cursorPosition);
  670. setElement(elem, true);
  671. }
  672. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  673. {
  674. if(!pos.isInside(cursorPosition) || (slider && slider->pos.isInside(cursorPosition)))
  675. return;
  676. int elem = getElement(cursorPosition);
  677. setElement(elem, false);
  678. }
  679. OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & playerSettings, SelType type)
  680. : Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
  681. , CPlayerSettingsHelper(playerSettings, type)
  682. {
  683. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  684. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
  685. subtitle = std::make_shared<CLabel>(24, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName(), 71);
  686. pos = image->pos;
  687. setPanningStep(pos.w);
  688. }
  689. void OptionsTab::SelectedBox::update()
  690. {
  691. image->setFrame(getImageIndex());
  692. subtitle->setText(getName());
  693. }
  694. void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
  695. {
  696. // cases when we do not need to display a message
  697. if(playerSettings.castle == FactionID::NONE && CPlayerSettingsHelper::selectionType == TOWN)
  698. return;
  699. if(playerSettings.hero == HeroTypeID::NONE && !SEL->getPlayerInfo(playerSettings.color).hasCustomMainHero() && CPlayerSettingsHelper::selectionType == HERO)
  700. return;
  701. if(settings["general"]["enableUiEnhancements"].Bool() && CPlayerSettingsHelper::selectionType == HERO && playerSettings.hero.isValid() && playerSettings.heroNameTextId.empty())
  702. GH.windows().createAndPushWindow<CHeroOverview>(playerSettings.hero);
  703. else
  704. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  705. }
  706. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  707. {
  708. if(SEL->screenType != ESelectionScreen::newGame)
  709. return;
  710. PlayerInfo pi = SEL->getPlayerInfo(playerSettings.color);
  711. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(playerSettings.color);
  712. if(selectionType == SelType::TOWN && ((pi.allowedFactions.size() < 2 && !pi.isFactionRandom) || foreignPlayer))
  713. return;
  714. if(selectionType == SelType::HERO && ((pi.defaultHero() == HeroTypeID::NONE || !playerSettings.castle.isValid() || foreignPlayer)))
  715. return;
  716. if(selectionType == SelType::BONUS && foreignPlayer)
  717. return;
  718. GH.input().hapticFeedback();
  719. GH.windows().createAndPushWindow<SelectionWindow>(playerSettings.color, selectionType);
  720. }
  721. void OptionsTab::SelectedBox::scrollBy(int distance)
  722. {
  723. // FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
  724. // because of that, panning event gets interrupted (due to destruction of element)
  725. // so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
  726. distance = std::clamp(distance, -1, 1);
  727. switch(CPlayerSettingsHelper::selectionType)
  728. {
  729. case TOWN:
  730. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, playerSettings.color);
  731. break;
  732. case HERO:
  733. CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, playerSettings.color);
  734. break;
  735. case BONUS:
  736. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, playerSettings.color);
  737. break;
  738. }
  739. setScrollingEnabled(false);
  740. }
  741. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
  742. : CIntObject(LCLICK | KEYBOARD | TEXTINPUT)
  743. , pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
  744. , s(std::make_unique<PlayerSettings>(S))
  745. , parentTab(parent)
  746. , name(S.name)
  747. {
  748. OBJ_CONSTRUCTION;
  749. defActions |= SHARE_POS;
  750. int serial = 0;
  751. for(PlayerColor g = PlayerColor(0); g < s->color; ++g)
  752. {
  753. auto itred = SEL->getPlayerInfo(g);
  754. if(itred.canComputerPlay || itred.canHumanPlay)
  755. serial++;
  756. }
  757. pos.x += 54;
  758. pos.y += 128 + serial * 50;
  759. assert(CSH->mi && CSH->mi->mapHeader);
  760. const PlayerInfo & p = SEL->getPlayerInfo(s->color);
  761. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  762. if(p.canHumanPlay && p.canComputerPlay)
  763. whoCanPlay = HUMAN_OR_CPU;
  764. else if(p.canComputerPlay)
  765. whoCanPlay = CPU;
  766. else
  767. whoCanPlay = HUMAN;
  768. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
  769. {{
  770. "AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  771. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
  772. }};
  773. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
  774. {{
  775. "ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  776. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
  777. }};
  778. background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
  779. if(s->isControlledByAI() || CSH->isGuest())
  780. labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name, 95);
  781. else
  782. {
  783. labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, ETextAlignment::CENTER, false);
  784. labelPlayerNameEdit->setText(name);
  785. }
  786. labelWhoCanPlay = std::make_shared<CMultiLineLabel>(Rect(6, 23, 45, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->arraytxt[206 + whoCanPlay]);
  787. labelHandicap = std::make_shared<CMultiLineLabel>(Rect(56, 24, 49, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, s->handicap.startBonus.empty() && s->handicap.percentIncome == 100 ? CGI->generaltexth->arraytxt[210] : MetaString::createFromTextID("vcmi.lobby.handicap").toString());
  788. handicap = std::make_shared<LRClickableArea>(Rect(56, 24, 49, (int)graphics->fonts[EFonts::FONT_TINY]->getLineHeight()*2), [this](){
  789. if(!CSH->isHost())
  790. return;
  791. TResources resourcesStart = TResources();
  792. resourcesStart[EGameResID::GOLD] = 50000;
  793. int resourcesPercent = 120;
  794. CSH->setPlayerHandicap(s->color, PlayerSettings::Handicap{resourcesStart, resourcesPercent});
  795. }, [this](){
  796. if(s->handicap.startBonus.empty() && s->handicap.percentIncome == 100)
  797. CRClickPopup::createAndPush(MetaString::createFromTextID("core.help.124.help").toString());
  798. else
  799. {
  800. auto str = MetaString::createFromTextID("vcmi.lobby.handicap");
  801. str.appendRawString(":\n");
  802. for(auto & res : EGameResID::ALL_RESOURCES())
  803. if(s->handicap.startBonus[res] != 0)
  804. {
  805. str.appendRawString("\n");
  806. str.appendName(res);
  807. str.appendRawString(": ");
  808. str.appendRawString(std::to_string(s->handicap.startBonus[res]));
  809. }
  810. if(s->handicap.percentIncome != 100)
  811. {
  812. str.appendRawString("\n");
  813. str.appendTextID("core.jktext.32");
  814. str.appendRawString(": ");
  815. str.appendRawString(std::to_string(s->handicap.percentIncome) + "%");
  816. }
  817. CRClickPopup::createAndPush(str.toString());
  818. }
  819. });
  820. if(SEL->screenType == ESelectionScreen::newGame)
  821. {
  822. buttonTownLeft = std::make_shared<CButton>(Point(107, 5), AnimationPath::builtin("ADOPLFA.DEF"), CGI->generaltexth->zelp[132], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, -1, s->color));
  823. buttonTownRight = std::make_shared<CButton>(Point(168, 5), AnimationPath::builtin("ADOPRTA.DEF"), CGI->generaltexth->zelp[133], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, +1, s->color));
  824. buttonHeroLeft = std::make_shared<CButton>(Point(183, 5), AnimationPath::builtin("ADOPLFA.DEF"), CGI->generaltexth->zelp[148], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, -1, s->color));
  825. buttonHeroRight = std::make_shared<CButton>(Point(244, 5), AnimationPath::builtin("ADOPRTA.DEF"), CGI->generaltexth->zelp[149], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, +1, s->color));
  826. buttonBonusLeft = std::make_shared<CButton>(Point(259, 5), AnimationPath::builtin("ADOPLFA.DEF"), CGI->generaltexth->zelp[164], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, -1, s->color));
  827. buttonBonusRight = std::make_shared<CButton>(Point(320, 5), AnimationPath::builtin("ADOPRTA.DEF"), CGI->generaltexth->zelp[165], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, +1, s->color));
  828. }
  829. hideUnavailableButtons();
  830. if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color).canHumanPlay)
  831. {
  832. flag = std::make_shared<CButton>(
  833. Point(-43, 2),
  834. AnimationPath::builtin(flags[s->color.getNum()]),
  835. CGI->generaltexth->zelp[180],
  836. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  837. );
  838. flag->setHoverable(true);
  839. flag->block(CSH->isGuest());
  840. }
  841. else
  842. flag = nullptr;
  843. town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
  844. hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
  845. bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
  846. }
  847. bool OptionsTab::PlayerOptionsEntry::captureThisKey(EShortcut key)
  848. {
  849. return labelPlayerNameEdit && labelPlayerNameEdit->hasFocus() && key == EShortcut::GLOBAL_ACCEPT;
  850. }
  851. void OptionsTab::PlayerOptionsEntry::keyPressed(EShortcut key)
  852. {
  853. if(labelPlayerNameEdit && key == EShortcut::GLOBAL_ACCEPT)
  854. updateName();
  855. }
  856. bool OptionsTab::PlayerOptionsEntry::receiveEvent(const Point & position, int eventType) const
  857. {
  858. return eventType == AEventsReceiver::LCLICK; // capture all left clicks (not only within control)
  859. }
  860. void OptionsTab::PlayerOptionsEntry::clickReleased(const Point & cursorPosition)
  861. {
  862. if(labelPlayerNameEdit && !labelPlayerNameEdit->pos.isInside(cursorPosition))
  863. updateName();
  864. }
  865. void OptionsTab::PlayerOptionsEntry::updateName() {
  866. if(labelPlayerNameEdit->getText() != name)
  867. {
  868. CSH->setPlayerName(s->color, labelPlayerNameEdit->getText());
  869. if(CSH->isMyColor(s->color))
  870. {
  871. Settings set = settings.write["general"]["playerName"];
  872. set->String() = labelPlayerNameEdit->getText();
  873. }
  874. }
  875. labelPlayerNameEdit->removeFocus();
  876. name = labelPlayerNameEdit->getText();
  877. }
  878. void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
  879. {
  880. if(ps.isControlledByAI() || humanPlayers > 1)
  881. CSH->setPlayer(ps.color);
  882. }
  883. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  884. {
  885. if(!buttonTownLeft)
  886. return;
  887. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
  888. if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
  889. {
  890. buttonTownLeft->disable();
  891. buttonTownRight->disable();
  892. }
  893. else
  894. {
  895. buttonTownLeft->enable();
  896. buttonTownRight->enable();
  897. }
  898. if((pi->defaultHero() != HeroTypeID::RANDOM || !s->castle.isValid()) //fixed hero
  899. || foreignPlayer) //or not our player
  900. {
  901. buttonHeroLeft->disable();
  902. buttonHeroRight->disable();
  903. }
  904. else
  905. {
  906. buttonHeroLeft->enable();
  907. buttonHeroRight->enable();
  908. }
  909. if(foreignPlayer)
  910. {
  911. buttonBonusLeft->disable();
  912. buttonBonusRight->disable();
  913. }
  914. else
  915. {
  916. buttonBonusLeft->enable();
  917. buttonBonusRight->enable();
  918. }
  919. }