OptionsTab.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 "../render/IRenderHandler.h"
  21. #include "../media/ISoundPlayer.h"
  22. #include "../widgets/CComponent.h"
  23. #include "../widgets/ComboBox.h"
  24. #include "../widgets/CTextInput.h"
  25. #include "../widgets/Buttons.h"
  26. #include "../widgets/Images.h"
  27. #include "../widgets/MiscWidgets.h"
  28. #include "../widgets/ObjectLists.h"
  29. #include "../widgets/Slider.h"
  30. #include "../widgets/TextControls.h"
  31. #include "../widgets/GraphicalPrimitiveCanvas.h"
  32. #include "../windows/GUIClasses.h"
  33. #include "../windows/InfoWindows.h"
  34. #include "../windows/CHeroOverview.h"
  35. #include "../eventsSDL/InputHandler.h"
  36. #include "../../lib/entities/faction/CFaction.h"
  37. #include "../../lib/entities/faction/CTown.h"
  38. #include "../../lib/entities/faction/CTownHandler.h"
  39. #include "../../lib/entities/hero/CHeroHandler.h"
  40. #include "../../lib/entities/hero/CHeroClass.h"
  41. #include "../../lib/filesystem/Filesystem.h"
  42. #include "../../lib/networkPacks/PacksForLobby.h"
  43. #include "../../lib/texts/CGeneralTextHandler.h"
  44. #include "../../lib/CArtHandler.h"
  45. #include "../../lib/CConfigHandler.h"
  46. #include "../../lib/mapping/CMapInfo.h"
  47. #include "../../lib/mapping/CMapHeader.h"
  48. static JsonPath optionsTabConfigLocation()
  49. {
  50. if(settings["general"]["enableUiEnhancements"].Bool())
  51. return JsonPath::builtin("config/widgets/playerOptionsTab.json");
  52. else
  53. return JsonPath::builtin("config/widgets/advancedOptionsTab.json");
  54. }
  55. OptionsTab::OptionsTab()
  56. : OptionsTabBase(optionsTabConfigLocation())
  57. , humanPlayers(0)
  58. {
  59. }
  60. void OptionsTab::recreate()
  61. {
  62. entries.clear();
  63. humanPlayers = 0;
  64. for (auto selectionWindow : GH.windows().findWindows<SelectionWindow>())
  65. {
  66. selectionWindow->reopen();
  67. }
  68. OBJECT_CONSTRUCTION;
  69. for(auto & pInfo : SEL->getStartInfo()->playerInfos)
  70. {
  71. if(pInfo.second.isControlledByHuman())
  72. humanPlayers++;
  73. entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
  74. }
  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. OBJECT_CONSTRUCTION;
  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::make_shared<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. OBJECT_CONSTRUCTION;
  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>(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. void OptionsTab::SelectionWindow::notFocusedClick()
  660. {
  661. close();
  662. }
  663. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
  664. {
  665. if(slider && slider->pos.isInside(cursorPosition))
  666. return;
  667. int elem = getElement(cursorPosition);
  668. setElement(elem, true);
  669. }
  670. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  671. {
  672. if(!pos.isInside(cursorPosition) || (slider && slider->pos.isInside(cursorPosition)))
  673. return;
  674. int elem = getElement(cursorPosition);
  675. setElement(elem, false);
  676. }
  677. OptionsTab::HandicapWindow::HandicapWindow()
  678. : CWindowObject(BORDERED)
  679. {
  680. OBJECT_CONSTRUCTION;
  681. addUsedEvents(LCLICK);
  682. pos = Rect(0, 0, 660, 100 + SEL->getStartInfo()->playerInfos.size() * 30);
  683. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(pos);
  684. backgroundTexture->setPlayerColor(PlayerColor(1));
  685. labels.push_back(std::make_shared<CLabel>(pos.w / 2 + 8, 15, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.handicap")));
  686. enum Columns : int32_t
  687. {
  688. INCOME = 1000,
  689. GROWTH = 2000,
  690. };
  691. auto columns = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS, Columns::INCOME, Columns::GROWTH};
  692. int i = 0;
  693. for(auto & pInfo : SEL->getStartInfo()->playerInfos)
  694. {
  695. PlayerColor player = pInfo.first;
  696. anim.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), player.getNum(), 0, 7, 57 + i * 30));
  697. for(int j = 0; j < columns.size(); j++)
  698. {
  699. bool isIncome = int(columns[j]) == Columns::INCOME;
  700. bool isGrowth = int(columns[j]) == Columns::GROWTH;
  701. EGameResID resource = columns[j];
  702. const PlayerSettings &ps = SEL->getStartInfo()->getIthPlayersSettings(player);
  703. int xPos = 30 + j * 70;
  704. xPos += j > 0 ? 10 : 0; // Gold field is larger
  705. if(i == 0)
  706. {
  707. if(isIncome)
  708. labels.push_back(std::make_shared<CLabel>(xPos, 38, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("core.jktext.32")));
  709. else if(isGrowth)
  710. labels.push_back(std::make_shared<CLabel>(xPos, 38, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.194")));
  711. else
  712. anim.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(resource), 0, 15 + xPos + (j == 0 ? 10 : 0), 35));
  713. }
  714. auto area = Rect(xPos, 60 + i * 30, j == 0 ? 60 : 50, 16);
  715. textinputbackgrounds.push_back(std::make_shared<TransparentFilledRectangle>(area.resize(3), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64)));
  716. textinputs[player][resource] = std::make_shared<CTextInput>(area, FONT_SMALL, ETextAlignment::CENTERLEFT, true);
  717. textinputs[player][resource]->setText(std::to_string(isIncome ? ps.handicap.percentIncome : (isGrowth ? ps.handicap.percentGrowth : ps.handicap.startBonus[resource])));
  718. textinputs[player][resource]->setCallback([this, player, resource, isIncome, isGrowth](const std::string & s){
  719. // text input processing: add/remove sign when pressing "-"; remove non digits; cut length; fill empty field with 0
  720. std::string tmp = s;
  721. bool negative = std::count_if( s.begin(), s.end(), []( char c ){ return c == '-'; }) == 1 && !isIncome && !isGrowth;
  722. tmp.erase(std::remove_if(tmp.begin(), tmp.end(), [](char c) { return !isdigit(c); }), tmp.end());
  723. int maxLength = isIncome || isGrowth ? 3 : (resource == EGameResID::GOLD ? 6 : 5);
  724. tmp = tmp.substr(0, maxLength);
  725. textinputs[player][resource]->setText(tmp.length() == 0 ? "0" : (negative ? "-" : "") + std::to_string(stoi(tmp)));
  726. });
  727. textinputs[player][resource]->setPopupCallback([isIncome, isGrowth](){
  728. // Help for the textinputs
  729. if(isIncome)
  730. CRClickPopup::createAndPush(CGI->generaltexth->translate("vcmi.lobby.handicap.income"));
  731. else if(isGrowth)
  732. CRClickPopup::createAndPush(CGI->generaltexth->translate("vcmi.lobby.handicap.growth"));
  733. else
  734. CRClickPopup::createAndPush(CGI->generaltexth->translate("vcmi.lobby.handicap.resource"));
  735. });
  736. if(isIncome || isGrowth)
  737. labels.push_back(std::make_shared<CLabel>(area.topRight().x, area.center().y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "%"));
  738. }
  739. i++;
  740. }
  741. buttons.push_back(std::make_shared<CButton>(Point(pos.w / 2 - 32, 60 + SEL->getStartInfo()->playerInfos.size() * 30), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){
  742. for (const auto& player : textinputs)
  743. {
  744. TResources resources = TResources();
  745. int income = 100;
  746. int growth = 100;
  747. for (const auto& resource : player.second)
  748. {
  749. bool isIncome = int(resource.first) == Columns::INCOME;
  750. bool isGrowth = int(resource.first) == Columns::GROWTH;
  751. if(isIncome)
  752. income = std::stoi(resource.second->getText());
  753. else if(isGrowth)
  754. growth = std::stoi(resource.second->getText());
  755. else
  756. resources[resource.first] = std::stoi(resource.second->getText());
  757. }
  758. CSH->setPlayerHandicap(player.first, Handicap{resources, income, growth});
  759. }
  760. close();
  761. }, EShortcut::GLOBAL_RETURN));
  762. updateShadow();
  763. center();
  764. }
  765. void OptionsTab::HandicapWindow::notFocusedClick()
  766. {
  767. close();
  768. }
  769. OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & playerSettings, SelType type)
  770. : Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
  771. , CPlayerSettingsHelper(playerSettings, type)
  772. {
  773. OBJECT_CONSTRUCTION;
  774. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
  775. subtitle = std::make_shared<CLabel>(24, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName(), 71);
  776. pos = image->pos;
  777. setPanningStep(pos.w);
  778. }
  779. void OptionsTab::SelectedBox::update()
  780. {
  781. image->setFrame(getImageIndex());
  782. subtitle->setText(getName());
  783. }
  784. void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
  785. {
  786. // cases when we do not need to display a message
  787. if(playerSettings.castle == FactionID::NONE && CPlayerSettingsHelper::selectionType == TOWN)
  788. return;
  789. if(playerSettings.hero == HeroTypeID::NONE && !SEL->getPlayerInfo(playerSettings.color).hasCustomMainHero() && CPlayerSettingsHelper::selectionType == HERO)
  790. return;
  791. if(settings["general"]["enableUiEnhancements"].Bool() && CPlayerSettingsHelper::selectionType == HERO && playerSettings.hero.isValid() && playerSettings.heroNameTextId.empty())
  792. GH.windows().createAndPushWindow<CHeroOverview>(playerSettings.hero);
  793. else
  794. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  795. }
  796. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  797. {
  798. if(SEL->screenType != ESelectionScreen::newGame)
  799. return;
  800. PlayerInfo pi = SEL->getPlayerInfo(playerSettings.color);
  801. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(playerSettings.color);
  802. if(selectionType == SelType::TOWN && ((pi.allowedFactions.size() < 2 && !pi.isFactionRandom) || foreignPlayer))
  803. return;
  804. if(selectionType == SelType::HERO && ((pi.defaultHero() == HeroTypeID::NONE || !playerSettings.castle.isValid() || foreignPlayer)))
  805. return;
  806. if(selectionType == SelType::BONUS && foreignPlayer)
  807. return;
  808. GH.input().hapticFeedback();
  809. GH.windows().createAndPushWindow<SelectionWindow>(playerSettings.color, selectionType);
  810. }
  811. void OptionsTab::SelectedBox::scrollBy(int distance)
  812. {
  813. // FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
  814. // because of that, panning event gets interrupted (due to destruction of element)
  815. // so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
  816. distance = std::clamp(distance, -1, 1);
  817. switch(CPlayerSettingsHelper::selectionType)
  818. {
  819. case TOWN:
  820. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, playerSettings.color);
  821. break;
  822. case HERO:
  823. CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, playerSettings.color);
  824. break;
  825. case BONUS:
  826. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, playerSettings.color);
  827. break;
  828. }
  829. setScrollingEnabled(false);
  830. }
  831. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
  832. : CIntObject(LCLICK | KEYBOARD | TEXTINPUT)
  833. , pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
  834. , s(std::make_unique<PlayerSettings>(S))
  835. , parentTab(parent)
  836. , name(S.name)
  837. {
  838. OBJECT_CONSTRUCTION;
  839. int serial = 0;
  840. for(PlayerColor g = PlayerColor(0); g < s->color; ++g)
  841. {
  842. auto itred = SEL->getPlayerInfo(g);
  843. if(itred.canComputerPlay || itred.canHumanPlay)
  844. serial++;
  845. }
  846. pos.x += 54;
  847. pos.y += 128 + serial * 50;
  848. assert(CSH->mi && CSH->mi->mapHeader);
  849. const PlayerInfo & p = SEL->getPlayerInfo(s->color);
  850. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  851. if(p.canHumanPlay && p.canComputerPlay)
  852. whoCanPlay = HUMAN_OR_CPU;
  853. else if(p.canComputerPlay)
  854. whoCanPlay = CPU;
  855. else
  856. whoCanPlay = HUMAN;
  857. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
  858. {{
  859. "AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  860. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
  861. }};
  862. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
  863. {{
  864. "ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  865. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
  866. }};
  867. background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
  868. if(s->isControlledByAI() || CSH->isGuest())
  869. labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name, 95);
  870. else
  871. {
  872. labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, ETextAlignment::CENTER, false);
  873. labelPlayerNameEdit->setText(name);
  874. }
  875. labelWhoCanPlay = std::make_shared<CMultiLineLabel>(Rect(6, 21, 45, 26), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->arraytxt[206 + whoCanPlay]);
  876. auto hasHandicap = [this](){ return s->handicap.startBonus.empty() && s->handicap.percentIncome == 100 && s->handicap.percentGrowth == 100; };
  877. std::string labelHandicapText = hasHandicap() ? CGI->generaltexth->arraytxt[210] : MetaString::createFromTextID("vcmi.lobby.handicap").toString();
  878. labelHandicap = std::make_shared<CMultiLineLabel>(Rect(55, 23, 46, 24), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, labelHandicapText);
  879. handicap = std::make_shared<LRClickableArea>(Rect(53, 23, 50, 24), [](){
  880. if(!CSH->isHost())
  881. return;
  882. GH.windows().createAndPushWindow<HandicapWindow>();
  883. }, [this, hasHandicap](){
  884. if(hasHandicap())
  885. CRClickPopup::createAndPush(MetaString::createFromTextID("core.help.124.help").toString());
  886. else
  887. {
  888. auto str = MetaString::createFromTextID("vcmi.lobby.handicap");
  889. str.appendRawString(":\n");
  890. for(auto & res : EGameResID::ALL_RESOURCES())
  891. if(s->handicap.startBonus[res] != 0)
  892. {
  893. str.appendRawString("\n");
  894. str.appendName(res);
  895. str.appendRawString(": ");
  896. str.appendRawString(std::to_string(s->handicap.startBonus[res]));
  897. }
  898. if(s->handicap.percentIncome != 100)
  899. {
  900. str.appendRawString("\n");
  901. str.appendTextID("core.jktext.32");
  902. str.appendRawString(": ");
  903. str.appendRawString(std::to_string(s->handicap.percentIncome) + "%");
  904. }
  905. if(s->handicap.percentGrowth != 100)
  906. {
  907. str.appendRawString("\n");
  908. str.appendTextID("core.genrltxt.194");
  909. str.appendRawString(": ");
  910. str.appendRawString(std::to_string(s->handicap.percentGrowth) + "%");
  911. }
  912. CRClickPopup::createAndPush(str.toString());
  913. }
  914. });
  915. if(SEL->screenType == ESelectionScreen::newGame)
  916. {
  917. 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));
  918. 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));
  919. 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));
  920. 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));
  921. 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));
  922. 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));
  923. }
  924. hideUnavailableButtons();
  925. if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color).canHumanPlay)
  926. {
  927. flag = std::make_shared<CButton>(
  928. Point(-43, 2),
  929. AnimationPath::builtin(flags[s->color.getNum()]),
  930. CGI->generaltexth->zelp[180],
  931. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  932. );
  933. flag->setHoverable(true);
  934. flag->block(CSH->isGuest());
  935. }
  936. else
  937. flag = nullptr;
  938. town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
  939. hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
  940. bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
  941. }
  942. bool OptionsTab::PlayerOptionsEntry::captureThisKey(EShortcut key)
  943. {
  944. return labelPlayerNameEdit && labelPlayerNameEdit->hasFocus() && key == EShortcut::GLOBAL_ACCEPT;
  945. }
  946. void OptionsTab::PlayerOptionsEntry::keyPressed(EShortcut key)
  947. {
  948. if(labelPlayerNameEdit && key == EShortcut::GLOBAL_ACCEPT)
  949. updateName();
  950. }
  951. bool OptionsTab::PlayerOptionsEntry::receiveEvent(const Point & position, int eventType) const
  952. {
  953. return eventType == AEventsReceiver::LCLICK; // capture all left clicks (not only within control)
  954. }
  955. void OptionsTab::PlayerOptionsEntry::clickReleased(const Point & cursorPosition)
  956. {
  957. if(labelPlayerNameEdit && !labelPlayerNameEdit->pos.isInside(cursorPosition))
  958. updateName();
  959. }
  960. void OptionsTab::PlayerOptionsEntry::updateName() {
  961. if(labelPlayerNameEdit->getText() != name)
  962. {
  963. CSH->setPlayerName(s->color, labelPlayerNameEdit->getText());
  964. if(CSH->isMyColor(s->color))
  965. {
  966. Settings set = settings.write["general"]["playerName"];
  967. set->String() = labelPlayerNameEdit->getText();
  968. }
  969. }
  970. labelPlayerNameEdit->removeFocus();
  971. name = labelPlayerNameEdit->getText();
  972. }
  973. void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
  974. {
  975. if(ps.isControlledByAI() || humanPlayers > 1)
  976. CSH->setPlayer(ps.color);
  977. }
  978. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  979. {
  980. if(!buttonTownLeft)
  981. return;
  982. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
  983. if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
  984. {
  985. buttonTownLeft->disable();
  986. buttonTownRight->disable();
  987. }
  988. else
  989. {
  990. buttonTownLeft->enable();
  991. buttonTownRight->enable();
  992. }
  993. if((pi->defaultHero() != HeroTypeID::RANDOM || !s->castle.isValid()) //fixed hero
  994. || foreignPlayer) //or not our player
  995. {
  996. buttonHeroLeft->disable();
  997. buttonHeroRight->disable();
  998. }
  999. else
  1000. {
  1001. buttonHeroLeft->enable();
  1002. buttonHeroRight->enable();
  1003. }
  1004. if(foreignPlayer)
  1005. {
  1006. buttonBonusLeft->disable();
  1007. buttonBonusRight->disable();
  1008. }
  1009. else
  1010. {
  1011. buttonBonusLeft->enable();
  1012. buttonBonusRight->enable();
  1013. }
  1014. }