OptionsTab.cpp 37 KB

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