OptionsTab.cpp 35 KB

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