OptionsTab.cpp 32 KB

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