CSelectionBase.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/filesystem/Filesystem.h"
  45. #include "../../lib/mapping/CMapInfo.h"
  46. #include "../../lib/mapping/CMapHeader.h"
  47. #include "../../lib/CRandomGenerator.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(30, 360), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[105], [=](){ CSH->getGlobalLobby().activateRoomInviteInterface(); } );
  126. buttonOpenGlobalLobby = std::make_shared<CButton>(Point(200, 360), AnimationPath::builtin("GSPBUT2.DEF"), CGI->generaltexth->zelp[105], [=](){ CSH->getGlobalLobby().activateInterface(); });
  127. if(SEL->screenType == ESelectionScreen::campaignList)
  128. {
  129. labelCampaignDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[38]);
  130. }
  131. else
  132. {
  133. background = std::make_shared<CPicture>(ImagePath::builtin("GSELPOP1.bmp"), 0, 0);
  134. parent->addChild(background.get());
  135. auto it = vstd::find(parent->children, this); //our position among parent children
  136. parent->children.insert(it, background.get()); //put BG before us
  137. parent->children.pop_back();
  138. pos.w = background->pos.w;
  139. pos.h = background->pos.h;
  140. iconsMapSizes = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRMPSZ"), 4, 0, 318, 22); //let it be custom size (frame 4) by default
  141. iconDifficulty = std::make_shared<CToggleGroup>(0);
  142. {
  143. constexpr std::array difButns = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  144. for(int i = 0; i < 5; i++)
  145. {
  146. auto button = std::make_shared<CToggleButton>(Point(110 + i * 32, 450), AnimationPath::builtin(difButns[i]), CGI->generaltexth->zelp[24 + i]);
  147. iconDifficulty->addToggle(i, button);
  148. if(SEL->screenType != ESelectionScreen::newGame)
  149. button->block(true);
  150. }
  151. }
  152. flagbox = std::make_shared<CFlagBox>(Rect(24, 400, 335, 23));
  153. labelMapDiff = std::make_shared<CLabel>(33, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[494]);
  154. labelPlayerDifficulty = std::make_shared<CLabel>(133, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[492] + ":");
  155. labelRating = std::make_shared<CLabel>(290, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[218] + ":");
  156. labelScenarioName = std::make_shared<CLabel>(26, 22, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[495]);
  157. labelScenarioDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[496]);
  158. labelVictoryCondition = std::make_shared<CLabel>(26, 283, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[497]);
  159. labelLossCondition = std::make_shared<CLabel>(26, 339, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[498]);
  160. iconsVictoryCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRVICT"), 0, 0, 24, 302);
  161. iconsLossCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRLOSS"), 0, 0, 24, 359);
  162. labelVictoryConditionText = std::make_shared<CLabel>(60, 307, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  163. labelLossConditionText = std::make_shared<CLabel>(60, 366, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  164. labelDifficulty = std::make_shared<CLabel>(62, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  165. labelDifficultyPercent = std::make_shared<CLabel>(311, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  166. labelGroupPlayers = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  167. disableLabelRedraws();
  168. }
  169. setChat(false);
  170. }
  171. void InfoCard::disableLabelRedraws()
  172. {
  173. labelSaveDate->setAutoRedraw(false);
  174. labelMapSize->setAutoRedraw(false);
  175. mapName->setAutoRedraw(false);
  176. mapDescription->label->setAutoRedraw(false);
  177. labelVictoryConditionText->setAutoRedraw(false);
  178. labelLossConditionText->setAutoRedraw(false);
  179. labelDifficulty->setAutoRedraw(false);
  180. labelDifficultyPercent->setAutoRedraw(false);
  181. }
  182. void InfoCard::changeSelection()
  183. {
  184. auto mapInfo = SEL->getMapInfo();
  185. if(!mapInfo)
  186. return;
  187. labelSaveDate->setText(mapInfo->date);
  188. mapName->setText(mapInfo->getNameTranslated());
  189. mapDescription->setText(mapInfo->getDescriptionTranslated());
  190. mapDescription->label->scrollTextTo(0, false);
  191. if(mapDescription->slider)
  192. mapDescription->slider->scrollToMin();
  193. if(SEL->screenType == ESelectionScreen::campaignList)
  194. return;
  195. const CMapHeader * header = mapInfo->mapHeader.get();
  196. labelMapSize->setText(std::to_string(header->width) + "x" + std::to_string(header->height));
  197. iconsMapSizes->setFrame(mapInfo->getMapSizeIconId());
  198. iconsVictoryCondition->setFrame(header->victoryIconIndex);
  199. labelVictoryConditionText->setText(header->victoryMessage.toString());
  200. iconsLossCondition->setFrame(header->defeatIconIndex);
  201. labelLossConditionText->setText(header->defeatMessage.toString());
  202. flagbox->recreate();
  203. labelDifficulty->setText(CGI->generaltexth->arraytxt[142 + vstd::to_underlying(mapInfo->mapHeader->difficulty)]);
  204. iconDifficulty->setSelected(SEL->getCurrentDifficulty());
  205. if(SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::saveGame)
  206. for(auto & button : iconDifficulty->buttons)
  207. button.second->setEnabled(button.first == SEL->getCurrentDifficulty());
  208. const std::array<std::string, 5> difficultyPercent = {"80%", "100%", "130%", "160%", "200%"};
  209. labelDifficultyPercent->setText(difficultyPercent[SEL->getCurrentDifficulty()]);
  210. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  211. // FIXME: We recreate them each time because CLabelGroup don't use smart pointers
  212. labelGroupPlayers = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  213. if(!showChat)
  214. labelGroupPlayers->disable();
  215. for(auto & p : CSH->playerNames)
  216. {
  217. int slotsUsed = labelGroupPlayers->currentSize();
  218. Point labelPosition;
  219. if (slotsUsed < 4)
  220. labelPosition = Point(24, 285 + slotsUsed * graphics->fonts[FONT_SMALL]->getLineHeight()); // left column
  221. else
  222. labelPosition = Point(193, 285 + (slotsUsed - 4) * graphics->fonts[FONT_SMALL]->getLineHeight()); // right column
  223. labelGroupPlayers->add(labelPosition.x, labelPosition.y, p.second.name);
  224. }
  225. }
  226. void InfoCard::toggleChat()
  227. {
  228. setChat(!showChat);
  229. }
  230. void InfoCard::setChat(bool activateChat)
  231. {
  232. if(showChat == activateChat)
  233. return;
  234. if(activateChat)
  235. {
  236. if(SEL->screenType == ESelectionScreen::campaignList)
  237. {
  238. labelCampaignDescription->disable();
  239. }
  240. else
  241. {
  242. labelScenarioDescription->disable();
  243. labelVictoryCondition->disable();
  244. labelLossCondition->disable();
  245. iconsVictoryCondition->disable();
  246. labelVictoryConditionText->disable();
  247. iconsLossCondition->disable();
  248. labelLossConditionText->disable();
  249. labelGroupPlayers->enable();
  250. }
  251. if (CSH->inLobbyRoom())
  252. {
  253. buttonInvitePlayers->enable();
  254. buttonOpenGlobalLobby->enable();
  255. }
  256. mapDescription->disable();
  257. chat->enable();
  258. playerListBg->enable();
  259. }
  260. else
  261. {
  262. buttonInvitePlayers->disable();
  263. buttonOpenGlobalLobby->disable();
  264. mapDescription->enable();
  265. chat->disable();
  266. playerListBg->disable();
  267. if(SEL->screenType == ESelectionScreen::campaignList)
  268. {
  269. labelCampaignDescription->enable();
  270. }
  271. else
  272. {
  273. labelScenarioDescription->enable();
  274. labelVictoryCondition->enable();
  275. labelLossCondition->enable();
  276. iconsVictoryCondition->enable();
  277. iconsLossCondition->enable();
  278. labelVictoryConditionText->enable();
  279. labelLossConditionText->enable();
  280. labelGroupPlayers->disable();
  281. }
  282. }
  283. showChat = activateChat;
  284. GH.windows().totalRedraw();
  285. }
  286. CChatBox::CChatBox(const Rect & rect)
  287. : CIntObject(KEYBOARD | TEXTINPUT)
  288. {
  289. OBJ_CONSTRUCTION;
  290. pos += rect.topLeft();
  291. setRedrawParent(true);
  292. const int height = static_cast<int>(graphics->fonts[FONT_SMALL]->getLineHeight());
  293. Rect textInputArea(1, rect.h - height, rect.w - 1, height);
  294. Rect chatHistoryArea(3, 1, rect.w - 3, rect.h - height - 1);
  295. inputBackground = std::make_shared<TransparentFilledRectangle>(textInputArea, ColorRGBA(0,0,0,192));
  296. inputBox = std::make_shared<CTextInput>(textInputArea, EFonts::FONT_SMALL, nullptr, ETextAlignment::TOPLEFT, true);
  297. inputBox->removeUsedEvents(KEYBOARD);
  298. chatHistory = std::make_shared<CTextBox>("", chatHistoryArea, 1);
  299. chatHistory->label->color = Colors::GREEN;
  300. }
  301. bool CChatBox::captureThisKey(EShortcut key)
  302. {
  303. return !inputBox->getText().empty() && key == EShortcut::GLOBAL_ACCEPT;
  304. }
  305. void CChatBox::keyPressed(EShortcut key)
  306. {
  307. if(key == EShortcut::GLOBAL_ACCEPT && inputBox->getText().size())
  308. {
  309. CSH->sendMessage(inputBox->getText());
  310. inputBox->setText("");
  311. }
  312. else
  313. inputBox->keyPressed(key);
  314. }
  315. void CChatBox::addNewMessage(const std::string & text)
  316. {
  317. CCS->soundh->playSound(AudioPath::builtin("CHAT"));
  318. chatHistory->setText(chatHistory->label->getText() + text + "\n");
  319. if(chatHistory->slider)
  320. chatHistory->slider->scrollToMax();
  321. }
  322. CFlagBox::CFlagBox(const Rect & rect)
  323. : CIntObject(SHOW_POPUP)
  324. {
  325. pos += rect.topLeft();
  326. pos.w = rect.w;
  327. pos.h = rect.h;
  328. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  329. labelAllies = std::make_shared<CLabel>(0, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[390] + ":");
  330. labelEnemies = std::make_shared<CLabel>(133, 0, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[391] + ":");
  331. iconsTeamFlags = GH.renderHandler().loadAnimation(AnimationPath::builtin("ITGFLAGS.DEF"));
  332. iconsTeamFlags->preload();
  333. }
  334. void CFlagBox::recreate()
  335. {
  336. flagsAllies.clear();
  337. flagsEnemies.clear();
  338. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  339. const int alliesX = 5 + (int)labelAllies->getWidth();
  340. const int enemiesX = 5 + 133 + (int)labelEnemies->getWidth();
  341. for(auto i = CSH->si->playerInfos.cbegin(); i != CSH->si->playerInfos.cend(); i++)
  342. {
  343. auto flag = std::make_shared<CAnimImage>(iconsTeamFlags, i->first.getNum(), 0);
  344. if(i->first == CSH->myFirstColor() || CSH->getPlayerTeamId(i->first) == CSH->getPlayerTeamId(CSH->myFirstColor()))
  345. {
  346. flag->moveTo(Point(pos.x + alliesX + (int)flagsAllies.size()*flag->pos.w, pos.y));
  347. flagsAllies.push_back(flag);
  348. }
  349. else
  350. {
  351. flag->moveTo(Point(pos.x + enemiesX + (int)flagsEnemies.size()*flag->pos.w, pos.y));
  352. flagsEnemies.push_back(flag);
  353. }
  354. }
  355. }
  356. void CFlagBox::showPopupWindow(const Point & cursorPosition)
  357. {
  358. if(SEL->getMapInfo())
  359. GH.windows().createAndPushWindow<CFlagBoxTooltipBox>(iconsTeamFlags);
  360. }
  361. CFlagBox::CFlagBoxTooltipBox::CFlagBoxTooltipBox(std::shared_ptr<CAnimation> icons)
  362. : CWindowObject(BORDERED | RCLICK_POPUP | SHADOW_DISABLED, ImagePath::builtin("DIBOXBCK"))
  363. {
  364. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  365. labelTeamAlignment = std::make_shared<CLabel>(128, 30, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[657]);
  366. labelGroupTeams = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  367. std::vector<std::set<PlayerColor>> teams(PlayerColor::PLAYER_LIMIT_I);
  368. for(PlayerColor j(0); j < PlayerColor::PLAYER_LIMIT; j++)
  369. {
  370. if(SEL->getPlayerInfo(j).canHumanPlay || SEL->getPlayerInfo(j).canComputerPlay)
  371. {
  372. teams[SEL->getPlayerInfo(j).team].insert(j);
  373. }
  374. }
  375. auto curIdx = 0;
  376. for(const auto & team : teams)
  377. {
  378. if(team.empty())
  379. continue;
  380. labelGroupTeams->add(128, 65 + 50 * curIdx, boost::str(boost::format(CGI->generaltexth->allTexts[656]) % (curIdx + 1)));
  381. int curx = 128 - 9 * team.size();
  382. for(const auto & player : team)
  383. {
  384. iconsFlags.push_back(std::make_shared<CAnimImage>(icons, player, 0, curx, 75 + 50 * curIdx));
  385. curx += 18;
  386. }
  387. ++curIdx;
  388. }
  389. pos.w = 256;
  390. pos.h = 90 + 50 * curIdx;
  391. background->scaleTo(Point(pos.w, pos.h));
  392. center();
  393. }