SelectionTab.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * SelectionTab.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 "SelectionTab.h"
  12. #include "CSelectionBase.h"
  13. #include "CLobbyScreen.h"
  14. #include "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../CServerHandler.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../widgets/CComponent.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../widgets/MiscWidgets.h"
  22. #include "../widgets/ObjectLists.h"
  23. #include "../widgets/Slider.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../windows/GUIClasses.h"
  26. #include "../windows/InfoWindows.h"
  27. #include "../render/CAnimation.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/NetPacksLobby.h"
  30. #include "../../lib/CGeneralTextHandler.h"
  31. #include "../../lib/CConfigHandler.h"
  32. #include "../../lib/CModHandler.h"
  33. #include "../../lib/GameSettings.h"
  34. #include "../../lib/filesystem/Filesystem.h"
  35. #include "../../lib/campaign/CampaignState.h"
  36. #include "../../lib/mapping/CMapInfo.h"
  37. #include "../../lib/mapping/CMapHeader.h"
  38. #include "../../lib/mapping/MapFormat.h"
  39. #include "../../lib/serializer/Connection.h"
  40. bool mapSorter::operator()(const std::shared_ptr<CMapInfo> aaa, const std::shared_ptr<CMapInfo> bbb)
  41. {
  42. auto a = aaa->mapHeader.get();
  43. auto b = bbb->mapHeader.get();
  44. if(a && b) //if we are sorting scenarios
  45. {
  46. switch(sortBy)
  47. {
  48. case _format: //by map format (RoE, WoG, etc)
  49. return (a->version < b->version);
  50. break;
  51. case _loscon: //by loss conditions
  52. return (a->defeatIconIndex < b->defeatIconIndex);
  53. break;
  54. case _playerAm: //by player amount
  55. int playerAmntB, humenPlayersB, playerAmntA, humenPlayersA;
  56. playerAmntB = humenPlayersB = playerAmntA = humenPlayersA = 0;
  57. for(int i = 0; i < 8; i++)
  58. {
  59. if(a->players[i].canHumanPlay)
  60. {
  61. playerAmntA++;
  62. humenPlayersA++;
  63. }
  64. else if(a->players[i].canComputerPlay)
  65. {
  66. playerAmntA++;
  67. }
  68. if(b->players[i].canHumanPlay)
  69. {
  70. playerAmntB++;
  71. humenPlayersB++;
  72. }
  73. else if(b->players[i].canComputerPlay)
  74. {
  75. playerAmntB++;
  76. }
  77. }
  78. if(playerAmntB != playerAmntA)
  79. return (playerAmntA < playerAmntB);
  80. else
  81. return (humenPlayersA < humenPlayersB);
  82. break;
  83. case _size: //by size of map
  84. return (a->width < b->width);
  85. break;
  86. case _viccon: //by victory conditions
  87. return (a->victoryIconIndex < b->victoryIconIndex);
  88. break;
  89. case _name: //by name
  90. return boost::ilexicographical_compare(a->name, b->name);
  91. case _fileName: //by filename
  92. return boost::ilexicographical_compare(aaa->fileURI, bbb->fileURI);
  93. default:
  94. return boost::ilexicographical_compare(a->name, b->name);
  95. }
  96. }
  97. else //if we are sorting campaigns
  98. {
  99. switch(sortBy)
  100. {
  101. case _numOfMaps: //by number of maps in campaign
  102. return aaa->campaign->scenariosCount() < bbb->campaign->scenariosCount();
  103. case _name: //by name
  104. return boost::ilexicographical_compare(aaa->campaign->getName(), bbb->campaign->getName());
  105. default:
  106. return boost::ilexicographical_compare(aaa->campaign->getName(), bbb->campaign->getName());
  107. }
  108. }
  109. }
  110. // pick sorting order based on selection
  111. static ESortBy getSortBySelectionScreen(ESelectionScreen Type)
  112. {
  113. switch(Type)
  114. {
  115. case ESelectionScreen::newGame:
  116. return ESortBy::_name;
  117. case ESelectionScreen::loadGame:
  118. case ESelectionScreen::saveGame:
  119. return ESortBy::_fileName;
  120. case ESelectionScreen::campaignList:
  121. return ESortBy::_name;
  122. }
  123. // Should not reach here. But let's not crash the game.
  124. return ESortBy::_name;
  125. }
  126. SelectionTab::SelectionTab(ESelectionScreen Type)
  127. : CIntObject(LCLICK | SHOW_POPUP | KEYBOARD | DOUBLECLICK), callOnSelect(nullptr), tabType(Type), selectionPos(0), sortModeAscending(true), inputNameRect{32, 539, 350, 20}
  128. {
  129. OBJ_CONSTRUCTION;
  130. generalSortingBy = getSortBySelectionScreen(tabType);
  131. if(tabType != ESelectionScreen::campaignList)
  132. {
  133. sortingBy = _format;
  134. background = std::make_shared<CPicture>("SCSELBCK.bmp", 0, 6);
  135. pos = background->pos;
  136. inputName = std::make_shared<CTextInput>(inputNameRect, Point(-32, -25), "GSSTRIP.bmp", 0);
  137. inputName->filters += CTextInput::filenameFilter;
  138. labelMapSizes = std::make_shared<CLabel>(87, 62, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[510]);
  139. int sizes[] = {36, 72, 108, 144, 0};
  140. const char * filterIconNmes[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"};
  141. for(int i = 0; i < 5; i++)
  142. buttonsSortBy.push_back(std::make_shared<CButton>(Point(158 + 47 * i, 46), filterIconNmes[i], CGI->generaltexth->zelp[54 + i], std::bind(&SelectionTab::filter, this, sizes[i], true)));
  143. int xpos[] = {23, 55, 88, 121, 306, 339};
  144. const char * sortIconNames[] = {"SCBUTT1.DEF", "SCBUTT2.DEF", "SCBUTCP.DEF", "SCBUTT3.DEF", "SCBUTT4.DEF", "SCBUTT5.DEF"};
  145. for(int i = 0; i < 6; i++)
  146. {
  147. ESortBy criteria = (ESortBy)i;
  148. if(criteria == _name)
  149. criteria = generalSortingBy;
  150. buttonsSortBy.push_back(std::make_shared<CButton>(Point(xpos[i], 86), sortIconNames[i], CGI->generaltexth->zelp[107 + i], std::bind(&SelectionTab::sortBy, this, criteria)));
  151. }
  152. }
  153. int positionsToShow = 18;
  154. std::string tabTitle;
  155. switch(tabType)
  156. {
  157. case ESelectionScreen::newGame:
  158. tabTitle = CGI->generaltexth->arraytxt[229];
  159. break;
  160. case ESelectionScreen::loadGame:
  161. tabTitle = CGI->generaltexth->arraytxt[230];
  162. break;
  163. case ESelectionScreen::saveGame:
  164. positionsToShow = 16;
  165. tabTitle = CGI->generaltexth->arraytxt[231];
  166. break;
  167. case ESelectionScreen::campaignList:
  168. tabTitle = CGI->generaltexth->allTexts[726];
  169. setRedrawParent(true); // we use parent background so we need to make sure it's will be redrawn too
  170. pos.w = parent->pos.w;
  171. pos.h = parent->pos.h;
  172. pos.x += 3;
  173. pos.y += 6;
  174. buttonsSortBy.push_back(std::make_shared<CButton>(Point(23, 86), "CamCusM.DEF", CButton::tooltip(), std::bind(&SelectionTab::sortBy, this, _numOfMaps)));
  175. buttonsSortBy.push_back(std::make_shared<CButton>(Point(55, 86), "CamCusL.DEF", CButton::tooltip(), std::bind(&SelectionTab::sortBy, this, _name)));
  176. break;
  177. default:
  178. assert(0);
  179. break;
  180. }
  181. iconsMapFormats = std::make_shared<CAnimation>("SCSELC.DEF");
  182. iconsMapFormats->preload();
  183. iconsVictoryCondition = std::make_shared<CAnimation>("SCNRVICT.DEF");
  184. iconsVictoryCondition->preload();
  185. iconsLossCondition = std::make_shared<CAnimation>("SCNRLOSS.DEF");
  186. iconsLossCondition->preload();
  187. for(int i = 0; i < positionsToShow; i++)
  188. listItems.push_back(std::make_shared<ListItem>(Point(30, 129 + i * 25), iconsMapFormats, iconsVictoryCondition, iconsLossCondition));
  189. labelTabTitle = std::make_shared<CLabel>(205, 28, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, tabTitle);
  190. slider = std::make_shared<CSlider>(Point(372, 86), tabType != ESelectionScreen::saveGame ? 480 : 430, std::bind(&SelectionTab::sliderMove, this, _1), positionsToShow, (int)curItems.size(), 0, Orientation::VERTICAL, CSlider::BLUE);
  191. slider->setPanningStep(24);
  192. // create scroll bounds that encompass all area in this UI element to the left of slider (including area of slider itself)
  193. // entire screen can't be used in here since map description might also have slider
  194. slider->setScrollBounds(Rect(pos.x - slider->pos.x, 0, slider->pos.x + slider->pos.w - pos.x, slider->pos.h ));
  195. filter(0);
  196. }
  197. void SelectionTab::toggleMode()
  198. {
  199. if(CSH->isGuest())
  200. {
  201. allItems.clear();
  202. curItems.clear();
  203. if(slider)
  204. slider->block(true);
  205. }
  206. else
  207. {
  208. switch(tabType)
  209. {
  210. case ESelectionScreen::newGame:
  211. inputName->disable();
  212. parseMaps(getFiles("Maps/", EResType::MAP));
  213. break;
  214. case ESelectionScreen::loadGame:
  215. inputName->disable();
  216. parseSaves(getFiles("Saves/", EResType::SAVEGAME));
  217. break;
  218. case ESelectionScreen::saveGame:
  219. parseSaves(getFiles("Saves/", EResType::SAVEGAME));
  220. inputName->enable();
  221. restoreLastSelection();
  222. break;
  223. case ESelectionScreen::campaignList:
  224. parseCampaigns(getFiles("Maps/", EResType::CAMPAIGN));
  225. break;
  226. default:
  227. assert(0);
  228. break;
  229. }
  230. if(slider)
  231. {
  232. slider->block(false);
  233. filter(0);
  234. }
  235. if(CSH->campaignStateToSend)
  236. {
  237. CSH->setCampaignState(CSH->campaignStateToSend);
  238. CSH->campaignStateToSend.reset();
  239. }
  240. else
  241. {
  242. restoreLastSelection();
  243. }
  244. }
  245. slider->setAmount((int)curItems.size());
  246. updateListItems();
  247. redraw();
  248. }
  249. void SelectionTab::clickReleased(const Point & cursorPosition)
  250. {
  251. int line = getLine();
  252. if(line != -1)
  253. {
  254. select(line);
  255. }
  256. #ifdef VCMI_IOS
  257. // focus input field if clicked inside it
  258. else if(inputName && inputName->isActive() && inputNameRect.isInside(cursorPosition))
  259. inputName->giveFocus();
  260. #endif
  261. }
  262. void SelectionTab::keyPressed(EShortcut key)
  263. {
  264. int moveBy = 0;
  265. switch(key)
  266. {
  267. case EShortcut::MOVE_UP:
  268. moveBy = -1;
  269. break;
  270. case EShortcut::MOVE_DOWN:
  271. moveBy = +1;
  272. break;
  273. case EShortcut::MOVE_PAGE_UP:
  274. moveBy = -(int)listItems.size() + 1;
  275. break;
  276. case EShortcut::MOVE_PAGE_DOWN:
  277. moveBy = +(int)listItems.size() - 1;
  278. break;
  279. case EShortcut::MOVE_FIRST:
  280. select(-slider->getValue());
  281. return;
  282. case EShortcut::MOVE_LAST:
  283. select((int)curItems.size() - slider->getValue());
  284. return;
  285. default:
  286. return;
  287. }
  288. select((int)selectionPos - slider->getValue() + moveBy);
  289. }
  290. void SelectionTab::clickDouble(const Point & cursorPosition)
  291. {
  292. if(getLine() != -1) //double clicked scenarios list
  293. {
  294. (static_cast<CLobbyScreen *>(parent))->buttonStart->clickPressed(cursorPosition);
  295. (static_cast<CLobbyScreen *>(parent))->buttonStart->clickReleased(cursorPosition);
  296. }
  297. }
  298. void SelectionTab::showPopupWindow(const Point & cursorPosition)
  299. {
  300. int position = getLine();
  301. int py = position + slider->getValue();
  302. if(py >= curItems.size())
  303. return;
  304. std::string text = "{" + curItems[py]->getName() + "}\r\n\r\n";
  305. text += CGI->generaltexth->translate("vcmi.lobby.filename") + ":\r\n" + curItems[py]->fileURI;
  306. if(curItems[py]->date != "")
  307. text += "\r\n\r\n" + CGI->generaltexth->translate("vcmi.lobby.creationDate") + ":\r\n" + curItems[py]->date;
  308. CRClickPopup::createAndPush(text);
  309. }
  310. // A new size filter (Small, Medium, ...) has been selected. Populate
  311. // selMaps with the relevant data.
  312. void SelectionTab::filter(int size, bool selectFirst)
  313. {
  314. curItems.clear();
  315. if(tabType == ESelectionScreen::campaignList)
  316. {
  317. for(auto elem : allItems)
  318. curItems.push_back(elem);
  319. }
  320. else
  321. {
  322. for(auto elem : allItems)
  323. {
  324. if(elem->mapHeader && (!size || elem->mapHeader->width == size))
  325. curItems.push_back(elem);
  326. }
  327. }
  328. if(curItems.size())
  329. {
  330. slider->block(false);
  331. slider->setAmount((int)curItems.size());
  332. sort();
  333. if(selectFirst)
  334. {
  335. slider->scrollTo(0);
  336. callOnSelect(curItems[0]);
  337. selectAbs(0);
  338. }
  339. }
  340. else
  341. {
  342. slider->block(true);
  343. if(callOnSelect)
  344. callOnSelect(nullptr);
  345. }
  346. }
  347. void SelectionTab::sortBy(int criteria)
  348. {
  349. if(criteria == sortingBy)
  350. {
  351. sortModeAscending = !sortModeAscending;
  352. }
  353. else
  354. {
  355. sortingBy = (ESortBy)criteria;
  356. sortModeAscending = true;
  357. }
  358. sort();
  359. selectAbs(0);
  360. }
  361. void SelectionTab::sort()
  362. {
  363. if(sortingBy != generalSortingBy)
  364. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(generalSortingBy));
  365. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(sortingBy));
  366. if(!sortModeAscending)
  367. std::reverse(curItems.begin(), curItems.end());
  368. updateListItems();
  369. redraw();
  370. }
  371. void SelectionTab::select(int position)
  372. {
  373. if(!curItems.size())
  374. return;
  375. // New selection. py is the index in curItems.
  376. int py = position + slider->getValue();
  377. vstd::amax(py, 0);
  378. vstd::amin(py, curItems.size() - 1);
  379. selectionPos = py;
  380. if(position < 0)
  381. slider->scrollBy(position);
  382. else if(position >= listItems.size())
  383. slider->scrollBy(position - (int)listItems.size() + 1);
  384. rememberCurrentSelection();
  385. if(inputName && inputName->isActive())
  386. {
  387. auto filename = *CResourceHandler::get("local")->getResourceName(ResourceID(curItems[py]->fileURI, EResType::SAVEGAME));
  388. inputName->setText(filename.stem().string());
  389. }
  390. updateListItems();
  391. redraw();
  392. if(callOnSelect)
  393. callOnSelect(curItems[py]);
  394. }
  395. void SelectionTab::selectAbs(int position)
  396. {
  397. select(position - slider->getValue());
  398. }
  399. void SelectionTab::sliderMove(int slidPos)
  400. {
  401. if(!slider)
  402. return; // ignore spurious call when slider is being created
  403. updateListItems();
  404. redraw();
  405. }
  406. void SelectionTab::updateListItems()
  407. {
  408. // elemIdx is the index of the maps or saved game to display on line 0
  409. // slider->capacity contains the number of available screen lines
  410. // slider->positionsAmnt is the number of elements after filtering
  411. int elemIdx = slider->getValue();
  412. for(auto item : listItems)
  413. {
  414. if(elemIdx < curItems.size())
  415. {
  416. item->updateItem(curItems[elemIdx], elemIdx == selectionPos);
  417. elemIdx++;
  418. }
  419. else
  420. {
  421. item->updateItem();
  422. }
  423. }
  424. }
  425. bool SelectionTab::receiveEvent(const Point & position, int eventType) const
  426. {
  427. // FIXME: widget should instead have well-defined pos so events will be filtered using standard routine
  428. return getLine(position - pos.topLeft()) != -1;
  429. }
  430. int SelectionTab::getLine() const
  431. {
  432. Point clickPos = GH.getCursorPosition() - pos.topLeft();
  433. return getLine(clickPos);
  434. }
  435. int SelectionTab::getLine(const Point & clickPos) const
  436. {
  437. int line = -1;
  438. // Ignore clicks on save name area
  439. int maxPosY;
  440. if(tabType == ESelectionScreen::saveGame)
  441. maxPosY = 516;
  442. else
  443. maxPosY = 564;
  444. if(clickPos.y > 115 && clickPos.y < maxPosY && clickPos.x > 22 && clickPos.x < 371)
  445. {
  446. line = (clickPos.y - 115) / 25; //which line
  447. }
  448. return line;
  449. }
  450. void SelectionTab::selectFileName(std::string fname)
  451. {
  452. boost::to_upper(fname);
  453. for(int i = (int)curItems.size() - 1; i >= 0; i--)
  454. {
  455. if(curItems[i]->fileURI == fname)
  456. {
  457. slider->scrollTo(i);
  458. selectAbs(i);
  459. return;
  460. }
  461. }
  462. selectAbs(0);
  463. }
  464. std::shared_ptr<CMapInfo> SelectionTab::getSelectedMapInfo() const
  465. {
  466. return curItems.empty() ? nullptr : curItems[selectionPos];
  467. }
  468. void SelectionTab::rememberCurrentSelection()
  469. {
  470. // TODO: this can be more elegant
  471. if(tabType == ESelectionScreen::newGame)
  472. {
  473. Settings lastMap = settings.write["general"]["lastMap"];
  474. lastMap->String() = getSelectedMapInfo()->fileURI;
  475. }
  476. else if(tabType == ESelectionScreen::loadGame)
  477. {
  478. Settings lastSave = settings.write["general"]["lastSave"];
  479. lastSave->String() = getSelectedMapInfo()->fileURI;
  480. }
  481. else if(tabType == ESelectionScreen::campaignList)
  482. {
  483. Settings lastCampaign = settings.write["general"]["lastCampaign"];
  484. lastCampaign->String() = getSelectedMapInfo()->fileURI;
  485. }
  486. }
  487. void SelectionTab::restoreLastSelection()
  488. {
  489. switch(tabType)
  490. {
  491. case ESelectionScreen::newGame:
  492. selectFileName(settings["general"]["lastMap"].String());
  493. break;
  494. case ESelectionScreen::campaignList:
  495. selectFileName(settings["general"]["lastCampaign"].String());
  496. break;
  497. case ESelectionScreen::loadGame:
  498. case ESelectionScreen::saveGame:
  499. selectFileName(settings["general"]["lastSave"].String());
  500. }
  501. }
  502. bool SelectionTab::isMapSupported(const CMapInfo & info)
  503. {
  504. switch (info.mapHeader->version)
  505. {
  506. case EMapFormat::ROE:
  507. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["supported"].Bool();
  508. case EMapFormat::AB:
  509. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["supported"].Bool();
  510. case EMapFormat::SOD:
  511. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["supported"].Bool();
  512. case EMapFormat::WOG:
  513. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["supported"].Bool();
  514. case EMapFormat::HOTA:
  515. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["supported"].Bool();
  516. case EMapFormat::VCMI:
  517. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["supported"].Bool();
  518. }
  519. return false;
  520. }
  521. void SelectionTab::parseMaps(const std::unordered_set<ResourceID> & files)
  522. {
  523. logGlobal->debug("Parsing %d maps", files.size());
  524. allItems.clear();
  525. for(auto & file : files)
  526. {
  527. try
  528. {
  529. auto mapInfo = std::make_shared<CMapInfo>();
  530. mapInfo->mapInit(file.getName());
  531. if (isMapSupported(*mapInfo))
  532. allItems.push_back(mapInfo);
  533. }
  534. catch(std::exception & e)
  535. {
  536. logGlobal->error("Map %s is invalid. Message: %s", file.getName(), e.what());
  537. }
  538. }
  539. }
  540. void SelectionTab::parseSaves(const std::unordered_set<ResourceID> & files)
  541. {
  542. for(auto & file : files)
  543. {
  544. try
  545. {
  546. auto mapInfo = std::make_shared<CMapInfo>();
  547. mapInfo->saveInit(file);
  548. // Filter out other game modes
  549. bool isCampaign = mapInfo->scenarioOptionsOfSave->mode == StartInfo::CAMPAIGN;
  550. bool isMultiplayer = mapInfo->amountOfHumanPlayersInSave > 1;
  551. switch(CSH->getLoadMode())
  552. {
  553. case ELoadMode::SINGLE:
  554. if(isMultiplayer || isCampaign)
  555. mapInfo->mapHeader.reset();
  556. break;
  557. case ELoadMode::CAMPAIGN:
  558. if(!isCampaign)
  559. mapInfo->mapHeader.reset();
  560. break;
  561. default:
  562. if(!isMultiplayer)
  563. mapInfo->mapHeader.reset();
  564. break;
  565. }
  566. allItems.push_back(mapInfo);
  567. }
  568. catch(const std::exception & e)
  569. {
  570. logGlobal->error("Error: Failed to process %s: %s", file.getName(), e.what());
  571. }
  572. }
  573. }
  574. void SelectionTab::parseCampaigns(const std::unordered_set<ResourceID> & files)
  575. {
  576. allItems.reserve(files.size());
  577. for(auto & file : files)
  578. {
  579. auto info = std::make_shared<CMapInfo>();
  580. //allItems[i].date = std::asctime(std::localtime(&files[i].date));
  581. info->fileURI = file.getName();
  582. info->campaignInit();
  583. if(info->campaign)
  584. allItems.push_back(info);
  585. }
  586. }
  587. std::unordered_set<ResourceID> SelectionTab::getFiles(std::string dirURI, int resType)
  588. {
  589. boost::to_upper(dirURI);
  590. CResourceHandler::get()->updateFilteredFiles([&](const std::string & mount)
  591. {
  592. return boost::algorithm::starts_with(mount, dirURI);
  593. });
  594. std::unordered_set<ResourceID> ret = CResourceHandler::get()->getFilteredFiles([&](const ResourceID & ident)
  595. {
  596. return ident.getType() == resType && boost::algorithm::starts_with(ident.getName(), dirURI);
  597. });
  598. return ret;
  599. }
  600. SelectionTab::ListItem::ListItem(Point position, std::shared_ptr<CAnimation> iconsFormats, std::shared_ptr<CAnimation> iconsVictory, std::shared_ptr<CAnimation> iconsLoss)
  601. : CIntObject(LCLICK, position)
  602. {
  603. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  604. labelName = std::make_shared<CLabel>(184, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  605. labelName->setAutoRedraw(false);
  606. labelAmountOfPlayers = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  607. labelAmountOfPlayers->setAutoRedraw(false);
  608. labelNumberOfCampaignMaps = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  609. labelNumberOfCampaignMaps->setAutoRedraw(false);
  610. labelMapSizeLetter = std::make_shared<CLabel>(41, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  611. labelMapSizeLetter->setAutoRedraw(false);
  612. // FIXME: This -12 should not be needed, but for some reason CAnimImage displaced otherwise
  613. iconFormat = std::make_shared<CAnimImage>(iconsFormats, 0, 0, 59, -12);
  614. iconVictoryCondition = std::make_shared<CAnimImage>(iconsVictory, 0, 0, 277, -12);
  615. iconLossCondition = std::make_shared<CAnimImage>(iconsLoss, 0, 0, 310, -12);
  616. }
  617. void SelectionTab::ListItem::updateItem(std::shared_ptr<CMapInfo> info, bool selected)
  618. {
  619. if(!info)
  620. {
  621. labelAmountOfPlayers->disable();
  622. labelMapSizeLetter->disable();
  623. iconFormat->disable();
  624. iconVictoryCondition->disable();
  625. iconLossCondition->disable();
  626. labelNumberOfCampaignMaps->disable();
  627. labelName->disable();
  628. return;
  629. }
  630. auto color = selected ? Colors::YELLOW : Colors::WHITE;
  631. if(info->campaign)
  632. {
  633. labelAmountOfPlayers->disable();
  634. labelMapSizeLetter->disable();
  635. iconFormat->disable();
  636. iconVictoryCondition->disable();
  637. iconLossCondition->disable();
  638. labelNumberOfCampaignMaps->enable();
  639. std::ostringstream ostr(std::ostringstream::out);
  640. ostr << info->campaign->scenariosCount();
  641. labelNumberOfCampaignMaps->setText(ostr.str());
  642. labelNumberOfCampaignMaps->setColor(color);
  643. }
  644. else
  645. {
  646. labelNumberOfCampaignMaps->disable();
  647. std::ostringstream ostr(std::ostringstream::out);
  648. ostr << info->amountOfPlayersOnMap << "/" << info->amountOfHumanControllablePlayers;
  649. labelAmountOfPlayers->enable();
  650. labelAmountOfPlayers->setText(ostr.str());
  651. labelAmountOfPlayers->setColor(color);
  652. labelMapSizeLetter->enable();
  653. labelMapSizeLetter->setText(info->getMapSizeName());
  654. labelMapSizeLetter->setColor(color);
  655. iconFormat->enable();
  656. iconFormat->setFrame(info->getMapSizeFormatIconId());
  657. iconVictoryCondition->enable();
  658. iconVictoryCondition->setFrame(info->mapHeader->victoryIconIndex, 0);
  659. iconLossCondition->enable();
  660. iconLossCondition->setFrame(info->mapHeader->defeatIconIndex, 0);
  661. }
  662. labelName->enable();
  663. labelName->setText(info->getNameForList());
  664. labelName->setColor(color);
  665. }