OptionsTab.cpp 32 KB

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