SelectionTab.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. 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::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. bool SelectionTab::receiveEvent(const Point & position, int eventType) const
  416. {
  417. // FIXME: widget should instead have well-defined pos so events will be filtered using standard routine
  418. return getLine(position - pos.topLeft()) != -1;
  419. }
  420. int SelectionTab::getLine() const
  421. {
  422. Point clickPos = GH.getCursorPosition() - pos.topLeft();
  423. return getLine(clickPos);
  424. }
  425. int SelectionTab::getLine(const Point & clickPos) const
  426. {
  427. int line = -1;
  428. // Ignore clicks on save name area
  429. int maxPosY;
  430. if(tabType == ESelectionScreen::saveGame)
  431. maxPosY = 516;
  432. else
  433. maxPosY = 564;
  434. if(clickPos.y > 115 && clickPos.y < maxPosY && clickPos.x > 22 && clickPos.x < 371)
  435. {
  436. line = (clickPos.y - 115) / 25; //which line
  437. }
  438. return line;
  439. }
  440. void SelectionTab::selectFileName(std::string fname)
  441. {
  442. boost::to_upper(fname);
  443. for(int i = (int)curItems.size() - 1; i >= 0; i--)
  444. {
  445. if(curItems[i]->fileURI == fname)
  446. {
  447. slider->scrollTo(i);
  448. selectAbs(i);
  449. return;
  450. }
  451. }
  452. selectAbs(0);
  453. }
  454. std::shared_ptr<CMapInfo> SelectionTab::getSelectedMapInfo() const
  455. {
  456. return curItems.empty() ? nullptr : curItems[selectionPos];
  457. }
  458. void SelectionTab::rememberCurrentSelection()
  459. {
  460. // TODO: this can be more elegant
  461. if(tabType == ESelectionScreen::newGame)
  462. {
  463. Settings lastMap = settings.write["general"]["lastMap"];
  464. lastMap->String() = getSelectedMapInfo()->fileURI;
  465. }
  466. else if(tabType == ESelectionScreen::loadGame)
  467. {
  468. Settings lastSave = settings.write["general"]["lastSave"];
  469. lastSave->String() = getSelectedMapInfo()->fileURI;
  470. }
  471. else if(tabType == ESelectionScreen::campaignList)
  472. {
  473. Settings lastCampaign = settings.write["general"]["lastCampaign"];
  474. lastCampaign->String() = getSelectedMapInfo()->fileURI;
  475. }
  476. }
  477. void SelectionTab::restoreLastSelection()
  478. {
  479. switch(tabType)
  480. {
  481. case ESelectionScreen::newGame:
  482. selectFileName(settings["general"]["lastMap"].String());
  483. break;
  484. case ESelectionScreen::campaignList:
  485. selectFileName(settings["general"]["lastCampaign"].String());
  486. break;
  487. case ESelectionScreen::loadGame:
  488. case ESelectionScreen::saveGame:
  489. selectFileName(settings["general"]["lastSave"].String());
  490. }
  491. }
  492. bool SelectionTab::isMapSupported(const CMapInfo & info)
  493. {
  494. switch (info.mapHeader->version)
  495. {
  496. case EMapFormat::ROE:
  497. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_RESTORATION_OF_ERATHIA)["supported"].Bool();
  498. case EMapFormat::AB:
  499. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_ARMAGEDDONS_BLADE)["supported"].Bool();
  500. case EMapFormat::SOD:
  501. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_SHADOW_OF_DEATH)["supported"].Bool();
  502. case EMapFormat::WOG:
  503. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_IN_THE_WAKE_OF_GODS)["supported"].Bool();
  504. case EMapFormat::HOTA:
  505. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_HORN_OF_THE_ABYSS)["supported"].Bool();
  506. case EMapFormat::VCMI:
  507. return CGI->settings()->getValue(EGameSettings::MAP_FORMAT_JSON_VCMI)["supported"].Bool();
  508. }
  509. return false;
  510. }
  511. void SelectionTab::parseMaps(const std::unordered_set<ResourceID> & files)
  512. {
  513. logGlobal->debug("Parsing %d maps", files.size());
  514. allItems.clear();
  515. for(auto & file : files)
  516. {
  517. try
  518. {
  519. auto mapInfo = std::make_shared<CMapInfo>();
  520. mapInfo->mapInit(file.getName());
  521. if (isMapSupported(*mapInfo))
  522. allItems.push_back(mapInfo);
  523. }
  524. catch(std::exception & e)
  525. {
  526. logGlobal->error("Map %s is invalid. Message: %s", file.getName(), e.what());
  527. }
  528. }
  529. }
  530. void SelectionTab::parseSaves(const std::unordered_set<ResourceID> & files)
  531. {
  532. for(auto & file : files)
  533. {
  534. try
  535. {
  536. auto mapInfo = std::make_shared<CMapInfo>();
  537. mapInfo->saveInit(file);
  538. // Filter out other game modes
  539. bool isCampaign = mapInfo->scenarioOptionsOfSave->mode == StartInfo::CAMPAIGN;
  540. bool isMultiplayer = mapInfo->amountOfHumanPlayersInSave > 1;
  541. switch(CSH->getLoadMode())
  542. {
  543. case ELoadMode::SINGLE:
  544. if(isMultiplayer || isCampaign)
  545. mapInfo->mapHeader.reset();
  546. break;
  547. case ELoadMode::CAMPAIGN:
  548. if(!isCampaign)
  549. mapInfo->mapHeader.reset();
  550. break;
  551. default:
  552. if(!isMultiplayer)
  553. mapInfo->mapHeader.reset();
  554. break;
  555. }
  556. allItems.push_back(mapInfo);
  557. }
  558. catch(const std::exception & e)
  559. {
  560. logGlobal->error("Error: Failed to process %s: %s", file.getName(), e.what());
  561. }
  562. }
  563. }
  564. void SelectionTab::parseCampaigns(const std::unordered_set<ResourceID> & files)
  565. {
  566. allItems.reserve(files.size());
  567. for(auto & file : files)
  568. {
  569. auto info = std::make_shared<CMapInfo>();
  570. //allItems[i].date = std::asctime(std::localtime(&files[i].date));
  571. info->fileURI = file.getName();
  572. info->campaignInit();
  573. if(info->campaign)
  574. allItems.push_back(info);
  575. }
  576. }
  577. std::unordered_set<ResourceID> SelectionTab::getFiles(std::string dirURI, int resType)
  578. {
  579. boost::to_upper(dirURI);
  580. CResourceHandler::get()->updateFilteredFiles([&](const std::string & mount)
  581. {
  582. return boost::algorithm::starts_with(mount, dirURI);
  583. });
  584. std::unordered_set<ResourceID> ret = CResourceHandler::get()->getFilteredFiles([&](const ResourceID & ident)
  585. {
  586. return ident.getType() == resType && boost::algorithm::starts_with(ident.getName(), dirURI);
  587. });
  588. return ret;
  589. }
  590. SelectionTab::ListItem::ListItem(Point position, std::shared_ptr<CAnimation> iconsFormats, std::shared_ptr<CAnimation> iconsVictory, std::shared_ptr<CAnimation> iconsLoss)
  591. : CIntObject(LCLICK, position)
  592. {
  593. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  594. labelName = std::make_shared<CLabel>(184, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  595. labelName->setAutoRedraw(false);
  596. labelAmountOfPlayers = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  597. labelAmountOfPlayers->setAutoRedraw(false);
  598. labelNumberOfCampaignMaps = std::make_shared<CLabel>(8, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  599. labelNumberOfCampaignMaps->setAutoRedraw(false);
  600. labelMapSizeLetter = std::make_shared<CLabel>(41, 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  601. labelMapSizeLetter->setAutoRedraw(false);
  602. // FIXME: This -12 should not be needed, but for some reason CAnimImage displaced otherwise
  603. iconFormat = std::make_shared<CAnimImage>(iconsFormats, 0, 0, 59, -12);
  604. iconVictoryCondition = std::make_shared<CAnimImage>(iconsVictory, 0, 0, 277, -12);
  605. iconLossCondition = std::make_shared<CAnimImage>(iconsLoss, 0, 0, 310, -12);
  606. }
  607. void SelectionTab::ListItem::updateItem(std::shared_ptr<CMapInfo> info, bool selected)
  608. {
  609. if(!info)
  610. {
  611. labelAmountOfPlayers->disable();
  612. labelMapSizeLetter->disable();
  613. iconFormat->disable();
  614. iconVictoryCondition->disable();
  615. iconLossCondition->disable();
  616. labelNumberOfCampaignMaps->disable();
  617. labelName->disable();
  618. return;
  619. }
  620. auto color = selected ? Colors::YELLOW : Colors::WHITE;
  621. if(info->campaign)
  622. {
  623. labelAmountOfPlayers->disable();
  624. labelMapSizeLetter->disable();
  625. iconFormat->disable();
  626. iconVictoryCondition->disable();
  627. iconLossCondition->disable();
  628. labelNumberOfCampaignMaps->enable();
  629. std::ostringstream ostr(std::ostringstream::out);
  630. ostr << info->campaign->scenariosCount();
  631. labelNumberOfCampaignMaps->setText(ostr.str());
  632. labelNumberOfCampaignMaps->setColor(color);
  633. }
  634. else
  635. {
  636. labelNumberOfCampaignMaps->disable();
  637. std::ostringstream ostr(std::ostringstream::out);
  638. ostr << info->amountOfPlayersOnMap << "/" << info->amountOfHumanControllablePlayers;
  639. labelAmountOfPlayers->enable();
  640. labelAmountOfPlayers->setText(ostr.str());
  641. labelAmountOfPlayers->setColor(color);
  642. labelMapSizeLetter->enable();
  643. labelMapSizeLetter->setText(info->getMapSizeName());
  644. labelMapSizeLetter->setColor(color);
  645. iconFormat->enable();
  646. iconFormat->setFrame(info->getMapSizeFormatIconId());
  647. iconVictoryCondition->enable();
  648. iconVictoryCondition->setFrame(info->mapHeader->victoryIconIndex, 0);
  649. iconLossCondition->enable();
  650. iconLossCondition->setFrame(info->mapHeader->defeatIconIndex, 0);
  651. }
  652. labelName->enable();
  653. labelName->setText(info->getNameForList());
  654. labelName->setColor(color);
  655. }