OptionsTab.cpp 31 KB

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