SelectionTab.cpp 20 KB

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