OptionsTab.cpp 39 KB

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