SelectionTab.cpp 20 KB

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