OptionsTab.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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/filesystem/Filesystem.h"
  32. #include "../../lib/NetPacksLobby.h"
  33. #include "../../lib/CGeneralTextHandler.h"
  34. #include "../../lib/CArtHandler.h"
  35. #include "../../lib/CTownHandler.h"
  36. #include "../../lib/CHeroHandler.h"
  37. #include "../../lib/mapping/CMapInfo.h"
  38. #include "../../lib/mapping/CMapHeader.h"
  39. OptionsTab::OptionsTab() : humanPlayers(0)
  40. {
  41. recActions = 0;
  42. addCallback("setTurnLength", std::bind(&IServerAPI::setTurnLength, CSH, _1));
  43. const JsonNode config(ResourceID("config/widgets/optionsTab.json"));
  44. build(config);
  45. if(SEL->screenType == ESelectionScreen::newGame || SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::scenarioInfo)
  46. {
  47. if(auto w = widget<CSlider>("sliderTurnDuration"))
  48. w->deactivate();
  49. if(auto w = widget<CLabel>("labelPlayerTurnDuration"))
  50. w->deactivate();
  51. if(auto w = widget<CLabel>("labelTurnDurationValue"))
  52. w->deactivate();
  53. }
  54. }
  55. void OptionsTab::recreate()
  56. {
  57. entries.clear();
  58. humanPlayers = 0;
  59. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  60. for(auto & pInfo : SEL->getStartInfo()->playerInfos)
  61. {
  62. if(pInfo.second.isControlledByHuman())
  63. humanPlayers++;
  64. entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
  65. }
  66. if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
  67. {
  68. turnSlider->scrollTo(vstd::find_pos(GameConstants::POSSIBLE_TURNTIME, SEL->getStartInfo()->turnTimerInfo.turnTimer / (60 * 1000)));
  69. if(auto w = widget<CLabel>("labelTurnDurationValue"))
  70. w->setText(CGI->generaltexth->turnDurations[turnSlider->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.getNum() >= CGI->townh->size() ? 0 : settings.castle.getNum();
  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 != HeroTypeID::NONE)
  107. return settings.heroPortrait;
  108. auto index = settings.hero.getNum() >= CGI->heroh->size() ? 0 : settings.hero.getNum();
  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.getNum() >= CGI->townh->size() ? 0 : settings.castle.getNum();
  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.getNum() >= CGI->heroh->size() ? 0 : settings.hero.getNum();
  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.getNum() < 0) ? CGI->generaltexth->allTexts[103] : CGI->generaltexth->allTexts[80];
  219. case OptionsTab::HERO:
  220. return (settings.hero.getNum() < 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.getNum() >= CGI->townh->size() ? 0 : settings.castle.getNum();
  241. auto heroIndex = settings.hero.getNum() >= CGI->heroh->size() ? 0 : settings.hero.getNum();
  242. switch(type)
  243. {
  244. case TOWN:
  245. return getName();
  246. case HERO:
  247. {
  248. if(settings.hero.getNum() >= 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.getNum() >= CGI->townh->size() ? 0 : settings.castle.getNum();
  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.getNum() >= CGI->townh->size() ? 0 : settings.castle.getNum();
  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.getNum() >= CGI->heroh->size() ? 0 : settings.hero.getNum();
  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, SelType _type)
  385. : CWindowObject(BORDERED)
  386. {
  387. addUsedEvents(LCLICK | SHOW_POPUP);
  388. color = _color;
  389. type = _type;
  390. initialFaction = SEL->getStartInfo()->playerInfos.find(color)->second.castle;
  391. initialHero = SEL->getStartInfo()->playerInfos.find(color)->second.hero;
  392. initialBonus = SEL->getStartInfo()->playerInfos.find(color)->second.bonus;
  393. selectedFaction = initialFaction;
  394. selectedHero = initialHero;
  395. selectedBonus = initialBonus;
  396. allowedFactions = SEL->getPlayerInfo(color.getNum()).allowedFactions;
  397. std::vector<bool> allowedHeroesFlag = SEL->getMapInfo()->mapHeader->allowedHeroes;
  398. for(int i = 0; i < allowedHeroesFlag.size(); i++)
  399. if(allowedHeroesFlag[i])
  400. allowedHeroes.insert(HeroTypeID(i));
  401. allowedBonus.push_back(-1); // random
  402. if(initialHero.getNum() >= -1)
  403. allowedBonus.push_back(0); // artifact
  404. allowedBonus.push_back(1); // gold
  405. if(initialFaction.getNum() >= 0)
  406. allowedBonus.push_back(2); // resource
  407. recreate();
  408. }
  409. int OptionsTab::SelectionWindow::calcLines(FactionID faction)
  410. {
  411. double additionalItems = 1; // random
  412. if(faction.getNum() < 0)
  413. return std::ceil(((double)allowedFactions.size() + additionalItems) / elementsPerLine);
  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 + additionalItems, (double)allowedFactions.size() + additionalItems) / (double)elementsPerLine);
  422. }
  423. void OptionsTab::SelectionWindow::apply()
  424. {
  425. if(GH.windows().isTopWindow(this))
  426. {
  427. GH.input().hapticFeedback();
  428. CCS->soundh->playSound(soundBase::button);
  429. close();
  430. setSelection();
  431. }
  432. }
  433. void OptionsTab::SelectionWindow::setSelection()
  434. {
  435. if(selectedFaction != initialFaction)
  436. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN_ID, selectedFaction, color);
  437. if(selectedHero != initialHero)
  438. CSH->setPlayerOption(LobbyChangePlayerOption::HERO_ID, selectedHero, color);
  439. if(selectedBonus != initialBonus)
  440. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS_ID, selectedBonus, color);
  441. }
  442. void OptionsTab::SelectionWindow::recreate()
  443. {
  444. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  445. int amountLines = 1;
  446. if(type == SelType::BONUS)
  447. elementsPerLine = allowedBonus.size();
  448. else
  449. {
  450. // try to make squarish
  451. if(type == SelType::TOWN)
  452. elementsPerLine = floor(sqrt(allowedFactions.size()));
  453. if(type == SelType::HERO)
  454. {
  455. int count = 0;
  456. for(auto & elem : allowedHeroes)
  457. {
  458. CHero * type = VLC->heroh->objects[elem];
  459. if(type->heroClass->faction == selectedFaction)
  460. {
  461. count++;
  462. }
  463. }
  464. elementsPerLine = floor(sqrt(count));
  465. }
  466. amountLines = calcLines((type > SelType::TOWN) ? selectedFaction : static_cast<FactionID>(PlayerSettings::RANDOM));
  467. }
  468. int x = (elementsPerLine) * (ICON_BIG_WIDTH-1);
  469. int y = (amountLines) * (ICON_BIG_HEIGHT-1);
  470. pos = Rect(0, 0, x, y);
  471. backgroundTexture = std::make_shared<FilledTexturePlayerColored>("DiBoxBck", pos);
  472. backgroundTexture->playerColored(PlayerColor(1));
  473. updateShadow();
  474. if(type == SelType::TOWN)
  475. genContentFactions();
  476. if(type == SelType::HERO)
  477. genContentHeroes();
  478. if(type == SelType::BONUS)
  479. genContentBonus();
  480. genContentGrid(amountLines);
  481. center();
  482. }
  483. void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
  484. {
  485. components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  486. components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  487. components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  488. components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  489. components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text));
  490. }
  491. void OptionsTab::SelectionWindow::genContentGrid(int lines)
  492. {
  493. for(int y = 0; y < lines; y++)
  494. {
  495. for(int x = 0; x < elementsPerLine; x++)
  496. {
  497. components.push_back(std::make_shared<CPicture>("lobby/townBorderBig", x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  498. }
  499. }
  500. }
  501. void OptionsTab::SelectionWindow::genContentFactions()
  502. {
  503. int i = 1;
  504. // random
  505. PlayerSettings set = PlayerSettings();
  506. set.castle = PlayerSettings::RANDOM;
  507. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  508. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  509. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedFaction.getNum() == PlayerSettings::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  510. if(selectedFaction.getNum() == PlayerSettings::RANDOM)
  511. components.push_back(std::make_shared<CPicture>("lobby/townBorderSmallActivated", 6, (ICON_SMALL_HEIGHT/2)));
  512. for(auto & elem : allowedFactions)
  513. {
  514. int x = i % elementsPerLine;
  515. int y = i / elementsPerLine;
  516. PlayerSettings set = PlayerSettings();
  517. set.castle = elem;
  518. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  519. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  520. components.push_back(std::make_shared<CPicture>(selectedFaction == elem ? "lobby/townBorderBigActivated" : "lobby/townBorderBig", x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  521. 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());
  522. factions.push_back(elem);
  523. i++;
  524. }
  525. }
  526. void OptionsTab::SelectionWindow::genContentHeroes()
  527. {
  528. int i = 1;
  529. // random
  530. PlayerSettings set = PlayerSettings();
  531. set.hero = PlayerSettings::RANDOM;
  532. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  533. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  534. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedHero.getNum() == PlayerSettings::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  535. if(selectedHero.getNum() == PlayerSettings::RANDOM)
  536. components.push_back(std::make_shared<CPicture>("lobby/townBorderSmallActivated", 6, (ICON_SMALL_HEIGHT/2)));
  537. for(auto & elem : allowedHeroes)
  538. {
  539. CHero * type = VLC->heroh->objects[elem];
  540. if(type->heroClass->faction == selectedFaction)
  541. {
  542. int x = i % elementsPerLine;
  543. int y = i / elementsPerLine;
  544. PlayerSettings set = PlayerSettings();
  545. set.hero = elem;
  546. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  547. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  548. components.push_back(std::make_shared<CPicture>(selectedHero == elem ? "lobby/townBorderBigActivated" : "lobby/townBorderBig", x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  549. 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());
  550. heroes.push_back(elem);
  551. i++;
  552. }
  553. }
  554. }
  555. void OptionsTab::SelectionWindow::genContentBonus()
  556. {
  557. PlayerSettings set = PlayerSettings();
  558. int i = 0;
  559. for(auto elem : allowedBonus)
  560. {
  561. int x = i;
  562. int y = 0;
  563. set.bonus = static_cast<PlayerSettings::Ebonus>(elem);
  564. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  565. 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)));
  566. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::WHITE , helper.getName());
  567. if(selectedBonus == elem)
  568. {
  569. components.push_back(std::make_shared<CPicture>("lobby/townBorderSmallActivated", x * (ICON_BIG_WIDTH-1) + 6, y * (ICON_BIG_HEIGHT-1) + (ICON_SMALL_HEIGHT/2)));
  570. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::YELLOW , helper.getName());
  571. }
  572. i++;
  573. }
  574. }
  575. int OptionsTab::SelectionWindow::getElement(const Point & cursorPosition)
  576. {
  577. int x = (cursorPosition.x - pos.x) / (ICON_BIG_WIDTH-1);
  578. int y = (cursorPosition.y - pos.y) / (ICON_BIG_HEIGHT-1);
  579. return x + y * elementsPerLine;
  580. }
  581. void OptionsTab::SelectionWindow::setElement(int elem, bool doApply)
  582. {
  583. PlayerSettings set = PlayerSettings();
  584. if(type == SelType::TOWN)
  585. {
  586. if(elem > 0)
  587. {
  588. elem--;
  589. if(elem >= factions.size())
  590. return;
  591. set.castle = factions[elem];
  592. }
  593. else
  594. {
  595. set.castle = PlayerSettings::RANDOM;
  596. }
  597. if(set.castle.getNum() != PlayerSettings::NONE)
  598. {
  599. if(!doApply)
  600. {
  601. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  602. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  603. }
  604. else
  605. selectedFaction = set.castle;
  606. }
  607. }
  608. if(type == SelType::HERO)
  609. {
  610. if(elem > 0)
  611. {
  612. elem--;
  613. if(elem >= heroes.size())
  614. return;
  615. set.hero = heroes[elem];
  616. }
  617. else
  618. {
  619. set.hero = PlayerSettings::RANDOM;
  620. }
  621. if(set.hero.getNum() != PlayerSettings::NONE)
  622. {
  623. if(!doApply)
  624. {
  625. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  626. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  627. }
  628. else
  629. selectedHero = set.hero;
  630. }
  631. }
  632. if(type == SelType::BONUS)
  633. {
  634. if(elem >= 4)
  635. return;
  636. set.bonus = static_cast<PlayerSettings::Ebonus>(elem-1);
  637. if(set.bonus != PlayerSettings::NONE)
  638. {
  639. if(!doApply)
  640. {
  641. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  642. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  643. }
  644. else
  645. selectedBonus = set.bonus;
  646. }
  647. }
  648. if(doApply)
  649. apply();
  650. }
  651. bool OptionsTab::SelectionWindow::receiveEvent(const Point & position, int eventType) const
  652. {
  653. return true; // capture click also outside of window
  654. }
  655. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
  656. {
  657. if(!pos.isInside(cursorPosition))
  658. {
  659. close();
  660. return;
  661. }
  662. int elem = getElement(cursorPosition);
  663. setElement(elem, true);
  664. }
  665. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  666. {
  667. if(!pos.isInside(cursorPosition))
  668. return;
  669. int elem = getElement(cursorPosition);
  670. setElement(elem, false);
  671. }
  672. OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & settings, SelType type)
  673. : Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
  674. , CPlayerSettingsHelper(settings, type)
  675. {
  676. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  677. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
  678. subtitle = std::make_shared<CLabel>(23, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName());
  679. pos = image->pos;
  680. setPanningStep(pos.w);
  681. }
  682. void OptionsTab::SelectedBox::update()
  683. {
  684. image->setFrame(getImageIndex());
  685. subtitle->setText(getName());
  686. }
  687. void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
  688. {
  689. // cases when we do not need to display a message
  690. if(settings.castle.getNum() == PlayerSettings::NONE && CPlayerSettingsHelper::type == TOWN)
  691. return;
  692. if(settings.hero.getNum() == PlayerSettings::NONE && !SEL->getPlayerInfo(settings.color.getNum()).hasCustomMainHero() && CPlayerSettingsHelper::type == HERO)
  693. return;
  694. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  695. }
  696. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  697. {
  698. PlayerInfo pi = SEL->getPlayerInfo(settings.color.getNum());
  699. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(settings.color);
  700. if(type == SelType::TOWN && ((pi.allowedFactions.size() < 2 && !pi.isFactionRandom) || foreignPlayer))
  701. return;
  702. if(type == SelType::HERO && ((pi.defaultHero() != -1 || settings.castle.getNum() < 0) || foreignPlayer))
  703. return;
  704. if(type == SelType::BONUS && foreignPlayer)
  705. return;
  706. GH.input().hapticFeedback();
  707. GH.windows().createAndPushWindow<SelectionWindow>(settings.color, type);
  708. }
  709. void OptionsTab::SelectedBox::scrollBy(int distance)
  710. {
  711. // FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
  712. // because of that, panning event gets interrupted (due to destruction of element)
  713. // so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
  714. distance = std::clamp(distance, -1, 1);
  715. switch(CPlayerSettingsHelper::type)
  716. {
  717. case TOWN:
  718. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, settings.color);
  719. break;
  720. case HERO:
  721. CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, settings.color);
  722. break;
  723. case BONUS:
  724. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, settings.color);
  725. break;
  726. }
  727. setScrollingEnabled(false);
  728. }
  729. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
  730. : pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color.getNum())))
  731. , s(std::make_unique<PlayerSettings>(S))
  732. , parentTab(parent)
  733. {
  734. OBJ_CONSTRUCTION;
  735. defActions |= SHARE_POS;
  736. int serial = 0;
  737. for(int g = 0; g < s->color.getNum(); ++g)
  738. {
  739. auto itred = SEL->getPlayerInfo(g);
  740. if(itred.canComputerPlay || itred.canHumanPlay)
  741. serial++;
  742. }
  743. pos.x += 54;
  744. pos.y += 128 + serial * 50;
  745. assert(CSH->mi && CSH->mi->mapHeader);
  746. const PlayerInfo & p = SEL->getPlayerInfo(s->color.getNum());
  747. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  748. if(p.canHumanPlay && p.canComputerPlay)
  749. whoCanPlay = HUMAN_OR_CPU;
  750. else if(p.canComputerPlay)
  751. whoCanPlay = CPU;
  752. else
  753. whoCanPlay = HUMAN;
  754. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
  755. {{
  756. "AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  757. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
  758. }};
  759. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
  760. {{
  761. "ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  762. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
  763. }};
  764. background = std::make_shared<CPicture>(bgs[s->color.getNum()], 0, 0);
  765. labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, s->name);
  766. 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]);
  767. if(SEL->screenType == ESelectionScreen::newGame)
  768. {
  769. buttonTownLeft = std::make_shared<CButton>(Point(107, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[132], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, -1, s->color));
  770. buttonTownRight = std::make_shared<CButton>(Point(168, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[133], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::TOWN, +1, s->color));
  771. buttonHeroLeft = std::make_shared<CButton>(Point(183, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[148], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, -1, s->color));
  772. buttonHeroRight = std::make_shared<CButton>(Point(244, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[149], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::HERO, +1, s->color));
  773. buttonBonusLeft = std::make_shared<CButton>(Point(259, 5), "ADOPLFA.DEF", CGI->generaltexth->zelp[164], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, -1, s->color));
  774. buttonBonusRight = std::make_shared<CButton>(Point(320, 5), "ADOPRTA.DEF", CGI->generaltexth->zelp[165], std::bind(&IServerAPI::setPlayerOption, CSH, LobbyChangePlayerOption::BONUS, +1, s->color));
  775. }
  776. hideUnavailableButtons();
  777. if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color.getNum()).canHumanPlay)
  778. {
  779. flag = std::make_shared<CButton>(
  780. Point(-43, 2),
  781. flags[s->color.getNum()],
  782. CGI->generaltexth->zelp[180],
  783. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  784. );
  785. flag->hoverable = true;
  786. flag->block(CSH->isGuest());
  787. }
  788. else
  789. flag = nullptr;
  790. town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
  791. hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
  792. bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
  793. }
  794. void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
  795. {
  796. if(ps.isControlledByAI() || humanPlayers > 0)
  797. CSH->setPlayer(ps.color);
  798. }
  799. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  800. {
  801. if(!buttonTownLeft)
  802. return;
  803. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
  804. if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
  805. {
  806. buttonTownLeft->disable();
  807. buttonTownRight->disable();
  808. }
  809. else
  810. {
  811. buttonTownLeft->enable();
  812. buttonTownRight->enable();
  813. }
  814. if((pi->defaultHero() != -1 || s->castle.getNum() < 0) //fixed hero
  815. || foreignPlayer) //or not our player
  816. {
  817. buttonHeroLeft->disable();
  818. buttonHeroRight->disable();
  819. }
  820. else
  821. {
  822. buttonHeroLeft->enable();
  823. buttonHeroRight->enable();
  824. }
  825. if(foreignPlayer)
  826. {
  827. buttonBonusLeft->disable();
  828. buttonBonusRight->disable();
  829. }
  830. else
  831. {
  832. buttonBonusLeft->enable();
  833. buttonBonusRight->enable();
  834. }
  835. }