OptionsTab.cpp 31 KB

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