CSelectionBase.cpp 14 KB

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