CSelectionBase.cpp 20 KB

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