2
0

OptionsTab.cpp 31 KB

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