OptionsTab.cpp 33 KB

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