2
0

CSelectionBase.cpp 14 KB

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