SelectionTab.cpp 22 KB

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