OptionsTab.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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 "../widgets/CComponent.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../widgets/MiscWidgets.h"
  23. #include "../widgets/ObjectLists.h"
  24. #include "../widgets/Slider.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../windows/GUIClasses.h"
  27. #include "../windows/InfoWindows.h"
  28. #include "../../lib/NetPacksLobby.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/CArtHandler.h"
  31. #include "../../lib/CTownHandler.h"
  32. #include "../../lib/CHeroHandler.h"
  33. #include "../../lib/mapping/CMapInfo.h"
  34. #include "../../lib/mapping/CMapHeader.h"
  35. OptionsTab::OptionsTab() : humanPlayers(0)
  36. {
  37. recActions = 0;
  38. OBJ_CONSTRUCTION;
  39. background = std::make_shared<CPicture>("ADVOPTBK", 0, 6);
  40. pos = background->pos;
  41. labelTitle = std::make_shared<CLabel>(222, 30, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[515]);
  42. labelSubTitle = std::make_shared<CMultiLineLabel>(Rect(60, 44, 320, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[516]);
  43. labelPlayerNameAndHandicap = std::make_shared<CMultiLineLabel>(Rect(58, 86, 100, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[517]);
  44. labelStartingTown = std::make_shared<CMultiLineLabel>(Rect(163, 86, 70, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[518]);
  45. labelStartingHero = std::make_shared<CMultiLineLabel>(Rect(239, 86, 70, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[519]);
  46. labelStartingBonus = std::make_shared<CMultiLineLabel>(Rect(315, 86, 70, (int)graphics->fonts[EFonts::FONT_SMALL]->getLineHeight()*2), EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[520]);
  47. if(SEL->screenType == ESelectionScreen::newGame || SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::scenarioInfo)
  48. {
  49. sliderTurnDuration = std::make_shared<CSlider>(Point(55, 551), 194, std::bind(&IServerAPI::setTurnLength, CSH, _1), 1, (int)GameConstants::POSSIBLE_TURNTIME.size(), (int)GameConstants::POSSIBLE_TURNTIME.size(), Orientation::HORIZONTAL, CSlider::BLUE);
  50. sliderTurnDuration->setScrollBounds(Rect(-3, -25, 337, 43));
  51. sliderTurnDuration->setPanningStep(20);
  52. labelPlayerTurnDuration = std::make_shared<CLabel>(222, 538, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[521]);
  53. labelTurnDurationValue = std::make_shared<CLabel>(319, 559, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  54. }
  55. }
  56. void OptionsTab::recreate()
  57. {
  58. entries.clear();
  59. humanPlayers = 0;
  60. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  61. for(auto & pInfo : SEL->getStartInfo()->playerInfos)
  62. {
  63. if(pInfo.second.isControlledByHuman())
  64. humanPlayers++;
  65. entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
  66. }
  67. if(sliderTurnDuration)
  68. {
  69. sliderTurnDuration->scrollTo(vstd::find_pos(GameConstants::POSSIBLE_TURNTIME, SEL->getStartInfo()->turnTime));
  70. labelTurnDurationValue->setText(CGI->generaltexth->turnDurations[sliderTurnDuration->getValue()]);
  71. }
  72. }
  73. size_t OptionsTab::CPlayerSettingsHelper::getImageIndex(bool big)
  74. {
  75. enum EBonusSelection //frames of bonuses file
  76. {
  77. WOOD_ORE = 0, CRYSTAL = 1, GEM = 2,
  78. MERCURY = 3, SULFUR = 5, GOLD = 8,
  79. ARTIFACT = 9, RANDOM = 10,
  80. WOOD = 0, ORE = 0, MITHRIL = 10, // resources unavailable in bonuses file
  81. TOWN_RANDOM = 38, TOWN_NONE = 39, // Special frames in ITPA
  82. HERO_RANDOM = 163, HERO_NONE = 164 // Special frames in PortraitsSmall
  83. };
  84. auto factionIndex = settings.castle >= CGI->townh->size() ? 0 : settings.castle;
  85. switch(type)
  86. {
  87. case TOWN:
  88. switch(settings.castle)
  89. {
  90. case PlayerSettings::NONE:
  91. return TOWN_NONE;
  92. case PlayerSettings::RANDOM:
  93. return TOWN_RANDOM;
  94. default:
  95. return (*CGI->townh)[factionIndex]->town->clientInfo.icons[true][false] + (big ? 0 : 2);
  96. }
  97. case HERO:
  98. switch(settings.hero)
  99. {
  100. case PlayerSettings::NONE:
  101. return HERO_NONE;
  102. case PlayerSettings::RANDOM:
  103. return HERO_RANDOM;
  104. default:
  105. {
  106. if(settings.heroPortrait >= 0)
  107. return settings.heroPortrait;
  108. auto index = settings.hero >= CGI->heroh->size() ? 0 : settings.hero;
  109. return (*CGI->heroh)[index]->imageIndex;
  110. }
  111. }
  112. case BONUS:
  113. {
  114. switch(settings.bonus)
  115. {
  116. case PlayerSettings::RANDOM:
  117. return RANDOM;
  118. case PlayerSettings::ARTIFACT:
  119. return ARTIFACT;
  120. case PlayerSettings::GOLD:
  121. return GOLD;
  122. case PlayerSettings::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. std::string OptionsTab::CPlayerSettingsHelper::getImageName(bool big)
  152. {
  153. switch(type)
  154. {
  155. case OptionsTab::TOWN:
  156. return big ? "ITPt": "ITPA";
  157. case OptionsTab::HERO:
  158. return big ? "PortraitsLarge": "PortraitsSmall";
  159. case OptionsTab::BONUS:
  160. return "SCNRSTAR";
  161. }
  162. return "";
  163. }
  164. std::string OptionsTab::CPlayerSettingsHelper::getName()
  165. {
  166. switch(type)
  167. {
  168. case TOWN:
  169. {
  170. switch(settings.castle)
  171. {
  172. case PlayerSettings::NONE:
  173. return CGI->generaltexth->allTexts[523];
  174. case PlayerSettings::RANDOM:
  175. return CGI->generaltexth->allTexts[522];
  176. default:
  177. {
  178. auto factionIndex = settings.castle >= CGI->townh->size() ? 0 : settings.castle;
  179. return (*CGI->townh)[factionIndex]->getNameTranslated();
  180. }
  181. }
  182. }
  183. case HERO:
  184. {
  185. switch(settings.hero)
  186. {
  187. case PlayerSettings::NONE:
  188. return CGI->generaltexth->allTexts[523];
  189. case PlayerSettings::RANDOM:
  190. return CGI->generaltexth->allTexts[522];
  191. default:
  192. {
  193. if(!settings.heroName.empty())
  194. return settings.heroName;
  195. auto index = settings.hero >= CGI->heroh->size() ? 0 : settings.hero;
  196. return (*CGI->heroh)[index]->getNameTranslated();
  197. }
  198. }
  199. }
  200. case BONUS:
  201. {
  202. switch(settings.bonus)
  203. {
  204. case PlayerSettings::RANDOM:
  205. return CGI->generaltexth->allTexts[522];
  206. default:
  207. return CGI->generaltexth->arraytxt[214 + settings.bonus];
  208. }
  209. }
  210. }
  211. return "";
  212. }
  213. std::string OptionsTab::CPlayerSettingsHelper::getTitle()
  214. {
  215. switch(type)
  216. {
  217. case OptionsTab::TOWN:
  218. return (settings.castle < 0) ? CGI->generaltexth->allTexts[103] : CGI->generaltexth->allTexts[80];
  219. case OptionsTab::HERO:
  220. return (settings.hero < 0) ? CGI->generaltexth->allTexts[101] : CGI->generaltexth->allTexts[77];
  221. case OptionsTab::BONUS:
  222. {
  223. switch(settings.bonus)
  224. {
  225. case PlayerSettings::RANDOM:
  226. return CGI->generaltexth->allTexts[86]; //{Random Bonus}
  227. case PlayerSettings::ARTIFACT:
  228. return CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  229. case PlayerSettings::GOLD:
  230. return CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  231. case PlayerSettings::RESOURCE:
  232. return CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  233. }
  234. }
  235. }
  236. return "";
  237. }
  238. std::string OptionsTab::CPlayerSettingsHelper::getSubtitle()
  239. {
  240. auto factionIndex = settings.castle >= CGI->townh->size() ? 0 : settings.castle;
  241. auto heroIndex = settings.hero >= CGI->heroh->size() ? 0 : settings.hero;
  242. switch(type)
  243. {
  244. case TOWN:
  245. return getName();
  246. case HERO:
  247. {
  248. if(settings.hero >= 0)
  249. return getName() + " - " + (*CGI->heroh)[heroIndex]->heroClass->getNameTranslated();
  250. return getName();
  251. }
  252. case BONUS:
  253. {
  254. switch(settings.bonus)
  255. {
  256. case PlayerSettings::GOLD:
  257. return CGI->generaltexth->allTexts[87]; //500-1000
  258. case PlayerSettings::RESOURCE:
  259. {
  260. switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
  261. {
  262. case EGameResID::MERCURY:
  263. return CGI->generaltexth->allTexts[694];
  264. case EGameResID::SULFUR:
  265. return CGI->generaltexth->allTexts[695];
  266. case EGameResID::CRYSTAL:
  267. return CGI->generaltexth->allTexts[692];
  268. case EGameResID::GEMS:
  269. return CGI->generaltexth->allTexts[693];
  270. case EGameResID::WOOD_AND_ORE:
  271. 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
  272. }
  273. }
  274. }
  275. }
  276. }
  277. return "";
  278. }
  279. std::string OptionsTab::CPlayerSettingsHelper::getDescription()
  280. {
  281. auto factionIndex = settings.castle >= CGI->townh->size() ? 0 : settings.castle;
  282. switch(type)
  283. {
  284. case TOWN:
  285. return CGI->generaltexth->allTexts[104];
  286. case HERO:
  287. return CGI->generaltexth->allTexts[102];
  288. case BONUS:
  289. {
  290. switch(settings.bonus)
  291. {
  292. case PlayerSettings::RANDOM:
  293. return CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  294. case PlayerSettings::ARTIFACT:
  295. return CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  296. case PlayerSettings::GOLD:
  297. return CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  298. case PlayerSettings::RESOURCE:
  299. {
  300. switch((*CGI->townh)[factionIndex]->town->primaryRes.toEnum())
  301. {
  302. case EGameResID::MERCURY:
  303. return CGI->generaltexth->allTexts[690];
  304. case EGameResID::SULFUR:
  305. return CGI->generaltexth->allTexts[691];
  306. case EGameResID::CRYSTAL:
  307. return CGI->generaltexth->allTexts[688];
  308. case EGameResID::GEMS:
  309. return CGI->generaltexth->allTexts[689];
  310. case EGameResID::WOOD_AND_ORE:
  311. 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
  312. }
  313. }
  314. }
  315. }
  316. }
  317. return "";
  318. }
  319. OptionsTab::CPlayerOptionTooltipBox::CPlayerOptionTooltipBox(CPlayerSettingsHelper & helper)
  320. : CWindowObject(BORDERED | RCLICK_POPUP), CPlayerSettingsHelper(helper)
  321. {
  322. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  323. int value = PlayerSettings::NONE;
  324. switch(CPlayerSettingsHelper::type)
  325. {
  326. break;
  327. case TOWN:
  328. value = settings.castle;
  329. break;
  330. case HERO:
  331. value = settings.hero;
  332. break;
  333. case BONUS:
  334. value = settings.bonus;
  335. }
  336. if(value == PlayerSettings::RANDOM)
  337. genBonusWindow();
  338. else if(CPlayerSettingsHelper::type == BONUS)
  339. genBonusWindow();
  340. else if(CPlayerSettingsHelper::type == HERO)
  341. genHeroWindow();
  342. else if(CPlayerSettingsHelper::type == TOWN)
  343. genTownWindow();
  344. center();
  345. }
  346. void OptionsTab::CPlayerOptionTooltipBox::genHeader()
  347. {
  348. backgroundTexture = std::make_shared<CFilledTexture>("DIBOXBCK", pos);
  349. updateShadow();
  350. labelTitle = std::make_shared<CLabel>(pos.w / 2 + 8, 21, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, getTitle());
  351. labelSubTitle = std::make_shared<CLabel>(pos.w / 2, 88, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, getSubtitle());
  352. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex(), 0, pos.w / 2 - 24, 45);
  353. }
  354. void OptionsTab::CPlayerOptionTooltipBox::genTownWindow()
  355. {
  356. pos = Rect(0, 0, 228, 290);
  357. genHeader();
  358. labelAssociatedCreatures = std::make_shared<CLabel>(pos.w / 2 + 8, 122, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[79]);
  359. auto factionIndex = settings.castle >= CGI->townh->size() ? 0 : settings.castle;
  360. std::vector<std::shared_ptr<CComponent>> components;
  361. const CTown * town = (*CGI->townh)[factionIndex]->town;
  362. for(auto & elem : town->creatures)
  363. {
  364. if(!elem.empty())
  365. components.push_back(std::make_shared<CComponent>(CComponent::creature, elem.front(), 0, CComponent::tiny));
  366. }
  367. boxAssociatedCreatures = std::make_shared<CComponentBox>(components, Rect(10, 140, pos.w - 20, 140));
  368. }
  369. void OptionsTab::CPlayerOptionTooltipBox::genHeroWindow()
  370. {
  371. pos = Rect(0, 0, 292, 226);
  372. genHeader();
  373. labelHeroSpeciality = std::make_shared<CLabel>(pos.w / 2 + 4, 117, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[78]);
  374. auto heroIndex = settings.hero >= CGI->heroh->size() ? 0 : settings.hero;
  375. imageSpeciality = std::make_shared<CAnimImage>("UN44", (*CGI->heroh)[heroIndex]->imageIndex, 0, pos.w / 2 - 22, 134);
  376. labelSpecialityName = std::make_shared<CLabel>(pos.w / 2, 188, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, (*CGI->heroh)[heroIndex]->getSpecialtyNameTranslated());
  377. }
  378. void OptionsTab::CPlayerOptionTooltipBox::genBonusWindow()
  379. {
  380. pos = Rect(0, 0, 228, 162);
  381. genHeader();
  382. textBonusDescription = std::make_shared<CTextBox>(getDescription(), Rect(10, 100, pos.w - 20, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  383. }
  384. OptionsTab::SelectionWindow::SelectionWindow(PlayerColor _color)
  385. : CWindowObject(BORDERED)
  386. {
  387. addUsedEvents(LCLICK | SHOW_POPUP);
  388. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  389. color = _color;
  390. pos = Rect(0, 0, (ELEMENTS_PER_LINE * 2 + 5) * 58, 700);
  391. backgroundTexture = std::make_shared<CFilledTexture>("DIBOXBCK", pos);
  392. updateShadow();
  393. initialFraction = SEL->getStartInfo()->playerInfos.find(color)->second.castle;
  394. initialHero = SEL->getStartInfo()->playerInfos.find(color)->second.hero;
  395. initialBonus = SEL->getStartInfo()->playerInfos.find(color)->second.bonus;
  396. selectedFraction = initialFraction;
  397. selectedHero = selectedHero;
  398. selectedBonus = selectedBonus;
  399. allowedFactions = SEL->getPlayerInfo(color.getNum()).allowedFactions;
  400. redraw();
  401. center();
  402. }
  403. void OptionsTab::SelectionWindow::apply()
  404. {
  405. if(GH.windows().isTopWindow(this))
  406. {
  407. close();
  408. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, 1, color);
  409. }
  410. }
  411. void OptionsTab::SelectionWindow::redraw()
  412. {
  413. components.clear();
  414. genContentTitle();
  415. genContentCastles();
  416. genContentHeroes();
  417. genContentBonus();
  418. }
  419. void OptionsTab::SelectionWindow::genContentTitle()
  420. {
  421. components.push_back(std::make_shared<CLabel>((ELEMENTS_PER_LINE - 1) * 58, 40, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, "Town"));
  422. components.push_back(std::make_shared<CLabel>((ELEMENTS_PER_LINE * 2) * 58, 40, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, "Hero"));
  423. components.push_back(std::make_shared<CLabel>((ELEMENTS_PER_LINE * 2 + 3) * 58 + 29, 40, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, "Bonus"));
  424. }
  425. void OptionsTab::SelectionWindow::genContentCastles()
  426. {
  427. factions.clear();
  428. PlayerSettings set = PlayerSettings();
  429. set.castle = set.RANDOM;
  430. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  431. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, (ELEMENTS_PER_LINE / 2) * 58 + 34, 32 / 2 + 64));
  432. int i = 0;
  433. for(auto & elem : allowedFactions)
  434. {
  435. int x = i % ELEMENTS_PER_LINE + 1;
  436. int y = i / ELEMENTS_PER_LINE + 2;
  437. PlayerSettings set = PlayerSettings();
  438. set.castle = elem;
  439. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  440. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * 58, y * 64));
  441. factions.push_back(elem);
  442. i++;
  443. }
  444. }
  445. void OptionsTab::SelectionWindow::genContentHeroes()
  446. {
  447. heroes.clear();
  448. PlayerSettings set = PlayerSettings();
  449. set.castle = set.RANDOM;
  450. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  451. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, (ELEMENTS_PER_LINE / 2) * 58 + (ELEMENTS_PER_LINE + 1) * 58 + 34, 32 / 2 + 64));
  452. std::vector<bool> allowedHeroesFlag = SEL->getMapInfo()->mapHeader->allowedHeroes;
  453. std::set<HeroTypeID> allowedHeroes;
  454. for(int i = 0; i < allowedHeroesFlag.size(); i++)
  455. if(allowedHeroesFlag[i])
  456. allowedHeroes.insert(HeroTypeID(i));
  457. int i = 0;
  458. for(auto & elem : allowedHeroes)
  459. {
  460. CHero * type = VLC->heroh->objects[elem];
  461. if(type->heroClass->faction == selectedFraction)
  462. {
  463. int x = (i % ELEMENTS_PER_LINE) + ELEMENTS_PER_LINE + 2;
  464. int y = i / ELEMENTS_PER_LINE + 2;
  465. PlayerSettings set = PlayerSettings();
  466. set.hero = elem;
  467. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  468. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * 58, y * 64));
  469. heroes.push_back(elem);
  470. i++;
  471. }
  472. }
  473. }
  474. void OptionsTab::SelectionWindow::genContentBonus()
  475. {
  476. PlayerSettings set = PlayerSettings();
  477. int i = 0;
  478. for(auto elem : {set.RANDOM, set.ARTIFACT, set.GOLD, set.RESOURCE})
  479. {
  480. int x = ELEMENTS_PER_LINE * 2 + 3;
  481. int y = i + 1;
  482. set.bonus = elem;
  483. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  484. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, x * 58, y * 64 + 32 / 2));
  485. i++;
  486. }
  487. }
  488. FactionID OptionsTab::SelectionWindow::getElementCastle(const Point & cursorPosition)
  489. {
  490. Point loc = getElement(cursorPosition, 0);
  491. FactionID faction;
  492. faction = PlayerSettings().NONE;
  493. if ((loc.x == ELEMENTS_PER_LINE / 2 || loc.x == ELEMENTS_PER_LINE / 2 - 1) && loc.y == 0)
  494. faction = PlayerSettings().RANDOM;
  495. else if(loc.y > 0 && loc.x < ELEMENTS_PER_LINE)
  496. {
  497. int index = loc.x + (loc.y - 1) * ELEMENTS_PER_LINE;
  498. if (index < factions.size())
  499. faction = factions[loc.x + (loc.y - 1) * ELEMENTS_PER_LINE];
  500. }
  501. return faction;
  502. }
  503. HeroTypeID OptionsTab::SelectionWindow::getElementHero(const Point & cursorPosition)
  504. {
  505. Point loc = getElement(cursorPosition, 1);
  506. HeroTypeID hero;
  507. hero = PlayerSettings().NONE;
  508. if(loc.x < 0)
  509. return hero;
  510. if ((loc.x == ELEMENTS_PER_LINE / 2 || loc.x == ELEMENTS_PER_LINE / 2 - 1) && loc.y == 0)
  511. hero = PlayerSettings().RANDOM;
  512. else if(loc.y > 0 && loc.x < ELEMENTS_PER_LINE)
  513. {
  514. int index = loc.x + (loc.y - 1) * ELEMENTS_PER_LINE;
  515. if (index < heroes.size())
  516. hero = heroes[loc.x + (loc.y - 1) * ELEMENTS_PER_LINE];
  517. }
  518. return hero;
  519. }
  520. int OptionsTab::SelectionWindow::getElementBonus(const Point & cursorPosition)
  521. {
  522. Point loc = getElement(cursorPosition, 2);
  523. if(loc.x != 0 || loc.y < 0 || loc.y > 3)
  524. return -2;
  525. return loc.y - 1;
  526. }
  527. Point OptionsTab::SelectionWindow::getElement(const Point & cursorPosition, int area)
  528. {
  529. int x = (cursorPosition.x - pos.x - area * (ELEMENTS_PER_LINE + 1) * 58) / 58;
  530. int y = (cursorPosition.y - pos.y) / 64;
  531. return Point(x - 1, y - 1);
  532. }
  533. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition) {
  534. FactionID faction = getElementCastle(cursorPosition);
  535. HeroTypeID hero = getElementHero(cursorPosition);
  536. int bonus = getElementBonus(cursorPosition);
  537. PlayerSettings set = PlayerSettings();
  538. set.castle = faction;
  539. set.hero = hero;
  540. set.bonus = static_cast<PlayerSettings::Ebonus>(bonus);
  541. if(set.castle != -2)
  542. {
  543. selectedFraction = set.castle;
  544. redraw();
  545. }
  546. else if(set.hero != -2)
  547. {
  548. selectedHero = set.hero;
  549. apply();
  550. }
  551. else if(set.bonus != -2)
  552. {
  553. selectedBonus = set.bonus;
  554. apply();
  555. }
  556. }
  557. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  558. {
  559. FactionID faction = getElementCastle(cursorPosition);
  560. HeroTypeID hero = getElementHero(cursorPosition);
  561. int bonus = getElementBonus(cursorPosition);
  562. PlayerSettings set = PlayerSettings();
  563. set.castle = faction;
  564. set.hero = hero;
  565. set.bonus = static_cast<PlayerSettings::Ebonus>(bonus);
  566. if(set.castle != -2)
  567. {
  568. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  569. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  570. }
  571. else if(set.hero != -2)
  572. {
  573. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  574. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  575. }
  576. else if(set.bonus != -2)
  577. {
  578. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  579. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  580. }
  581. }
  582. OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & settings, SelType type)
  583. : Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
  584. , CPlayerSettingsHelper(settings, type)
  585. {
  586. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  587. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
  588. subtitle = std::make_shared<CLabel>(23, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName());
  589. pos = image->pos;
  590. setPanningStep(pos.w);
  591. }
  592. void OptionsTab::SelectedBox::update()
  593. {
  594. image->setFrame(getImageIndex());
  595. subtitle->setText(getName());
  596. }
  597. void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
  598. {
  599. // cases when we do not need to display a message
  600. if(settings.castle == -2 && CPlayerSettingsHelper::type == TOWN)
  601. return;
  602. if(settings.hero == -2 && !SEL->getPlayerInfo(settings.color.getNum()).hasCustomMainHero() && CPlayerSettingsHelper::type == HERO)
  603. return;
  604. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  605. }
  606. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  607. {
  608. GH.windows().createAndPushWindow<SelectionWindow>(settings.color);
  609. }
  610. void OptionsTab::SelectedBox::scrollBy(int distance)
  611. {
  612. // FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
  613. // because of that, panning event gets interrupted (due to destruction of element)
  614. // so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
  615. distance = std::clamp(distance, -1, 1);
  616. switch(CPlayerSettingsHelper::type)
  617. {
  618. case TOWN:
  619. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, settings.color);
  620. break;
  621. case HERO:
  622. CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, settings.color);
  623. break;
  624. case BONUS:
  625. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, settings.color);
  626. break;
  627. }
  628. setScrollingEnabled(false);
  629. }
  630. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
  631. : pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color.getNum())))
  632. , s(std::make_unique<PlayerSettings>(S))
  633. , parentTab(parent)
  634. {
  635. OBJ_CONSTRUCTION;
  636. defActions |= SHARE_POS;
  637. int serial = 0;
  638. for(int g = 0; g < s->color.getNum(); ++g)
  639. {
  640. auto itred = SEL->getPlayerInfo(g);
  641. if(itred.canComputerPlay || itred.canHumanPlay)
  642. serial++;
  643. }
  644. pos.x += 54;
  645. pos.y += 122 + serial * 50;
  646. assert(CSH->mi && CSH->mi->mapHeader);
  647. const PlayerInfo & p = SEL->getPlayerInfo(s->color.getNum());
  648. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  649. if(p.canHumanPlay && p.canComputerPlay)
  650. whoCanPlay = HUMAN_OR_CPU;
  651. else if(p.canComputerPlay)
  652. whoCanPlay = CPU;
  653. else
  654. whoCanPlay = HUMAN;
  655. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
  656. {{
  657. "AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  658. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
  659. }};
  660. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
  661. {{
  662. "ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  663. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
  664. }};
  665. background = std::make_shared<CPicture>(bgs[s->color.getNum()], 0, 0);
  666. labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, s->name);
  667. 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]);
  668. if(SEL->screenType == ESelectionScreen::newGame)
  669. {
  670. buttonTownLeft = std::make_shared<CButton>(Point(107, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[132], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, -1, s->color));
  671. buttonTownRight = std::make_shared<CButton>(Point(168, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[133], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, +1, s->color));
  672. buttonHeroLeft = std::make_shared<CButton>(Point(183, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[148], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, -1, s->color));
  673. buttonHeroRight = std::make_shared<CButton>(Point(244, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[149], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, +1, s->color));
  674. buttonBonusLeft = std::make_shared<CButton>(Point(259, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[164], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, -1, s->color));
  675. buttonBonusRight = std::make_shared<CButton>(Point(320, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[165], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, +1, s->color));
  676. }
  677. hideUnavailableButtons();
  678. if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color.getNum()).canHumanPlay)
  679. {
  680. flag = std::make_shared<CButton>(
  681. Point(-43, 2),
  682. flags[s->color.getNum()],
  683. CGI->generaltexth->zelp[180],
  684. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  685. );
  686. flag->hoverable = true;
  687. flag->block(CSH->isGuest());
  688. }
  689. else
  690. flag = nullptr;
  691. town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
  692. hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
  693. bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
  694. }
  695. void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
  696. {
  697. if(ps.isControlledByAI() || humanPlayers > 0)
  698. CSH->setPlayer(ps.color);
  699. }
  700. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  701. {
  702. if(!buttonTownLeft)
  703. return;
  704. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
  705. if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
  706. {
  707. buttonTownLeft->disable();
  708. buttonTownRight->disable();
  709. }
  710. else
  711. {
  712. buttonTownLeft->enable();
  713. buttonTownRight->enable();
  714. }
  715. if((pi->defaultHero() != -1 || s->castle < 0) //fixed hero
  716. || foreignPlayer) //or not our player
  717. {
  718. buttonHeroLeft->disable();
  719. buttonHeroRight->disable();
  720. }
  721. else
  722. {
  723. buttonHeroLeft->enable();
  724. buttonHeroRight->enable();
  725. }
  726. if(foreignPlayer)
  727. {
  728. buttonBonusLeft->disable();
  729. buttonBonusRight->disable();
  730. }
  731. else
  732. {
  733. buttonBonusLeft->enable();
  734. buttonBonusRight->enable();
  735. }
  736. }