CSelectionBase.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 "../CPlayerInterface.h"
  23. #include "../CServerHandler.h"
  24. #include "../gui/CGuiHandler.h"
  25. #include "../gui/Shortcut.h"
  26. #include "../gui/WindowHandler.h"
  27. #include "../mainmenu/CMainMenu.h"
  28. #include "../widgets/Buttons.h"
  29. #include "../widgets/CComponent.h"
  30. #include "../widgets/MiscWidgets.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/NetPacksLobby.h"
  41. #include "../../lib/CGeneralTextHandler.h"
  42. #include "../../lib/CHeroHandler.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/serializer/Connection.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[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(26, 132, 340, 132));
  125. if(SEL->screenType == ESelectionScreen::campaignList)
  126. {
  127. labelCampaignDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[38]);
  128. }
  129. else
  130. {
  131. background = std::make_shared<CPicture>(ImagePath::builtin("GSELPOP1.bmp"), 0, 0);
  132. parent->addChild(background.get());
  133. auto it = vstd::find(parent->children, this); //our position among parent children
  134. parent->children.insert(it, background.get()); //put BG before us
  135. parent->children.pop_back();
  136. pos.w = background->pos.w;
  137. pos.h = background->pos.h;
  138. iconsMapSizes = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRMPSZ"), 4, 0, 318, 22); //let it be custom size (frame 4) by default
  139. iconDifficulty = std::make_shared<CToggleGroup>(0);
  140. {
  141. static const char * difButns[] = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  142. for(int i = 0; i < 5; i++)
  143. {
  144. auto button = std::make_shared<CToggleButton>(Point(110 + i * 32, 450), AnimationPath::builtin(difButns[i]), CGI->generaltexth->zelp[24 + i]);
  145. iconDifficulty->addToggle(i, button);
  146. if(SEL->screenType != ESelectionScreen::newGame)
  147. button->block(true);
  148. }
  149. }
  150. flagbox = std::make_shared<CFlagBox>(Rect(24, 400, 335, 23));
  151. labelMapDiff = std::make_shared<CLabel>(33, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[494]);
  152. labelPlayerDifficulty = std::make_shared<CLabel>(133, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[492] + ":");
  153. labelRating = std::make_shared<CLabel>(290, 430, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[218] + ":");
  154. labelScenarioName = std::make_shared<CLabel>(26, 22, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[495]);
  155. labelScenarioDescription = std::make_shared<CLabel>(26, 132, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[496]);
  156. labelVictoryCondition = std::make_shared<CLabel>(26, 283, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[497]);
  157. labelLossCondition = std::make_shared<CLabel>(26, 339, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, CGI->generaltexth->allTexts[498]);
  158. iconsVictoryCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRVICT"), 0, 0, 24, 302);
  159. iconsLossCondition = std::make_shared<CAnimImage>(AnimationPath::builtin("SCNRLOSS"), 0, 0, 24, 359);
  160. labelVictoryConditionText = std::make_shared<CLabel>(60, 307, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  161. labelLossConditionText = std::make_shared<CLabel>(60, 366, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  162. labelDifficulty = std::make_shared<CLabel>(62, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  163. labelDifficultyPercent = std::make_shared<CLabel>(311, 472, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  164. labelGroupPlayersAssigned = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  165. labelGroupPlayersUnassigned = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  166. disableLabelRedraws();
  167. }
  168. setChat(false);
  169. }
  170. void InfoCard::disableLabelRedraws()
  171. {
  172. labelSaveDate->setAutoRedraw(false);
  173. labelMapSize->setAutoRedraw(false);
  174. mapName->setAutoRedraw(false);
  175. mapDescription->label->setAutoRedraw(false);
  176. labelVictoryConditionText->setAutoRedraw(false);
  177. labelLossConditionText->setAutoRedraw(false);
  178. labelDifficulty->setAutoRedraw(false);
  179. labelDifficultyPercent->setAutoRedraw(false);
  180. }
  181. void InfoCard::changeSelection()
  182. {
  183. auto mapInfo = SEL->getMapInfo();
  184. if(!mapInfo)
  185. return;
  186. labelSaveDate->setText(mapInfo->date);
  187. labelMapSize->setText(std::to_string(mapInfo->mapHeader->width) + "x" + std::to_string(mapInfo->mapHeader->height));
  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. iconsMapSizes->setFrame(mapInfo->getMapSizeIconId());
  196. const CMapHeader * header = mapInfo->mapHeader.get();
  197. iconsVictoryCondition->setFrame(header->victoryIconIndex);
  198. labelVictoryConditionText->setText(header->victoryMessage.toString());
  199. iconsLossCondition->setFrame(header->defeatIconIndex);
  200. labelLossConditionText->setText(header->defeatMessage.toString());
  201. flagbox->recreate();
  202. labelDifficulty->setText(CGI->generaltexth->arraytxt[142 + mapInfo->mapHeader->difficulty]);
  203. iconDifficulty->setSelected(SEL->getCurrentDifficulty());
  204. if(SEL->screenType == ESelectionScreen::loadGame || SEL->screenType == ESelectionScreen::saveGame)
  205. for(auto & button : iconDifficulty->buttons)
  206. button.second->setEnabled(button.first == SEL->getCurrentDifficulty());
  207. const std::array<std::string, 5> difficultyPercent = {"80%", "100%", "130%", "160%", "200%"};
  208. labelDifficultyPercent->setText(difficultyPercent[SEL->getCurrentDifficulty()]);
  209. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  210. // FIXME: We recreate them each time because CLabelGroup don't use smart pointers
  211. labelGroupPlayersAssigned = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  212. labelGroupPlayersUnassigned = std::make_shared<CLabelGroup>(FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  213. if(!showChat)
  214. {
  215. labelGroupPlayersAssigned->disable();
  216. labelGroupPlayersUnassigned->disable();
  217. }
  218. for(auto & p : CSH->playerNames)
  219. {
  220. const auto pset = CSH->si->getPlayersSettings(p.first);
  221. int pid = p.first;
  222. if(pset)
  223. {
  224. auto name = boost::str(boost::format("%s (%d-%d %s)") % p.second.name % p.second.connection % pid % pset->color.toString());
  225. labelGroupPlayersAssigned->add(24, 285 + (int)labelGroupPlayersAssigned->currentSize()*(int)graphics->fonts[FONT_SMALL]->getLineHeight(), name);
  226. }
  227. else
  228. {
  229. auto name = boost::str(boost::format("%s (%d-%d)") % p.second.name % p.second.connection % pid);
  230. labelGroupPlayersUnassigned->add(193, 285 + (int)labelGroupPlayersUnassigned->currentSize()*(int)graphics->fonts[FONT_SMALL]->getLineHeight(), name);
  231. }
  232. }
  233. }
  234. void InfoCard::toggleChat()
  235. {
  236. setChat(!showChat);
  237. }
  238. void InfoCard::setChat(bool activateChat)
  239. {
  240. if(showChat == activateChat)
  241. return;
  242. if(activateChat)
  243. {
  244. if(SEL->screenType == ESelectionScreen::campaignList)
  245. {
  246. labelCampaignDescription->disable();
  247. }
  248. else
  249. {
  250. labelScenarioDescription->disable();
  251. labelVictoryCondition->disable();
  252. labelLossCondition->disable();
  253. iconsVictoryCondition->disable();
  254. labelVictoryConditionText->disable();
  255. iconsLossCondition->disable();
  256. labelLossConditionText->disable();
  257. labelGroupPlayersAssigned->enable();
  258. labelGroupPlayersUnassigned->enable();
  259. }
  260. mapDescription->disable();
  261. chat->enable();
  262. playerListBg->enable();
  263. }
  264. else
  265. {
  266. mapDescription->enable();
  267. chat->disable();
  268. playerListBg->disable();
  269. if(SEL->screenType == ESelectionScreen::campaignList)
  270. {
  271. labelCampaignDescription->enable();
  272. }
  273. else
  274. {
  275. labelScenarioDescription->enable();
  276. labelVictoryCondition->enable();
  277. labelLossCondition->enable();
  278. iconsVictoryCondition->enable();
  279. iconsLossCondition->enable();
  280. labelVictoryConditionText->enable();
  281. labelLossConditionText->enable();
  282. labelGroupPlayersAssigned->disable();
  283. labelGroupPlayersUnassigned->disable();
  284. }
  285. }
  286. showChat = activateChat;
  287. GH.windows().totalRedraw();
  288. }
  289. CChatBox::CChatBox(const Rect & rect)
  290. : CIntObject(KEYBOARD | TEXTINPUT)
  291. {
  292. OBJ_CONSTRUCTION;
  293. pos += rect.topLeft();
  294. setRedrawParent(true);
  295. const int height = static_cast<int>(graphics->fonts[FONT_SMALL]->getLineHeight());
  296. inputBox = std::make_shared<CTextInput>(Rect(0, rect.h - height, rect.w, height), EFonts::FONT_SMALL, 0);
  297. inputBox->removeUsedEvents(KEYBOARD);
  298. chatHistory = std::make_shared<CTextBox>("", Rect(0, 0, rect.w, rect.h - height), 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. }