CSelectionBase.cpp 14 KB

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