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(int 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. genContentTitle();
  394. genContentCastles();
  395. genContentHeroes();
  396. genContentBonus();
  397. center();
  398. }
  399. void OptionsTab::SelectionWindow::apply()
  400. {
  401. if(GH.windows().isTopWindow(this))
  402. {
  403. close();
  404. }
  405. }
  406. void OptionsTab::SelectionWindow::redraw()
  407. {
  408. components.clear();
  409. genContentTitle();
  410. genContentCastles();
  411. genContentHeroes();
  412. genContentBonus();
  413. }
  414. void OptionsTab::SelectionWindow::genContentTitle()
  415. {
  416. components.push_back(std::make_shared<CLabel>((ELEMENTS_PER_LINE - 1) * 58, 40, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, "Town"));
  417. components.push_back(std::make_shared<CLabel>((ELEMENTS_PER_LINE * 2) * 58, 40, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, "Hero"));
  418. components.push_back(std::make_shared<CLabel>((ELEMENTS_PER_LINE * 2 + 3) * 58 + 29, 40, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, "Bonus"));
  419. }
  420. void OptionsTab::SelectionWindow::genContentCastles()
  421. {
  422. factions.clear();
  423. PlayerSettings set = PlayerSettings();
  424. set.castle = set.RANDOM;
  425. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  426. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, (ELEMENTS_PER_LINE / 2) * 58 + 34, 32 / 2 + 64));
  427. int i = 0;
  428. for(auto & elem : SEL->getPlayerInfo(0).allowedFactions)
  429. {
  430. int x = i % ELEMENTS_PER_LINE + 1;
  431. int y = i / ELEMENTS_PER_LINE + 2;
  432. PlayerSettings set = PlayerSettings();
  433. set.castle = elem;
  434. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  435. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * 58, y * 64));
  436. factions.push_back(elem);
  437. i++;
  438. }
  439. }
  440. void OptionsTab::SelectionWindow::genContentHeroes()
  441. {
  442. heroes.clear();
  443. PlayerSettings set = PlayerSettings();
  444. set.castle = set.RANDOM;
  445. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  446. 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));
  447. std::vector<bool> allowedHeroesFlag = SEL->getMapInfo()->mapHeader->allowedHeroes;
  448. std::set<HeroTypeID> allowedHeroes;
  449. for(int i = 0; i < allowedHeroesFlag.size(); i++)
  450. if(allowedHeroesFlag[i])
  451. allowedHeroes.insert(HeroTypeID(i));
  452. int i = 0;
  453. for(auto & elem : allowedHeroes)
  454. {
  455. CHero * type = VLC->heroh->objects[elem];
  456. if(type->heroClass->faction == SEL->getStartInfo()->playerInfos.find(PlayerColor(color))->second.castle)
  457. {
  458. int x = (i % ELEMENTS_PER_LINE) + ELEMENTS_PER_LINE + 2;
  459. int y = i / ELEMENTS_PER_LINE + 2;
  460. PlayerSettings set = PlayerSettings();
  461. set.hero = elem;
  462. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  463. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * 58, y * 64));
  464. heroes.push_back(elem);
  465. i++;
  466. }
  467. }
  468. }
  469. void OptionsTab::SelectionWindow::genContentBonus()
  470. {
  471. PlayerSettings set = PlayerSettings();
  472. int i = 0;
  473. for(auto elem : {set.RANDOM, set.ARTIFACT, set.GOLD, set.RESOURCE})
  474. {
  475. int x = ELEMENTS_PER_LINE * 2 + 3;
  476. int y = i + 1;
  477. set.bonus = elem;
  478. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  479. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, x * 58, y * 64 + 32 / 2));
  480. i++;
  481. }
  482. }
  483. FactionID OptionsTab::SelectionWindow::getElementCastle(const Point & cursorPosition)
  484. {
  485. Point loc = getElement(cursorPosition, 0);
  486. FactionID faction;
  487. faction = PlayerSettings().NONE;
  488. if ((loc.x == ELEMENTS_PER_LINE / 2 || loc.x == ELEMENTS_PER_LINE / 2 - 1) && loc.y == 0)
  489. faction = PlayerSettings().RANDOM;
  490. else if(loc.y > 0 && loc.x < ELEMENTS_PER_LINE)
  491. {
  492. int index = loc.x + (loc.y - 1) * ELEMENTS_PER_LINE;
  493. if (index < factions.size())
  494. faction = factions[loc.x + (loc.y - 1) * ELEMENTS_PER_LINE];
  495. }
  496. return faction;
  497. }
  498. HeroTypeID OptionsTab::SelectionWindow::getElementHero(const Point & cursorPosition)
  499. {
  500. Point loc = getElement(cursorPosition, 1);
  501. HeroTypeID hero;
  502. hero = PlayerSettings().NONE;
  503. if(loc.x < 0)
  504. return hero;
  505. if ((loc.x == ELEMENTS_PER_LINE / 2 || loc.x == ELEMENTS_PER_LINE / 2 - 1) && loc.y == 0)
  506. hero = PlayerSettings().RANDOM;
  507. else if(loc.y > 0 && loc.x < ELEMENTS_PER_LINE)
  508. {
  509. int index = loc.x + (loc.y - 1) * ELEMENTS_PER_LINE;
  510. if (index < heroes.size())
  511. hero = heroes[loc.x + (loc.y - 1) * ELEMENTS_PER_LINE];
  512. }
  513. return hero;
  514. }
  515. int OptionsTab::SelectionWindow::getElementBonus(const Point & cursorPosition)
  516. {
  517. Point loc = getElement(cursorPosition, 2);
  518. if(loc.x != 0 || loc.y < 0 || loc.y > 3)
  519. return -2;
  520. return loc.y - 1;
  521. }
  522. Point OptionsTab::SelectionWindow::getElement(const Point & cursorPosition, int area)
  523. {
  524. int x = (cursorPosition.x - pos.x - area * (ELEMENTS_PER_LINE + 1) * 58) / 58;
  525. int y = (cursorPosition.y - pos.y) / 64;
  526. return Point(x - 1, y - 1);
  527. }
  528. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition) {
  529. FactionID faction = getElementCastle(cursorPosition);
  530. HeroTypeID hero = getElementHero(cursorPosition);
  531. int bonus = getElementBonus(cursorPosition);
  532. PlayerSettings set = PlayerSettings();
  533. set.castle = faction;
  534. set.hero = hero;
  535. set.bonus = static_cast<PlayerSettings::Ebonus>(bonus);
  536. if(set.castle != -2)
  537. {
  538. //SEL->getStartInfo()->playerInfos.find(PlayerColor(color))->second.castle = faction;
  539. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, 1, PlayerColor(color));
  540. redraw();
  541. //CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  542. }
  543. else if(set.hero != -2)
  544. {
  545. //CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  546. apply();
  547. }
  548. else if(set.bonus != -2)
  549. {
  550. //CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  551. apply();
  552. }
  553. }
  554. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  555. {
  556. FactionID faction = getElementCastle(cursorPosition);
  557. HeroTypeID hero = getElementHero(cursorPosition);
  558. int bonus = getElementBonus(cursorPosition);
  559. PlayerSettings set = PlayerSettings();
  560. set.castle = faction;
  561. set.hero = hero;
  562. set.bonus = static_cast<PlayerSettings::Ebonus>(bonus);
  563. if(set.castle != -2)
  564. {
  565. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  566. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  567. }
  568. else if(set.hero != -2)
  569. {
  570. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  571. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  572. }
  573. else if(set.bonus != -2)
  574. {
  575. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  576. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  577. }
  578. }
  579. OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & settings, SelType type)
  580. : Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
  581. , CPlayerSettingsHelper(settings, type)
  582. {
  583. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  584. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
  585. subtitle = std::make_shared<CLabel>(23, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName());
  586. pos = image->pos;
  587. setPanningStep(pos.w);
  588. }
  589. void OptionsTab::SelectedBox::update()
  590. {
  591. image->setFrame(getImageIndex());
  592. subtitle->setText(getName());
  593. }
  594. void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
  595. {
  596. // cases when we do not need to display a message
  597. if(settings.castle == -2 && CPlayerSettingsHelper::type == TOWN)
  598. return;
  599. if(settings.hero == -2 && !SEL->getPlayerInfo(settings.color.getNum()).hasCustomMainHero() && CPlayerSettingsHelper::type == HERO)
  600. return;
  601. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  602. }
  603. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  604. {
  605. GH.windows().createAndPushWindow<SelectionWindow>(settings.color.getNum());
  606. }
  607. void OptionsTab::SelectedBox::scrollBy(int distance)
  608. {
  609. // FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
  610. // because of that, panning event gets interrupted (due to destruction of element)
  611. // so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
  612. distance = std::clamp(distance, -1, 1);
  613. switch(CPlayerSettingsHelper::type)
  614. {
  615. case TOWN:
  616. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, settings.color);
  617. break;
  618. case HERO:
  619. CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, settings.color);
  620. break;
  621. case BONUS:
  622. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, settings.color);
  623. break;
  624. }
  625. setScrollingEnabled(false);
  626. }
  627. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
  628. : pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color.getNum())))
  629. , s(std::make_unique<PlayerSettings>(S))
  630. , parentTab(parent)
  631. {
  632. OBJ_CONSTRUCTION;
  633. defActions |= SHARE_POS;
  634. int serial = 0;
  635. for(int g = 0; g < s->color.getNum(); ++g)
  636. {
  637. auto itred = SEL->getPlayerInfo(g);
  638. if(itred.canComputerPlay || itred.canHumanPlay)
  639. serial++;
  640. }
  641. pos.x += 54;
  642. pos.y += 122 + serial * 50;
  643. assert(CSH->mi && CSH->mi->mapHeader);
  644. const PlayerInfo & p = SEL->getPlayerInfo(s->color.getNum());
  645. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  646. if(p.canHumanPlay && p.canComputerPlay)
  647. whoCanPlay = HUMAN_OR_CPU;
  648. else if(p.canComputerPlay)
  649. whoCanPlay = CPU;
  650. else
  651. whoCanPlay = HUMAN;
  652. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
  653. {{
  654. "AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  655. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
  656. }};
  657. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
  658. {{
  659. "ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  660. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
  661. }};
  662. background = std::make_shared<CPicture>(bgs[s->color.getNum()], 0, 0);
  663. labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, s->name);
  664. 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]);
  665. if(SEL->screenType == ESelectionScreen::newGame)
  666. {
  667. buttonTownLeft = std::make_shared<CButton>(Point(107, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[132], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, -1, s->color));
  668. buttonTownRight = std::make_shared<CButton>(Point(168, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[133], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, +1, s->color));
  669. buttonHeroLeft = std::make_shared<CButton>(Point(183, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[148], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, -1, s->color));
  670. buttonHeroRight = std::make_shared<CButton>(Point(244, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[149], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, +1, s->color));
  671. buttonBonusLeft = std::make_shared<CButton>(Point(259, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[164], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, -1, s->color));
  672. buttonBonusRight = std::make_shared<CButton>(Point(320, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[165], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, +1, s->color));
  673. }
  674. hideUnavailableButtons();
  675. if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color.getNum()).canHumanPlay)
  676. {
  677. flag = std::make_shared<CButton>(
  678. Point(-43, 2),
  679. flags[s->color.getNum()],
  680. CGI->generaltexth->zelp[180],
  681. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  682. );
  683. flag->hoverable = true;
  684. flag->block(CSH->isGuest());
  685. }
  686. else
  687. flag = nullptr;
  688. town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
  689. hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
  690. bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
  691. }
  692. void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
  693. {
  694. if(ps.isControlledByAI() || humanPlayers > 0)
  695. CSH->setPlayer(ps.color);
  696. }
  697. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  698. {
  699. if(!buttonTownLeft)
  700. return;
  701. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
  702. if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
  703. {
  704. buttonTownLeft->disable();
  705. buttonTownRight->disable();
  706. }
  707. else
  708. {
  709. buttonTownLeft->enable();
  710. buttonTownRight->enable();
  711. }
  712. if((pi->defaultHero() != -1 || s->castle < 0) //fixed hero
  713. || foreignPlayer) //or not our player
  714. {
  715. buttonHeroLeft->disable();
  716. buttonHeroRight->disable();
  717. }
  718. else
  719. {
  720. buttonHeroLeft->enable();
  721. buttonHeroRight->enable();
  722. }
  723. if(foreignPlayer)
  724. {
  725. buttonBonusLeft->disable();
  726. buttonBonusRight->disable();
  727. }
  728. else
  729. {
  730. buttonBonusLeft->enable();
  731. buttonBonusRight->enable();
  732. }
  733. }