OptionsTab.cpp 39 KB

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