SelectionTab.cpp 19 KB

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