OptionsTab.cpp 30 KB

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