OptionsTab.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. pos = Rect(0, 0, 228, 290);
  507. genHeader();
  508. labelAssociatedCreatures = std::make_shared<CLabel>(pos.w / 2 + 8, 122, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[79]);
  509. auto factionIndex = playerSettings.getCastleValidated();
  510. std::vector<std::shared_ptr<CComponent>> components;
  511. const CTown * town = (*CGI->townh)[factionIndex]->town;
  512. for(auto & elem : town->creatures)
  513. {
  514. if(!elem.empty())
  515. components.push_back(std::make_shared<CComponent>(ComponentType::CREATURE, elem.front(), 0, CComponent::tiny));
  516. }
  517. boxAssociatedCreatures = std::make_shared<CComponentBox>(components, Rect(10, 140, pos.w - 20, 140));
  518. }
  519. void OptionsTab::CPlayerOptionTooltipBox::genHeroWindow()
  520. {
  521. pos = Rect(0, 0, 292, 226);
  522. genHeader();
  523. labelHeroSpeciality = std::make_shared<CLabel>(pos.w / 2 + 4, 117, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[78]);
  524. auto heroIndex = playerSettings.getHeroValidated();
  525. imageSpeciality = std::make_shared<CAnimImage>(AnimationPath::builtin("UN44"), (*CGI->heroh)[heroIndex]->imageIndex, 0, pos.w / 2 - 22, 134);
  526. labelSpecialityName = std::make_shared<CLabel>(pos.w / 2, 188, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, (*CGI->heroh)[heroIndex]->getSpecialtyNameTranslated());
  527. }
  528. void OptionsTab::CPlayerOptionTooltipBox::genBonusWindow()
  529. {
  530. pos = Rect(0, 0, 228, 162);
  531. genHeader();
  532. textBonusDescription = std::make_shared<CTextBox>(getDescription(), Rect(10, 100, pos.w - 20, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  533. }
  534. OptionsTab::SelectionWindow::SelectionWindow(PlayerColor _color, SelType _type)
  535. : CWindowObject(BORDERED)
  536. {
  537. addUsedEvents(LCLICK | SHOW_POPUP);
  538. color = _color;
  539. type = _type;
  540. initialFaction = SEL->getStartInfo()->playerInfos.find(color)->second.castle;
  541. initialHero = SEL->getStartInfo()->playerInfos.find(color)->second.hero;
  542. initialBonus = SEL->getStartInfo()->playerInfos.find(color)->second.bonus;
  543. selectedFaction = initialFaction;
  544. selectedHero = initialHero;
  545. selectedBonus = initialBonus;
  546. allowedFactions = SEL->getPlayerInfo(color).allowedFactions;
  547. std::vector<bool> allowedHeroesFlag = SEL->getMapInfo()->mapHeader->allowedHeroes;
  548. for(int i = 0; i < allowedHeroesFlag.size(); i++)
  549. if(allowedHeroesFlag[i])
  550. allowedHeroes.insert(HeroTypeID(i));
  551. for(auto & player : SEL->getStartInfo()->playerInfos)
  552. {
  553. if(player.first != color && (int)player.second.hero > HeroTypeID::RANDOM)
  554. unusableHeroes.insert(player.second.hero);
  555. }
  556. allowedBonus.push_back(PlayerStartingBonus::RANDOM);
  557. if(initialHero != HeroTypeID::NONE|| SEL->getPlayerInfo(color).heroesNames.size() > 0)
  558. allowedBonus.push_back(PlayerStartingBonus::ARTIFACT);
  559. allowedBonus.push_back(PlayerStartingBonus::GOLD);
  560. if(initialFaction.isValid())
  561. allowedBonus.push_back(PlayerStartingBonus::RESOURCE);
  562. recreate();
  563. }
  564. int OptionsTab::SelectionWindow::calcLines(FactionID faction)
  565. {
  566. double additionalItems = 1; // random
  567. if(!faction.isValid())
  568. return std::ceil(((double)allowedFactions.size() + additionalItems) / elementsPerLine);
  569. int count = 0;
  570. for(auto & elemh : allowedHeroes)
  571. {
  572. CHero * type = VLC->heroh->objects[elemh];
  573. if(type->heroClass->faction == faction)
  574. count++;
  575. }
  576. return std::ceil(std::max((double)count + additionalItems, (double)allowedFactions.size() + additionalItems) / (double)elementsPerLine);
  577. }
  578. void OptionsTab::SelectionWindow::apply()
  579. {
  580. if(GH.windows().isTopWindow(this))
  581. {
  582. GH.input().hapticFeedback();
  583. CCS->soundh->playSound(soundBase::button);
  584. close();
  585. setSelection();
  586. }
  587. }
  588. void OptionsTab::SelectionWindow::setSelection()
  589. {
  590. if(selectedFaction != initialFaction)
  591. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN_ID, selectedFaction, color);
  592. if(selectedHero != initialHero)
  593. CSH->setPlayerOption(LobbyChangePlayerOption::HERO_ID, selectedHero, color);
  594. if(selectedBonus != initialBonus)
  595. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS_ID, static_cast<int>(selectedBonus), color);
  596. }
  597. void OptionsTab::SelectionWindow::reopen()
  598. {
  599. std::shared_ptr<SelectionWindow> window = std::shared_ptr<SelectionWindow>(new SelectionWindow(color, type));
  600. close();
  601. GH.windows().pushWindow(window);
  602. }
  603. void OptionsTab::SelectionWindow::recreate()
  604. {
  605. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  606. int amountLines = 1;
  607. if(type == SelType::BONUS)
  608. elementsPerLine = allowedBonus.size();
  609. else
  610. {
  611. // try to make squarish
  612. if(type == SelType::TOWN)
  613. elementsPerLine = floor(sqrt(allowedFactions.size()));
  614. if(type == SelType::HERO)
  615. {
  616. int count = 0;
  617. for(auto & elem : allowedHeroes)
  618. {
  619. CHero * type = VLC->heroh->objects[elem];
  620. if(type->heroClass->faction == selectedFaction)
  621. {
  622. count++;
  623. }
  624. }
  625. elementsPerLine = floor(sqrt(count));
  626. }
  627. amountLines = calcLines((type > SelType::TOWN) ? selectedFaction : FactionID::RANDOM);
  628. }
  629. int x = (elementsPerLine) * (ICON_BIG_WIDTH-1);
  630. int y = (amountLines) * (ICON_BIG_HEIGHT-1);
  631. pos = Rect(0, 0, x, y);
  632. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), pos);
  633. backgroundTexture->playerColored(PlayerColor(1));
  634. updateShadow();
  635. if(type == SelType::TOWN)
  636. genContentFactions();
  637. if(type == SelType::HERO)
  638. genContentHeroes();
  639. if(type == SelType::BONUS)
  640. genContentBonus();
  641. genContentGrid(amountLines);
  642. center();
  643. }
  644. void OptionsTab::SelectionWindow::drawOutlinedText(int x, int y, ColorRGBA color, std::string text)
  645. {
  646. components.push_back(std::make_shared<CLabel>(x-1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  647. components.push_back(std::make_shared<CLabel>(x+1, y, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  648. components.push_back(std::make_shared<CLabel>(x, y-1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  649. components.push_back(std::make_shared<CLabel>(x, y+1, FONT_TINY, ETextAlignment::CENTER, Colors::BLACK, text));
  650. components.push_back(std::make_shared<CLabel>(x, y, FONT_TINY, ETextAlignment::CENTER, color, text));
  651. }
  652. void OptionsTab::SelectionWindow::genContentGrid(int lines)
  653. {
  654. for(int y = 0; y < lines; y++)
  655. {
  656. for(int x = 0; x < elementsPerLine; x++)
  657. {
  658. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderBig"), x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  659. }
  660. }
  661. }
  662. void OptionsTab::SelectionWindow::genContentFactions()
  663. {
  664. int i = 1;
  665. // random
  666. PlayerSettings set = PlayerSettings();
  667. set.castle = FactionID::RANDOM;
  668. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  669. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  670. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedFaction == FactionID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  671. if(selectedFaction == FactionID::RANDOM)
  672. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  673. for(auto & elem : allowedFactions)
  674. {
  675. int x = i % elementsPerLine;
  676. int y = i / elementsPerLine;
  677. PlayerSettings set = PlayerSettings();
  678. set.castle = elem;
  679. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  680. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  681. 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)));
  682. 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());
  683. factions.push_back(elem);
  684. i++;
  685. }
  686. }
  687. void OptionsTab::SelectionWindow::genContentHeroes()
  688. {
  689. int i = 1;
  690. // random
  691. PlayerSettings set = PlayerSettings();
  692. set.hero = HeroTypeID::RANDOM;
  693. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  694. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(), helper.getImageIndex(), 0, 6, (ICON_SMALL_HEIGHT/2)));
  695. drawOutlinedText(TEXT_POS_X, TEXT_POS_Y, (selectedHero == HeroTypeID::RANDOM) ? Colors::YELLOW : Colors::WHITE, helper.getName());
  696. if(selectedHero == HeroTypeID::RANDOM)
  697. components.push_back(std::make_shared<CPicture>(ImagePath::builtin("lobby/townBorderSmallActivated"), 6, (ICON_SMALL_HEIGHT/2)));
  698. for(auto & elem : allowedHeroes)
  699. {
  700. CHero * type = VLC->heroh->objects[elem];
  701. if(type->heroClass->faction == selectedFaction)
  702. {
  703. int x = i % elementsPerLine;
  704. int y = i / elementsPerLine;
  705. PlayerSettings set = PlayerSettings();
  706. set.hero = elem;
  707. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  708. components.push_back(std::make_shared<CAnimImage>(helper.getImageName(true), helper.getImageIndex(true), 0, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  709. 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());
  710. ImagePath image = ImagePath::builtin("lobby/townBorderBig");
  711. if(selectedHero == elem)
  712. image = ImagePath::builtin("lobby/townBorderBigActivated");
  713. if(unusableHeroes.count(elem))
  714. image = ImagePath::builtin("lobby/townBorderBigGrayedOut");
  715. components.push_back(std::make_shared<CPicture>(image, x * (ICON_BIG_WIDTH-1), y * (ICON_BIG_HEIGHT-1)));
  716. heroes.push_back(elem);
  717. i++;
  718. }
  719. }
  720. }
  721. void OptionsTab::SelectionWindow::genContentBonus()
  722. {
  723. PlayerSettings set = PlayerSettings();
  724. int i = 0;
  725. for(auto elem : allowedBonus)
  726. {
  727. int x = i;
  728. int y = 0;
  729. set.bonus = elem;
  730. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  731. 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)));
  732. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::WHITE , helper.getName());
  733. if(selectedBonus == elem)
  734. {
  735. 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)));
  736. drawOutlinedText(x * (ICON_BIG_WIDTH-1) + TEXT_POS_X, y * (ICON_BIG_HEIGHT-1) + TEXT_POS_Y, Colors::YELLOW , helper.getName());
  737. }
  738. i++;
  739. }
  740. }
  741. int OptionsTab::SelectionWindow::getElement(const Point & cursorPosition)
  742. {
  743. int x = (cursorPosition.x - pos.x) / (ICON_BIG_WIDTH-1);
  744. int y = (cursorPosition.y - pos.y) / (ICON_BIG_HEIGHT-1);
  745. return x + y * elementsPerLine;
  746. }
  747. void OptionsTab::SelectionWindow::setElement(int elem, bool doApply)
  748. {
  749. PlayerSettings set = PlayerSettings();
  750. if(type == SelType::TOWN)
  751. {
  752. if(elem > 0)
  753. {
  754. elem--;
  755. if(elem >= factions.size())
  756. return;
  757. set.castle = factions[elem];
  758. }
  759. else
  760. {
  761. set.castle = FactionID::RANDOM;
  762. }
  763. if(set.castle != FactionID::NONE)
  764. {
  765. if(!doApply)
  766. {
  767. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::TOWN);
  768. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  769. }
  770. else
  771. selectedFaction = set.castle;
  772. }
  773. }
  774. if(type == SelType::HERO)
  775. {
  776. if(elem > 0)
  777. {
  778. elem--;
  779. if(elem >= heroes.size())
  780. return;
  781. set.hero = heroes[elem];
  782. }
  783. else
  784. {
  785. set.hero = HeroTypeID::RANDOM;
  786. }
  787. if(doApply && unusableHeroes.count(heroes[elem]))
  788. return;
  789. if(set.hero != HeroTypeID::NONE)
  790. {
  791. if(!doApply)
  792. {
  793. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::HERO);
  794. if(settings["general"]["enableUiEnhancements"].Bool() && helper.playerSettings.hero.isValid() && helper.playerSettings.heroNameTextId.empty())
  795. GH.windows().createAndPushWindow<CHeroOverview>(helper.playerSettings.hero);
  796. else
  797. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  798. }
  799. else
  800. selectedHero = set.hero;
  801. }
  802. }
  803. if(type == SelType::BONUS)
  804. {
  805. if(elem >= 4)
  806. return;
  807. set.bonus = static_cast<PlayerStartingBonus>(allowedBonus[elem]);
  808. if(!doApply)
  809. {
  810. CPlayerSettingsHelper helper = CPlayerSettingsHelper(set, SelType::BONUS);
  811. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(helper);
  812. }
  813. else
  814. selectedBonus = set.bonus;
  815. }
  816. if(doApply)
  817. apply();
  818. }
  819. bool OptionsTab::SelectionWindow::receiveEvent(const Point & position, int eventType) const
  820. {
  821. return true; // capture click also outside of window
  822. }
  823. void OptionsTab::SelectionWindow::clickReleased(const Point & cursorPosition)
  824. {
  825. if(!pos.isInside(cursorPosition))
  826. {
  827. close();
  828. return;
  829. }
  830. int elem = getElement(cursorPosition);
  831. setElement(elem, true);
  832. }
  833. void OptionsTab::SelectionWindow::showPopupWindow(const Point & cursorPosition)
  834. {
  835. if(!pos.isInside(cursorPosition))
  836. return;
  837. int elem = getElement(cursorPosition);
  838. setElement(elem, false);
  839. }
  840. OptionsTab::SelectedBox::SelectedBox(Point position, PlayerSettings & playerSettings, SelType type)
  841. : Scrollable(LCLICK | SHOW_POPUP, position, Orientation::HORIZONTAL)
  842. , CPlayerSettingsHelper(playerSettings, type)
  843. {
  844. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  845. image = std::make_shared<CAnimImage>(getImageName(), getImageIndex());
  846. subtitle = std::make_shared<CLabel>(23, 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, getName());
  847. pos = image->pos;
  848. setPanningStep(pos.w);
  849. }
  850. void OptionsTab::SelectedBox::update()
  851. {
  852. image->setFrame(getImageIndex());
  853. subtitle->setText(getName());
  854. }
  855. void OptionsTab::SelectedBox::showPopupWindow(const Point & cursorPosition)
  856. {
  857. // cases when we do not need to display a message
  858. if(playerSettings.castle == FactionID::NONE && CPlayerSettingsHelper::selectionType == TOWN)
  859. return;
  860. if(playerSettings.hero == HeroTypeID::NONE && !SEL->getPlayerInfo(playerSettings.color).hasCustomMainHero() && CPlayerSettingsHelper::selectionType == HERO)
  861. return;
  862. if(settings["general"]["enableUiEnhancements"].Bool() && CPlayerSettingsHelper::selectionType == HERO && playerSettings.hero.isValid() && playerSettings.heroNameTextId.empty())
  863. GH.windows().createAndPushWindow<CHeroOverview>(playerSettings.hero);
  864. else
  865. GH.windows().createAndPushWindow<CPlayerOptionTooltipBox>(*this);
  866. }
  867. void OptionsTab::SelectedBox::clickReleased(const Point & cursorPosition)
  868. {
  869. if(SEL->screenType != ESelectionScreen::newGame)
  870. return;
  871. PlayerInfo pi = SEL->getPlayerInfo(playerSettings.color);
  872. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(playerSettings.color);
  873. if(selectionType == SelType::TOWN && ((pi.allowedFactions.size() < 2 && !pi.isFactionRandom) || foreignPlayer))
  874. return;
  875. if(selectionType == SelType::HERO && ((pi.defaultHero() == HeroTypeID::NONE || !playerSettings.castle.isValid() || foreignPlayer)))
  876. return;
  877. if(selectionType == SelType::BONUS && foreignPlayer)
  878. return;
  879. GH.input().hapticFeedback();
  880. GH.windows().createAndPushWindow<SelectionWindow>(playerSettings.color, selectionType);
  881. }
  882. void OptionsTab::SelectedBox::scrollBy(int distance)
  883. {
  884. // FIXME: currently options tab is completely recreacted from scratch whenever we receive any information from server
  885. // because of that, panning event gets interrupted (due to destruction of element)
  886. // so, currently, gesture will always move selection only by 1, and then wait for recreation from server info
  887. distance = std::clamp(distance, -1, 1);
  888. switch(CPlayerSettingsHelper::selectionType)
  889. {
  890. case TOWN:
  891. CSH->setPlayerOption(LobbyChangePlayerOption::TOWN, distance, playerSettings.color);
  892. break;
  893. case HERO:
  894. CSH->setPlayerOption(LobbyChangePlayerOption::HERO, distance, playerSettings.color);
  895. break;
  896. case BONUS:
  897. CSH->setPlayerOption(LobbyChangePlayerOption::BONUS, distance, playerSettings.color);
  898. break;
  899. }
  900. setScrollingEnabled(false);
  901. }
  902. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry(const PlayerSettings & S, const OptionsTab & parent)
  903. : CIntObject(LCLICK | KEYBOARD | TEXTINPUT)
  904. , pi(std::make_unique<PlayerInfo>(SEL->getPlayerInfo(S.color)))
  905. , s(std::make_unique<PlayerSettings>(S))
  906. , parentTab(parent)
  907. , name(S.name)
  908. {
  909. OBJ_CONSTRUCTION;
  910. defActions |= SHARE_POS;
  911. int serial = 0;
  912. for(PlayerColor g = PlayerColor(0); g < s->color; ++g)
  913. {
  914. auto itred = SEL->getPlayerInfo(g);
  915. if(itred.canComputerPlay || itred.canHumanPlay)
  916. serial++;
  917. }
  918. pos.x += 54;
  919. pos.y += 128 + serial * 50;
  920. assert(CSH->mi && CSH->mi->mapHeader);
  921. const PlayerInfo & p = SEL->getPlayerInfo(s->color);
  922. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  923. if(p.canHumanPlay && p.canComputerPlay)
  924. whoCanPlay = HUMAN_OR_CPU;
  925. else if(p.canComputerPlay)
  926. whoCanPlay = CPU;
  927. else
  928. whoCanPlay = HUMAN;
  929. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> flags =
  930. {{
  931. "AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  932. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"
  933. }};
  934. static const std::array<std::string, PlayerColor::PLAYER_LIMIT_I> bgs =
  935. {{
  936. "ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  937. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"
  938. }};
  939. background = std::make_shared<CPicture>(ImagePath::builtin(bgs[s->color]), 0, 0);
  940. if(s->isControlledByAI() || CSH->isGuest())
  941. labelPlayerName = std::make_shared<CLabel>(55, 10, EFonts::FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, name);
  942. else
  943. {
  944. labelPlayerNameEdit = std::make_shared<CTextInput>(Rect(6, 3, 95, 15), EFonts::FONT_SMALL, nullptr, false);
  945. labelPlayerNameEdit->setText(name);
  946. }
  947. 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]);
  948. if(SEL->screenType == ESelectionScreen::newGame)
  949. {
  950. 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));
  951. 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));
  952. 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));
  953. 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));
  954. 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));
  955. 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));
  956. }
  957. hideUnavailableButtons();
  958. if(SEL->screenType != ESelectionScreen::scenarioInfo && SEL->getPlayerInfo(s->color).canHumanPlay)
  959. {
  960. flag = std::make_shared<CButton>(
  961. Point(-43, 2),
  962. AnimationPath::builtin(flags[s->color.getNum()]),
  963. CGI->generaltexth->zelp[180],
  964. std::bind(&OptionsTab::onSetPlayerClicked, &parentTab, *s)
  965. );
  966. flag->hoverable = true;
  967. flag->block(CSH->isGuest());
  968. }
  969. else
  970. flag = nullptr;
  971. town = std::make_shared<SelectedBox>(Point(119, 2), *s, TOWN);
  972. hero = std::make_shared<SelectedBox>(Point(195, 2), *s, HERO);
  973. bonus = std::make_shared<SelectedBox>(Point(271, 2), *s, BONUS);
  974. }
  975. bool OptionsTab::PlayerOptionsEntry::captureThisKey(EShortcut key)
  976. {
  977. return labelPlayerNameEdit && labelPlayerNameEdit->hasFocus() && key == EShortcut::GLOBAL_ACCEPT;
  978. }
  979. void OptionsTab::PlayerOptionsEntry::keyPressed(EShortcut key)
  980. {
  981. if(labelPlayerNameEdit && key == EShortcut::GLOBAL_ACCEPT)
  982. updateName();
  983. }
  984. bool OptionsTab::PlayerOptionsEntry::receiveEvent(const Point & position, int eventType) const
  985. {
  986. return eventType == AEventsReceiver::LCLICK; // capture all left clicks (not only within control)
  987. }
  988. void OptionsTab::PlayerOptionsEntry::clickReleased(const Point & cursorPosition)
  989. {
  990. if(labelPlayerNameEdit && !labelPlayerNameEdit->pos.isInside(cursorPosition))
  991. updateName();
  992. }
  993. void OptionsTab::PlayerOptionsEntry::updateName() {
  994. if(labelPlayerNameEdit->getText() != name)
  995. {
  996. CSH->setPlayerName(s->color, labelPlayerNameEdit->getText());
  997. if(CSH->isMyColor(s->color))
  998. {
  999. Settings set = settings.write["general"]["playerName"];
  1000. set->String() = labelPlayerNameEdit->getText();
  1001. }
  1002. }
  1003. labelPlayerNameEdit->removeFocus();
  1004. name = labelPlayerNameEdit->getText();
  1005. }
  1006. void OptionsTab::onSetPlayerClicked(const PlayerSettings & ps) const
  1007. {
  1008. if(ps.isControlledByAI() || humanPlayers > 0)
  1009. CSH->setPlayer(ps.color);
  1010. }
  1011. void OptionsTab::PlayerOptionsEntry::hideUnavailableButtons()
  1012. {
  1013. if(!buttonTownLeft)
  1014. return;
  1015. const bool foreignPlayer = CSH->isGuest() && !CSH->isMyColor(s->color);
  1016. if((pi->allowedFactions.size() < 2 && !pi->isFactionRandom) || foreignPlayer)
  1017. {
  1018. buttonTownLeft->disable();
  1019. buttonTownRight->disable();
  1020. }
  1021. else
  1022. {
  1023. buttonTownLeft->enable();
  1024. buttonTownRight->enable();
  1025. }
  1026. if((pi->defaultHero() != HeroTypeID::RANDOM || !s->castle.isValid()) //fixed hero
  1027. || foreignPlayer) //or not our player
  1028. {
  1029. buttonHeroLeft->disable();
  1030. buttonHeroRight->disable();
  1031. }
  1032. else
  1033. {
  1034. buttonHeroLeft->enable();
  1035. buttonHeroRight->enable();
  1036. }
  1037. if(foreignPlayer)
  1038. {
  1039. buttonBonusLeft->disable();
  1040. buttonBonusRight->disable();
  1041. }
  1042. else
  1043. {
  1044. buttonBonusLeft->enable();
  1045. buttonBonusRight->enable();
  1046. }
  1047. }