OptionsTab.cpp 34 KB

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