CSelectionBase.cpp 21 KB

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