OptionsTab.cpp 36 KB

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