OptionsTab.cpp 39 KB

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