CSelectionBase.cpp 15 KB

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