OptionsTab.cpp 39 KB

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