OptionsTab.cpp 39 KB

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