OptionsTab.cpp 32 KB

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