OptionsTab.cpp 39 KB

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