OptionsTab.cpp 28 KB

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