OptionsTab.cpp 36 KB

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