2
0

CSelectionBase.cpp 20 KB

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