OptionsTab.cpp 30 KB

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