SelectionTab.cpp 20 KB

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