2
0

OptionsTab.cpp 31 KB

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