CSelectionBase.cpp 14 KB

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