OptionsTab.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 "../CServerHandler.h"
  14. #include "../GameEngine.h"
  15. #include "../GameInstance.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/entities/ResourceTypeHandler.h"
  42. #include "../../lib/filesystem/Filesystem.h"
  43. #include "../../lib/networkPacks/PacksForLobby.h"
  44. #include "../../lib/texts/CGeneralTextHandler.h"
  45. #include "../../lib/CConfigHandler.h"
  46. #include "../../lib/mapping/CMapInfo.h"
  47. #include "../../lib/mapping/CMapHeader.h"
  48. #include "../../lib/GameLibrary.h"
  49. static JsonPath optionsTabConfigLocation()
  50. {
  51. if(settings["general"]["enableUiEnhancements"].Bool())
  52. return JsonPath::builtin("config/widgets/playerOptionsTab.json");
  53. else
  54. return JsonPath::builtin("config/widgets/advancedOptionsTab.json");
  55. }
  56. OptionsTab::OptionsTab()
  57. : OptionsTabBase(optionsTabConfigLocation())
  58. , humanPlayers(0)
  59. {
  60. }
  61. void OptionsTab::recreate()
  62. {
  63. entries.clear();
  64. humanPlayers = 0;
  65. for (auto tooltipWindow : ENGINE->windows().findWindows<CPlayerOptionTooltipBox>())
  66. tooltipWindow->close();
  67. for (auto heroOverview : ENGINE->windows().findWindows<CHeroOverview>())
  68. heroOverview->close();
  69. for (auto selectionWindow : ENGINE->windows().findWindows<SelectionWindow>())
  70. selectionWindow->reopen();
  71. OBJECT_CONSTRUCTION;
  72. for(auto & pInfo : SEL->getStartInfo()->playerInfos)
  73. {
  74. if(pInfo.second.isControlledByHuman())
  75. humanPlayers++;
  76. entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
  77. }
  78. OptionsTabBase::recreate();
  79. }
  80. size_t OptionsTab::CPlayerSettingsHelper::getImageIndex(bool big)
  81. {
  82. enum EBonusSelection //frames of bonuses file
  83. {
  84. WOOD_ORE = 0, CRYSTAL = 1, GEM = 2,
  85. MERCURY = 3, SULFUR = 5, GOLD = 8,
  86. ARTIFACT = 9, RANDOM = 10,
  87. WOOD = 0, ORE = 0, MITHRIL = 10, // resources unavailable in bonuses file
  88. TOWN_RANDOM = 38, TOWN_NONE = 39, // Special frames in ITPA
  89. HERO_RANDOM = 156, HERO_NONE = 157 // Special frames in PortraitsSmall
  90. };
  91. auto factionIndex = playerSettings.getCastleValidated();
  92. switch(selectionType)
  93. {
  94. case TOWN:
  95. {
  96. if (playerSettings.castle == FactionID::NONE)
  97. return TOWN_NONE;
  98. if (playerSettings.castle == FactionID::RANDOM)
  99. return TOWN_RANDOM;
  100. return (*LIBRARY->townh)[factionIndex]->town->clientInfo.icons[true][false] + (big ? 0 : 2);
  101. }
  102. case HERO:
  103. {
  104. if (playerSettings.hero == HeroTypeID::NONE)
  105. return HERO_NONE;
  106. if (playerSettings.hero == HeroTypeID::RANDOM)
  107. return HERO_RANDOM;
  108. if(playerSettings.heroPortrait != HeroTypeID::NONE)
  109. return playerSettings.heroPortrait;
  110. auto index = playerSettings.getHeroValidated();
  111. return (*LIBRARY->heroh)[index]->imageIndex;
  112. }
  113. case BONUS:
  114. {
  115. switch(playerSettings.bonus)
  116. {
  117. case PlayerStartingBonus::RANDOM:
  118. return RANDOM;
  119. case PlayerStartingBonus::ARTIFACT:
  120. return ARTIFACT;
  121. case PlayerStartingBonus::GOLD:
  122. return GOLD;
  123. case PlayerStartingBonus::RESOURCE:
  124. {
  125. switch((*LIBRARY->townh)[factionIndex]->town->primaryRes.toEnum())
  126. {
  127. case EGameResID::WOOD_AND_ORE:
  128. return WOOD_ORE;
  129. case EGameResID::WOOD:
  130. return WOOD;
  131. case EGameResID::MERCURY:
  132. return MERCURY;
  133. case EGameResID::ORE:
  134. return ORE;
  135. case EGameResID::SULFUR:
  136. return SULFUR;
  137. case EGameResID::CRYSTAL:
  138. return CRYSTAL;
  139. case EGameResID::GEMS:
  140. return GEM;
  141. case EGameResID::GOLD:
  142. return GOLD;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. return 0;
  149. }
  150. AnimationPath OptionsTab::CPlayerSettingsHelper::getImageName(bool big)
  151. {
  152. switch(selectionType)
  153. {
  154. case OptionsTab::TOWN:
  155. return AnimationPath::builtin(big ? "ITPt": "ITPA");
  156. case OptionsTab::HERO:
  157. return AnimationPath::builtin(big ? "PortraitsLarge": "PortraitsSmall");
  158. case OptionsTab::BONUS:
  159. return AnimationPath::builtin("SCNRSTAR");
  160. }
  161. return {};
  162. }
  163. std::string OptionsTab::CPlayerSettingsHelper::getName()
  164. {
  165. switch(selectionType)
  166. {
  167. case TOWN:
  168. {
  169. if (playerSettings.castle == FactionID::NONE)
  170. return LIBRARY->generaltexth->allTexts[523];
  171. if (playerSettings.castle == FactionID::RANDOM)
  172. return LIBRARY->generaltexth->allTexts[522];
  173. auto factionIndex = playerSettings.getCastleValidated();
  174. return (*LIBRARY->townh)[factionIndex]->getNameTranslated();
  175. }
  176. case HERO:
  177. {
  178. if (playerSettings.hero == HeroTypeID::NONE)
  179. return LIBRARY->generaltexth->allTexts[523];
  180. if (playerSettings.hero == HeroTypeID::RANDOM)
  181. return LIBRARY->generaltexth->allTexts[522];
  182. if(!playerSettings.heroNameTextId.empty())
  183. return LIBRARY->generaltexth->translate(playerSettings.heroNameTextId);
  184. auto index = playerSettings.getHeroValidated();
  185. return (*LIBRARY->heroh)[index]->getNameTranslated();
  186. }
  187. case BONUS:
  188. {
  189. if (playerSettings.bonus == PlayerStartingBonus::RANDOM)
  190. return LIBRARY->generaltexth->allTexts[522];
  191. return LIBRARY->generaltexth->arraytxt[214 + static_cast<int>(playerSettings.bonus)];
  192. }
  193. }
  194. return "";
  195. }
  196. std::string OptionsTab::CPlayerSettingsHelper::getTitle()
  197. {
  198. switch(selectionType)
  199. {
  200. case OptionsTab::TOWN:
  201. return playerSettings.castle.isValid() ? LIBRARY->generaltexth->allTexts[80] : LIBRARY->generaltexth->allTexts[103];
  202. case OptionsTab::HERO:
  203. return playerSettings.hero.isValid() ? LIBRARY->generaltexth->allTexts[77] : LIBRARY->generaltexth->allTexts[101];
  204. case OptionsTab::BONUS:
  205. {
  206. switch(playerSettings.bonus)
  207. {
  208. case PlayerStartingBonus::RANDOM:
  209. return LIBRARY->generaltexth->allTexts[86]; //{Random Bonus}
  210. case PlayerStartingBonus::ARTIFACT:
  211. return LIBRARY->generaltexth->allTexts[83]; //{Artifact Bonus}
  212. case PlayerStartingBonus::GOLD:
  213. return LIBRARY->generaltexth->allTexts[84]; //{Gold Bonus}
  214. case PlayerStartingBonus::RESOURCE:
  215. return LIBRARY->generaltexth->allTexts[85]; //{Resource Bonus}
  216. }
  217. }
  218. }
  219. return "";
  220. }
  221. std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
  222. {
  223. auto factionIndex = playerSettings.getCastleValidated();
  224. auto heroIndex = playerSettings.getHeroValidated();
  225. switch(selectionType)
  226. {
  227. case TOWN:
  228. return getName();
  229. case HERO:
  230. {
  231. if(playerSettings.hero.isValid())
  232. return getName() + " - " + (*LIBRARY->heroh)[heroIndex]->heroClass->getNameTranslated();
  233. return getName();
  234. }
  235. case BONUS:
  236. {
  237. switch(playerSettings.bonus)
  238. {
  239. case PlayerStartingBonus::GOLD:
  240. return LIBRARY->generaltexth->allTexts[87]; //500-1000
  241. case PlayerStartingBonus::RESOURCE:
  242. {
  243. switch((*LIBRARY->townh)[factionIndex]->town->primaryRes.toEnum())
  244. {
  245. case EGameResID::MERCURY:
  246. return LIBRARY->generaltexth->allTexts[694];
  247. case EGameResID::SULFUR:
  248. return LIBRARY->generaltexth->allTexts[695];
  249. case EGameResID::CRYSTAL:
  250. return LIBRARY->generaltexth->allTexts[692];
  251. case EGameResID::GEMS:
  252. return LIBRARY->generaltexth->allTexts[693];
  253. case EGameResID::WOOD_AND_ORE:
  254. return LIBRARY->generaltexth->allTexts[89]; //At the start of the game, 5-10 wood and 5-10 ore are added to your Kingdom's resource pool
  255. }
  256. }
  257. }
  258. }
  259. }
  260. return "";
  261. }
  262. std::string OptionsTab::CPlayerSettingsHelper::getDescription()
  263. {
  264. auto factionIndex = playerSettings.getCastleValidated();
  265. switch(selectionType)
  266. {
  267. case TOWN:
  268. return LIBRARY->generaltexth->allTexts[104];
  269. case HERO:
  270. return LIBRARY->generaltexth->allTexts[102];
  271. case BONUS:
  272. {
  273. switch(playerSettings.bonus)
  274. {
  275. case PlayerStartingBonus::RANDOM:
  276. return LIBRARY->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  277. case PlayerStartingBonus::ARTIFACT:
  278. return LIBRARY->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  279. case PlayerStartingBonus::GOLD:
  280. return LIBRARY->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  281. case PlayerStartingBonus::RESOURCE:
  282. {
  283. switch((*LIBRARY->townh)[factionIndex]->town->primaryRes.toEnum())
  284. {
  285. case EGameResID::MERCURY:
  286. return LIBRARY->generaltexth->allTexts[690];
  287. case EGameResID::SULFUR:
  288. return LIBRARY->generaltexth->allTexts[691];
  289. case EGameResID::CRYSTAL:
  290. return LIBRARY->generaltexth->allTexts[688];
  291. case EGameResID::GEMS:
  292. return LIBRARY->generaltexth->allTexts[689];
  293. case EGameResID::WOOD_AND_ORE:
  294. return LIBRARY->generaltexth->allTexts[93]; //At the start of the game, 5-10 wood and 5-10 ore are added to your Kingdom's resource pool
  295. }
  296. }
  297. }
  298. }
  299. }
  300. return "";
  301. }
  302. OptionsTab::CPlayerOptionTooltipBox::CPlayerOptionTooltipBox(CPlayerSettingsHelper & helper)
  303. : CWindowObject(BORDERED | RCLICK_POPUP), CPlayerSettingsHelper(helper)
  304. {
  305. OBJECT_CONSTRUCTION;
  306. switch(selectionType)
  307. {
  308. case TOWN:
  309. genTownWindow();
  310. break;
  311. case HERO:
  312. genHeroWindow();
  313. break;
  314. case BONUS:
  315. genBonusWindow();
  316. break;
  317. }
  318. center();
  319. }
  320. void OptionsTab::CPlayerOptionTooltipBox::genHeader()
  321. {
  322. backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
  323. updateShadow();
  324. labelTitle = std::make_shared<CLabel>(pos.w / 2 + 8, 21, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, getTitle());
  325. labelSubTitle = std::make_shared<CLabel>(pos.w / 2, 88, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getSubtitle());
  326. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex(), 0, pos.w / 2 - 24, 45);
  327. }
  328. void OptionsTab::CPlayerOptionTooltipBox::genTownWindow()
  329. {
  330. auto factionIndex = playerSettings.getCastleValidated();
  331. if (playerSettings.castle == FactionID::RANDOM)
  332. return genBonusWindow();
  333. pos = Rect(0, 0, 228, 290);
  334. genHeader();
  335. labelAssociatedCreatures = std::make_shared<CLabel>(pos.w / 2 + 8, 122, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->allTexts[79]);
  336. std::vector<std::shared_ptr<CComponent>> components;
  337. const CTown * town = (*LIBRARY->townh)[factionIndex]->town.get();
  338. for(auto & elem : town->creatures)
  339. {
  340. if(!elem.empty())
  341. components.push_back(std::make_shared<CComponent>(ComponentType::CREATURE, elem.front(), std::nullopt, CComponent::tiny));
  342. }
  343. boxAssociatedCreatures = std::make_shared<CComponentBox>(components, Rect(10, 140, pos.w - 20, 140), 20, 10, 22, 4);
  344. }
  345. void OptionsTab::CPlayerOptionTooltipBox::genHeroWindow()
  346. {
  347. auto heroIndex = playerSettings.getHeroValidated();
  348. if (playerSettings.hero == HeroTypeID::RANDOM)
  349. return genBonusWindow();
  350. pos = Rect(0, 0, 292, 226);
  351. genHeader();
  352. labelHeroSpeciality = std::make_shared<CLabel>(pos.w / 2 + 4, 117, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->allTexts[78]);
  353. imageSpeciality = std::make_shared<CAnimImage>(AnimationPath::builtin("UN44"), (*LIBRARY->heroh)[heroIndex]->imageIndex, 0, pos.w / 2 - 22, 134);
  354. labelSpecialityName = std::make_shared<CLabel>(pos.w / 2, 188, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, (*LIBRARY->heroh)[heroIndex]->getSpecialtyNameTranslated());
  355. }
  356. void OptionsTab::CPlayerOptionTooltipBox::genBonusWindow()
  357. {
  358. pos = Rect(0, 0, 228, 162);
  359. genHeader();
  360. textBonusDescription = std::make_shared<CTextBox>(getDescription(), Rect(10, 100, pos.w - 20, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  361. }
  362. OptionsTab::SelectionWindow::SelectionWindow(const PlayerColor & color, SelType _type, int sliderPos)
  363. : CWindowObject(BORDERED), color(color)
  364. {
  365. addUsedEvents(LCLICK | SHOW_POPUP);
  366. type = _type;
  367. initialFaction = SEL->getStartInfo()->playerInfos.find(color)->second.castle;
  368. initialHero = SEL->getStartInfo()->playerInfos.find(color)->second.hero;
  369. initialBonus = SEL->getStartInfo()->playerInfos.find(color)->second.bonus;
  370. selectedFaction = initialFaction;
  371. selectedHero = initialHero;
  372. selectedBonus = initialBonus;
  373. allowedFactions = SEL->getPlayerInfo(color).allowedFactions;
  374. allowedHeroes = SEL->getMapInfo()->mapHeader->allowedHeroes;
  375. for(auto & player : SEL->getStartInfo()->playerInfos)
  376. {
  377. if(player.first != color && (int)player.second.hero > HeroTypeID::RANDOM)
  378. unusableHeroes.insert(player.second.hero);
  379. }
  380. for (const auto & disposedHero : SEL->getMapInfo()->mapHeader->disposedHeroes)
  381. {
  382. if (!disposedHero.players.count(color))
  383. allowedHeroes.erase(disposedHero.heroId);
  384. }
  385. allowedBonus.push_back(PlayerStartingBonus::RANDOM);
  386. if(initialHero != HeroTypeID::NONE|| SEL->getPlayerInfo(color).heroesNames.size() > 0)
  387. allowedBonus.push_back(PlayerStartingBonus::ARTIFACT);
  388. allowedBonus.push_back(PlayerStartingBonus::GOLD);
  389. if(initialFaction.isValid())
  390. allowedBonus.push_back(PlayerStartingBonus::RESOURCE);
  391. recreate(sliderPos);
  392. }
  393. std::tuple<int, int> OptionsTab::SelectionWindow::calcLines(FactionID faction)
  394. {
  395. int additionalItems = 1; // random
  396. if(!faction.isValid())
  397. return std::make_tuple(
  398. std::ceil(((double)allowedFactions.size() + additionalItems) / MAX_ELEM_PER_LINES),
  399. (allowedFactions.size() + additionalItems) % MAX_ELEM_PER_LINES
  400. );
  401. int count = 0;
  402. for(auto & elemh : allowedHeroes)
  403. {
  404. const CHero * type = elemh.toHeroType();
  405. if(type->heroClass->faction != faction)
  406. continue;
  407. count++;
  408. }
  409. return std::make_tuple(
  410. std::ceil(((double)count + additionalItems) / MAX_ELEM_PER_LINES),
  411. (count + additionalItems) % MAX_ELEM_PER_LINES
  412. );
  413. }
  414. void OptionsTab::SelectionWindow::apply()
  415. {
  416. if(ENGINE->windows().isTopWindow(this))
  417. {
  418. ENGINE->input().hapticFeedback();
  419. ENGINE->sound().playSound(soundBase::button);
  420. close();
  421. setSelection();
  422. }
  423. }
  424. void OptionsTab::SelectionWindow::setSelection()
  425. {
  426. if(selectedFaction != initialFaction)
  427. GAME->server().setPlayerOption(LobbyChangePlayerOption::TOWN_ID, selectedFaction, color);
  428. if(selectedHero != initialHero)
  429. GAME->server().setPlayerOption(LobbyChangePlayerOption::HERO_ID, selectedHero, color);
  430. if(selectedBonus != initialBonus)
  431. GAME->server().setPlayerOption(LobbyChangePlayerOption::BONUS_ID, static_cast<int>(selectedBonus), color);
  432. }
  433. void OptionsTab::SelectionWindow::reopen()
  434. {
  435. if(type == SelType::HERO && SEL->getStartInfo()->playerInfos.find(color)->second.castle == FactionID::RANDOM)
  436. close();
  437. else{
  438. auto window = std::make_shared<SelectionWindow>(color, type, slider ? slider->getValue() : 0);
  439. close();
  440. if(GAME->server().isMyColor(color) || GAME->server().isHost())
  441. ENGINE->windows().pushWindow(window);
  442. }
  443. }
  444. void OptionsTab::SelectionWindow::recreate(int sliderPos)
  445. {
  446. OBJECT_CONSTRUCTION;
  447. int amountLines = 1;
  448. if(type == SelType::BONUS)
  449. elementsPerLine = allowedBonus.size();
  450. else
  451. {
  452. std::tie(amountLines, elementsPerLine) = calcLines((type > SelType::TOWN) ? selectedFaction : FactionID::RANDOM);
  453. if(amountLines > 1 || elementsPerLine == 0)
  454. elementsPerLine = MAX_ELEM_PER_LINES;
  455. }
  456. int x = (elementsPerLine) * (ICON_BIG_WIDTH-1);
  457. int y = (std::min(amountLines, MAX_LINES)) * (ICON_BIG_HEIGHT-1);
  458. int sliderWidth = ((amountLines > MAX_LINES) ? 16 : 0);
  459. pos = Rect(pos.x, pos.y, x + sliderWidth, y);
  460. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w - sliderWidth, pos.h));
  461. backgroundTexture->setPlayerColor(PlayerColor(1));
  462. updateShadow();
  463. if(type == SelType::TOWN)
  464. genContentFactions();
  465. if(type == SelType::HERO)
  466. genContentHeroes();
  467. if(type == SelType::BONUS)
  468. genContentBonus();
  469. genContentGrid(std::min(amountLines, MAX_LINES));
  470. if(!slider && amountLines > MAX_LINES)
  471. {
  472. slider = std::make_shared<CSlider>(Point(x, 0), y, std::bind(&OptionsTab::SelectionWindow::sliderMove, this, _1), MAX_LINES, amountLines, 0, Orientation::VERTICAL, CSlider::BLUE);
  473. slider->setPanningStep(ICON_BIG_HEIGHT);
  474. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, x + slider->pos.w, y));
  475. slider->scrollTo(sliderPos);
  476. }
  477. center();
  478. }
  479. void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
  480. {
  481. components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  482. components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  483. components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  484. components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text, 56));
  485. components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text, 56));
  486. }
  487. void OptionsTab::SelectionWindow::genContentGrid(int lines)
  488. {
  489. for(int y = 0; y < lines; y++)
  490. {
  491. for(int x = 0; x < elementsPerLine; x++)
  492. {
  493. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  494. }
  495. }
  496. }
  497. void OptionsTab::SelectionWindow::genContentFactions()
  498. {
  499. int i = 1;
  500. // random
  501. PlayerSettings set = PlayerSettings();
  502. set.castle = FactionID::RANDOM;
  503. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  504. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  505. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedFaction == FactionID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  506. if(selectedFaction == FactionID::RANDOM)
  507. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  508. factions.clear();
  509. for(auto & elem : allowedFactions)
  510. {
  511. int x = i % elementsPerLine;
  512. int y = (i / elementsPerLine) - (slider ? slider->getValue() : 0);
  513. PlayerSettings set = PlayerSettings();
  514. set.castle = elem;
  515. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  516. factions.push_back(elem);
  517. i++;
  518. if(y < 0 || y > MAX_LINES - 1)
  519. continue;
  520. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  521. 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)));
  522. 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());
  523. }
  524. }
  525. void OptionsTab::SelectionWindow::genContentHeroes()
  526. {
  527. int i = 1;
  528. // random
  529. PlayerSettings set = PlayerSettings();
  530. set.hero = HeroTypeID::RANDOM;
  531. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  532. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  533. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedHero == HeroTypeID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  534. if(selectedHero == HeroTypeID::RANDOM)
  535. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  536. heroes.clear();
  537. for(auto & elem : allowedHeroes)
  538. {
  539. const CHero * type = elem.toHeroType();
  540. if(type->heroClass->faction != selectedFaction)
  541. continue;
  542. int x = i % elementsPerLine;
  543. int y = (i / elementsPerLine) - (slider ? slider->getValue() : 0);
  544. PlayerSettings set = PlayerSettings();
  545. set.hero = elem;
  546. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  547. heroes.push_back(elem);
  548. i++;
  549. if(y < 0 || y > MAX_LINES - 1)
  550. continue;
  551. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  552. 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());
  553. ImagePath image = ImagePath::builtin("lobby/townBorderBig");
  554. if(selectedHero == elem)
  555. image = ImagePath::builtin("lobby/townBorderBigActivated");
  556. if(unusableHeroes.count(elem))
  557. image = ImagePath::builtin("lobby/townBorderBigGrayedOut");
  558. components.push_back(std::make_shared<CPicture>(image, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  559. }
  560. }
  561. void OptionsTab::SelectionWindow::genContentBonus()
  562. {
  563. PlayerSettings set = SEL->getStartInfo()->playerInfos.find(color)->second;
  564. int i = 0;
  565. for(auto elem : allowedBonus)
  566. {
  567. int x = i;
  568. int y = 0;
  569. set.bonus = elem;
  570. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  571. 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)));
  572. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::WHITE , helper.getName());
  573. if(selectedBonus == elem)
  574. {
  575. 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)));
  576. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::YELLOW , helper.getName());
  577. }
  578. i++;
  579. }
  580. }
  581. int OptionsTab::SelectionWindow::getElement(const Point & cursorPosition)
  582. {
  583. int x = (cursorPosition.x - pos.x) / (ICON_BIG_WIDTH-1);
  584. int y = (cursorPosition.y - pos.y) / (ICON_BIG_HEIGHT-1) + (slider ? slider->getValue() : 0);
  585. return x + y * elementsPerLine;
  586. }
  587. void OptionsTab::SelectionWindow::setElement(int elem, bool doApply)
  588. {
  589. PlayerSettings set = PlayerSettings();
  590. if(type == SelType::TOWN)
  591. {
  592. if(elem > 0)
  593. {
  594. elem--;
  595. if(elem >= factions.size())
  596. return;
  597. set.castle = factions[elem];
  598. }
  599. else
  600. {
  601. set.castle = FactionID::RANDOM;
  602. }
  603. if(set.castle != FactionID::NONE)
  604. {
  605. if(!doApply)
  606. {
  607. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  608. ENGINE->windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  609. }
  610. else
  611. selectedFaction = set.castle;
  612. }
  613. }
  614. if(type == SelType::HERO)
  615. {
  616. if(elem > 0)
  617. {
  618. elem--;
  619. if(elem >= heroes.size())
  620. return;
  621. set.hero = heroes[elem];
  622. }
  623. else
  624. {
  625. set.hero = HeroTypeID::RANDOM;
  626. }
  627. if(doApply && unusableHeroes.count(heroes[elem]))
  628. return;
  629. if(set.hero != HeroTypeID::NONE)
  630. {
  631. if(!doApply)
  632. {
  633. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  634. if(settings["general"]["enableUiEnhancements"].Bool() && helper.playerSettings.hero.isValid() && helper.playerSettings.heroNameTextId.empty())
  635. ENGINE->windows().createAndPushWindow<CHeroOverview>(helper.playerSettings.hero);
  636. else
  637. ENGINE->windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  638. }
  639. else
  640. selectedHero = set.hero;
  641. }
  642. }
  643. if(type == SelType::BONUS)
  644. {
  645. if(elem >= 4)
  646. return;
  647. set.bonus = static_cast<PlayerStartingBonus>(allowedBonus[elem]);
  648. if(!doApply)
  649. {
  650. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  651. ENGINE->windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  652. }
  653. else
  654. selectedBonus = set.bonus;
  655. }
  656. if(doApply)
  657. apply();
  658. }
  659. void OptionsTab::SelectionWindow::sliderMove(int slidPos)
  660. {
  661. if(!slider)
  662. return; // ignore spurious call when slider is being created
  663. recreate();
  664. redraw();
  665. }
  666. void OptionsTab::SelectionWindow::notFocusedClick()
  667. {
  668. close();
  669. }
  670. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
  671. {
  672. if(slider && slider->pos.isInside(cursorPosition))
  673. return;
  674. int elem = getElement(cursorPosition);
  675. setElement(elem, true);
  676. }
  677. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  678. {
  679. if(!pos.isInside(cursorPosition) || (slider && slider->pos.isInside(cursorPosition)))
  680. return;
  681. int elem = getElement(cursorPosition);
  682. setElement(elem, false);
  683. }
  684. OptionsTab::HandicapWindow::HandicapWindow()
  685. : CWindowObject(BORDERED)
  686. {
  687. OBJECT_CONSTRUCTION;
  688. addUsedEvents(LCLICK);
  689. pos = Rect(0, 0, 660, 100 + SEL->getStartInfo()->playerInfos.size() * 30);
  690. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(pos);
  691. backgroundTexture->setPlayerColor(PlayerColor(1));
  692. labels.push_back(std::make_shared<CLabel>(pos.w / 2 + 8, 15, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->translate("vcmi.lobby.handicap")));
  693. enum Columns : int32_t
  694. {
  695. INCOME = 1000,
  696. GROWTH = 2000,
  697. };
  698. // TODO: configurable resources
  699. auto columns = std::vector<int>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS, Columns::INCOME, Columns::GROWTH};
  700. int i = 0;
  701. for(auto & pInfo : SEL->getStartInfo()->playerInfos)
  702. {
  703. PlayerColor player = pInfo.first;
  704. anim.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), player.getNum(), 0, 7, 57 + i * 30));
  705. for(int j = 0; j < columns.size(); j++)
  706. {
  707. bool isIncome = int(columns[j]) == Columns::INCOME;
  708. bool isGrowth = int(columns[j]) == Columns::GROWTH;
  709. EGameResID resource = columns[j];
  710. const PlayerSettings &ps = SEL->getStartInfo()->getIthPlayersSettings(player);
  711. int xPos = 30 + j * 70;
  712. xPos += j > 0 ? 10 : 0; // Gold field is larger
  713. if(i == 0)
  714. {
  715. if(isIncome)
  716. labels.push_back(std::make_shared<CLabel>(xPos, 38, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, LIBRARY->generaltexth->translate("core.jktext.32")));
  717. else if(isGrowth)
  718. labels.push_back(std::make_shared<CLabel>(xPos, 38, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, LIBRARY->generaltexth->translate("core.genrltxt.194")));
  719. else
  720. anim.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(resource).getNum(), 0, 15 + xPos + (j == 0 ? 10 : 0), 35));
  721. }
  722. auto area = Rect(xPos, 60 + i * 30, j == 0 ? 60 : 50, 16);
  723. textinputbackgrounds.push_back(std::make_shared<TransparentFilledRectangle>(area.resize(3), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64)));
  724. textinputs[player][resource] = std::make_shared<CTextInput>(area, FONT_SMALL, ETextAlignment::CENTERLEFT, true);
  725. textinputs[player][resource]->setText(std::to_string(isIncome ? ps.handicap.percentIncome : (isGrowth ? ps.handicap.percentGrowth : ps.handicap.startBonus[resource])));
  726. textinputs[player][resource]->setCallback([this, player, resource, isIncome, isGrowth](const std::string & s){
  727. // text input processing: add/remove sign when pressing "-"; remove non digits; cut length; fill empty field with 0
  728. std::string tmp = s;
  729. bool negative = std::count_if( s.begin(), s.end(), []( char c ){ return c == '-'; }) == 1 && !isIncome && !isGrowth;
  730. tmp.erase(std::remove_if(tmp.begin(), tmp.end(), [](char c) { return !isdigit(c); }), tmp.end());
  731. int maxLength = isIncome || isGrowth ? 3 : (resource == EGameResID::GOLD ? 6 : 5);
  732. tmp = tmp.substr(0, maxLength);
  733. textinputs[player][resource]->setText(tmp.length() == 0 ? "0" : (negative ? "-" : "") + std::to_string(stoi(tmp)));
  734. });
  735. textinputs[player][resource]->setPopupCallback([isIncome, isGrowth](){
  736. // Help for the textinputs
  737. if(isIncome)
  738. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("vcmi.lobby.handicap.income"));
  739. else if(isGrowth)
  740. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("vcmi.lobby.handicap.growth"));
  741. else
  742. CRClickPopup::createAndPush(LIBRARY->generaltexth->translate("vcmi.lobby.handicap.resource"));
  743. });
  744. if(isIncome || isGrowth)
  745. labels.push_back(std::make_shared<CLabel>(area.topRight().x, area.center().y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "%"));
  746. }
  747. i++;
  748. }
  749. buttons.push_back(std::make_shared<CButton>(Point(pos.w / 2 - 32, 60 + SEL->getStartInfo()->playerInfos.size() * 30), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){
  750. for (const auto& player : textinputs)
  751. {
  752. TResources resources = TResources();
  753. int income = 100;
  754. int growth = 100;
  755. for (const auto& resource : player.second)
  756. {
  757. bool isIncome = int(resource.first) == Columns::INCOME;
  758. bool isGrowth = int(resource.first) == Columns::GROWTH;
  759. if(isIncome)
  760. income = std::stoi(resource.second->getText());
  761. else if(isGrowth)
  762. growth = std::stoi(resource.second->getText());
  763. else
  764. resources[resource.first] = std::stoi(resource.second->getText());
  765. }
  766. GAME->server().setPlayerHandicap(player.first, Handicap{resources, income, growth});
  767. }
  768. close();
  769. }, EShortcut::GLOBAL_RETURN));
  770. updateShadow();
  771. center();
  772. }
  773. void OptionsTab::HandicapWindow::notFocusedClick()
  774. {
  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. ENGINE->windows().createAndPushWindow<CHeroOverview>(playerSettings.hero);
  801. else
  802. ENGINE->windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  803. }
  804. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  805. {
  806. if (!SEL)
  807. return;
  808. if(SEL->screenType != ESelectionScreen::newGame)
  809. return;
  810. PlayerInfo pi = SEL->getPlayerInfo(playerSettings.color);
  811. const bool foreignPlayer = GAME->server().isGuest() && !GAME->server().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. ENGINE->input().hapticFeedback();
  819. ENGINE->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. GAME->server().setPlayerOption(LobbyChangePlayerOption::TOWN, distance, playerSettings.color);
  831. break;
  832. case HERO:
  833. GAME->server().setPlayerOption(LobbyChangePlayerOption::HERO, distance, playerSettings.color);
  834. break;
  835. case BONUS:
  836. GAME->server().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(GAME->server().mi && GAME->server().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() || GAME->server().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, LIBRARY->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() ? LIBRARY->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(!GAME->server().isHost())
  891. return;
  892. ENGINE->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 : LIBRARY->resourceTypeHandler->getAllObjects())
  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"), LIBRARY->generaltexth->zelp[132], std::bind(&IServerAPI::setPlayerOption, &GAME->server(), LobbyChangePlayerOption::TOWN, -1, s->color));
  928. buttonTownRight = std::make_shared<CButton>(Point(168, 5), AnimationPath::builtin("ADOPRTA.DEF"), LIBRARY->generaltexth->zelp[133], std::bind(&IServerAPI::setPlayerOption, &GAME->server(), LobbyChangePlayerOption::TOWN, +1, s->color));
  929. buttonHeroLeft = std::make_shared<CButton>(Point(183, 5), AnimationPath::builtin("ADOPLFA.DEF"), LIBRARY->generaltexth->zelp[148], std::bind(&IServerAPI::setPlayerOption, &GAME->server(), LobbyChangePlayerOption::HERO, -1, s->color));
  930. buttonHeroRight = std::make_shared<CButton>(Point(244, 5), AnimationPath::builtin("ADOPRTA.DEF"), LIBRARY->generaltexth->zelp[149], std::bind(&IServerAPI::setPlayerOption, &GAME->server(), LobbyChangePlayerOption::HERO, +1, s->color));
  931. buttonBonusLeft = std::make_shared<CButton>(Point(259, 5), AnimationPath::builtin("ADOPLFA.DEF"), LIBRARY->generaltexth->zelp[164], std::bind(&IServerAPI::setPlayerOption, &GAME->server(), LobbyChangePlayerOption::BONUS, -1, s->color));
  932. buttonBonusRight = std::make_shared<CButton>(Point(320, 5), AnimationPath::builtin("ADOPRTA.DEF"), LIBRARY->generaltexth->zelp[165], std::bind(&IServerAPI::setPlayerOption, &GAME->server(), 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. LIBRARY->generaltexth->zelp[180],
  941. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  942. );
  943. flag->setHoverable(true);
  944. flag->block(GAME->server().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. GAME->server().setPlayerName(s->color, labelPlayerNameEdit->getText());
  974. if(GAME->server().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. GAME->server().setPlayer(ps.color);
  987. }
  988. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  989. {
  990. if(!buttonTownLeft)
  991. return;
  992. const bool foreignPlayer = GAME->server().isGuest() && !GAME->server().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. }