SelectionTab.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 | 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. type |= REDRAW_PARENT; // 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::SERVER_SAVEGAME));
  217. break;
  218. case ESelectionScreen::saveGame:
  219. parseSaves(getFiles("Saves/", EResType::SERVER_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::clickLeft(tribool down, bool previousState)
  250. {
  251. if(down)
  252. {
  253. int line = getLine();
  254. if(line != -1)
  255. {
  256. select(line);
  257. }
  258. #ifdef VCMI_IOS
  259. // focus input field if clicked inside it
  260. else if(inputName && inputName->isActive() && inputNameRect.isInside(GH.getCursorPosition()))
  261. inputName->giveFocus();
  262. #endif
  263. }
  264. }
  265. void SelectionTab::keyPressed(EShortcut key)
  266. {
  267. int moveBy = 0;
  268. switch(key)
  269. {
  270. case EShortcut::MOVE_UP:
  271. moveBy = -1;
  272. break;
  273. case EShortcut::MOVE_DOWN:
  274. moveBy = +1;
  275. break;
  276. case EShortcut::MOVE_PAGE_UP:
  277. moveBy = -(int)listItems.size() + 1;
  278. break;
  279. case EShortcut::MOVE_PAGE_DOWN:
  280. moveBy = +(int)listItems.size() - 1;
  281. break;
  282. case EShortcut::MOVE_FIRST:
  283. select(-slider->getValue());
  284. return;
  285. case EShortcut::MOVE_LAST:
  286. select((int)curItems.size() - slider->getValue());
  287. return;
  288. default:
  289. return;
  290. }
  291. select((int)selectionPos - slider->getValue() + moveBy);
  292. }
  293. void SelectionTab::clickDouble()
  294. {
  295. if(getLine() != -1) //double clicked scenarios list
  296. {
  297. (static_cast<CLobbyScreen *>(parent))->buttonStart->clickLeft(true, false);
  298. (static_cast<CLobbyScreen *>(parent))->buttonStart->clickLeft(false, true);
  299. }
  300. }
  301. // A new size filter (Small, Medium, ...) has been selected. Populate
  302. // selMaps with the relevant data.
  303. void SelectionTab::filter(int size, bool selectFirst)
  304. {
  305. curItems.clear();
  306. if(tabType == ESelectionScreen::campaignList)
  307. {
  308. for(auto elem : allItems)
  309. curItems.push_back(elem);
  310. }
  311. else
  312. {
  313. for(auto elem : allItems)
  314. {
  315. if(elem->mapHeader && (!size || elem->mapHeader->width == size))
  316. curItems.push_back(elem);
  317. }
  318. }
  319. if(curItems.size())
  320. {
  321. slider->block(false);
  322. slider->setAmount((int)curItems.size());
  323. sort();
  324. if(selectFirst)
  325. {
  326. slider->scrollTo(0);
  327. callOnSelect(curItems[0]);
  328. selectAbs(0);
  329. }
  330. }
  331. else
  332. {
  333. slider->block(true);
  334. if(callOnSelect)
  335. callOnSelect(nullptr);
  336. }
  337. }
  338. void SelectionTab::sortBy(int criteria)
  339. {
  340. if(criteria == sortingBy)
  341. {
  342. sortModeAscending = !sortModeAscending;
  343. }
  344. else
  345. {
  346. sortingBy = (ESortBy)criteria;
  347. sortModeAscending = true;
  348. }
  349. sort();
  350. selectAbs(0);
  351. }
  352. void SelectionTab::sort()
  353. {
  354. if(sortingBy != generalSortingBy)
  355. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(generalSortingBy));
  356. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(sortingBy));
  357. if(!sortModeAscending)
  358. std::reverse(curItems.begin(), curItems.end());
  359. updateListItems();
  360. redraw();
  361. }
  362. void SelectionTab::select(int position)
  363. {
  364. if(!curItems.size())
  365. return;
  366. // New selection. py is the index in curItems.
  367. int py = position + slider->getValue();
  368. vstd::amax(py, 0);
  369. vstd::amin(py, curItems.size() - 1);
  370. selectionPos = py;
  371. if(position < 0)
  372. slider->scrollBy(position);
  373. else if(position >= listItems.size())
  374. slider->scrollBy(position - (int)listItems.size() + 1);
  375. rememberCurrentSelection();
  376. if(inputName && inputName->isActive())
  377. {
  378. auto filename = *CResourceHandler::get("local")->getResourceName(ResourceID(curItems[py]->fileURI, EResType::CLIENT_SAVEGAME));
  379. inputName->setText(filename.stem().string());
  380. }
  381. updateListItems();
  382. if(callOnSelect)
  383. callOnSelect(curItems[py]);
  384. }
  385. void SelectionTab::selectAbs(int position)
  386. {
  387. select(position - slider->getValue());
  388. }
  389. void SelectionTab::sliderMove(int slidPos)
  390. {
  391. if(!slider)
  392. return; // ignore spurious call when slider is being created
  393. updateListItems();
  394. redraw();
  395. }
  396. void SelectionTab::updateListItems()
  397. {
  398. // elemIdx is the index of the maps or saved game to display on line 0
  399. // slider->capacity contains the number of available screen lines
  400. // slider->positionsAmnt is the number of elements after filtering
  401. int elemIdx = slider->getValue();
  402. for(auto item : listItems)
  403. {
  404. if(elemIdx < curItems.size())
  405. {
  406. item->updateItem(curItems[elemIdx], elemIdx == selectionPos);
  407. elemIdx++;
  408. }
  409. else
  410. {
  411. item->updateItem();
  412. }
  413. }
  414. }
  415. int SelectionTab::getLine()
  416. {
  417. int line = -1;
  418. Point clickPos = GH.getCursorPosition() - pos.topLeft();
  419. // Ignore clicks on save name area
  420. int maxPosY;
  421. if(tabType == ESelectionScreen::saveGame)
  422. maxPosY = 516;
  423. else
  424. maxPosY = 564;
  425. if(clickPos.y > 115 && clickPos.y < maxPosY && clickPos.x > 22 && clickPos.x < 371)
  426. {
  427. line = (clickPos.y - 115) / 25; //which line
  428. }
  429. return line;
  430. }
  431. void SelectionTab::selectFileName(std::string fname)
  432. {
  433. boost::to_upper(fname);
  434. for(int i = (int)curItems.size() - 1; i >= 0; i--)
  435. {
  436. if(curItems[i]->fileURI == fname)
  437. {
  438. slider->scrollTo(i);
  439. selectAbs(i);
  440. return;
  441. }
  442. }
  443. selectAbs(0);
  444. }
  445. std::shared_ptr<CMapInfo> SelectionTab::getSelectedMapInfo() const
  446. {
  447. return curItems.empty() ? nullptr : curItems[selectionPos];
  448. }
  449. void SelectionTab::rememberCurrentSelection()
  450. {
  451. // TODO: this can be more elegant
  452. if(tabType == ESelectionScreen::newGame)
  453. {
  454. Settings lastMap = settings.write["general"]["lastMap"];
  455. lastMap->String() = getSelectedMapInfo()->fileURI;
  456. }
  457. else if(tabType == ESelectionScreen::loadGame)
  458. {
  459. Settings lastSave = settings.write["general"]["lastSave"];
  460. lastSave->String() = getSelectedMapInfo()->fileURI;
  461. }
  462. else if(tabType == ESelectionScreen::campaignList)
  463. {
  464. Settings lastCampaign = settings.write["general"]["lastCampaign"];
  465. lastCampaign->String() = getSelectedMapInfo()->fileURI;
  466. }
  467. }
  468. void SelectionTab::restoreLastSelection()
  469. {
  470. switch(tabType)
  471. {
  472. case ESelectionScreen::newGame:
  473. selectFileName(settings["general"]["lastMap"].String());
  474. break;
  475. case ESelectionScreen::campaignList:
  476. selectFileName(settings["general"]["lastCampaign"].String());
  477. break;
  478. case ESelectionScreen::loadGame:
  479. case ESelectionScreen::saveGame:
  480. selectFileName(settings["general"]["lastSave"].String());
  481. }
  482. }
  483. bool SelectionTab::isMapSupported(const CMapInfo & info)
  484. {
  485. switch (info.mapHeader->version)
  486. {
  487. case EMapFormat::ROE:
  488. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["supported"].Bool();
  489. case EMapFormat::AB:
  490. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["supported"].Bool();
  491. case EMapFormat::SOD:
  492. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["supported"].Bool();
  493. case EMapFormat::WOG:
  494. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["supported"].Bool();
  495. case EMapFormat::HOTA:
  496. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["supported"].Bool();
  497. case EMapFormat::VCMI:
  498. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["supported"].Bool();
  499. }
  500. return false;
  501. }
  502. void SelectionTab::parseMaps(const std::unordered_set<ResourceID> & files)
  503. {
  504. logGlobal->debug("Parsing %d maps", files.size());
  505. allItems.clear();
  506. for(auto & file : files)
  507. {
  508. try
  509. {
  510. auto mapInfo = std::make_shared<CMapInfo>();
  511. mapInfo->mapInit(file.getName());
  512. if (isMapSupported(*mapInfo))
  513. allItems.push_back(mapInfo);
  514. }
  515. catch(std::exception & e)
  516. {
  517. logGlobal->error("Map %s is invalid. Message: %s", file.getName(), e.what());
  518. }
  519. }
  520. }
  521. void SelectionTab::parseSaves(const std::unordered_set<ResourceID> & files)
  522. {
  523. for(auto & file : files)
  524. {
  525. try
  526. {
  527. auto mapInfo = std::make_shared<CMapInfo>();
  528. mapInfo->saveInit(file);
  529. // Filter out other game modes
  530. bool isCampaign = mapInfo->scenarioOptionsOfSave->mode == StartInfo::CAMPAIGN;
  531. bool isMultiplayer = mapInfo->amountOfHumanPlayersInSave > 1;
  532. switch(CSH->getLoadMode())
  533. {
  534. case ELoadMode::SINGLE:
  535. if(isMultiplayer || isCampaign)
  536. mapInfo->mapHeader.reset();
  537. break;
  538. case ELoadMode::CAMPAIGN:
  539. if(!isCampaign)
  540. mapInfo->mapHeader.reset();
  541. break;
  542. default:
  543. if(!isMultiplayer)
  544. mapInfo->mapHeader.reset();
  545. break;
  546. }
  547. allItems.push_back(mapInfo);
  548. }
  549. catch(const std::exception & e)
  550. {
  551. logGlobal->error("Error: Failed to process %s: %s", file.getName(), e.what());
  552. }
  553. }
  554. }
  555. void SelectionTab::parseCampaigns(const std::unordered_set<ResourceID> & files)
  556. {
  557. allItems.reserve(files.size());
  558. for(auto & file : files)
  559. {
  560. auto info = std::make_shared<CMapInfo>();
  561. //allItems[i].date = std::asctime(std::localtime(&files[i].date));
  562. info->fileURI = file.getName();
  563. info->campaignInit();
  564. if(info->campaign)
  565. allItems.push_back(info);
  566. }
  567. }
  568. std::unordered_set<ResourceID> SelectionTab::getFiles(std::string dirURI, int resType)
  569. {
  570. boost::to_upper(dirURI);
  571. CResourceHandler::get()->updateFilteredFiles([&](const std::string & mount)
  572. {
  573. return boost::algorithm::starts_with(mount, dirURI);
  574. });
  575. std::unordered_set<ResourceID> ret = CResourceHandler::get()->getFilteredFiles([&](const ResourceID & ident)
  576. {
  577. return ident.getType() == resType && boost::algorithm::starts_with(ident.getName(), dirURI);
  578. });
  579. return ret;
  580. }
  581. SelectionTab::ListItem::ListItem(Point position, std::shared_ptr<CAnimation> iconsFormats, std::shared_ptr<CAnimation> iconsVictory, std::shared_ptr<CAnimation> iconsLoss)
  582. : CIntObject(LCLICK, position)
  583. {
  584. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  585. labelName = std::make_shared<CLabel>(184, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  586. labelName->setAutoRedraw(false);
  587. labelAmountOfPlayers = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  588. labelAmountOfPlayers->setAutoRedraw(false);
  589. labelNumberOfCampaignMaps = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  590. labelNumberOfCampaignMaps->setAutoRedraw(false);
  591. labelMapSizeLetter = std::make_shared<CLabel>(41, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  592. labelMapSizeLetter->setAutoRedraw(false);
  593. // FIXME: This -12 should not be needed, but for some reason CAnimImage displaced otherwise
  594. iconFormat = std::make_shared<CAnimImage>(iconsFormats, 0, 0, 59, -12);
  595. iconVictoryCondition = std::make_shared<CAnimImage>(iconsVictory, 0, 0, 277, -12);
  596. iconLossCondition = std::make_shared<CAnimImage>(iconsLoss, 0, 0, 310, -12);
  597. }
  598. void SelectionTab::ListItem::updateItem(std::shared_ptr<CMapInfo> info, bool selected)
  599. {
  600. if(!info)
  601. {
  602. labelAmountOfPlayers->disable();
  603. labelMapSizeLetter->disable();
  604. iconFormat->disable();
  605. iconVictoryCondition->disable();
  606. iconLossCondition->disable();
  607. labelNumberOfCampaignMaps->disable();
  608. labelName->disable();
  609. return;
  610. }
  611. auto color = selected ? Colors::YELLOW : Colors::WHITE;
  612. if(info->campaign)
  613. {
  614. labelAmountOfPlayers->disable();
  615. labelMapSizeLetter->disable();
  616. iconFormat->disable();
  617. iconVictoryCondition->disable();
  618. iconLossCondition->disable();
  619. labelNumberOfCampaignMaps->enable();
  620. std::ostringstream ostr(std::ostringstream::out);
  621. ostr << info->campaign->scenariosCount();
  622. labelNumberOfCampaignMaps->setText(ostr.str());
  623. labelNumberOfCampaignMaps->setColor(color);
  624. }
  625. else
  626. {
  627. labelNumberOfCampaignMaps->disable();
  628. std::ostringstream ostr(std::ostringstream::out);
  629. ostr << info->amountOfPlayersOnMap << "/" << info->amountOfHumanControllablePlayers;
  630. labelAmountOfPlayers->enable();
  631. labelAmountOfPlayers->setText(ostr.str());
  632. labelAmountOfPlayers->setColor(color);
  633. labelMapSizeLetter->enable();
  634. labelMapSizeLetter->setText(info->getMapSizeName());
  635. labelMapSizeLetter->setColor(color);
  636. iconFormat->enable();
  637. iconFormat->setFrame(info->getMapSizeFormatIconId());
  638. iconVictoryCondition->enable();
  639. iconVictoryCondition->setFrame(info->mapHeader->victoryIconIndex, 0);
  640. iconLossCondition->enable();
  641. iconLossCondition->setFrame(info->mapHeader->defeatIconIndex, 0);
  642. }
  643. labelName->enable();
  644. labelName->setText(info->getNameForList());
  645. labelName->setColor(color);
  646. }