OptionsTab.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. allowedHeroes = SEL->getMapInfo()->mapHeader->allowedHeroes;
  361. for(auto & player : SEL->getStartInfo()->playerInfos)
  362. {
  363. if(player.first != color && (int)player.second.hero > HeroTypeID::RANDOM)
  364. unusableHeroes.insert(player.second.hero);
  365. }
  366. allowedBonus.push_back(PlayerStartingBonus::RANDOM);
  367. if(initialHero != HeroTypeID::NONE|| SEL->getPlayerInfo(color).heroesNames.size() > 0)
  368. allowedBonus.push_back(PlayerStartingBonus::ARTIFACT);
  369. allowedBonus.push_back(PlayerStartingBonus::GOLD);
  370. if(initialFaction.isValid())
  371. allowedBonus.push_back(PlayerStartingBonus::RESOURCE);
  372. recreate();
  373. }
  374. int OptionsTab::SelectionWindow::calcLines(FactionID faction)
  375. {
  376. double additionalItems = 1; // random
  377. if(!faction.isValid())
  378. return std::ceil(((double)allowedFactions.size() + additionalItems) / elementsPerLine);
  379. int count = 0;
  380. for(auto & elemh : allowedHeroes)
  381. {
  382. CHero * type = VLC->heroh->objects[elemh];
  383. if(type->heroClass->faction == faction)
  384. count++;
  385. }
  386. return std::ceil(std::max((double)count + additionalItems, (double)allowedFactions.size() + additionalItems) / (double)elementsPerLine);
  387. }
  388. void OptionsTab::SelectionWindow::apply()
  389. {
  390. if(GH.windows().isTopWindow(this))
  391. {
  392. GH.input().hapticFeedback();
  393. CCS->soundh->playSound(soundBase::button);
  394. close();
  395. setSelection();
  396. }
  397. }
  398. void OptionsTab::SelectionWindow::setSelection()
  399. {
  400. if(selectedFaction != initialFaction)
  401. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN_ID, selectedFaction, color);
  402. if(selectedHero != initialHero)
  403. CSH->setPlayerOption(LobbyChangePlayerOption::HERO_ID, selectedHero, color);
  404. if(selectedBonus != initialBonus)
  405. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS_ID, static_cast<int>(selectedBonus), color);
  406. }
  407. void OptionsTab::SelectionWindow::reopen()
  408. {
  409. std::shared_ptr<SelectionWindow> window = std::shared_ptr<SelectionWindow>(new SelectionWindow(color, type));
  410. close();
  411. GH.windows().pushWindow(window);
  412. }
  413. void OptionsTab::SelectionWindow::recreate()
  414. {
  415. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  416. int amountLines = 1;
  417. if(type == SelType::BONUS)
  418. elementsPerLine = allowedBonus.size();
  419. else
  420. {
  421. // try to make squarish
  422. if(type == SelType::TOWN)
  423. elementsPerLine = floor(sqrt(allowedFactions.size()));
  424. if(type == SelType::HERO)
  425. {
  426. int count = 0;
  427. for(auto & elem : allowedHeroes)
  428. {
  429. CHero * type = VLC->heroh->objects[elem];
  430. if(type->heroClass->faction == selectedFaction)
  431. {
  432. count++;
  433. }
  434. }
  435. elementsPerLine = floor(sqrt(count));
  436. }
  437. amountLines = calcLines((type > SelType::TOWN) ? selectedFaction : FactionID::RANDOM);
  438. }
  439. int x = (elementsPerLine) * (ICON_BIG_WIDTH-1);
  440. int y = (amountLines) * (ICON_BIG_HEIGHT-1);
  441. pos = Rect(0, 0, x, y);
  442. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), pos);
  443. backgroundTexture->playerColored(PlayerColor(1));
  444. updateShadow();
  445. if(type == SelType::TOWN)
  446. genContentFactions();
  447. if(type == SelType::HERO)
  448. genContentHeroes();
  449. if(type == SelType::BONUS)
  450. genContentBonus();
  451. genContentGrid(amountLines);
  452. center();
  453. }
  454. void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
  455. {
  456. components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  457. components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  458. components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  459. components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  460. components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text));
  461. }
  462. void OptionsTab::SelectionWindow::genContentGrid(int lines)
  463. {
  464. for(int y = 0; y < lines; y++)
  465. {
  466. for(int x = 0; x < elementsPerLine; x++)
  467. {
  468. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  469. }
  470. }
  471. }
  472. void OptionsTab::SelectionWindow::genContentFactions()
  473. {
  474. int i = 1;
  475. // random
  476. PlayerSettings set = PlayerSettings();
  477. set.castle = FactionID::RANDOM;
  478. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  479. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  480. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedFaction == FactionID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  481. if(selectedFaction == FactionID::RANDOM)
  482. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  483. for(auto & elem : allowedFactions)
  484. {
  485. int x = i % elementsPerLine;
  486. int y = i / elementsPerLine;
  487. PlayerSettings set = PlayerSettings();
  488. set.castle = elem;
  489. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  490. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  491. 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)));
  492. 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());
  493. factions.push_back(elem);
  494. i++;
  495. }
  496. }
  497. void OptionsTab::SelectionWindow::genContentHeroes()
  498. {
  499. int i = 1;
  500. // random
  501. PlayerSettings set = PlayerSettings();
  502. set.hero = HeroTypeID::RANDOM;
  503. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  504. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  505. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedHero == HeroTypeID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  506. if(selectedHero == HeroTypeID::RANDOM)
  507. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  508. for(auto & elem : allowedHeroes)
  509. {
  510. CHero * type = VLC->heroh->objects[elem];
  511. if(type->heroClass->faction == selectedFaction)
  512. {
  513. int x = i % elementsPerLine;
  514. int y = i / elementsPerLine;
  515. PlayerSettings set = PlayerSettings();
  516. set.hero = elem;
  517. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  518. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  519. 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());
  520. ImagePath image = ImagePath::builtin("lobby/townBorderBig");
  521. if(selectedHero == elem)
  522. image = ImagePath::builtin("lobby/townBorderBigActivated");
  523. if(unusableHeroes.count(elem))
  524. image = ImagePath::builtin("lobby/townBorderBigGrayedOut");
  525. components.push_back(std::make_shared<CPicture>(image, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  526. heroes.push_back(elem);
  527. i++;
  528. }
  529. }
  530. }
  531. void OptionsTab::SelectionWindow::genContentBonus()
  532. {
  533. PlayerSettings set = PlayerSettings();
  534. int i = 0;
  535. for(auto elem : allowedBonus)
  536. {
  537. int x = i;
  538. int y = 0;
  539. set.bonus = elem;
  540. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  541. 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)));
  542. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::WHITE , helper.getName());
  543. if(selectedBonus == elem)
  544. {
  545. 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)));
  546. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::YELLOW , helper.getName());
  547. }
  548. i++;
  549. }
  550. }
  551. int OptionsTab::SelectionWindow::getElement(const Point & cursorPosition)
  552. {
  553. int x = (cursorPosition.x - pos.x) / (ICON_BIG_WIDTH-1);
  554. int y = (cursorPosition.y - pos.y) / (ICON_BIG_HEIGHT-1);
  555. return x + y * elementsPerLine;
  556. }
  557. void OptionsTab::SelectionWindow::setElement(int elem, bool doApply)
  558. {
  559. PlayerSettings set = PlayerSettings();
  560. if(type == SelType::TOWN)
  561. {
  562. if(elem > 0)
  563. {
  564. elem--;
  565. if(elem >= factions.size())
  566. return;
  567. set.castle = factions[elem];
  568. }
  569. else
  570. {
  571. set.castle = FactionID::RANDOM;
  572. }
  573. if(set.castle != FactionID::NONE)
  574. {
  575. if(!doApply)
  576. {
  577. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  578. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  579. }
  580. else
  581. selectedFaction = set.castle;
  582. }
  583. }
  584. if(type == SelType::HERO)
  585. {
  586. if(elem > 0)
  587. {
  588. elem--;
  589. if(elem >= heroes.size())
  590. return;
  591. set.hero = heroes[elem];
  592. }
  593. else
  594. {
  595. set.hero = HeroTypeID::RANDOM;
  596. }
  597. if(doApply && unusableHeroes.count(heroes[elem]))
  598. return;
  599. if(set.hero != HeroTypeID::NONE)
  600. {
  601. if(!doApply)
  602. {
  603. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  604. if(settings["general"]["enableUiEnhancements"].Bool() && helper.playerSettings.hero.isValid() && helper.playerSettings.heroNameTextId.empty())
  605. GH.windows().createAndPushWindow<CHeroOverview>(helper.playerSettings.hero);
  606. else
  607. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  608. }
  609. else
  610. selectedHero = set.hero;
  611. }
  612. }
  613. if(type == SelType::BONUS)
  614. {
  615. if(elem >= 4)
  616. return;
  617. set.bonus = static_cast<PlayerStartingBonus>(allowedBonus[elem]);
  618. if(!doApply)
  619. {
  620. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  621. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  622. }
  623. else
  624. selectedBonus = set.bonus;
  625. }
  626. if(doApply)
  627. apply();
  628. }
  629. bool OptionsTab::SelectionWindow::receiveEvent(const Point & position, int eventType) const
  630. {
  631. return true; // capture click also outside of window
  632. }
  633. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
  634. {
  635. if(!pos.isInside(cursorPosition))
  636. {
  637. close();
  638. return;
  639. }
  640. int elem = getElement(cursorPosition);
  641. setElement(elem, true);
  642. }
  643. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  644. {
  645. if(!pos.isInside(cursorPosition))
  646. return;
  647. int elem = getElement(cursorPosition);
  648. setElement(elem, false);
  649. }
  650. OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & playerSettings, SelType type)
  651. : Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
  652. , CPlayerSettingsHelper(playerSettings, type)
  653. {
  654. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  655. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
  656. subtitle = std::make_shared<CLabel>(23, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName());
  657. pos = image->pos;
  658. setPanningStep(pos.w);
  659. }
  660. void OptionsTab::SelectedBox::update()
  661. {
  662. image->setFrame(getImageIndex());
  663. subtitle->setText(getName());
  664. }
  665. void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
  666. {
  667. // cases when we do not need to display a message
  668. if(playerSettings.castle == FactionID::NONE && CPlayerSettingsHelper::selectionType == TOWN)
  669. return;
  670. if(playerSettings.hero == HeroTypeID::NONE && !SEL->getPlayerInfo(playerSettings.color).hasCustomMainHero() && CPlayerSettingsHelper::selectionType == HERO)
  671. return;
  672. if(settings["general"]["enableUiEnhancements"].Bool() && CPlayerSettingsHelper::selectionType == HERO && playerSettings.hero.isValid() && playerSettings.heroNameTextId.empty())
  673. GH.windows().createAndPushWindow<CHeroOverview>(playerSettings.hero);
  674. else
  675. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  676. }
  677. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  678. {
  679. if(SEL->screenType != ESelectionScreen::newGame)
  680. return;
  681. PlayerInfo pi = SEL->getPlayerInfo(playerSettings.color);
  682. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(playerSettings.color);
  683. if(selectionType == SelType::TOWN && ((pi.allowedFactions.size() < 2 && !pi.isFactionRandom) || foreignPlayer))
  684. return;
  685. if(selectionType == SelType::HERO && ((pi.defaultHero() == HeroTypeID::NONE || !playerSettings.castle.isValid() || foreignPlayer)))
  686. return;
  687. if(selectionType == SelType::BONUS && foreignPlayer)
  688. return;
  689. GH.input().hapticFeedback();
  690. GH.windows().createAndPushWindow<SelectionWindow>(playerSettings.color, selectionType);
  691. }
  692. void OptionsTab::SelectedBox::scrollBy(int distance)
  693. {
  694. // FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
  695. // because of that, panning event gets interrupted (due to destruction of element)
  696. // so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
  697. distance = std::clamp(distance, -1, 1);
  698. switch(CPlayerSettingsHelper::selectionType)
  699. {
  700. case TOWN:
  701. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, playerSettings.color);
  702. break;
  703. case HERO:
  704. CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, playerSettings.color);
  705. break;
  706. case BONUS:
  707. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, playerSettings.color);
  708. break;
  709. }
  710. setScrollingEnabled(false);
  711. }
  712. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
  713. : CIntObject(LCLICK | KEYBOARD | TEXTINPUT)
  714. , pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
  715. , s(std::make_unique<PlayerSettings>(S))
  716. , parentTab(parent)
  717. , name(S.name)
  718. {
  719. OBJ_CONSTRUCTION;
  720. defActions |= SHARE_POS;
  721. int serial = 0;
  722. for(PlayerColor g = PlayerColor(0); g < s->color; ++g)
  723. {
  724. auto itred = SEL->getPlayerInfo(g);
  725. if(itred.canComputerPlay || itred.canHumanPlay)
  726. serial++;
  727. }
  728. pos.x += 54;
  729. pos.y += 128 + serial * 50;
  730. assert(CSH->mi && CSH->mi->mapHeader);
  731. const PlayerInfo & p = SEL->getPlayerInfo(s->color);
  732. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  733. if(p.canHumanPlay && p.canComputerPlay)
  734. whoCanPlay = HUMAN_OR_CPU;
  735. else if(p.canComputerPlay)
  736. whoCanPlay = CPU;
  737. else
  738. whoCanPlay = HUMAN;
  739. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
  740. {{
  741. "AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  742. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
  743. }};
  744. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
  745. {{
  746. "ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  747. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
  748. }};
  749. background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
  750. if(s->isControlledByAI() || CSH->isGuest())
  751. labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name);
  752. else
  753. {
  754. labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, nullptr, false);
  755. labelPlayerNameEdit->setText(name);
  756. }
  757. 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]);
  758. if(SEL->screenType == ESelectionScreen::newGame)
  759. {
  760. 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));
  761. 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));
  762. 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));
  763. 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));
  764. 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));
  765. 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));
  766. }
  767. hideUnavailableButtons();
  768. if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color).canHumanPlay)
  769. {
  770. flag = std::make_shared<CButton>(
  771. Point(-43, 2),
  772. AnimationPath::builtin(flags[s->color.getNum()]),
  773. CGI->generaltexth->zelp[180],
  774. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  775. );
  776. flag->hoverable = true;
  777. flag->block(CSH->isGuest());
  778. }
  779. else
  780. flag = nullptr;
  781. town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
  782. hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
  783. bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
  784. }
  785. bool OptionsTab::PlayerOptionsEntry::captureThisKey(EShortcut key)
  786. {
  787. return labelPlayerNameEdit && labelPlayerNameEdit->hasFocus() && key == EShortcut::GLOBAL_ACCEPT;
  788. }
  789. void OptionsTab::PlayerOptionsEntry::keyPressed(EShortcut key)
  790. {
  791. if(labelPlayerNameEdit && key == EShortcut::GLOBAL_ACCEPT)
  792. updateName();
  793. }
  794. bool OptionsTab::PlayerOptionsEntry::receiveEvent(const Point & position, int eventType) const
  795. {
  796. return eventType == AEventsReceiver::LCLICK; // capture all left clicks (not only within control)
  797. }
  798. void OptionsTab::PlayerOptionsEntry::clickReleased(const Point & cursorPosition)
  799. {
  800. if(labelPlayerNameEdit && !labelPlayerNameEdit->pos.isInside(cursorPosition))
  801. updateName();
  802. }
  803. void OptionsTab::PlayerOptionsEntry::updateName() {
  804. if(labelPlayerNameEdit->getText() != name)
  805. {
  806. CSH->setPlayerName(s->color, labelPlayerNameEdit->getText());
  807. if(CSH->isMyColor(s->color))
  808. {
  809. Settings set = settings.write["general"]["playerName"];
  810. set->String() = labelPlayerNameEdit->getText();
  811. }
  812. }
  813. labelPlayerNameEdit->removeFocus();
  814. name = labelPlayerNameEdit->getText();
  815. }
  816. void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
  817. {
  818. if(ps.isControlledByAI() || humanPlayers > 0)
  819. CSH->setPlayer(ps.color);
  820. }
  821. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  822. {
  823. if(!buttonTownLeft)
  824. return;
  825. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
  826. if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
  827. {
  828. buttonTownLeft->disable();
  829. buttonTownRight->disable();
  830. }
  831. else
  832. {
  833. buttonTownLeft->enable();
  834. buttonTownRight->enable();
  835. }
  836. if((pi->defaultHero() != HeroTypeID::RANDOM || !s->castle.isValid()) //fixed hero
  837. || foreignPlayer) //or not our player
  838. {
  839. buttonHeroLeft->disable();
  840. buttonHeroRight->disable();
  841. }
  842. else
  843. {
  844. buttonHeroLeft->enable();
  845. buttonHeroRight->enable();
  846. }
  847. if(foreignPlayer)
  848. {
  849. buttonBonusLeft->disable();
  850. buttonBonusRight->disable();
  851. }
  852. else
  853. {
  854. buttonBonusLeft->enable();
  855. buttonBonusRight->enable();
  856. }
  857. }