OptionsTab.cpp 26 KB

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