CSelectionBase.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * CSelectionBase.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 "CSelectionBase.h"
  12. #include "CBonusSelection.h"
  13. #include "CLobbyScreen.h"
  14. #include "OptionsTab.h"
  15. #include "RandomMapTab.h"
  16. #include "SelectionTab.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../CServerHandler.h"
  19. #include "../GameEngine.h"
  20. #include "../GameInstance.h"
  21. #include "../gui/Shortcut.h"
  22. #include "../gui/WindowHandler.h"
  23. #include "../globalLobby/GlobalLobbyClient.h"
  24. #include "../mainmenu/CMainMenu.h"
  25. #include "../media/ISoundPlayer.h"
  26. #include "../widgets/Buttons.h"
  27. #include "../widgets/CComponent.h"
  28. #include "../widgets/CTextInput.h"
  29. #include "../widgets/GraphicalPrimitiveCanvas.h"
  30. #include "../widgets/Images.h"
  31. #include "../widgets/MiscWidgets.h"
  32. #include "../widgets/ObjectLists.h"
  33. #include "../widgets/Slider.h"
  34. #include "../widgets/TextControls.h"
  35. #include "../windows/GUIClasses.h"
  36. #include "../windows/InfoWindows.h"
  37. #include "../render/CAnimation.h"
  38. #include "../render/Graphics.h"
  39. #include "../render/IFont.h"
  40. #include "../render/IRenderHandler.h"
  41. #include "../../lib/CRandomGenerator.h"
  42. #include "../../lib/CThreadHelper.h"
  43. #include "../../lib/filesystem/Filesystem.h"
  44. #include "../../lib/mapping/CMapHeader.h"
  45. #include "../../lib/mapping/CMapInfo.h"
  46. #include "../../lib/networkPacks/PacksForLobby.h"
  47. #include "../../lib/texts/CGeneralTextHandler.h"
  48. #include "../../lib/entities/faction/CFaction.h"
  49. #include "../../lib/entities/faction/CTown.h"
  50. #include "../../lib/entities/faction/CTownHandler.h"
  51. #include "../../lib/GameLibrary.h"
  52. ISelectionScreenInfo::ISelectionScreenInfo(ESelectionScreen ScreenType)
  53. : screenType(ScreenType)
  54. {
  55. assert(!SEL);
  56. SEL = this;
  57. }
  58. ISelectionScreenInfo::~ISelectionScreenInfo()
  59. {
  60. assert(SEL == this);
  61. SEL = nullptr;
  62. }
  63. int ISelectionScreenInfo::getCurrentDifficulty()
  64. {
  65. return getStartInfo()->difficulty;
  66. }
  67. PlayerInfo ISelectionScreenInfo::getPlayerInfo(PlayerColor color)
  68. {
  69. auto mapInfo = getMapInfo();
  70. if (!mapInfo)
  71. throw std::runtime_error("Attempt to get player info for invalid map!");
  72. if (!mapInfo->mapHeader)
  73. throw std::runtime_error("Attempt to get player info for invalid map header!");
  74. return mapInfo->mapHeader->players.at(color.getNum());
  75. }
  76. CSelectionBase::CSelectionBase(ESelectionScreen type)
  77. : CWindowObject(BORDERED | SHADOW_DISABLED), ISelectionScreenInfo(type)
  78. {
  79. OBJECT_CONSTRUCTION;
  80. pos.w = 762;
  81. pos.h = 584;
  82. if(screenType == ESelectionScreen::campaignList)
  83. {
  84. setBackground(ImagePath::builtin("CamCust.bmp"));
  85. pos = background->center();
  86. }
  87. else
  88. {
  89. const JsonNode& gameSelectConfig = CMainMenuConfig::get().getConfig()["scenario-selection"];
  90. const JsonVector* bgNames = nullptr;
  91. if (!gameSelectConfig.isStruct())
  92. bgNames = &CMainMenuConfig::get().getConfig()["game-select"].Vector(); // Fallback for 1.6 mods
  93. else
  94. bgNames = &gameSelectConfig["background"].Vector();
  95. setBackground(ImagePath::fromJson(*RandomGeneratorUtil::nextItem(*bgNames, CRandomGenerator::getDefault())));
  96. pos = background->center();
  97. for (const JsonNode& node : gameSelectConfig["images"].Vector())
  98. {
  99. auto image = std::make_shared<CPicture>(ImagePath::fromJson(*RandomGeneratorUtil::nextItem(node["name"].Vector(), CRandomGenerator::getDefault())), Point(node["x"].Integer(), node["y"].Integer()));
  100. images.push_back(image);
  101. }
  102. }
  103. card = std::make_shared<InfoCard>();
  104. buttonBack = std::make_shared<CButton>(Point(581, 535), AnimationPath::builtin("SCNRBACK.DEF"), LIBRARY->generaltexth->zelp[105], [this](){ close();}, EShortcut::GLOBAL_CANCEL);
  105. }
  106. void CSelectionBase::toggleTab(std::shared_ptr<CIntObject> tab)
  107. {
  108. if(curTab && curTab->isActive())
  109. {
  110. curTab->disable();
  111. }
  112. if(curTab != tab)
  113. {
  114. tab->enable();
  115. curTab = tab;
  116. }
  117. else
  118. {
  119. curTab.reset();
  120. }
  121. if(tabSel->showRandom && tab != tabOpt)
  122. {
  123. tabSel->curFolder = "";
  124. tabSel->showRandom = false;
  125. tabSel->filter(0, true);
  126. }
  127. ENGINE->windows().totalRedraw();
  128. }
  129. InfoCard::InfoCard()
  130. : showChat(true)
  131. {
  132. OBJECT_CONSTRUCTION;
  133. setRedrawParent(true);
  134. pos.x += 393;
  135. pos.y += 6;
  136. labelSaveDate = std::make_shared<CLabel>(310, 38, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE);
  137. labelMapSize = std::make_shared<CLabel>(333, 56, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE);
  138. mapName = std::make_shared<CLabel>(26, 39, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW, "", SEL->screenType == ESelectionScreen::campaignList ? 325 : 285);
  139. Rect descriptionRect(26, 149, 320, 115);
  140. mapDescription = std::make_shared<CTextBox>("", descriptionRect, 1);
  141. playerListBg = std::make_shared<CPicture>(ImagePath::builtin("CHATPLUG.bmp"), 16, 276);
  142. chat = std::make_shared<CChatBox>(Rect(18, 126, 335, 143));
  143. pvpBox = std::make_shared<PvPBox>(Rect(17, 396, 338, 105));
  144. buttonInvitePlayers = std::make_shared<CButton>(Point(20, 365), AnimationPath::builtin("pregameInvitePlayers"), LIBRARY->generaltexth->zelp[105], [](){ GAME->server().getGlobalLobby().activateRoomInviteInterface(); }, EShortcut::LOBBY_INVITE_PLAYERS );
  145. buttonOpenGlobalLobby = std::make_shared<CButton>(Point(188, 365), AnimationPath::builtin("pregameReturnToLobby"), LIBRARY->generaltexth->zelp[105], [](){ GAME->server().getGlobalLobby().activateInterface(); }, EShortcut::MAIN_MENU_LOBBY );
  146. buttonInvitePlayers->setTextOverlay (MetaString::createFromTextID("vcmi.lobby.invite.header").toString(), EFonts::FONT_SMALL, Colors::WHITE);
  147. buttonOpenGlobalLobby->setTextOverlay(MetaString::createFromTextID("vcmi.lobby.backToLobby").toString(), EFonts::FONT_SMALL, Colors::WHITE);
  148. if(SEL->screenType == ESelectionScreen::campaignList)
  149. {
  150. labelCampaignDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[38]);
  151. }
  152. else
  153. {
  154. background = std::make_shared<CPicture>(ImagePath::builtin("GSELPOP1.bmp"), 0, 0);
  155. parent->addChild(background.get());
  156. auto it = vstd::find(parent->children, this); //our position among parent children
  157. parent->children.insert(it, background.get()); //put BG before us
  158. parent->children.pop_back();
  159. pos.w = background->pos.w;
  160. pos.h = background->pos.h;
  161. iconsMapSizes = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRMPSZ"), 4, 0, 318, 22); //let it be custom size (frame 4) by default
  162. iconDifficulty = std::make_shared<CToggleGroup>(0);
  163. {
  164. constexpr std::array difButns = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  165. for(int i = 0; i < 5; i++)
  166. {
  167. auto button = std::make_shared<CToggleButton>(Point(110 + i * 32, 450), AnimationPath::builtin(difButns[i]), LIBRARY->generaltexth->zelp[24 + i]);
  168. iconDifficulty->addToggle(i, button);
  169. if(SEL->screenType != ESelectionScreen::newGame)
  170. button->block(true);
  171. }
  172. }
  173. flagbox = std::make_shared<CFlagBox>(Rect(24, 400, 335, 23));
  174. labelMapDiff = std::make_shared<CLabel>(33, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[494]);
  175. labelPlayerDifficulty = std::make_shared<CLabel>(133, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[492] + ":");
  176. labelRating = std::make_shared<CLabel>(290, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[218] + ":");
  177. labelScenarioName = std::make_shared<CLabel>(26, 22, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[495]);
  178. labelScenarioDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[496]);
  179. labelVictoryCondition = std::make_shared<CLabel>(26, 283, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[497]);
  180. labelLossCondition = std::make_shared<CLabel>(26, 339, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, LIBRARY->generaltexth->allTexts[498]);
  181. iconsVictoryCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRVICT"), 0, 0, 24, 302);
  182. iconsLossCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRLOSS"), 0, 0, 24, 359);
  183. labelVictoryConditionText = std::make_shared<CLabel>(60, 307, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, "", 290);
  184. labelLossConditionText = std::make_shared<CLabel>(60, 366, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, "", 290);
  185. labelDifficulty = std::make_shared<CLabel>(62, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  186. labelDifficultyPercent = std::make_shared<CLabel>(311, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  187. labelGroupPlayers = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  188. disableLabelRedraws();
  189. }
  190. setChat(false);
  191. if (GAME->server().inLobbyRoom())
  192. setChat(true); // FIXME: less ugly version?
  193. }
  194. void InfoCard::disableLabelRedraws()
  195. {
  196. labelSaveDate->setAutoRedraw(false);
  197. labelMapSize->setAutoRedraw(false);
  198. mapName->setAutoRedraw(false);
  199. mapDescription->label->setAutoRedraw(false);
  200. labelVictoryConditionText->setAutoRedraw(false);
  201. labelLossConditionText->setAutoRedraw(false);
  202. labelDifficulty->setAutoRedraw(false);
  203. labelDifficultyPercent->setAutoRedraw(false);
  204. }
  205. void InfoCard::changeSelection()
  206. {
  207. auto mapInfo = SEL->getMapInfo();
  208. if(!mapInfo)
  209. return;
  210. labelSaveDate->setText(mapInfo->date);
  211. mapName->setText(mapInfo->getNameTranslated());
  212. mapDescription->setText(mapInfo->getDescriptionTranslated());
  213. mapDescription->label->scrollTextTo(0, false);
  214. if(mapDescription->slider)
  215. mapDescription->slider->scrollToMin();
  216. if(SEL->screenType == ESelectionScreen::campaignList)
  217. return;
  218. const CMapHeader * header = mapInfo->mapHeader.get();
  219. labelMapSize->setText(std::to_string(header->width) + "x" + std::to_string(header->height));
  220. iconsMapSizes->setFrame(mapInfo->getMapSizeIconId());
  221. iconsVictoryCondition->setFrame(header->victoryIconIndex);
  222. labelVictoryConditionText->setText(header->victoryMessage.toString());
  223. iconsLossCondition->setFrame(header->defeatIconIndex);
  224. labelLossConditionText->setText(header->defeatMessage.toString());
  225. flagbox->recreate();
  226. labelDifficulty->setText(LIBRARY->generaltexth->arraytxt[142 + vstd::to_underlying(mapInfo->mapHeader->difficulty)]);
  227. iconDifficulty->setSelected(SEL->getCurrentDifficulty());
  228. if(SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::saveGame)
  229. for(auto & button : iconDifficulty->buttons)
  230. button.second->setEnabled(button.first == SEL->getCurrentDifficulty());
  231. const std::array<std::string, 5> difficultyPercent = {"80%", "100%", "130%", "160%", "200%"};
  232. labelDifficultyPercent->setText(difficultyPercent[SEL->getCurrentDifficulty()]);
  233. OBJECT_CONSTRUCTION;
  234. // FIXME: We recreate them each time because CLabelGroup don't use smart pointers
  235. labelGroupPlayers = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  236. if(!showChat)
  237. labelGroupPlayers->disable();
  238. const auto & font = ENGINE->renderHandler().loadFont(FONT_SMALL);
  239. for(const auto & p : GAME->server().playerNames)
  240. {
  241. int slotsUsed = labelGroupPlayers->currentSize();
  242. Point labelPosition;
  243. if(slotsUsed < 4)
  244. labelPosition = Point(24, 285 + slotsUsed * font->getLineHeight()); // left column
  245. else
  246. labelPosition = Point(193, 285 + (slotsUsed - 4) * font->getLineHeight()); // right column
  247. labelGroupPlayers->add(labelPosition.x, labelPosition.y, p.second.name);
  248. }
  249. }
  250. void InfoCard::toggleChat()
  251. {
  252. setChat(!showChat);
  253. }
  254. void InfoCard::setChat(bool activateChat)
  255. {
  256. if(showChat == activateChat)
  257. return;
  258. if(activateChat)
  259. {
  260. if(SEL->screenType == ESelectionScreen::campaignList)
  261. {
  262. labelCampaignDescription->disable();
  263. }
  264. else
  265. {
  266. labelScenarioDescription->disable();
  267. labelVictoryCondition->disable();
  268. labelLossCondition->disable();
  269. iconsVictoryCondition->disable();
  270. labelVictoryConditionText->disable();
  271. iconsLossCondition->disable();
  272. labelLossConditionText->disable();
  273. labelGroupPlayers->enable();
  274. labelMapDiff->disable();
  275. labelPlayerDifficulty->disable();
  276. labelRating->disable();
  277. labelDifficulty->disable();
  278. labelDifficultyPercent->disable();
  279. flagbox->disable();
  280. iconDifficulty->disable();
  281. mapDescription->disable();
  282. chat->enable();
  283. pvpBox->enable();
  284. playerListBg->enable();
  285. }
  286. if (GAME->server().inLobbyRoom())
  287. {
  288. buttonInvitePlayers->enable();
  289. buttonOpenGlobalLobby->enable();
  290. }
  291. }
  292. else
  293. {
  294. buttonInvitePlayers->disable();
  295. buttonOpenGlobalLobby->disable();
  296. mapDescription->enable();
  297. chat->disable();
  298. pvpBox->disable();
  299. playerListBg->disable();
  300. if(SEL->screenType == ESelectionScreen::campaignList)
  301. {
  302. labelCampaignDescription->enable();
  303. }
  304. else
  305. {
  306. labelScenarioDescription->enable();
  307. labelVictoryCondition->enable();
  308. labelLossCondition->enable();
  309. iconsVictoryCondition->enable();
  310. iconsLossCondition->enable();
  311. labelVictoryConditionText->enable();
  312. labelLossConditionText->enable();
  313. labelGroupPlayers->disable();
  314. labelMapDiff->enable();
  315. labelPlayerDifficulty->enable();
  316. labelRating->enable();
  317. labelDifficulty->enable();
  318. labelDifficultyPercent->enable();
  319. flagbox->enable();
  320. iconDifficulty->enable();
  321. }
  322. }
  323. showChat = activateChat;
  324. ENGINE->windows().totalRedraw();
  325. }
  326. CChatBox::CChatBox(const Rect & rect)
  327. : CIntObject(KEYBOARD | TEXTINPUT)
  328. {
  329. OBJECT_CONSTRUCTION;
  330. pos += rect.topLeft();
  331. setRedrawParent(true);
  332. const auto & font = ENGINE->renderHandler().loadFont(FONT_SMALL);
  333. const int height = font->getLineHeight();
  334. Rect textInputArea(1, rect.h - height, rect.w - 1, height);
  335. Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1);
  336. inputBackground = std::make_shared<TransparentFilledRectangle>(textInputArea, ColorRGBA(0,0,0,192));
  337. inputBox = std::make_shared<CTextInput>(textInputArea, EFonts::FONT_SMALL, ETextAlignment::CENTERLEFT, true);
  338. inputBox->removeUsedEvents(KEYBOARD);
  339. chatHistory = std::make_shared<CTextBox>("", chatHistoryArea, 1);
  340. chatHistory->label->color = Colors::GREEN;
  341. }
  342. bool CChatBox::captureThisKey(EShortcut key)
  343. {
  344. return !inputBox->getText().empty() && key == EShortcut::GLOBAL_ACCEPT;
  345. }
  346. void CChatBox::keyPressed(EShortcut key)
  347. {
  348. if(key == EShortcut::GLOBAL_ACCEPT && inputBox->getText().size())
  349. {
  350. GAME->server().sendMessage(inputBox->getText());
  351. inputBox->setText("");
  352. }
  353. else
  354. inputBox->keyPressed(key);
  355. }
  356. void CChatBox::addNewMessage(const std::string & text)
  357. {
  358. ENGINE->sound().playSound(AudioPath::builtin("CHAT"));
  359. chatHistory->setText(chatHistory->label->getText() + text + "\n");
  360. if(chatHistory->slider)
  361. chatHistory->slider->scrollToMax();
  362. }
  363. PvPBox::PvPBox(const Rect & rect)
  364. {
  365. OBJECT_CONSTRUCTION;
  366. pos += rect.topLeft();
  367. setRedrawParent(true);
  368. backgroundTexture = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, rect.w, rect.h));
  369. backgroundTexture->setPlayerColor(PlayerColor(1));
  370. backgroundBorder = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, rect.w, rect.h), ColorRGBA(0, 0, 0, 64), ColorRGBA(96, 96, 96, 255), 1);
  371. townSelector = std::make_shared<TownSelector>(Point(5, 3));
  372. auto getBannedTowns = [this](){
  373. std::vector<FactionID> bannedTowns;
  374. for(const auto & town : townSelector->townsEnabled)
  375. if(!town.second)
  376. bannedTowns.push_back(town.first);
  377. return bannedTowns;
  378. };
  379. buttonFlipCoin = std::make_shared<CButton>(Point(190, 6), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("", LIBRARY->generaltexth->translate("vcmi.lobby.pvp.coin.help")), [](){
  380. LobbyPvPAction lpa;
  381. lpa.action = LobbyPvPAction::COIN;
  382. GAME->server().sendLobbyPack(lpa);
  383. }, EShortcut::LOBBY_FLIP_COIN);
  384. buttonFlipCoin->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.pvp.coin.hover"), EFonts::FONT_SMALL, Colors::WHITE);
  385. buttonRandomTown = std::make_shared<CButton>(Point(190, 31), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("", LIBRARY->generaltexth->translate("vcmi.lobby.pvp.randomTown.help")), [getBannedTowns](){
  386. LobbyPvPAction lpa;
  387. lpa.action = LobbyPvPAction::RANDOM_TOWN;
  388. lpa.bannedTowns = getBannedTowns();
  389. GAME->server().sendLobbyPack(lpa);
  390. }, EShortcut::LOBBY_RANDOM_TOWN);
  391. buttonRandomTown->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.pvp.randomTown.hover"), EFonts::FONT_SMALL, Colors::WHITE);
  392. buttonRandomTownVs = std::make_shared<CButton>(Point(190, 56), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("", LIBRARY->generaltexth->translate("vcmi.lobby.pvp.randomTownVs.help")), [getBannedTowns](){
  393. LobbyPvPAction lpa;
  394. lpa.action = LobbyPvPAction::RANDOM_TOWN_VS;
  395. lpa.bannedTowns = getBannedTowns();
  396. GAME->server().sendLobbyPack(lpa);
  397. }, EShortcut::LOBBY_RANDOM_TOWN_VS);
  398. buttonRandomTownVs->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.pvp.randomTownVs.hover"), EFonts::FONT_SMALL, Colors::WHITE);
  399. buttonHandicap = std::make_shared<CButton>(Point(190, 81), AnimationPath::builtin("GSPBUT2.DEF"), CButton::tooltip("", LIBRARY->generaltexth->translate("vcmi.lobby.handicap")), [](){
  400. if(!GAME->server().isHost())
  401. return;
  402. ENGINE->windows().createAndPushWindow<OptionsTab::HandicapWindow>();
  403. }, EShortcut::LOBBY_HANDICAP);
  404. buttonHandicap->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.lobby.handicap"), EFonts::FONT_SMALL, Colors::WHITE);
  405. }
  406. TownSelector::TownSelector(const Point & loc)
  407. {
  408. OBJECT_CONSTRUCTION;
  409. pos += loc;
  410. setRedrawParent(true);
  411. int count = 0;
  412. for(auto const & factionID : LIBRARY->townh->getDefaultAllowed())
  413. {
  414. townsEnabled[factionID] = true;
  415. count++;
  416. }
  417. auto divisionRoundUp = [](int x, int y){ return (x + (y - 1)) / y; };
  418. if(count > 9)
  419. {
  420. slider = std::make_shared<CSlider>(Point(144, 0), 96, std::bind(&TownSelector::sliderMove, this, _1), 3, divisionRoundUp(count, 3), 0, Orientation::VERTICAL, CSlider::BLUE);
  421. slider->setPanningStep(24);
  422. slider->setScrollBounds(Rect(-144, 0, slider->pos.x - pos.x + slider->pos.w, slider->pos.h));
  423. }
  424. updateListItems();
  425. }
  426. void TownSelector::updateListItems()
  427. {
  428. OBJECT_CONSTRUCTION;
  429. int line = slider ? slider->getValue() : 0;
  430. int x_offset = slider ? 0 : 8;
  431. towns.clear();
  432. townsArea.clear();
  433. int x = 0;
  434. int y = 0;
  435. for (auto const & factionID : LIBRARY->townh->getDefaultAllowed())
  436. {
  437. if(y >= line && (y - line) < 3)
  438. {
  439. auto getImageIndex = [](FactionID targetFactionID, bool enabled){ return targetFactionID.toFaction()->town->clientInfo.icons[true][!enabled] + 2; };
  440. towns[factionID] = std::make_shared<CAnimImage>(AnimationPath::builtin("ITPA"), getImageIndex(factionID, townsEnabled[factionID]), 0, x_offset + 48 * x, 32 * (y - line));
  441. townsArea[factionID] = std::make_shared<LRClickableArea>(Rect(x_offset + 48 * x, 32 * (y - line), 48, 32), [this, getImageIndex, factionID](){
  442. townsEnabled[factionID] = !townsEnabled[factionID];
  443. towns[factionID]->setFrame(getImageIndex(factionID, townsEnabled[factionID]));
  444. redraw();
  445. }, [factionID](){ CRClickPopup::createAndPush(factionID.toFaction()->town->faction->getNameTranslated()); });
  446. }
  447. if (x < 2)
  448. x++;
  449. else
  450. {
  451. x = 0;
  452. y++;
  453. }
  454. }
  455. }
  456. void TownSelector::sliderMove(int slidPos)
  457. {
  458. if(!slider)
  459. return; // ignore spurious call when slider is being created
  460. updateListItems();
  461. redraw();
  462. }
  463. CFlagBox::CFlagBox(const Rect & rect)
  464. : CIntObject(SHOW_POPUP)
  465. {
  466. pos += rect.topLeft();
  467. pos.w = rect.w;
  468. pos.h = rect.h;
  469. OBJECT_CONSTRUCTION;
  470. labelAllies = std::make_shared<CLabel>(0, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, LIBRARY->generaltexth->allTexts[390] + ":");
  471. labelEnemies = std::make_shared<CLabel>(133, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, LIBRARY->generaltexth->allTexts[391] + ":");
  472. }
  473. void CFlagBox::recreate()
  474. {
  475. flagsAllies.clear();
  476. flagsEnemies.clear();
  477. OBJECT_CONSTRUCTION;
  478. const int alliesX = 5 + (int)labelAllies->getWidth();
  479. const int enemiesX = 5 + 133 + (int)labelEnemies->getWidth();
  480. for(auto i = GAME->server().si->playerInfos.cbegin(); i != GAME->server().si->playerInfos.cend(); i++)
  481. {
  482. auto flag = std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS.DEF"), i->first.getNum(), 0);
  483. if(i->first == GAME->server().myFirstColor() || GAME->server().getPlayerTeamId(i->first) == GAME->server().getPlayerTeamId(GAME->server().myFirstColor()))
  484. {
  485. flag->moveTo(Point(pos.x + alliesX + (int)flagsAllies.size()*flag->pos.w, pos.y));
  486. flagsAllies.push_back(flag);
  487. }
  488. else
  489. {
  490. flag->moveTo(Point(pos.x + enemiesX + (int)flagsEnemies.size()*flag->pos.w, pos.y));
  491. flagsEnemies.push_back(flag);
  492. }
  493. }
  494. }
  495. void CFlagBox::showPopupWindow(const Point & cursorPosition)
  496. {
  497. if(SEL->getMapInfo())
  498. ENGINE->windows().createAndPushWindow<CFlagBoxTooltipBox>();
  499. }
  500. CFlagBox::CFlagBoxTooltipBox::CFlagBoxTooltipBox()
  501. : CWindowObject(BORDERED | RCLICK_POPUP | SHADOW_DISABLED, ImagePath::builtin("DIBOXBCK"))
  502. {
  503. OBJECT_CONSTRUCTION;
  504. labelTeamAlignment = std::make_shared<CLabel>(128, 30, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, LIBRARY->generaltexth->allTexts[657]);
  505. labelGroupTeams = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  506. std::vector<std::set<PlayerColor>> teams(PlayerColor::PLAYER_LIMIT_I);
  507. for(PlayerColor j(0); j < PlayerColor::PLAYER_LIMIT; j++)
  508. {
  509. if(SEL->getPlayerInfo(j).canHumanPlay || SEL->getPlayerInfo(j).canComputerPlay)
  510. {
  511. teams[SEL->getPlayerInfo(j).team].insert(j);
  512. }
  513. }
  514. auto curIdx = 0;
  515. for(const auto & team : teams)
  516. {
  517. if(team.empty())
  518. continue;
  519. labelGroupTeams->add(128, 65 + 50 * curIdx, boost::str(boost::format(LIBRARY->generaltexth->allTexts[656]) % (curIdx + 1)));
  520. int curx = 128 - 9 * team.size();
  521. for(const auto & player : team)
  522. {
  523. iconsFlags.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS.DEF"), player, 0, curx, 75 + 50 * curIdx));
  524. curx += 18;
  525. }
  526. ++curIdx;
  527. }
  528. pos.w = 256;
  529. pos.h = 90 + 50 * curIdx;
  530. background->scaleTo(Point(pos.w, pos.h));
  531. center();
  532. }