OptionsTab.cpp 39 KB

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