CSelectionBase.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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/GraphicalPrimitiveCanvas.h"
  31. #include "../widgets/ObjectLists.h"
  32. #include "../widgets/Slider.h"
  33. #include "../widgets/TextControls.h"
  34. #include "../windows/GUIClasses.h"
  35. #include "../windows/InfoWindows.h"
  36. #include "../render/CAnimation.h"
  37. #include "../render/Graphics.h"
  38. #include "../render/IFont.h"
  39. #include "../render/IRenderHandler.h"
  40. #include "../../lib/CGeneralTextHandler.h"
  41. #include "../../lib/CHeroHandler.h"
  42. #include "../../lib/CRandomGenerator.h"
  43. #include "../../lib/CThreadHelper.h"
  44. #include "../../lib/MetaString.h"
  45. #include "../../lib/filesystem/Filesystem.h"
  46. #include "../../lib/mapping/CMapHeader.h"
  47. #include "../../lib/mapping/CMapInfo.h"
  48. ISelectionScreenInfo::ISelectionScreenInfo(ESelectionScreen ScreenType)
  49. : screenType(ScreenType)
  50. {
  51. assert(!SEL);
  52. SEL = this;
  53. }
  54. ISelectionScreenInfo::~ISelectionScreenInfo()
  55. {
  56. assert(SEL == this);
  57. SEL = nullptr;
  58. }
  59. int ISelectionScreenInfo::getCurrentDifficulty()
  60. {
  61. return getStartInfo()->difficulty;
  62. }
  63. PlayerInfo ISelectionScreenInfo::getPlayerInfo(PlayerColor color)
  64. {
  65. return getMapInfo()->mapHeader->players.at(color.getNum());
  66. }
  67. CSelectionBase::CSelectionBase(ESelectionScreen type)
  68. : CWindowObject(BORDERED | SHADOW_DISABLED), ISelectionScreenInfo(type)
  69. {
  70. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  71. pos.w = 762;
  72. pos.h = 584;
  73. if(screenType == ESelectionScreen::campaignList)
  74. {
  75. setBackground(ImagePath::builtin("CamCust.bmp"));
  76. }
  77. else
  78. {
  79. const JsonVector & bgNames = CMainMenuConfig::get().getConfig()["game-select"].Vector();
  80. setBackground(ImagePath::fromJson(*RandomGeneratorUtil::nextItem(bgNames, CRandomGenerator::getDefault())));
  81. }
  82. pos = background->center();
  83. card = std::make_shared<InfoCard>();
  84. buttonBack = std::make_shared<CButton>(Point(581, 535), AnimationPath::builtin("SCNRBACK.DEF"), CGI->generaltexth->zelp[105], [=](){ close();}, EShortcut::GLOBAL_CANCEL);
  85. }
  86. void CSelectionBase::toggleTab(std::shared_ptr<CIntObject> tab)
  87. {
  88. if(curTab && curTab->isActive())
  89. {
  90. curTab->deactivate();
  91. curTab->recActions = 0;
  92. }
  93. if(curTab != tab)
  94. {
  95. tab->recActions = 255 - DISPOSE;
  96. tab->activate();
  97. curTab = tab;
  98. }
  99. else
  100. {
  101. curTab.reset();
  102. }
  103. if(tabSel->showRandom && tab != tabOpt)
  104. {
  105. tabSel->curFolder = "";
  106. tabSel->showRandom = false;
  107. tabSel->filter(0, true);
  108. }
  109. GH.windows().totalRedraw();
  110. }
  111. InfoCard::InfoCard()
  112. : showChat(true)
  113. {
  114. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  115. setRedrawParent(true);
  116. pos.x += 393;
  117. pos.y += 6;
  118. labelSaveDate = std::make_shared<CLabel>(310, 38, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE);
  119. labelMapSize = std::make_shared<CLabel>(333, 56, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE);
  120. mapName = std::make_shared<CLabel>(26, 39, FONT_BIG, ETextAlignment::TOPLEFT, Colors::YELLOW);
  121. Rect descriptionRect(26, 149, 320, 115);
  122. mapDescription = std::make_shared<CTextBox>("", descriptionRect, 1);
  123. playerListBg = std::make_shared<CPicture>(ImagePath::builtin("CHATPLUG.bmp"), 16, 276);
  124. chat = std::make_shared<CChatBox>(Rect(18, 126, 335, 143));
  125. buttonInvitePlayers = std::make_shared<CButton>(Point(20, 365), AnimationPath::builtin("pregameInvitePlayers"), CGI->generaltexth->zelp[105], [](){ CSH->getGlobalLobby().activateRoomInviteInterface(); } );
  126. buttonOpenGlobalLobby = std::make_shared<CButton>(Point(188, 365), AnimationPath::builtin("pregameReturnToLobby"), CGI->generaltexth->zelp[105], [](){ CSH->getGlobalLobby().activateInterface(); });
  127. buttonInvitePlayers->setTextOverlay (MetaString::createFromTextID("vcmi.lobby.invite.header").toString(), EFonts::FONT_SMALL, Colors::WHITE);
  128. buttonOpenGlobalLobby->setTextOverlay(MetaString::createFromTextID("vcmi.lobby.backToLobby").toString(), EFonts::FONT_SMALL, Colors::WHITE);
  129. if(SEL->screenType == ESelectionScreen::campaignList)
  130. {
  131. labelCampaignDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[38]);
  132. }
  133. else
  134. {
  135. background = std::make_shared<CPicture>(ImagePath::builtin("GSELPOP1.bmp"), 0, 0);
  136. parent->addChild(background.get());
  137. auto it = vstd::find(parent->children, this); //our position among parent children
  138. parent->children.insert(it, background.get()); //put BG before us
  139. parent->children.pop_back();
  140. pos.w = background->pos.w;
  141. pos.h = background->pos.h;
  142. iconsMapSizes = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRMPSZ"), 4, 0, 318, 22); //let it be custom size (frame 4) by default
  143. iconDifficulty = std::make_shared<CToggleGroup>(0);
  144. {
  145. constexpr std::array difButns = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  146. for(int i = 0; i < 5; i++)
  147. {
  148. auto button = std::make_shared<CToggleButton>(Point(110 + i * 32, 450), AnimationPath::builtin(difButns[i]), CGI->generaltexth->zelp[24 + i]);
  149. iconDifficulty->addToggle(i, button);
  150. if(SEL->screenType != ESelectionScreen::newGame)
  151. button->block(true);
  152. }
  153. }
  154. flagbox = std::make_shared<CFlagBox>(Rect(24, 400, 335, 23));
  155. labelMapDiff = std::make_shared<CLabel>(33, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[494]);
  156. labelPlayerDifficulty = std::make_shared<CLabel>(133, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[492] + ":");
  157. labelRating = std::make_shared<CLabel>(290, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[218] + ":");
  158. labelScenarioName = std::make_shared<CLabel>(26, 22, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[495]);
  159. labelScenarioDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[496]);
  160. labelVictoryCondition = std::make_shared<CLabel>(26, 283, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[497]);
  161. labelLossCondition = std::make_shared<CLabel>(26, 339, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[498]);
  162. iconsVictoryCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRVICT"), 0, 0, 24, 302);
  163. iconsLossCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRLOSS"), 0, 0, 24, 359);
  164. labelVictoryConditionText = std::make_shared<CLabel>(60, 307, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  165. labelLossConditionText = std::make_shared<CLabel>(60, 366, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  166. labelDifficulty = std::make_shared<CLabel>(62, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  167. labelDifficultyPercent = std::make_shared<CLabel>(311, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  168. labelGroupPlayers = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  169. disableLabelRedraws();
  170. }
  171. setChat(false);
  172. if (CSH->inLobbyRoom())
  173. setChat(true); // FIXME: less ugly version?
  174. }
  175. void InfoCard::disableLabelRedraws()
  176. {
  177. labelSaveDate->setAutoRedraw(false);
  178. labelMapSize->setAutoRedraw(false);
  179. mapName->setAutoRedraw(false);
  180. mapDescription->label->setAutoRedraw(false);
  181. labelVictoryConditionText->setAutoRedraw(false);
  182. labelLossConditionText->setAutoRedraw(false);
  183. labelDifficulty->setAutoRedraw(false);
  184. labelDifficultyPercent->setAutoRedraw(false);
  185. }
  186. void InfoCard::changeSelection()
  187. {
  188. auto mapInfo = SEL->getMapInfo();
  189. if(!mapInfo)
  190. return;
  191. labelSaveDate->setText(mapInfo->date);
  192. mapName->setText(mapInfo->getNameTranslated());
  193. mapDescription->setText(mapInfo->getDescriptionTranslated());
  194. mapDescription->label->scrollTextTo(0, false);
  195. if(mapDescription->slider)
  196. mapDescription->slider->scrollToMin();
  197. if(SEL->screenType == ESelectionScreen::campaignList)
  198. return;
  199. const CMapHeader * header = mapInfo->mapHeader.get();
  200. labelMapSize->setText(std::to_string(header->width) + "x" + std::to_string(header->height));
  201. iconsMapSizes->setFrame(mapInfo->getMapSizeIconId());
  202. iconsVictoryCondition->setFrame(header->victoryIconIndex);
  203. labelVictoryConditionText->setText(header->victoryMessage.toString());
  204. iconsLossCondition->setFrame(header->defeatIconIndex);
  205. labelLossConditionText->setText(header->defeatMessage.toString());
  206. flagbox->recreate();
  207. labelDifficulty->setText(CGI->generaltexth->arraytxt[142 + vstd::to_underlying(mapInfo->mapHeader->difficulty)]);
  208. iconDifficulty->setSelected(SEL->getCurrentDifficulty());
  209. if(SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::saveGame)
  210. for(auto & button : iconDifficulty->buttons)
  211. button.second->setEnabled(button.first == SEL->getCurrentDifficulty());
  212. const std::array<std::string, 5> difficultyPercent = {"80%", "100%", "130%", "160%", "200%"};
  213. labelDifficultyPercent->setText(difficultyPercent[SEL->getCurrentDifficulty()]);
  214. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  215. // FIXME: We recreate them each time because CLabelGroup don't use smart pointers
  216. labelGroupPlayers = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  217. if(!showChat)
  218. labelGroupPlayers->disable();
  219. for(const auto & p : CSH->playerNames)
  220. {
  221. int slotsUsed = labelGroupPlayers->currentSize();
  222. Point labelPosition;
  223. if(slotsUsed < 4)
  224. labelPosition = Point(24, 285 + slotsUsed * graphics->fonts[FONT_SMALL]->getLineHeight()); // left column
  225. else
  226. labelPosition = Point(193, 285 + (slotsUsed - 4) * graphics->fonts[FONT_SMALL]->getLineHeight()); // right column
  227. labelGroupPlayers->add(labelPosition.x, labelPosition.y, p.second.name);
  228. }
  229. }
  230. void InfoCard::toggleChat()
  231. {
  232. setChat(!showChat);
  233. }
  234. void InfoCard::setChat(bool activateChat)
  235. {
  236. if(showChat == activateChat)
  237. return;
  238. if(activateChat)
  239. {
  240. if(SEL->screenType == ESelectionScreen::campaignList)
  241. {
  242. labelCampaignDescription->disable();
  243. }
  244. else
  245. {
  246. labelScenarioDescription->disable();
  247. labelVictoryCondition->disable();
  248. labelLossCondition->disable();
  249. iconsVictoryCondition->disable();
  250. labelVictoryConditionText->disable();
  251. iconsLossCondition->disable();
  252. labelLossConditionText->disable();
  253. labelGroupPlayers->enable();
  254. }
  255. if (CSH->inLobbyRoom())
  256. {
  257. buttonInvitePlayers->enable();
  258. buttonOpenGlobalLobby->enable();
  259. }
  260. mapDescription->disable();
  261. chat->enable();
  262. playerListBg->enable();
  263. }
  264. else
  265. {
  266. buttonInvitePlayers->disable();
  267. buttonOpenGlobalLobby->disable();
  268. mapDescription->enable();
  269. chat->disable();
  270. playerListBg->disable();
  271. if(SEL->screenType == ESelectionScreen::campaignList)
  272. {
  273. labelCampaignDescription->enable();
  274. }
  275. else
  276. {
  277. labelScenarioDescription->enable();
  278. labelVictoryCondition->enable();
  279. labelLossCondition->enable();
  280. iconsVictoryCondition->enable();
  281. iconsLossCondition->enable();
  282. labelVictoryConditionText->enable();
  283. labelLossConditionText->enable();
  284. labelGroupPlayers->disable();
  285. }
  286. }
  287. showChat = activateChat;
  288. GH.windows().totalRedraw();
  289. }
  290. CChatBox::CChatBox(const Rect & rect)
  291. : CIntObject(KEYBOARD | TEXTINPUT)
  292. {
  293. OBJ_CONSTRUCTION;
  294. pos += rect.topLeft();
  295. setRedrawParent(true);
  296. const int height = static_cast<int>(graphics->fonts[FONT_SMALL]->getLineHeight());
  297. Rect textInputArea(1, rect.h - height, rect.w - 1, height);
  298. Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1);
  299. inputBackground = std::make_shared<TransparentFilledRectangle>(textInputArea, ColorRGBA(0,0,0,192));
  300. inputBox = std::make_shared<CTextInput>(textInputArea, EFonts::FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true);
  301. inputBox->removeUsedEvents(KEYBOARD);
  302. chatHistory = std::make_shared<CTextBox>("", chatHistoryArea, 1);
  303. chatHistory->label->color = Colors::GREEN;
  304. }
  305. bool CChatBox::captureThisKey(EShortcut key)
  306. {
  307. return !inputBox->getText().empty() && key == EShortcut::GLOBAL_ACCEPT;
  308. }
  309. void CChatBox::keyPressed(EShortcut key)
  310. {
  311. if(key == EShortcut::GLOBAL_ACCEPT && inputBox->getText().size())
  312. {
  313. CSH->sendMessage(inputBox->getText());
  314. inputBox->setText("");
  315. }
  316. else
  317. inputBox->keyPressed(key);
  318. }
  319. void CChatBox::addNewMessage(const std::string & text)
  320. {
  321. CCS->soundh->playSound(AudioPath::builtin("CHAT"));
  322. chatHistory->setText(chatHistory->label->getText() + text + "\n");
  323. if(chatHistory->slider)
  324. chatHistory->slider->scrollToMax();
  325. }
  326. CFlagBox::CFlagBox(const Rect & rect)
  327. : CIntObject(SHOW_POPUP)
  328. {
  329. pos += rect.topLeft();
  330. pos.w = rect.w;
  331. pos.h = rect.h;
  332. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  333. labelAllies = std::make_shared<CLabel>(0, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  334. labelEnemies = std::make_shared<CLabel>(133, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  335. iconsTeamFlags = GH.renderHandler().loadAnimation(AnimationPath::builtin("ITGFLAGS.DEF"));
  336. iconsTeamFlags->preload();
  337. }
  338. void CFlagBox::recreate()
  339. {
  340. flagsAllies.clear();
  341. flagsEnemies.clear();
  342. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  343. const int alliesX = 5 + (int)labelAllies->getWidth();
  344. const int enemiesX = 5 + 133 + (int)labelEnemies->getWidth();
  345. for(auto i = CSH->si->playerInfos.cbegin(); i != CSH->si->playerInfos.cend(); i++)
  346. {
  347. auto flag = std::make_shared<CAnimImage>(iconsTeamFlags, i->first.getNum(), 0);
  348. if(i->first == CSH->myFirstColor() || CSH->getPlayerTeamId(i->first) == CSH->getPlayerTeamId(CSH->myFirstColor()))
  349. {
  350. flag->moveTo(Point(pos.x + alliesX + (int)flagsAllies.size()*flag->pos.w, pos.y));
  351. flagsAllies.push_back(flag);
  352. }
  353. else
  354. {
  355. flag->moveTo(Point(pos.x + enemiesX + (int)flagsEnemies.size()*flag->pos.w, pos.y));
  356. flagsEnemies.push_back(flag);
  357. }
  358. }
  359. }
  360. void CFlagBox::showPopupWindow(const Point & cursorPosition)
  361. {
  362. if(SEL->getMapInfo())
  363. GH.windows().createAndPushWindow<CFlagBoxTooltipBox>(iconsTeamFlags);
  364. }
  365. CFlagBox::CFlagBoxTooltipBox::CFlagBoxTooltipBox(std::shared_ptr<CAnimation> icons)
  366. : CWindowObject(BORDERED | RCLICK_POPUP | SHADOW_DISABLED, ImagePath::builtin("DIBOXBCK"))
  367. {
  368. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  369. labelTeamAlignment = std::make_shared<CLabel>(128, 30, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[657]);
  370. labelGroupTeams = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  371. std::vector<std::set<PlayerColor>> teams(PlayerColor::PLAYER_LIMIT_I);
  372. for(PlayerColor j(0); j < PlayerColor::PLAYER_LIMIT; j++)
  373. {
  374. if(SEL->getPlayerInfo(j).canHumanPlay || SEL->getPlayerInfo(j).canComputerPlay)
  375. {
  376. teams[SEL->getPlayerInfo(j).team].insert(j);
  377. }
  378. }
  379. auto curIdx = 0;
  380. for(const auto & team : teams)
  381. {
  382. if(team.empty())
  383. continue;
  384. labelGroupTeams->add(128, 65 + 50 * curIdx, boost::str(boost::format(CGI->generaltexth->allTexts[656]) % (curIdx + 1)));
  385. int curx = 128 - 9 * team.size();
  386. for(const auto & player : team)
  387. {
  388. iconsFlags.push_back(std::make_shared<CAnimImage>(icons, player, 0, curx, 75 + 50 * curIdx));
  389. curx += 18;
  390. }
  391. ++curIdx;
  392. }
  393. pos.w = 256;
  394. pos.h = 90 + 50 * curIdx;
  395. background->scaleTo(Point(pos.w, pos.h));
  396. center();
  397. }