OptionsTab.cpp 28 KB

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