OptionsTab.cpp 32 KB

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